* [PATCH] Implement the ability to transmit environment variables to GDBserver when starting the inferior
@ 2017-06-29 19:41 Sergio Durigan Junior
2017-07-10 21:32 ` Sergio Durigan Junior
` (5 more replies)
0 siblings, 6 replies; 35+ messages in thread
From: Sergio Durigan Junior @ 2017-06-29 19:41 UTC (permalink / raw)
To: GDB Patches; +Cc: Pedro Alves, Sergio Durigan Junior
This is the first version of this patch, which aims to implement the
ability to transmit user-set environment variables to GDBserver when
starting the remote inferior.
User-set environment variables are only the variables that are
explicitly set by the user, using the 'set environment' command. This
means that variables that were already present in the environment when
starting GDB/GDBserver are not transmitted/considered by this feature.
The idea behind this patch is to store user-set environment variables
in a separate vector, part of gdb_environ, and provide this list to
extended_remote_create_inferior when the preparations to start the
inferior are happening. extended_remote_create_inferior, then,
iterates over this list and sends a new packet,
QEnvironmentHexEncoded, which contains the hex-encoded string that
represents the "VAR=VALUE" format (VALUE can be empty if the user set
a variable with a null value, by doing 'set environment VAR=').
The QEnvironmentHexEncoded packet is inspired on LLDB's extensions to
the RSP. Details about it can be seen here:
<https://raw.githubusercontent.com/llvm-mirror/lldb/master/docs/lldb-gdb-remote.txt>
I decided not to implement the QEnvironment packet because it is
considered deprecated by LLDB. This packet, on LLDB, serves the same
purpose of QEnvironmentHexEncoded, but sends the information using a
plain text, non-hex-encoded string.
This patch also includes updates to the documentation, testsuite, and
unit tests, without introducing regressions.
gdb/ChangeLog:
yyyy-mm-dd Sergio Durigan Junior <sergiodj@redhat.com>
* NEWS (Changes since GDB 8.0): Add entry mentioning new support
for sending environment variables to GDBserver.
(New remote packets): Add entry of QEnvironmentHexEncoded.
* common/environ.c (gdb_environ::operator=): Extend method to
handle m_user_env_list.
(gdb_environ::clear): Likewise.
(match_var_in_string): Change type of first parameter from 'char
*' to 'const char *'.
(gdb_environ::get): New variable 'fullvar'. Handle
m_user_env_list.
(gdb_environ::unset): Extend method to handle m_user_env_list.
(gdb_environ::user_envp): New method.
* common/environ.h (gdb_environ): Add NULL to m_user_env_list;
handle m_user_env_list on move constructor/assignment.
(user_envp): New method.
(m_user_env_list): New vector.
* remote.c: Include "environ.h". Add QEnvironmentHexEncoded.
(remote_protocol_features): Add
QEnvironmentHexEncoded packet.
(extended_remote_environment_support): New function.
(extended_remote_create_inferior): Call
extended_remote_environment_support.
(_initialize_remote): Add QEnvironmentHexEncoded packet config.
* unittests/environ-selftests.c (run_tests): Add tests for
m_user_env_list.
gdb/gdbserver/ChangeLog:
yyyy-mm-dd Sergio Durigan Junior <sergiodj@redhat.com>
* server.c (handle_general_set): Handle QEnvironmentHexEncoded
packet.
(handle_query): Inform remote that QEnvironmentHexEncoded is
supported.
gdb/doc/ChangeLog:
yyyy-mm-dd Sergio Durigan Junior <sergiodj@redhat.com>
* gdb.texinfo (set environment): Add @anchor. Explain that
environment variables set by the user are sent to GDBserver.
(Connecting) <Remote Packet>: Add "environment-hex-encoded"
and "QEnvironmentHexEncoded" to the table.
(Remote Protocol) <QEnvironmentHexEncoded>: New item, explaining
the packet.
gdb/testsuite/ChangeLog:
yyyy-mm-dd Sergio Durigan Junior <sergiodj@redhat.com>
* gdb.base/share-env-with-gdbserver.c: New file.
* gdb.base/share-env-with-gdbserver.exp: Likewise.
---
gdb/NEWS | 12 +++
gdb/common/environ.c | 59 +++++++++---
gdb/common/environ.h | 14 ++-
gdb/doc/gdb.texinfo | 45 +++++++++
gdb/gdbserver/server.c | 54 ++++++++++-
gdb/remote.c | 50 ++++++++++
gdb/testsuite/gdb.base/share-env-with-gdbserver.c | 40 ++++++++
.../gdb.base/share-env-with-gdbserver.exp | 105 +++++++++++++++++++++
gdb/unittests/environ-selftests.c | 30 ++++++
9 files changed, 395 insertions(+), 14 deletions(-)
create mode 100644 gdb/testsuite/gdb.base/share-env-with-gdbserver.c
create mode 100644 gdb/testsuite/gdb.base/share-env-with-gdbserver.exp
diff --git a/gdb/NEWS b/gdb/NEWS
index 7c8a8f6..9026733 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -3,6 +3,14 @@
*** Changes since GDB 8.0
+* On Unix systems, GDB now supports transmitting environment variables
+ to GDBserver that are to be passed to the inferior when it is being
+ started.
+
+ To inform GDB of environment variables that are to be transmitted to
+ GDBserver, use the "set environment" command. Only user set
+ environment variables are sent to GDBserver.
+
* On Unix systems, GDBserver now does globbing expansion and variable
substitution in inferior command line arguments.
@@ -14,6 +22,10 @@
* New remote packets
+QEnvironmentHexEncoded
+ Inform GDBserver of an environment variable that is to be passed to
+ the inferior when starting it.
+
QStartupWithShell
Indicates whether the inferior must be started with a shell or not.
diff --git a/gdb/common/environ.c b/gdb/common/environ.c
index 698bda3..da0af98 100644
--- a/gdb/common/environ.c
+++ b/gdb/common/environ.c
@@ -30,8 +30,11 @@ gdb_environ::operator= (gdb_environ &&e)
return *this;
m_environ_vector = std::move (e.m_environ_vector);
+ m_user_env_list = std::move (e.m_user_env_list);
e.m_environ_vector.clear ();
+ e.m_user_env_list.clear ();
e.m_environ_vector.push_back (NULL);
+ e.m_user_env_list.push_back (NULL);
return *this;
}
@@ -63,8 +66,10 @@ gdb_environ::clear ()
for (char *v : m_environ_vector)
xfree (v);
m_environ_vector.clear ();
+ m_user_env_list.clear ();
/* Always add the NULL element. */
m_environ_vector.push_back (NULL);
+ m_user_env_list.push_back (NULL);
}
/* Helper function to check if STRING contains an environment variable
@@ -72,7 +77,7 @@ gdb_environ::clear ()
if it contains, false otherwise. */
static bool
-match_var_in_string (char *string, const char *var, size_t var_len)
+match_var_in_string (const char *string, const char *var, size_t var_len)
{
if (strncmp (string, var, var_len) == 0 && string[var_len] == '=')
return true;
@@ -99,12 +104,14 @@ gdb_environ::get (const char *var) const
void
gdb_environ::set (const char *var, const char *value)
{
+ char *fullvar = concat (var, "=", value, NULL);
+
/* We have to unset the variable in the vector if it exists. */
unset (var);
/* Insert the element before the last one, which is always NULL. */
- m_environ_vector.insert (m_environ_vector.end () - 1,
- concat (var, "=", value, NULL));
+ m_environ_vector.insert (m_environ_vector.end () - 1, fullvar);
+ m_user_env_list.insert (m_user_env_list.end () - 1, fullvar);
}
/* See common/environ.h. */
@@ -113,18 +120,38 @@ void
gdb_environ::unset (const char *var)
{
size_t len = strlen (var);
+ std::vector<char *>::iterator it_env;
+ std::vector<const char *>::iterator it_user_env;
+ char *found_var;
/* We iterate until '.end () - 1' because the last element is
always NULL. */
- for (std::vector<char *>::iterator el = m_environ_vector.begin ();
- el != m_environ_vector.end () - 1;
- ++el)
- if (match_var_in_string (*el, var, len))
- {
- xfree (*el);
- m_environ_vector.erase (el);
- break;
- }
+ for (it_env = m_environ_vector.begin ();
+ it_env != m_environ_vector.end () - 1;
+ ++it_env)
+ if (match_var_in_string (*it_env, var, len))
+ break;
+
+ if (it_env == m_environ_vector.end () - 1)
+ {
+ /* No element has been found. */
+ return;
+ }
+
+ found_var = *it_env;
+
+ /* We iterate until '.end () - 1' because the last element is
+ always NULL. */
+ for (it_user_env = m_user_env_list.begin ();
+ it_user_env != m_user_env_list.end () - 1;
+ ++it_user_env)
+ if (match_var_in_string (*it_user_env, var, len))
+ break;
+
+ m_environ_vector.erase (it_env);
+ if (it_user_env != m_user_env_list.end () - 1)
+ m_user_env_list.erase (it_user_env);
+ xfree (found_var);
}
/* See common/environ.h. */
@@ -134,3 +161,11 @@ gdb_environ::envp () const
{
return const_cast<char **> (&m_environ_vector[0]);
}
+
+/* See common/environ.h. */
+
+const char **
+gdb_environ::user_envp () const
+{
+ return const_cast<const char **> (&m_user_env_list[0]);
+}
diff --git a/gdb/common/environ.h b/gdb/common/environ.h
index 0bbb191..cce7f87 100644
--- a/gdb/common/environ.h
+++ b/gdb/common/environ.h
@@ -32,6 +32,7 @@ public:
If/when we add more variables to it, NULL will always be the
last element. */
m_environ_vector.push_back (NULL);
+ m_user_env_list.push_back (NULL);
}
~gdb_environ ()
@@ -41,12 +42,15 @@ public:
/* Move constructor. */
gdb_environ (gdb_environ &&e)
- : m_environ_vector (std::move (e.m_environ_vector))
+ : m_environ_vector (std::move (e.m_environ_vector)),
+ m_user_env_list (std::move (e.m_user_env_list))
{
/* Make sure that the moved-from vector is left at a valid
state (only one NULL element). */
e.m_environ_vector.clear ();
+ e.m_user_env_list.clear ();
e.m_environ_vector.push_back (NULL);
+ e.m_user_env_list.push_back (NULL);
}
/* Move assignment. */
@@ -73,9 +77,17 @@ public:
/* Return the environment vector represented as a 'char **'. */
char **envp () const;
+ /* Return the user-set environment vector represented as a 'const
+ char **'. */
+ const char **user_envp () const;
+
private:
/* A vector containing the environment variables. */
std::vector<char *> m_environ_vector;
+
+ /* The vector containing the environment variables set by the
+ user. */
+ std::vector<const char *> m_user_env_list;
};
#endif /* defined (ENVIRON_H) */
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index c167a86..d632523 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -2363,6 +2363,7 @@ print the names and values of all environment variables to be given to
your program. You can abbreviate @code{environment} as @code{env}.
@kindex set environment
+@anchor{set environment}
@item set environment @var{varname} @r{[}=@var{value}@r{]}
Set environment variable @var{varname} to @var{value}. The value
changes for your program (and the shell @value{GDBN} uses to launch
@@ -2391,6 +2392,10 @@ If necessary, you can avoid that by using the @samp{env} program as a
wrapper instead of using @code{set environment}. @xref{set
exec-wrapper}, for an example doing just that.
+Environment variables that are set by the user are also transmitted to
+@command{gdbserver} to be used when starting the remote inferior.
+@pxref{QEnvironmentHexEncoded}.
+
@kindex unset environment
@item unset environment @var{varname}
Remove variable @var{varname} from the environment to be passed to your
@@ -20816,6 +20821,10 @@ are:
@tab @code{QStartupWithShell}
@tab @code{set startup-with-shell}
+@item @code{environment-hex-encoded}
+@tab @code{QEnvironmentHexEncoded}
+@tab @code{set environment}
+
@item @code{conditional-breakpoints-packet}
@tab @code{Z0 and Z1}
@tab @code{Support for target-side breakpoint condition evaluation}
@@ -36480,6 +36489,42 @@ actually support starting the inferior using a shell.
Use of this packet is controlled by the @code{set startup-with-shell}
command; @pxref{set startup-with-shell}.
+@item QEnvironmentHexEncoded:@var{hex-value}
+@anchor{QEnvironmentHexEncoded}
+@cindex environment variable, remote request
+@cindex @samp{QEnvironmentHexEncoded} packet
+On UNIX-like targets, it is possible to set environment variables that
+will be passed to the inferior during the startup process. This
+packet is used to inform @command{gdbserver} of an environment
+variable that has been defined by the user on @value{GDBN} (@pxref{set
+environment}).
+
+The packet is composed by @var{hex-value}, an hex encoded
+representation of the @var{name=value} format representing an
+environment variable. The name of the environment variable is
+represented by @var{name}, and the value to be assigned to the
+environment variable is represented by @var{value}. If the variable
+has no value (i.e., the value is @code{null}), then @var{value} will
+not be present.
+
+This packet is only available in extended mode (@pxref{extended
+mode}).
+
+Reply:
+@table @samp
+@item OK
+The request succeeded.
+@end table
+
+This packet is not probed by default; the remote stub must request it,
+by supplying an appropriate @samp{qSupported} response
+(@pxref{qSupported}). This should only be done on targets that
+actually support passing environment variables to the starting
+inferior.
+
+This packet is related to the @code{set environment} command;
+@pxref{set environment}.
+
@item qfThreadInfo
@itemx qsThreadInfo
@cindex list active threads, remote request
diff --git a/gdb/gdbserver/server.c b/gdb/gdbserver/server.c
index 3838351..622898d 100644
--- a/gdb/gdbserver/server.c
+++ b/gdb/gdbserver/server.c
@@ -631,6 +631,57 @@ handle_general_set (char *own_buf)
return;
}
+ if (startswith (own_buf, "QEnvironmentHexEncoded:"))
+ {
+ const char *p = own_buf + sizeof ("QEnvironmentHexEncoded:") - 1;
+ /* The final form of the environment variable. FINAL_VAR will
+ hold the 'VAR=VALUE' format. */
+ char *final_var = (char *) xcalloc (1, strlen (p) * 2);
+ struct cleanup *c = make_cleanup (xfree, final_var);
+ /* VAR_NAME will hold the environment variable name; VAR_VALUE
+ will hold its value. These will be just pointers to
+ FINAL_VAR. */
+ char *var_name, *var_value;
+
+ if (remote_debug)
+ debug_printf (_("[QEnvironmentHexEncoded received '%s']\n"), p);
+
+ /* Un-hexify the string received from the remote, which should
+ be in the form 'VAR=VALUE'. */
+ hex2bin (p, (gdb_byte *) final_var, strlen (p));
+
+ if (remote_debug)
+ {
+ debug_printf (_("[Environment variable to be set: '%s']\n"),
+ final_var);
+ debug_flush ();
+ }
+
+ var_name = final_var;
+ var_value = strchr (final_var, '=');
+ /* There should always be an equal sign, even if the variable is
+ empty. */
+ if (var_value == NULL)
+ {
+ warning (_("Unknown environment variable '%s' from remote side."),
+ final_var);
+ write_enn (own_buf);
+ do_cleanups (c);
+ return;
+ }
+ /* Mark the end of the variable's name. */
+ *var_value = '\0';
+ /* And skip the '=' sign (which now is the '\0'). */
+ ++var_value;
+
+ /* Finally, set the variable to be passed to the inferior. */
+ our_environ.set (var_name, var_value);
+
+ write_ok (own_buf);
+ do_cleanups (c);
+ return;
+ }
+
if (strcmp (own_buf, "QStartNoAckMode") == 0)
{
if (remote_debug)
@@ -2228,7 +2279,8 @@ handle_query (char *own_buf, int packet_len, int *new_packet_len_p)
}
sprintf (own_buf,
- "PacketSize=%x;QPassSignals+;QProgramSignals+;QStartupWithShell+",
+ "PacketSize=%x;QPassSignals+;QProgramSignals+;"
+ "QStartupWithShell+;QEnvironmentHexEncoded+",
PBUFSIZ - 1);
if (target_supports_catch_syscall ())
diff --git a/gdb/remote.c b/gdb/remote.c
index 8e8ee6f..8be6fd1 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -72,6 +72,7 @@
#include "btrace.h"
#include "record-btrace.h"
#include <algorithm>
+#include "environ.h"
/* Temp hacks for tracepoint encoding migration. */
static char *target_buf;
@@ -1429,6 +1430,7 @@ enum {
PACKET_QCatchSyscalls,
PACKET_QProgramSignals,
PACKET_QStartupWithShell,
+ PACKET_QEnvironmentHexEncoded,
PACKET_qCRC,
PACKET_qSearch_memory,
PACKET_vAttach,
@@ -4636,6 +4638,8 @@ static const struct protocol_feature remote_protocol_features[] = {
PACKET_QProgramSignals },
{ "QStartupWithShell", PACKET_DISABLE, remote_supported_packet,
PACKET_QStartupWithShell },
+ { "QEnvironmentHexEncoded", PACKET_DISABLE, remote_supported_packet,
+ PACKET_QEnvironmentHexEncoded },
{ "QStartNoAckMode", PACKET_DISABLE, remote_supported_packet,
PACKET_QStartNoAckMode },
{ "multiprocess", PACKET_DISABLE, remote_supported_packet,
@@ -9585,6 +9589,44 @@ extended_remote_run (const std::string &args)
}
}
+/* Helper function to handle the QEnvironmentHexEncoded packet and
+ send the user-defined environment variables to the remote. */
+
+static void
+extended_remote_environment_support (struct remote_state *rs)
+{
+ gdb_environ *e = ¤t_inferior ()->environment;
+ const char **user_envp = e->user_envp ();
+
+ for (int i = 0; user_envp[i] != NULL; ++i)
+ {
+ char *encoded_fullvar;
+ const char *fullvar = user_envp[i];
+ struct cleanup *c;
+
+ encoded_fullvar = (char *) xmalloc (get_remote_packet_size ());
+ c = make_cleanup (xfree, encoded_fullvar);
+
+ /* Convert the environment variable to an hex string, which
+ is the best format to be transmitted over the wire. */
+ bin2hex ((gdb_byte *) fullvar, encoded_fullvar, strlen (fullvar));
+
+ xsnprintf (rs->buf, get_remote_packet_size (),
+ "QEnvironmentHexEncoded:%s", encoded_fullvar);
+
+ if (remote_debug)
+ fprintf_unfiltered (gdb_stdlog, _("sending packet '%s'\n"),
+ rs->buf);
+
+ putpkt (rs->buf);
+ getpkt (&rs->buf, &rs->buf_size, 0);
+ if (strcmp (rs->buf, "OK") != 0)
+ warning (_("Unable to set environment variable '%s' on remote."),
+ fullvar);
+ do_cleanups (c);
+ }
+}
+
/* In the extended protocol we want to be able to do things like
"run" and have them basically work as expected. So we need
a special create_inferior function. We support changing the
@@ -9625,6 +9667,9 @@ Remote replied unexpectedly while setting startup-with-shell: %s"),
rs->buf);
}
+ if (packet_support (PACKET_QEnvironmentHexEncoded) != PACKET_DISABLE)
+ extended_remote_environment_support (rs);
+
/* Now restart the remote server. */
run_worked = extended_remote_run (args) != -1;
if (!run_worked)
@@ -14118,6 +14163,11 @@ Show the maximum size of the address (in bits) in a memory packet."), NULL,
add_packet_config_cmd (&remote_protocol_packets[PACKET_QStartupWithShell],
"QStartupWithShell", "startup-with-shell", 0);
+ add_packet_config_cmd (&remote_protocol_packets
+ [PACKET_QEnvironmentHexEncoded],
+ "QEnvironmentHexEncoded", "environment-hex-encoded",
+ 0);
+
add_packet_config_cmd (&remote_protocol_packets[PACKET_qSymbol],
"qSymbol", "symbol-lookup", 0);
diff --git a/gdb/testsuite/gdb.base/share-env-with-gdbserver.c b/gdb/testsuite/gdb.base/share-env-with-gdbserver.c
new file mode 100644
index 0000000..740bd0f
--- /dev/null
+++ b/gdb/testsuite/gdb.base/share-env-with-gdbserver.c
@@ -0,0 +1,40 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+ Copyright 2017 Free Software Foundation, Inc.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+/* Wrapper around getenv for GDB. */
+
+static const char *
+my_getenv (const char *name)
+{
+ return getenv (name);
+}
+
+int
+main (int argc, char *argv[])
+{
+ const char *myvar = getenv ("GDB_TEST_VAR");
+
+ if (myvar != NULL)
+ printf ("It worked! myvar = '%s'\n", myvar);
+ else
+ printf ("It failed.");
+
+ return 0; /* break-here */
+}
diff --git a/gdb/testsuite/gdb.base/share-env-with-gdbserver.exp b/gdb/testsuite/gdb.base/share-env-with-gdbserver.exp
new file mode 100644
index 0000000..5cae141
--- /dev/null
+++ b/gdb/testsuite/gdb.base/share-env-with-gdbserver.exp
@@ -0,0 +1,105 @@
+# This testcase is part of GDB, the GNU debugger.
+
+# Copyright 2017 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+# This test doesn't make sense on native-gdbserver.
+if { [use_gdb_stub] } {
+ untested "not supported"
+ return
+}
+
+standard_testfile
+
+if { [prepare_for_testing "failed to prepare" $testfile $srcfile debug] } {
+ return -1
+}
+
+set test_var_name "GDB_TEST_VAR"
+
+# Helper function that does the actual testing.
+#
+# - VAR_VALUE is the value of the environment variable.
+#
+# - VAR_NAME is the name of the environment variable. If empty,
+# defaults to $test_var_name.
+#
+# - VAR_NAME_MATCH is the name (regex) that will be used to query the
+# environment about the variable (via getenv). This is useful when
+# we're testing variables with strange names (e.g., with an equal
+# sign in the name) and we know that the variable will actually be
+# set using another name. If empty, defatults, to $var_name.
+#
+# - VAR_VALUE_MATCH is the value (regex) that will be used to match
+# the result of getenv. The rationale is the same as explained for
+# VAR_NAME_MATCH. If empty, defaults, to $var_value.
+
+proc do_test { var_value { var_name "" } { var_name_match "" } { var_value_match "" } } {
+ global hex decimal binfile test_var_name
+
+ clean_restart $binfile
+
+ if { $var_name == "" } {
+ set var_name $test_var_name
+ }
+
+ if { $var_name_match == "" } {
+ set var_name_match $var_name
+ }
+
+ if { $var_value_match == "" } {
+ set var_value_match $var_value
+ }
+
+ if { $var_value != "" } {
+ gdb_test_no_output "set environment $var_name = $var_value" \
+ "set $var_name = $var_value"
+ } else {
+ gdb_test "set environment $var_name =" \
+ "Setting environment variable \"$var_name\" to null value." \
+ "set $var_name to null value"
+ }
+
+ if { ![runto_main] } {
+ return -1
+ }
+
+ gdb_breakpoint [gdb_get_line_number "break-here"]
+
+ gdb_test "continue" "Breakpoint $decimal, main \\\(argc=1, argv=$hex\\\) at.*" \
+ "continue until breakpoint"
+
+ gdb_test "print my_getenv (\"$var_name_match\")" "\\\$$decimal = $hex \"$var_value_match\"" \
+ "print result of getenv for $var_name"
+}
+
+with_test_prefix "long var value" {
+ do_test "this is my test variable; testing long vars; {}"
+}
+
+with_test_prefix "empty var" {
+ do_test ""
+}
+
+with_test_prefix "strange named var" {
+ # In this test we're doing the following:
+ #
+ # (gdb) set environment 'asd =' = 123 43; asd b ### [];;;
+ #
+ # However, due to how GDB parses this line, the environment
+ # variable will end up named <'asd> (without the <>), and its
+ # value will be <' = 123 43; asd b ### [];;;> (without the <>).
+ do_test "123 43; asd b ### [];;;" "'asd ='" "'asd" "' = 123 43; asd b ### [];;;"
+}
diff --git a/gdb/unittests/environ-selftests.c b/gdb/unittests/environ-selftests.c
index 28b16f8..b740583 100644
--- a/gdb/unittests/environ-selftests.c
+++ b/gdb/unittests/environ-selftests.c
@@ -38,6 +38,7 @@ run_tests ()
/* When the vector is initialized, there should always be one NULL
element in it. */
SELF_CHECK (env.envp ()[0] == NULL);
+ SELF_CHECK (env.user_envp ()[0] == NULL);
/* Make sure that there is no other element. */
SELF_CHECK (env.get ("PWD") == NULL);
@@ -45,14 +46,20 @@ run_tests ()
/* Check if unset followed by a set in an empty vector works. */
env.set ("PWD", "test");
SELF_CHECK (strcmp (env.get ("PWD"), "test") == 0);
+ SELF_CHECK (strcmp (env.user_envp ()[0], "PWD=test") == 0);
/* The second element must be NULL. */
SELF_CHECK (env.envp ()[1] == NULL);
+ SELF_CHECK (env.user_envp ()[1] == NULL);
env.unset ("PWD");
SELF_CHECK (env.envp ()[0] == NULL);
+ SELF_CHECK (env.user_envp ()[0] == NULL);
/* Initialize the environment vector using the host's environ. */
env = gdb_environ::from_host_environ ();
+ /* The user-set list must be empty. */
+ SELF_CHECK (env.user_envp ()[0] == NULL);
+
/* Our test environment variable should be present at the
vector. */
SELF_CHECK (strcmp (env.get ("GDB_SELFTEST_ENVIRON"), "1") == 0);
@@ -75,6 +82,7 @@ run_tests ()
variable. */
env.clear ();
SELF_CHECK (env.envp ()[0] == NULL);
+ SELF_CHECK (env.user_envp ()[0] == NULL);
SELF_CHECK (env.get ("GDB_SELFTEST_ENVIRON") == NULL);
/* Reinitialize our environ vector using the host environ. We
@@ -97,6 +105,9 @@ run_tests ()
vector, but can still find B. */
env.set ("GDB_SELFTEST_ENVIRON_1", "aaa");
SELF_CHECK (strcmp (env.get ("GDB_SELFTEST_ENVIRON_1"), "aaa") == 0);
+ /* User-set environ var list must contain one element. */
+ SELF_CHECK (strcmp (env.user_envp ()[0], "GDB_SELFTEST_ENVIRON_1=aaa") == 0);
+ SELF_CHECK (env.user_envp ()[1] == NULL);
env.set ("GDB_SELFTEST_ENVIRON_2", "bbb");
SELF_CHECK (strcmp (env.get ("GDB_SELFTEST_ENVIRON_2"), "bbb") == 0);
@@ -104,6 +115,10 @@ run_tests ()
env.unset ("GDB_SELFTEST_ENVIRON_1");
SELF_CHECK (env.get ("GDB_SELFTEST_ENVIRON_1") == NULL);
SELF_CHECK (strcmp (env.get ("GDB_SELFTEST_ENVIRON_2"), "bbb") == 0);
+ /* The user-set environ var list must contain only one element
+ now. */
+ SELF_CHECK (strcmp (env.user_envp ()[0], "GDB_SELFTEST_ENVIRON_2=bbb") == 0);
+ SELF_CHECK (env.user_envp ()[1] == NULL);
env.clear ();
@@ -111,11 +126,16 @@ run_tests ()
valid state (i.e., its only element is NULL). */
env.set ("A", "1");
SELF_CHECK (strcmp (env.get ("A"), "1") == 0);
+ SELF_CHECK (strcmp (env.user_envp ()[0], "A=1") == 0);
+ SELF_CHECK (env.user_envp ()[1] == NULL);
gdb_environ env2;
env2 = std::move (env);
SELF_CHECK (env.envp ()[0] == NULL);
SELF_CHECK (strcmp (env2.get ("A"), "1") == 0);
SELF_CHECK (env2.envp ()[1] == NULL);
+ SELF_CHECK (env.user_envp ()[0] == NULL);
+ SELF_CHECK (strcmp (env2.user_envp ()[0], "A=1") == 0);
+ SELF_CHECK (env2.user_envp ()[1] == NULL);
env.set ("B", "2");
SELF_CHECK (strcmp (env.get ("B"), "2") == 0);
SELF_CHECK (env.envp ()[1] == NULL);
@@ -125,18 +145,26 @@ run_tests ()
env.clear ();
env.set ("A", "1");
SELF_CHECK (strcmp (env.get ("A"), "1") == 0);
+ SELF_CHECK (strcmp (env.user_envp ()[0], "A=1") == 0);
gdb_environ env3 = std::move (env);
SELF_CHECK (env.envp ()[0] == NULL);
+ SELF_CHECK (env.user_envp ()[0] == NULL);
SELF_CHECK (strcmp (env3.get ("A"), "1") == 0);
SELF_CHECK (env3.envp ()[1] == NULL);
+ SELF_CHECK (strcmp (env3.user_envp ()[0], "A=1") == 0);
+ SELF_CHECK (env3.user_envp ()[1] == NULL);
env.set ("B", "2");
SELF_CHECK (strcmp (env.get ("B"), "2") == 0);
SELF_CHECK (env.envp ()[1] == NULL);
+ SELF_CHECK (strcmp (env.user_envp ()[0], "B=2") == 0);
+ SELF_CHECK (env.user_envp ()[2] == NULL);
/* Test self-move. */
env.clear ();
env.set ("A", "1");
SELF_CHECK (strcmp (env.get ("A"), "1") == 0);
+ SELF_CHECK (strcmp (env.user_envp ()[0], "A=1") == 0);
+ SELF_CHECK (env.user_envp ()[1] == NULL);
/* Some compilers warn about moving to self, but that's precisely what we want
to test here, so turn this warning off. */
@@ -148,6 +176,8 @@ run_tests ()
SELF_CHECK (strcmp (env.get ("A"), "1") == 0);
SELF_CHECK (strcmp (env.envp ()[0], "A=1") == 0);
SELF_CHECK (env.envp ()[1] == NULL);
+ SELF_CHECK (strcmp (env.user_envp ()[0], "A=1") == 0);
+ SELF_CHECK (env.user_envp ()[1] == NULL);
}
} /* namespace gdb_environ */
} /* namespace selftests */
--
2.9.3
^ permalink raw reply [flat|nested] 35+ messages in thread
* Re: [PATCH] Implement the ability to transmit environment variables to GDBserver when starting the inferior
2017-06-29 19:41 [PATCH] Implement the ability to transmit environment variables to GDBserver when starting the inferior Sergio Durigan Junior
@ 2017-07-10 21:32 ` Sergio Durigan Junior
2017-07-13 21:47 ` Simon Marchi
` (4 subsequent siblings)
5 siblings, 0 replies; 35+ messages in thread
From: Sergio Durigan Junior @ 2017-07-10 21:32 UTC (permalink / raw)
To: GDB Patches; +Cc: Pedro Alves
On Thursday, June 29 2017, I wrote:
> This is the first version of this patch, which aims to implement the
> ability to transmit user-set environment variables to GDBserver when
> starting the remote inferior.
Ping.
> User-set environment variables are only the variables that are
> explicitly set by the user, using the 'set environment' command. This
> means that variables that were already present in the environment when
> starting GDB/GDBserver are not transmitted/considered by this feature.
>
> The idea behind this patch is to store user-set environment variables
> in a separate vector, part of gdb_environ, and provide this list to
> extended_remote_create_inferior when the preparations to start the
> inferior are happening. extended_remote_create_inferior, then,
> iterates over this list and sends a new packet,
> QEnvironmentHexEncoded, which contains the hex-encoded string that
> represents the "VAR=VALUE" format (VALUE can be empty if the user set
> a variable with a null value, by doing 'set environment VAR=').
>
> The QEnvironmentHexEncoded packet is inspired on LLDB's extensions to
> the RSP. Details about it can be seen here:
>
> <https://raw.githubusercontent.com/llvm-mirror/lldb/master/docs/lldb-gdb-remote.txt>
>
> I decided not to implement the QEnvironment packet because it is
> considered deprecated by LLDB. This packet, on LLDB, serves the same
> purpose of QEnvironmentHexEncoded, but sends the information using a
> plain text, non-hex-encoded string.
>
> This patch also includes updates to the documentation, testsuite, and
> unit tests, without introducing regressions.
>
> gdb/ChangeLog:
> yyyy-mm-dd Sergio Durigan Junior <sergiodj@redhat.com>
>
> * NEWS (Changes since GDB 8.0): Add entry mentioning new support
> for sending environment variables to GDBserver.
> (New remote packets): Add entry of QEnvironmentHexEncoded.
> * common/environ.c (gdb_environ::operator=): Extend method to
> handle m_user_env_list.
> (gdb_environ::clear): Likewise.
> (match_var_in_string): Change type of first parameter from 'char
> *' to 'const char *'.
> (gdb_environ::get): New variable 'fullvar'. Handle
> m_user_env_list.
> (gdb_environ::unset): Extend method to handle m_user_env_list.
> (gdb_environ::user_envp): New method.
> * common/environ.h (gdb_environ): Add NULL to m_user_env_list;
> handle m_user_env_list on move constructor/assignment.
> (user_envp): New method.
> (m_user_env_list): New vector.
> * remote.c: Include "environ.h". Add QEnvironmentHexEncoded.
> (remote_protocol_features): Add
> QEnvironmentHexEncoded packet.
> (extended_remote_environment_support): New function.
> (extended_remote_create_inferior): Call
> extended_remote_environment_support.
> (_initialize_remote): Add QEnvironmentHexEncoded packet config.
> * unittests/environ-selftests.c (run_tests): Add tests for
> m_user_env_list.
>
> gdb/gdbserver/ChangeLog:
> yyyy-mm-dd Sergio Durigan Junior <sergiodj@redhat.com>
>
> * server.c (handle_general_set): Handle QEnvironmentHexEncoded
> packet.
> (handle_query): Inform remote that QEnvironmentHexEncoded is
> supported.
>
> gdb/doc/ChangeLog:
> yyyy-mm-dd Sergio Durigan Junior <sergiodj@redhat.com>
>
> * gdb.texinfo (set environment): Add @anchor. Explain that
> environment variables set by the user are sent to GDBserver.
> (Connecting) <Remote Packet>: Add "environment-hex-encoded"
> and "QEnvironmentHexEncoded" to the table.
> (Remote Protocol) <QEnvironmentHexEncoded>: New item, explaining
> the packet.
>
> gdb/testsuite/ChangeLog:
> yyyy-mm-dd Sergio Durigan Junior <sergiodj@redhat.com>
>
> * gdb.base/share-env-with-gdbserver.c: New file.
> * gdb.base/share-env-with-gdbserver.exp: Likewise.
> ---
> gdb/NEWS | 12 +++
> gdb/common/environ.c | 59 +++++++++---
> gdb/common/environ.h | 14 ++-
> gdb/doc/gdb.texinfo | 45 +++++++++
> gdb/gdbserver/server.c | 54 ++++++++++-
> gdb/remote.c | 50 ++++++++++
> gdb/testsuite/gdb.base/share-env-with-gdbserver.c | 40 ++++++++
> .../gdb.base/share-env-with-gdbserver.exp | 105 +++++++++++++++++++++
> gdb/unittests/environ-selftests.c | 30 ++++++
> 9 files changed, 395 insertions(+), 14 deletions(-)
> create mode 100644 gdb/testsuite/gdb.base/share-env-with-gdbserver.c
> create mode 100644 gdb/testsuite/gdb.base/share-env-with-gdbserver.exp
>
> diff --git a/gdb/NEWS b/gdb/NEWS
> index 7c8a8f6..9026733 100644
> --- a/gdb/NEWS
> +++ b/gdb/NEWS
> @@ -3,6 +3,14 @@
>
> *** Changes since GDB 8.0
>
> +* On Unix systems, GDB now supports transmitting environment variables
> + to GDBserver that are to be passed to the inferior when it is being
> + started.
> +
> + To inform GDB of environment variables that are to be transmitted to
> + GDBserver, use the "set environment" command. Only user set
> + environment variables are sent to GDBserver.
> +
> * On Unix systems, GDBserver now does globbing expansion and variable
> substitution in inferior command line arguments.
>
> @@ -14,6 +22,10 @@
>
> * New remote packets
>
> +QEnvironmentHexEncoded
> + Inform GDBserver of an environment variable that is to be passed to
> + the inferior when starting it.
> +
> QStartupWithShell
> Indicates whether the inferior must be started with a shell or not.
>
> diff --git a/gdb/common/environ.c b/gdb/common/environ.c
> index 698bda3..da0af98 100644
> --- a/gdb/common/environ.c
> +++ b/gdb/common/environ.c
> @@ -30,8 +30,11 @@ gdb_environ::operator= (gdb_environ &&e)
> return *this;
>
> m_environ_vector = std::move (e.m_environ_vector);
> + m_user_env_list = std::move (e.m_user_env_list);
> e.m_environ_vector.clear ();
> + e.m_user_env_list.clear ();
> e.m_environ_vector.push_back (NULL);
> + e.m_user_env_list.push_back (NULL);
> return *this;
> }
>
> @@ -63,8 +66,10 @@ gdb_environ::clear ()
> for (char *v : m_environ_vector)
> xfree (v);
> m_environ_vector.clear ();
> + m_user_env_list.clear ();
> /* Always add the NULL element. */
> m_environ_vector.push_back (NULL);
> + m_user_env_list.push_back (NULL);
> }
>
> /* Helper function to check if STRING contains an environment variable
> @@ -72,7 +77,7 @@ gdb_environ::clear ()
> if it contains, false otherwise. */
>
> static bool
> -match_var_in_string (char *string, const char *var, size_t var_len)
> +match_var_in_string (const char *string, const char *var, size_t var_len)
> {
> if (strncmp (string, var, var_len) == 0 && string[var_len] == '=')
> return true;
> @@ -99,12 +104,14 @@ gdb_environ::get (const char *var) const
> void
> gdb_environ::set (const char *var, const char *value)
> {
> + char *fullvar = concat (var, "=", value, NULL);
> +
> /* We have to unset the variable in the vector if it exists. */
> unset (var);
>
> /* Insert the element before the last one, which is always NULL. */
> - m_environ_vector.insert (m_environ_vector.end () - 1,
> - concat (var, "=", value, NULL));
> + m_environ_vector.insert (m_environ_vector.end () - 1, fullvar);
> + m_user_env_list.insert (m_user_env_list.end () - 1, fullvar);
> }
>
> /* See common/environ.h. */
> @@ -113,18 +120,38 @@ void
> gdb_environ::unset (const char *var)
> {
> size_t len = strlen (var);
> + std::vector<char *>::iterator it_env;
> + std::vector<const char *>::iterator it_user_env;
> + char *found_var;
>
> /* We iterate until '.end () - 1' because the last element is
> always NULL. */
> - for (std::vector<char *>::iterator el = m_environ_vector.begin ();
> - el != m_environ_vector.end () - 1;
> - ++el)
> - if (match_var_in_string (*el, var, len))
> - {
> - xfree (*el);
> - m_environ_vector.erase (el);
> - break;
> - }
> + for (it_env = m_environ_vector.begin ();
> + it_env != m_environ_vector.end () - 1;
> + ++it_env)
> + if (match_var_in_string (*it_env, var, len))
> + break;
> +
> + if (it_env == m_environ_vector.end () - 1)
> + {
> + /* No element has been found. */
> + return;
> + }
> +
> + found_var = *it_env;
> +
> + /* We iterate until '.end () - 1' because the last element is
> + always NULL. */
> + for (it_user_env = m_user_env_list.begin ();
> + it_user_env != m_user_env_list.end () - 1;
> + ++it_user_env)
> + if (match_var_in_string (*it_user_env, var, len))
> + break;
> +
> + m_environ_vector.erase (it_env);
> + if (it_user_env != m_user_env_list.end () - 1)
> + m_user_env_list.erase (it_user_env);
> + xfree (found_var);
> }
>
> /* See common/environ.h. */
> @@ -134,3 +161,11 @@ gdb_environ::envp () const
> {
> return const_cast<char **> (&m_environ_vector[0]);
> }
> +
> +/* See common/environ.h. */
> +
> +const char **
> +gdb_environ::user_envp () const
> +{
> + return const_cast<const char **> (&m_user_env_list[0]);
> +}
> diff --git a/gdb/common/environ.h b/gdb/common/environ.h
> index 0bbb191..cce7f87 100644
> --- a/gdb/common/environ.h
> +++ b/gdb/common/environ.h
> @@ -32,6 +32,7 @@ public:
> If/when we add more variables to it, NULL will always be the
> last element. */
> m_environ_vector.push_back (NULL);
> + m_user_env_list.push_back (NULL);
> }
>
> ~gdb_environ ()
> @@ -41,12 +42,15 @@ public:
>
> /* Move constructor. */
> gdb_environ (gdb_environ &&e)
> - : m_environ_vector (std::move (e.m_environ_vector))
> + : m_environ_vector (std::move (e.m_environ_vector)),
> + m_user_env_list (std::move (e.m_user_env_list))
> {
> /* Make sure that the moved-from vector is left at a valid
> state (only one NULL element). */
> e.m_environ_vector.clear ();
> + e.m_user_env_list.clear ();
> e.m_environ_vector.push_back (NULL);
> + e.m_user_env_list.push_back (NULL);
> }
>
> /* Move assignment. */
> @@ -73,9 +77,17 @@ public:
> /* Return the environment vector represented as a 'char **'. */
> char **envp () const;
>
> + /* Return the user-set environment vector represented as a 'const
> + char **'. */
> + const char **user_envp () const;
> +
> private:
> /* A vector containing the environment variables. */
> std::vector<char *> m_environ_vector;
> +
> + /* The vector containing the environment variables set by the
> + user. */
> + std::vector<const char *> m_user_env_list;
> };
>
> #endif /* defined (ENVIRON_H) */
> diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
> index c167a86..d632523 100644
> --- a/gdb/doc/gdb.texinfo
> +++ b/gdb/doc/gdb.texinfo
> @@ -2363,6 +2363,7 @@ print the names and values of all environment variables to be given to
> your program. You can abbreviate @code{environment} as @code{env}.
>
> @kindex set environment
> +@anchor{set environment}
> @item set environment @var{varname} @r{[}=@var{value}@r{]}
> Set environment variable @var{varname} to @var{value}. The value
> changes for your program (and the shell @value{GDBN} uses to launch
> @@ -2391,6 +2392,10 @@ If necessary, you can avoid that by using the @samp{env} program as a
> wrapper instead of using @code{set environment}. @xref{set
> exec-wrapper}, for an example doing just that.
>
> +Environment variables that are set by the user are also transmitted to
> +@command{gdbserver} to be used when starting the remote inferior.
> +@pxref{QEnvironmentHexEncoded}.
> +
> @kindex unset environment
> @item unset environment @var{varname}
> Remove variable @var{varname} from the environment to be passed to your
> @@ -20816,6 +20821,10 @@ are:
> @tab @code{QStartupWithShell}
> @tab @code{set startup-with-shell}
>
> +@item @code{environment-hex-encoded}
> +@tab @code{QEnvironmentHexEncoded}
> +@tab @code{set environment}
> +
> @item @code{conditional-breakpoints-packet}
> @tab @code{Z0 and Z1}
> @tab @code{Support for target-side breakpoint condition evaluation}
> @@ -36480,6 +36489,42 @@ actually support starting the inferior using a shell.
> Use of this packet is controlled by the @code{set startup-with-shell}
> command; @pxref{set startup-with-shell}.
>
> +@item QEnvironmentHexEncoded:@var{hex-value}
> +@anchor{QEnvironmentHexEncoded}
> +@cindex environment variable, remote request
> +@cindex @samp{QEnvironmentHexEncoded} packet
> +On UNIX-like targets, it is possible to set environment variables that
> +will be passed to the inferior during the startup process. This
> +packet is used to inform @command{gdbserver} of an environment
> +variable that has been defined by the user on @value{GDBN} (@pxref{set
> +environment}).
> +
> +The packet is composed by @var{hex-value}, an hex encoded
> +representation of the @var{name=value} format representing an
> +environment variable. The name of the environment variable is
> +represented by @var{name}, and the value to be assigned to the
> +environment variable is represented by @var{value}. If the variable
> +has no value (i.e., the value is @code{null}), then @var{value} will
> +not be present.
> +
> +This packet is only available in extended mode (@pxref{extended
> +mode}).
> +
> +Reply:
> +@table @samp
> +@item OK
> +The request succeeded.
> +@end table
> +
> +This packet is not probed by default; the remote stub must request it,
> +by supplying an appropriate @samp{qSupported} response
> +(@pxref{qSupported}). This should only be done on targets that
> +actually support passing environment variables to the starting
> +inferior.
> +
> +This packet is related to the @code{set environment} command;
> +@pxref{set environment}.
> +
> @item qfThreadInfo
> @itemx qsThreadInfo
> @cindex list active threads, remote request
> diff --git a/gdb/gdbserver/server.c b/gdb/gdbserver/server.c
> index 3838351..622898d 100644
> --- a/gdb/gdbserver/server.c
> +++ b/gdb/gdbserver/server.c
> @@ -631,6 +631,57 @@ handle_general_set (char *own_buf)
> return;
> }
>
> + if (startswith (own_buf, "QEnvironmentHexEncoded:"))
> + {
> + const char *p = own_buf + sizeof ("QEnvironmentHexEncoded:") - 1;
> + /* The final form of the environment variable. FINAL_VAR will
> + hold the 'VAR=VALUE' format. */
> + char *final_var = (char *) xcalloc (1, strlen (p) * 2);
> + struct cleanup *c = make_cleanup (xfree, final_var);
> + /* VAR_NAME will hold the environment variable name; VAR_VALUE
> + will hold its value. These will be just pointers to
> + FINAL_VAR. */
> + char *var_name, *var_value;
> +
> + if (remote_debug)
> + debug_printf (_("[QEnvironmentHexEncoded received '%s']\n"), p);
> +
> + /* Un-hexify the string received from the remote, which should
> + be in the form 'VAR=VALUE'. */
> + hex2bin (p, (gdb_byte *) final_var, strlen (p));
> +
> + if (remote_debug)
> + {
> + debug_printf (_("[Environment variable to be set: '%s']\n"),
> + final_var);
> + debug_flush ();
> + }
> +
> + var_name = final_var;
> + var_value = strchr (final_var, '=');
> + /* There should always be an equal sign, even if the variable is
> + empty. */
> + if (var_value == NULL)
> + {
> + warning (_("Unknown environment variable '%s' from remote side."),
> + final_var);
> + write_enn (own_buf);
> + do_cleanups (c);
> + return;
> + }
> + /* Mark the end of the variable's name. */
> + *var_value = '\0';
> + /* And skip the '=' sign (which now is the '\0'). */
> + ++var_value;
> +
> + /* Finally, set the variable to be passed to the inferior. */
> + our_environ.set (var_name, var_value);
> +
> + write_ok (own_buf);
> + do_cleanups (c);
> + return;
> + }
> +
> if (strcmp (own_buf, "QStartNoAckMode") == 0)
> {
> if (remote_debug)
> @@ -2228,7 +2279,8 @@ handle_query (char *own_buf, int packet_len, int *new_packet_len_p)
> }
>
> sprintf (own_buf,
> - "PacketSize=%x;QPassSignals+;QProgramSignals+;QStartupWithShell+",
> + "PacketSize=%x;QPassSignals+;QProgramSignals+;"
> + "QStartupWithShell+;QEnvironmentHexEncoded+",
> PBUFSIZ - 1);
>
> if (target_supports_catch_syscall ())
> diff --git a/gdb/remote.c b/gdb/remote.c
> index 8e8ee6f..8be6fd1 100644
> --- a/gdb/remote.c
> +++ b/gdb/remote.c
> @@ -72,6 +72,7 @@
> #include "btrace.h"
> #include "record-btrace.h"
> #include <algorithm>
> +#include "environ.h"
>
> /* Temp hacks for tracepoint encoding migration. */
> static char *target_buf;
> @@ -1429,6 +1430,7 @@ enum {
> PACKET_QCatchSyscalls,
> PACKET_QProgramSignals,
> PACKET_QStartupWithShell,
> + PACKET_QEnvironmentHexEncoded,
> PACKET_qCRC,
> PACKET_qSearch_memory,
> PACKET_vAttach,
> @@ -4636,6 +4638,8 @@ static const struct protocol_feature remote_protocol_features[] = {
> PACKET_QProgramSignals },
> { "QStartupWithShell", PACKET_DISABLE, remote_supported_packet,
> PACKET_QStartupWithShell },
> + { "QEnvironmentHexEncoded", PACKET_DISABLE, remote_supported_packet,
> + PACKET_QEnvironmentHexEncoded },
> { "QStartNoAckMode", PACKET_DISABLE, remote_supported_packet,
> PACKET_QStartNoAckMode },
> { "multiprocess", PACKET_DISABLE, remote_supported_packet,
> @@ -9585,6 +9589,44 @@ extended_remote_run (const std::string &args)
> }
> }
>
> +/* Helper function to handle the QEnvironmentHexEncoded packet and
> + send the user-defined environment variables to the remote. */
> +
> +static void
> +extended_remote_environment_support (struct remote_state *rs)
> +{
> + gdb_environ *e = ¤t_inferior ()->environment;
> + const char **user_envp = e->user_envp ();
> +
> + for (int i = 0; user_envp[i] != NULL; ++i)
> + {
> + char *encoded_fullvar;
> + const char *fullvar = user_envp[i];
> + struct cleanup *c;
> +
> + encoded_fullvar = (char *) xmalloc (get_remote_packet_size ());
> + c = make_cleanup (xfree, encoded_fullvar);
> +
> + /* Convert the environment variable to an hex string, which
> + is the best format to be transmitted over the wire. */
> + bin2hex ((gdb_byte *) fullvar, encoded_fullvar, strlen (fullvar));
> +
> + xsnprintf (rs->buf, get_remote_packet_size (),
> + "QEnvironmentHexEncoded:%s", encoded_fullvar);
> +
> + if (remote_debug)
> + fprintf_unfiltered (gdb_stdlog, _("sending packet '%s'\n"),
> + rs->buf);
> +
> + putpkt (rs->buf);
> + getpkt (&rs->buf, &rs->buf_size, 0);
> + if (strcmp (rs->buf, "OK") != 0)
> + warning (_("Unable to set environment variable '%s' on remote."),
> + fullvar);
> + do_cleanups (c);
> + }
> +}
> +
> /* In the extended protocol we want to be able to do things like
> "run" and have them basically work as expected. So we need
> a special create_inferior function. We support changing the
> @@ -9625,6 +9667,9 @@ Remote replied unexpectedly while setting startup-with-shell: %s"),
> rs->buf);
> }
>
> + if (packet_support (PACKET_QEnvironmentHexEncoded) != PACKET_DISABLE)
> + extended_remote_environment_support (rs);
> +
> /* Now restart the remote server. */
> run_worked = extended_remote_run (args) != -1;
> if (!run_worked)
> @@ -14118,6 +14163,11 @@ Show the maximum size of the address (in bits) in a memory packet."), NULL,
> add_packet_config_cmd (&remote_protocol_packets[PACKET_QStartupWithShell],
> "QStartupWithShell", "startup-with-shell", 0);
>
> + add_packet_config_cmd (&remote_protocol_packets
> + [PACKET_QEnvironmentHexEncoded],
> + "QEnvironmentHexEncoded", "environment-hex-encoded",
> + 0);
> +
> add_packet_config_cmd (&remote_protocol_packets[PACKET_qSymbol],
> "qSymbol", "symbol-lookup", 0);
>
> diff --git a/gdb/testsuite/gdb.base/share-env-with-gdbserver.c b/gdb/testsuite/gdb.base/share-env-with-gdbserver.c
> new file mode 100644
> index 0000000..740bd0f
> --- /dev/null
> +++ b/gdb/testsuite/gdb.base/share-env-with-gdbserver.c
> @@ -0,0 +1,40 @@
> +/* This testcase is part of GDB, the GNU debugger.
> +
> + Copyright 2017 Free Software Foundation, Inc.
> +
> + This program is free software; you can redistribute it and/or modify
> + it under the terms of the GNU General Public License as published by
> + the Free Software Foundation; either version 3 of the License, or
> + (at your option) any later version.
> +
> + This program is distributed in the hope that it will be useful,
> + but WITHOUT ANY WARRANTY; without even the implied warranty of
> + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
> + GNU General Public License for more details.
> +
> + You should have received a copy of the GNU General Public License
> + along with this program. If not, see <http://www.gnu.org/licenses/>. */
> +
> +#include <stdio.h>
> +#include <stdlib.h>
> +
> +/* Wrapper around getenv for GDB. */
> +
> +static const char *
> +my_getenv (const char *name)
> +{
> + return getenv (name);
> +}
> +
> +int
> +main (int argc, char *argv[])
> +{
> + const char *myvar = getenv ("GDB_TEST_VAR");
> +
> + if (myvar != NULL)
> + printf ("It worked! myvar = '%s'\n", myvar);
> + else
> + printf ("It failed.");
> +
> + return 0; /* break-here */
> +}
> diff --git a/gdb/testsuite/gdb.base/share-env-with-gdbserver.exp b/gdb/testsuite/gdb.base/share-env-with-gdbserver.exp
> new file mode 100644
> index 0000000..5cae141
> --- /dev/null
> +++ b/gdb/testsuite/gdb.base/share-env-with-gdbserver.exp
> @@ -0,0 +1,105 @@
> +# This testcase is part of GDB, the GNU debugger.
> +
> +# Copyright 2017 Free Software Foundation, Inc.
> +
> +# This program is free software; you can redistribute it and/or modify
> +# it under the terms of the GNU General Public License as published by
> +# the Free Software Foundation; either version 3 of the License, or
> +# (at your option) any later version.
> +#
> +# This program is distributed in the hope that it will be useful,
> +# but WITHOUT ANY WARRANTY; without even the implied warranty of
> +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
> +# GNU General Public License for more details.
> +#
> +# You should have received a copy of the GNU General Public License
> +# along with this program. If not, see <http://www.gnu.org/licenses/>.
> +
> +# This test doesn't make sense on native-gdbserver.
> +if { [use_gdb_stub] } {
> + untested "not supported"
> + return
> +}
> +
> +standard_testfile
> +
> +if { [prepare_for_testing "failed to prepare" $testfile $srcfile debug] } {
> + return -1
> +}
> +
> +set test_var_name "GDB_TEST_VAR"
> +
> +# Helper function that does the actual testing.
> +#
> +# - VAR_VALUE is the value of the environment variable.
> +#
> +# - VAR_NAME is the name of the environment variable. If empty,
> +# defaults to $test_var_name.
> +#
> +# - VAR_NAME_MATCH is the name (regex) that will be used to query the
> +# environment about the variable (via getenv). This is useful when
> +# we're testing variables with strange names (e.g., with an equal
> +# sign in the name) and we know that the variable will actually be
> +# set using another name. If empty, defatults, to $var_name.
> +#
> +# - VAR_VALUE_MATCH is the value (regex) that will be used to match
> +# the result of getenv. The rationale is the same as explained for
> +# VAR_NAME_MATCH. If empty, defaults, to $var_value.
> +
> +proc do_test { var_value { var_name "" } { var_name_match "" } { var_value_match "" } } {
> + global hex decimal binfile test_var_name
> +
> + clean_restart $binfile
> +
> + if { $var_name == "" } {
> + set var_name $test_var_name
> + }
> +
> + if { $var_name_match == "" } {
> + set var_name_match $var_name
> + }
> +
> + if { $var_value_match == "" } {
> + set var_value_match $var_value
> + }
> +
> + if { $var_value != "" } {
> + gdb_test_no_output "set environment $var_name = $var_value" \
> + "set $var_name = $var_value"
> + } else {
> + gdb_test "set environment $var_name =" \
> + "Setting environment variable \"$var_name\" to null value." \
> + "set $var_name to null value"
> + }
> +
> + if { ![runto_main] } {
> + return -1
> + }
> +
> + gdb_breakpoint [gdb_get_line_number "break-here"]
> +
> + gdb_test "continue" "Breakpoint $decimal, main \\\(argc=1, argv=$hex\\\) at.*" \
> + "continue until breakpoint"
> +
> + gdb_test "print my_getenv (\"$var_name_match\")" "\\\$$decimal = $hex \"$var_value_match\"" \
> + "print result of getenv for $var_name"
> +}
> +
> +with_test_prefix "long var value" {
> + do_test "this is my test variable; testing long vars; {}"
> +}
> +
> +with_test_prefix "empty var" {
> + do_test ""
> +}
> +
> +with_test_prefix "strange named var" {
> + # In this test we're doing the following:
> + #
> + # (gdb) set environment 'asd =' = 123 43; asd b ### [];;;
> + #
> + # However, due to how GDB parses this line, the environment
> + # variable will end up named <'asd> (without the <>), and its
> + # value will be <' = 123 43; asd b ### [];;;> (without the <>).
> + do_test "123 43; asd b ### [];;;" "'asd ='" "'asd" "' = 123 43; asd b ### [];;;"
> +}
> diff --git a/gdb/unittests/environ-selftests.c b/gdb/unittests/environ-selftests.c
> index 28b16f8..b740583 100644
> --- a/gdb/unittests/environ-selftests.c
> +++ b/gdb/unittests/environ-selftests.c
> @@ -38,6 +38,7 @@ run_tests ()
> /* When the vector is initialized, there should always be one NULL
> element in it. */
> SELF_CHECK (env.envp ()[0] == NULL);
> + SELF_CHECK (env.user_envp ()[0] == NULL);
>
> /* Make sure that there is no other element. */
> SELF_CHECK (env.get ("PWD") == NULL);
> @@ -45,14 +46,20 @@ run_tests ()
> /* Check if unset followed by a set in an empty vector works. */
> env.set ("PWD", "test");
> SELF_CHECK (strcmp (env.get ("PWD"), "test") == 0);
> + SELF_CHECK (strcmp (env.user_envp ()[0], "PWD=test") == 0);
> /* The second element must be NULL. */
> SELF_CHECK (env.envp ()[1] == NULL);
> + SELF_CHECK (env.user_envp ()[1] == NULL);
> env.unset ("PWD");
> SELF_CHECK (env.envp ()[0] == NULL);
> + SELF_CHECK (env.user_envp ()[0] == NULL);
>
> /* Initialize the environment vector using the host's environ. */
> env = gdb_environ::from_host_environ ();
>
> + /* The user-set list must be empty. */
> + SELF_CHECK (env.user_envp ()[0] == NULL);
> +
> /* Our test environment variable should be present at the
> vector. */
> SELF_CHECK (strcmp (env.get ("GDB_SELFTEST_ENVIRON"), "1") == 0);
> @@ -75,6 +82,7 @@ run_tests ()
> variable. */
> env.clear ();
> SELF_CHECK (env.envp ()[0] == NULL);
> + SELF_CHECK (env.user_envp ()[0] == NULL);
> SELF_CHECK (env.get ("GDB_SELFTEST_ENVIRON") == NULL);
>
> /* Reinitialize our environ vector using the host environ. We
> @@ -97,6 +105,9 @@ run_tests ()
> vector, but can still find B. */
> env.set ("GDB_SELFTEST_ENVIRON_1", "aaa");
> SELF_CHECK (strcmp (env.get ("GDB_SELFTEST_ENVIRON_1"), "aaa") == 0);
> + /* User-set environ var list must contain one element. */
> + SELF_CHECK (strcmp (env.user_envp ()[0], "GDB_SELFTEST_ENVIRON_1=aaa") == 0);
> + SELF_CHECK (env.user_envp ()[1] == NULL);
>
> env.set ("GDB_SELFTEST_ENVIRON_2", "bbb");
> SELF_CHECK (strcmp (env.get ("GDB_SELFTEST_ENVIRON_2"), "bbb") == 0);
> @@ -104,6 +115,10 @@ run_tests ()
> env.unset ("GDB_SELFTEST_ENVIRON_1");
> SELF_CHECK (env.get ("GDB_SELFTEST_ENVIRON_1") == NULL);
> SELF_CHECK (strcmp (env.get ("GDB_SELFTEST_ENVIRON_2"), "bbb") == 0);
> + /* The user-set environ var list must contain only one element
> + now. */
> + SELF_CHECK (strcmp (env.user_envp ()[0], "GDB_SELFTEST_ENVIRON_2=bbb") == 0);
> + SELF_CHECK (env.user_envp ()[1] == NULL);
>
> env.clear ();
>
> @@ -111,11 +126,16 @@ run_tests ()
> valid state (i.e., its only element is NULL). */
> env.set ("A", "1");
> SELF_CHECK (strcmp (env.get ("A"), "1") == 0);
> + SELF_CHECK (strcmp (env.user_envp ()[0], "A=1") == 0);
> + SELF_CHECK (env.user_envp ()[1] == NULL);
> gdb_environ env2;
> env2 = std::move (env);
> SELF_CHECK (env.envp ()[0] == NULL);
> SELF_CHECK (strcmp (env2.get ("A"), "1") == 0);
> SELF_CHECK (env2.envp ()[1] == NULL);
> + SELF_CHECK (env.user_envp ()[0] == NULL);
> + SELF_CHECK (strcmp (env2.user_envp ()[0], "A=1") == 0);
> + SELF_CHECK (env2.user_envp ()[1] == NULL);
> env.set ("B", "2");
> SELF_CHECK (strcmp (env.get ("B"), "2") == 0);
> SELF_CHECK (env.envp ()[1] == NULL);
> @@ -125,18 +145,26 @@ run_tests ()
> env.clear ();
> env.set ("A", "1");
> SELF_CHECK (strcmp (env.get ("A"), "1") == 0);
> + SELF_CHECK (strcmp (env.user_envp ()[0], "A=1") == 0);
> gdb_environ env3 = std::move (env);
> SELF_CHECK (env.envp ()[0] == NULL);
> + SELF_CHECK (env.user_envp ()[0] == NULL);
> SELF_CHECK (strcmp (env3.get ("A"), "1") == 0);
> SELF_CHECK (env3.envp ()[1] == NULL);
> + SELF_CHECK (strcmp (env3.user_envp ()[0], "A=1") == 0);
> + SELF_CHECK (env3.user_envp ()[1] == NULL);
> env.set ("B", "2");
> SELF_CHECK (strcmp (env.get ("B"), "2") == 0);
> SELF_CHECK (env.envp ()[1] == NULL);
> + SELF_CHECK (strcmp (env.user_envp ()[0], "B=2") == 0);
> + SELF_CHECK (env.user_envp ()[2] == NULL);
>
> /* Test self-move. */
> env.clear ();
> env.set ("A", "1");
> SELF_CHECK (strcmp (env.get ("A"), "1") == 0);
> + SELF_CHECK (strcmp (env.user_envp ()[0], "A=1") == 0);
> + SELF_CHECK (env.user_envp ()[1] == NULL);
>
> /* Some compilers warn about moving to self, but that's precisely what we want
> to test here, so turn this warning off. */
> @@ -148,6 +176,8 @@ run_tests ()
> SELF_CHECK (strcmp (env.get ("A"), "1") == 0);
> SELF_CHECK (strcmp (env.envp ()[0], "A=1") == 0);
> SELF_CHECK (env.envp ()[1] == NULL);
> + SELF_CHECK (strcmp (env.user_envp ()[0], "A=1") == 0);
> + SELF_CHECK (env.user_envp ()[1] == NULL);
> }
> } /* namespace gdb_environ */
> } /* namespace selftests */
> --
> 2.9.3
--
Sergio
GPG key ID: 237A 54B1 0287 28BF 00EF 31F4 D0EB 7628 65FC 5E36
Please send encrypted e-mail if possible
http://sergiodj.net/
^ permalink raw reply [flat|nested] 35+ messages in thread
* Re: [PATCH] Implement the ability to transmit environment variables to GDBserver when starting the inferior
2017-06-29 19:41 [PATCH] Implement the ability to transmit environment variables to GDBserver when starting the inferior Sergio Durigan Junior
2017-07-10 21:32 ` Sergio Durigan Junior
@ 2017-07-13 21:47 ` Simon Marchi
2017-07-14 17:34 ` Sergio Durigan Junior
2017-07-27 3:36 ` [PATCH v2] Implement the ability to set/unset " Sergio Durigan Junior
` (3 subsequent siblings)
5 siblings, 1 reply; 35+ messages in thread
From: Simon Marchi @ 2017-07-13 21:47 UTC (permalink / raw)
To: Sergio Durigan Junior; +Cc: GDB Patches, Pedro Alves
On 2017-06-29 21:41, Sergio Durigan Junior wrote:
> This is the first version of this patch, which aims to implement the
> ability to transmit user-set environment variables to GDBserver when
> starting the remote inferior.
>
> User-set environment variables are only the variables that are
> explicitly set by the user, using the 'set environment' command. This
> means that variables that were already present in the environment when
> starting GDB/GDBserver are not transmitted/considered by this feature.
>
> The idea behind this patch is to store user-set environment variables
> in a separate vector, part of gdb_environ, and provide this list to
> extended_remote_create_inferior when the preparations to start the
> inferior are happening. extended_remote_create_inferior, then,
> iterates over this list and sends a new packet,
> QEnvironmentHexEncoded, which contains the hex-encoded string that
> represents the "VAR=VALUE" format (VALUE can be empty if the user set
> a variable with a null value, by doing 'set environment VAR=').
>
> The QEnvironmentHexEncoded packet is inspired on LLDB's extensions to
> the RSP. Details about it can be seen here:
>
>
> <https://raw.githubusercontent.com/llvm-mirror/lldb/master/docs/lldb-gdb-remote.txt>
>
> I decided not to implement the QEnvironment packet because it is
> considered deprecated by LLDB. This packet, on LLDB, serves the same
> purpose of QEnvironmentHexEncoded, but sends the information using a
> plain text, non-hex-encoded string.
>
> This patch also includes updates to the documentation, testsuite, and
> unit tests, without introducing regressions.
>
> gdb/ChangeLog:
> yyyy-mm-dd Sergio Durigan Junior <sergiodj@redhat.com>
>
> * NEWS (Changes since GDB 8.0): Add entry mentioning new support
> for sending environment variables to GDBserver.
> (New remote packets): Add entry of QEnvironmentHexEncoded.
> * common/environ.c (gdb_environ::operator=): Extend method to
> handle m_user_env_list.
> (gdb_environ::clear): Likewise.
> (match_var_in_string): Change type of first parameter from 'char
> *' to 'const char *'.
> (gdb_environ::get): New variable 'fullvar'. Handle
> m_user_env_list.
> (gdb_environ::unset): Extend method to handle m_user_env_list.
> (gdb_environ::user_envp): New method.
> * common/environ.h (gdb_environ): Add NULL to m_user_env_list;
> handle m_user_env_list on move constructor/assignment.
> (user_envp): New method.
> (m_user_env_list): New vector.
> * remote.c: Include "environ.h". Add QEnvironmentHexEncoded.
> (remote_protocol_features): Add
> QEnvironmentHexEncoded packet.
> (extended_remote_environment_support): New function.
> (extended_remote_create_inferior): Call
> extended_remote_environment_support.
> (_initialize_remote): Add QEnvironmentHexEncoded packet config.
> * unittests/environ-selftests.c (run_tests): Add tests for
> m_user_env_list.
>
> gdb/gdbserver/ChangeLog:
> yyyy-mm-dd Sergio Durigan Junior <sergiodj@redhat.com>
>
> * server.c (handle_general_set): Handle QEnvironmentHexEncoded
> packet.
> (handle_query): Inform remote that QEnvironmentHexEncoded is
> supported.
>
> gdb/doc/ChangeLog:
> yyyy-mm-dd Sergio Durigan Junior <sergiodj@redhat.com>
>
> * gdb.texinfo (set environment): Add @anchor. Explain that
> environment variables set by the user are sent to GDBserver.
> (Connecting) <Remote Packet>: Add "environment-hex-encoded"
> and "QEnvironmentHexEncoded" to the table.
> (Remote Protocol) <QEnvironmentHexEncoded>: New item, explaining
> the packet.
>
> gdb/testsuite/ChangeLog:
> yyyy-mm-dd Sergio Durigan Junior <sergiodj@redhat.com>
>
> * gdb.base/share-env-with-gdbserver.c: New file.
> * gdb.base/share-env-with-gdbserver.exp: Likewise.
> ---
> gdb/NEWS | 12 +++
> gdb/common/environ.c | 59 +++++++++---
> gdb/common/environ.h | 14 ++-
> gdb/doc/gdb.texinfo | 45 +++++++++
> gdb/gdbserver/server.c | 54 ++++++++++-
> gdb/remote.c | 50 ++++++++++
> gdb/testsuite/gdb.base/share-env-with-gdbserver.c | 40 ++++++++
> .../gdb.base/share-env-with-gdbserver.exp | 105
> +++++++++++++++++++++
> gdb/unittests/environ-selftests.c | 30 ++++++
> 9 files changed, 395 insertions(+), 14 deletions(-)
> create mode 100644 gdb/testsuite/gdb.base/share-env-with-gdbserver.c
> create mode 100644 gdb/testsuite/gdb.base/share-env-with-gdbserver.exp
>
> diff --git a/gdb/NEWS b/gdb/NEWS
> index 7c8a8f6..9026733 100644
> --- a/gdb/NEWS
> +++ b/gdb/NEWS
> @@ -3,6 +3,14 @@
>
> *** Changes since GDB 8.0
>
> +* On Unix systems, GDB now supports transmitting environment variables
> + to GDBserver that are to be passed to the inferior when it is being
> + started.
> +
> + To inform GDB of environment variables that are to be transmitted to
> + GDBserver, use the "set environment" command. Only user set
> + environment variables are sent to GDBserver.
> +
> * On Unix systems, GDBserver now does globbing expansion and variable
> substitution in inferior command line arguments.
>
> @@ -14,6 +22,10 @@
>
> * New remote packets
>
> +QEnvironmentHexEncoded
> + Inform GDBserver of an environment variable that is to be passed to
> + the inferior when starting it.
> +
> QStartupWithShell
> Indicates whether the inferior must be started with a shell or not.
>
> diff --git a/gdb/common/environ.c b/gdb/common/environ.c
> index 698bda3..da0af98 100644
> --- a/gdb/common/environ.c
> +++ b/gdb/common/environ.c
> @@ -30,8 +30,11 @@ gdb_environ::operator= (gdb_environ &&e)
> return *this;
>
> m_environ_vector = std::move (e.m_environ_vector);
> + m_user_env_list = std::move (e.m_user_env_list);
> e.m_environ_vector.clear ();
> + e.m_user_env_list.clear ();
> e.m_environ_vector.push_back (NULL);
> + e.m_user_env_list.push_back (NULL);
> return *this;
> }
>
> @@ -63,8 +66,10 @@ gdb_environ::clear ()
> for (char *v : m_environ_vector)
> xfree (v);
> m_environ_vector.clear ();
> + m_user_env_list.clear ();
> /* Always add the NULL element. */
> m_environ_vector.push_back (NULL);
> + m_user_env_list.push_back (NULL);
> }
>
> /* Helper function to check if STRING contains an environment variable
> @@ -72,7 +77,7 @@ gdb_environ::clear ()
> if it contains, false otherwise. */
>
> static bool
> -match_var_in_string (char *string, const char *var, size_t var_len)
> +match_var_in_string (const char *string, const char *var, size_t
> var_len)
> {
> if (strncmp (string, var, var_len) == 0 && string[var_len] == '=')
> return true;
> @@ -99,12 +104,14 @@ gdb_environ::get (const char *var) const
> void
> gdb_environ::set (const char *var, const char *value)
> {
> + char *fullvar = concat (var, "=", value, NULL);
> +
> /* We have to unset the variable in the vector if it exists. */
> unset (var);
>
> /* Insert the element before the last one, which is always NULL. */
> - m_environ_vector.insert (m_environ_vector.end () - 1,
> - concat (var, "=", value, NULL));
> + m_environ_vector.insert (m_environ_vector.end () - 1, fullvar);
> + m_user_env_list.insert (m_user_env_list.end () - 1, fullvar);
> }
>
> /* See common/environ.h. */
> @@ -113,18 +120,38 @@ void
> gdb_environ::unset (const char *var)
> {
> size_t len = strlen (var);
> + std::vector<char *>::iterator it_env;
> + std::vector<const char *>::iterator it_user_env;
> + char *found_var;
>
> /* We iterate until '.end () - 1' because the last element is
> always NULL. */
> - for (std::vector<char *>::iterator el = m_environ_vector.begin ();
> - el != m_environ_vector.end () - 1;
> - ++el)
> - if (match_var_in_string (*el, var, len))
> - {
> - xfree (*el);
> - m_environ_vector.erase (el);
> - break;
> - }
> + for (it_env = m_environ_vector.begin ();
> + it_env != m_environ_vector.end () - 1;
> + ++it_env)
> + if (match_var_in_string (*it_env, var, len))
> + break;
> +
> + if (it_env == m_environ_vector.end () - 1)
> + {
> + /* No element has been found. */
> + return;
> + }
> +
> + found_var = *it_env;
> +
> + /* We iterate until '.end () - 1' because the last element is
> + always NULL. */
> + for (it_user_env = m_user_env_list.begin ();
> + it_user_env != m_user_env_list.end () - 1;
> + ++it_user_env)
> + if (match_var_in_string (*it_user_env, var, len))
> + break;
In this second loop, can you search by pointer value instead? A value
in m_user_env_list will always have a duplicate value in
m_environ_vector, right? If so, std::remove might be a good option.
> +
> + m_environ_vector.erase (it_env);
> + if (it_user_env != m_user_env_list.end () - 1)
> + m_user_env_list.erase (it_user_env);
> + xfree (found_var);
> }
>
> /* See common/environ.h. */
> @@ -134,3 +161,11 @@ gdb_environ::envp () const
> {
> return const_cast<char **> (&m_environ_vector[0]);
> }
> +
> +/* See common/environ.h. */
> +
> +const char **
> +gdb_environ::user_envp () const
> +{
> + return const_cast<const char **> (&m_user_env_list[0]);
> +}
For m_environ_p, we have the envp method the way it is because we have
to pass it to execve. We don't have such limitation for
m_user_env_list. We don't need to add a NULL element, and user_envp can
return a const reference to the vector instead.
> diff --git a/gdb/common/environ.h b/gdb/common/environ.h
> index 0bbb191..cce7f87 100644
> --- a/gdb/common/environ.h
> +++ b/gdb/common/environ.h
> @@ -32,6 +32,7 @@ public:
> If/when we add more variables to it, NULL will always be the
> last element. */
> m_environ_vector.push_back (NULL);
> + m_user_env_list.push_back (NULL);
> }
>
> ~gdb_environ ()
> @@ -41,12 +42,15 @@ public:
>
> /* Move constructor. */
> gdb_environ (gdb_environ &&e)
> - : m_environ_vector (std::move (e.m_environ_vector))
> + : m_environ_vector (std::move (e.m_environ_vector)),
> + m_user_env_list (std::move (e.m_user_env_list))
> {
> /* Make sure that the moved-from vector is left at a valid
> state (only one NULL element). */
> e.m_environ_vector.clear ();
> + e.m_user_env_list.clear ();
> e.m_environ_vector.push_back (NULL);
> + e.m_user_env_list.push_back (NULL);
> }
>
> /* Move assignment. */
> @@ -73,9 +77,17 @@ public:
> /* Return the environment vector represented as a 'char **'. */
> char **envp () const;
>
> + /* Return the user-set environment vector represented as a 'const
> + char **'. */
> + const char **user_envp () const;
> +
> private:
> /* A vector containing the environment variables. */
> std::vector<char *> m_environ_vector;
> +
> + /* The vector containing the environment variables set by the
> + user. */
> + std::vector<const char *> m_user_env_list;
> };
>
> #endif /* defined (ENVIRON_H) */
> diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
> index c167a86..d632523 100644
> --- a/gdb/doc/gdb.texinfo
> +++ b/gdb/doc/gdb.texinfo
> @@ -2363,6 +2363,7 @@ print the names and values of all environment
> variables to be given to
> your program. You can abbreviate @code{environment} as @code{env}.
>
> @kindex set environment
> +@anchor{set environment}
> @item set environment @var{varname} @r{[}=@var{value}@r{]}
> Set environment variable @var{varname} to @var{value}. The value
> changes for your program (and the shell @value{GDBN} uses to launch
> @@ -2391,6 +2392,10 @@ If necessary, you can avoid that by using the
> @samp{env} program as a
> wrapper instead of using @code{set environment}. @xref{set
> exec-wrapper}, for an example doing just that.
>
> +Environment variables that are set by the user are also transmitted to
> +@command{gdbserver} to be used when starting the remote inferior.
> +@pxref{QEnvironmentHexEncoded}.
> +
> @kindex unset environment
> @item unset environment @var{varname}
> Remove variable @var{varname} from the environment to be passed to
> your
> @@ -20816,6 +20821,10 @@ are:
> @tab @code{QStartupWithShell}
> @tab @code{set startup-with-shell}
>
> +@item @code{environment-hex-encoded}
> +@tab @code{QEnvironmentHexEncoded}
> +@tab @code{set environment}
> +
> @item @code{conditional-breakpoints-packet}
> @tab @code{Z0 and Z1}
> @tab @code{Support for target-side breakpoint condition evaluation}
> @@ -36480,6 +36489,42 @@ actually support starting the inferior using a
> shell.
> Use of this packet is controlled by the @code{set startup-with-shell}
> command; @pxref{set startup-with-shell}.
>
> +@item QEnvironmentHexEncoded:@var{hex-value}
> +@anchor{QEnvironmentHexEncoded}
> +@cindex environment variable, remote request
> +@cindex @samp{QEnvironmentHexEncoded} packet
> +On UNIX-like targets, it is possible to set environment variables that
> +will be passed to the inferior during the startup process. This
> +packet is used to inform @command{gdbserver} of an environment
> +variable that has been defined by the user on @value{GDBN} (@pxref{set
> +environment}).
> +
> +The packet is composed by @var{hex-value}, an hex encoded
> +representation of the @var{name=value} format representing an
> +environment variable. The name of the environment variable is
> +represented by @var{name}, and the value to be assigned to the
> +environment variable is represented by @var{value}. If the variable
> +has no value (i.e., the value is @code{null}), then @var{value} will
> +not be present.
> +
> +This packet is only available in extended mode (@pxref{extended
> +mode}).
> +
> +Reply:
> +@table @samp
> +@item OK
> +The request succeeded.
> +@end table
> +
> +This packet is not probed by default; the remote stub must request it,
> +by supplying an appropriate @samp{qSupported} response
> +(@pxref{qSupported}). This should only be done on targets that
> +actually support passing environment variables to the starting
> +inferior.
> +
> +This packet is related to the @code{set environment} command;
> +@pxref{set environment}.
> +
> @item qfThreadInfo
> @itemx qsThreadInfo
> @cindex list active threads, remote request
> diff --git a/gdb/gdbserver/server.c b/gdb/gdbserver/server.c
> index 3838351..622898d 100644
> --- a/gdb/gdbserver/server.c
> +++ b/gdb/gdbserver/server.c
> @@ -631,6 +631,57 @@ handle_general_set (char *own_buf)
> return;
> }
>
> + if (startswith (own_buf, "QEnvironmentHexEncoded:"))
> + {
> + const char *p = own_buf + sizeof ("QEnvironmentHexEncoded:") -
> 1;
> + /* The final form of the environment variable. FINAL_VAR will
> + hold the 'VAR=VALUE' format. */
> + char *final_var = (char *) xcalloc (1, strlen (p) * 2);
> + struct cleanup *c = make_cleanup (xfree, final_var);
> + /* VAR_NAME will hold the environment variable name; VAR_VALUE
> + will hold its value. These will be just pointers to
> + FINAL_VAR. */
> + char *var_name, *var_value;
> +
> + if (remote_debug)
> + debug_printf (_("[QEnvironmentHexEncoded received '%s']\n"), p);
> +
> + /* Un-hexify the string received from the remote, which should
> + be in the form 'VAR=VALUE'. */
> + hex2bin (p, (gdb_byte *) final_var, strlen (p));
> +
> + if (remote_debug)
> + {
> + debug_printf (_("[Environment variable to be set: '%s']\n"),
> + final_var);
> + debug_flush ();
> + }
> +
> + var_name = final_var;
> + var_value = strchr (final_var, '=');
> + /* There should always be an equal sign, even if the variable is
> + empty. */
> + if (var_value == NULL)
> + {
> + warning (_("Unknown environment variable '%s' from remote side."),
> + final_var);
> + write_enn (own_buf);
> + do_cleanups (c);
> + return;
> + }
> + /* Mark the end of the variable's name. */
> + *var_value = '\0';
> + /* And skip the '=' sign (which now is the '\0'). */
> + ++var_value;
> +
> + /* Finally, set the variable to be passed to the inferior. */
> + our_environ.set (var_name, var_value);
> +
> + write_ok (own_buf);
> + do_cleanups (c);
> + return;
> + }
> +
> if (strcmp (own_buf, "QStartNoAckMode") == 0)
> {
> if (remote_debug)
> @@ -2228,7 +2279,8 @@ handle_query (char *own_buf, int packet_len, int
> *new_packet_len_p)
> }
>
> sprintf (own_buf,
> -
> "PacketSize=%x;QPassSignals+;QProgramSignals+;QStartupWithShell+",
> + "PacketSize=%x;QPassSignals+;QProgramSignals+;"
> + "QStartupWithShell+;QEnvironmentHexEncoded+",
> PBUFSIZ - 1);
>
> if (target_supports_catch_syscall ())
> diff --git a/gdb/remote.c b/gdb/remote.c
> index 8e8ee6f..8be6fd1 100644
> --- a/gdb/remote.c
> +++ b/gdb/remote.c
> @@ -72,6 +72,7 @@
> #include "btrace.h"
> #include "record-btrace.h"
> #include <algorithm>
> +#include "environ.h"
>
> /* Temp hacks for tracepoint encoding migration. */
> static char *target_buf;
> @@ -1429,6 +1430,7 @@ enum {
> PACKET_QCatchSyscalls,
> PACKET_QProgramSignals,
> PACKET_QStartupWithShell,
> + PACKET_QEnvironmentHexEncoded,
> PACKET_qCRC,
> PACKET_qSearch_memory,
> PACKET_vAttach,
> @@ -4636,6 +4638,8 @@ static const struct protocol_feature
> remote_protocol_features[] = {
> PACKET_QProgramSignals },
> { "QStartupWithShell", PACKET_DISABLE, remote_supported_packet,
> PACKET_QStartupWithShell },
> + { "QEnvironmentHexEncoded", PACKET_DISABLE, remote_supported_packet,
> + PACKET_QEnvironmentHexEncoded },
> { "QStartNoAckMode", PACKET_DISABLE, remote_supported_packet,
> PACKET_QStartNoAckMode },
> { "multiprocess", PACKET_DISABLE, remote_supported_packet,
> @@ -9585,6 +9589,44 @@ extended_remote_run (const std::string &args)
> }
> }
>
> +/* Helper function to handle the QEnvironmentHexEncoded packet and
> + send the user-defined environment variables to the remote. */
> +
> +static void
> +extended_remote_environment_support (struct remote_state *rs)
> +{
> + gdb_environ *e = ¤t_inferior ()->environment;
> + const char **user_envp = e->user_envp ();
> +
> + for (int i = 0; user_envp[i] != NULL; ++i)
> + {
> + char *encoded_fullvar;
> + const char *fullvar = user_envp[i];
> + struct cleanup *c;
> +
> + encoded_fullvar = (char *) xmalloc (get_remote_packet_size ());
> + c = make_cleanup (xfree, encoded_fullvar);
> +
> + /* Convert the environment variable to an hex string, which
> + is the best format to be transmitted over the wire. */
> + bin2hex ((gdb_byte *) fullvar, encoded_fullvar, strlen
> (fullvar));
A bin2hex version that returns an std::string would be nice :)
> +
> + xsnprintf (rs->buf, get_remote_packet_size (),
> + "QEnvironmentHexEncoded:%s", encoded_fullvar);
> +
> + if (remote_debug)
> + fprintf_unfiltered (gdb_stdlog, _("sending packet '%s'\n"),
> + rs->buf);
Is this useful? When "set debug remote" is active, we already have the
packets printed out, isn't it the same thing?
> +
> + putpkt (rs->buf);
> + getpkt (&rs->buf, &rs->buf_size, 0);
> + if (strcmp (rs->buf, "OK") != 0)
> + warning (_("Unable to set environment variable '%s' on remote."),
> + fullvar);
> + do_cleanups (c);
> + }
> +}
> +
> /* In the extended protocol we want to be able to do things like
> "run" and have them basically work as expected. So we need
> a special create_inferior function. We support changing the
> @@ -9625,6 +9667,9 @@ Remote replied unexpectedly while setting
> startup-with-shell: %s"),
> rs->buf);
> }
>
> + if (packet_support (PACKET_QEnvironmentHexEncoded) !=
> PACKET_DISABLE)
> + extended_remote_environment_support (rs);
It doesn't make a big difference, but I would prefer the check for
packet support in extended_remove_environment_support(), closer to the
code its related to. If we have to re-use that function for some
reason, the check will be already there so we can't forget it.
I have to go now, I haven't had time to look at the test yet.
One thing that worries me about the protocol is that there doesn't seem
to be a way to unset an environment variable (empty value is different
than unset right?). It doesn't seem possible to start an inferior with
just A=1 and another with just B=2. Once you set A=1, it's there for
good.
So do we need another packet for GDB to tell GDBserver "forget about all
variables set with QEnvironmentHexEncoded previously", like
QEnvironmentReset? GDB would send that just before setting the
variables again with QEnvironmentHexEncoded.
Simon
^ permalink raw reply [flat|nested] 35+ messages in thread
* Re: [PATCH] Implement the ability to transmit environment variables to GDBserver when starting the inferior
2017-07-13 21:47 ` Simon Marchi
@ 2017-07-14 17:34 ` Sergio Durigan Junior
0 siblings, 0 replies; 35+ messages in thread
From: Sergio Durigan Junior @ 2017-07-14 17:34 UTC (permalink / raw)
To: Simon Marchi; +Cc: GDB Patches, Pedro Alves
Thanks for the review, Simon. Comments below.
On Thursday, July 13 2017, Simon Marchi wrote:
> On 2017-06-29 21:41, Sergio Durigan Junior wrote:
>> This is the first version of this patch, which aims to implement the
>> ability to transmit user-set environment variables to GDBserver when
>> starting the remote inferior.
>>
>> User-set environment variables are only the variables that are
>> explicitly set by the user, using the 'set environment' command. This
>> means that variables that were already present in the environment when
>> starting GDB/GDBserver are not transmitted/considered by this feature.
>>
>> The idea behind this patch is to store user-set environment variables
>> in a separate vector, part of gdb_environ, and provide this list to
>> extended_remote_create_inferior when the preparations to start the
>> inferior are happening. extended_remote_create_inferior, then,
>> iterates over this list and sends a new packet,
>> QEnvironmentHexEncoded, which contains the hex-encoded string that
>> represents the "VAR=VALUE" format (VALUE can be empty if the user set
>> a variable with a null value, by doing 'set environment VAR=').
>>
>> The QEnvironmentHexEncoded packet is inspired on LLDB's extensions to
>> the RSP. Details about it can be seen here:
>>
>>
>> <https://raw.githubusercontent.com/llvm-mirror/lldb/master/docs/lldb-gdb-remote.txt>
>>
>> I decided not to implement the QEnvironment packet because it is
>> considered deprecated by LLDB. This packet, on LLDB, serves the same
>> purpose of QEnvironmentHexEncoded, but sends the information using a
>> plain text, non-hex-encoded string.
>>
>> This patch also includes updates to the documentation, testsuite, and
>> unit tests, without introducing regressions.
>>
>> gdb/ChangeLog:
>> yyyy-mm-dd Sergio Durigan Junior <sergiodj@redhat.com>
>>
>> * NEWS (Changes since GDB 8.0): Add entry mentioning new support
>> for sending environment variables to GDBserver.
>> (New remote packets): Add entry of QEnvironmentHexEncoded.
>> * common/environ.c (gdb_environ::operator=): Extend method to
>> handle m_user_env_list.
>> (gdb_environ::clear): Likewise.
>> (match_var_in_string): Change type of first parameter from 'char
>> *' to 'const char *'.
>> (gdb_environ::get): New variable 'fullvar'. Handle
>> m_user_env_list.
>> (gdb_environ::unset): Extend method to handle m_user_env_list.
>> (gdb_environ::user_envp): New method.
>> * common/environ.h (gdb_environ): Add NULL to m_user_env_list;
>> handle m_user_env_list on move constructor/assignment.
>> (user_envp): New method.
>> (m_user_env_list): New vector.
>> * remote.c: Include "environ.h". Add QEnvironmentHexEncoded.
>> (remote_protocol_features): Add
>> QEnvironmentHexEncoded packet.
>> (extended_remote_environment_support): New function.
>> (extended_remote_create_inferior): Call
>> extended_remote_environment_support.
>> (_initialize_remote): Add QEnvironmentHexEncoded packet config.
>> * unittests/environ-selftests.c (run_tests): Add tests for
>> m_user_env_list.
>>
>> gdb/gdbserver/ChangeLog:
>> yyyy-mm-dd Sergio Durigan Junior <sergiodj@redhat.com>
>>
>> * server.c (handle_general_set): Handle QEnvironmentHexEncoded
>> packet.
>> (handle_query): Inform remote that QEnvironmentHexEncoded is
>> supported.
>>
>> gdb/doc/ChangeLog:
>> yyyy-mm-dd Sergio Durigan Junior <sergiodj@redhat.com>
>>
>> * gdb.texinfo (set environment): Add @anchor. Explain that
>> environment variables set by the user are sent to GDBserver.
>> (Connecting) <Remote Packet>: Add "environment-hex-encoded"
>> and "QEnvironmentHexEncoded" to the table.
>> (Remote Protocol) <QEnvironmentHexEncoded>: New item, explaining
>> the packet.
>>
>> gdb/testsuite/ChangeLog:
>> yyyy-mm-dd Sergio Durigan Junior <sergiodj@redhat.com>
>>
>> * gdb.base/share-env-with-gdbserver.c: New file.
>> * gdb.base/share-env-with-gdbserver.exp: Likewise.
>> ---
>> gdb/NEWS | 12 +++
>> gdb/common/environ.c | 59 +++++++++---
>> gdb/common/environ.h | 14 ++-
>> gdb/doc/gdb.texinfo | 45 +++++++++
>> gdb/gdbserver/server.c | 54 ++++++++++-
>> gdb/remote.c | 50 ++++++++++
>> gdb/testsuite/gdb.base/share-env-with-gdbserver.c | 40 ++++++++
>> .../gdb.base/share-env-with-gdbserver.exp | 105
>> +++++++++++++++++++++
>> gdb/unittests/environ-selftests.c | 30 ++++++
>> 9 files changed, 395 insertions(+), 14 deletions(-)
>> create mode 100644 gdb/testsuite/gdb.base/share-env-with-gdbserver.c
>> create mode 100644 gdb/testsuite/gdb.base/share-env-with-gdbserver.exp
>>
>> diff --git a/gdb/NEWS b/gdb/NEWS
>> index 7c8a8f6..9026733 100644
>> --- a/gdb/NEWS
>> +++ b/gdb/NEWS
>> @@ -3,6 +3,14 @@
>>
>> *** Changes since GDB 8.0
>>
>> +* On Unix systems, GDB now supports transmitting environment variables
>> + to GDBserver that are to be passed to the inferior when it is being
>> + started.
>> +
>> + To inform GDB of environment variables that are to be transmitted to
>> + GDBserver, use the "set environment" command. Only user set
>> + environment variables are sent to GDBserver.
>> +
>> * On Unix systems, GDBserver now does globbing expansion and variable
>> substitution in inferior command line arguments.
>>
>> @@ -14,6 +22,10 @@
>>
>> * New remote packets
>>
>> +QEnvironmentHexEncoded
>> + Inform GDBserver of an environment variable that is to be passed to
>> + the inferior when starting it.
>> +
>> QStartupWithShell
>> Indicates whether the inferior must be started with a shell or not.
>>
>> diff --git a/gdb/common/environ.c b/gdb/common/environ.c
>> index 698bda3..da0af98 100644
>> --- a/gdb/common/environ.c
>> +++ b/gdb/common/environ.c
>> @@ -30,8 +30,11 @@ gdb_environ::operator= (gdb_environ &&e)
>> return *this;
>>
>> m_environ_vector = std::move (e.m_environ_vector);
>> + m_user_env_list = std::move (e.m_user_env_list);
>> e.m_environ_vector.clear ();
>> + e.m_user_env_list.clear ();
>> e.m_environ_vector.push_back (NULL);
>> + e.m_user_env_list.push_back (NULL);
>> return *this;
>> }
>>
>> @@ -63,8 +66,10 @@ gdb_environ::clear ()
>> for (char *v : m_environ_vector)
>> xfree (v);
>> m_environ_vector.clear ();
>> + m_user_env_list.clear ();
>> /* Always add the NULL element. */
>> m_environ_vector.push_back (NULL);
>> + m_user_env_list.push_back (NULL);
>> }
>>
>> /* Helper function to check if STRING contains an environment variable
>> @@ -72,7 +77,7 @@ gdb_environ::clear ()
>> if it contains, false otherwise. */
>>
>> static bool
>> -match_var_in_string (char *string, const char *var, size_t var_len)
>> +match_var_in_string (const char *string, const char *var, size_t
>> var_len)
>> {
>> if (strncmp (string, var, var_len) == 0 && string[var_len] == '=')
>> return true;
>> @@ -99,12 +104,14 @@ gdb_environ::get (const char *var) const
>> void
>> gdb_environ::set (const char *var, const char *value)
>> {
>> + char *fullvar = concat (var, "=", value, NULL);
>> +
>> /* We have to unset the variable in the vector if it exists. */
>> unset (var);
>>
>> /* Insert the element before the last one, which is always NULL. */
>> - m_environ_vector.insert (m_environ_vector.end () - 1,
>> - concat (var, "=", value, NULL));
>> + m_environ_vector.insert (m_environ_vector.end () - 1, fullvar);
>> + m_user_env_list.insert (m_user_env_list.end () - 1, fullvar);
>> }
>>
>> /* See common/environ.h. */
>> @@ -113,18 +120,38 @@ void
>> gdb_environ::unset (const char *var)
>> {
>> size_t len = strlen (var);
>> + std::vector<char *>::iterator it_env;
>> + std::vector<const char *>::iterator it_user_env;
>> + char *found_var;
>>
>> /* We iterate until '.end () - 1' because the last element is
>> always NULL. */
>> - for (std::vector<char *>::iterator el = m_environ_vector.begin ();
>> - el != m_environ_vector.end () - 1;
>> - ++el)
>> - if (match_var_in_string (*el, var, len))
>> - {
>> - xfree (*el);
>> - m_environ_vector.erase (el);
>> - break;
>> - }
>> + for (it_env = m_environ_vector.begin ();
>> + it_env != m_environ_vector.end () - 1;
>> + ++it_env)
>> + if (match_var_in_string (*it_env, var, len))
>> + break;
>> +
>> + if (it_env == m_environ_vector.end () - 1)
>> + {
>> + /* No element has been found. */
>> + return;
>> + }
>> +
>> + found_var = *it_env;
>> +
>> + /* We iterate until '.end () - 1' because the last element is
>> + always NULL. */
>> + for (it_user_env = m_user_env_list.begin ();
>> + it_user_env != m_user_env_list.end () - 1;
>> + ++it_user_env)
>> + if (match_var_in_string (*it_user_env, var, len))
>> + break;
>
> In this second loop, can you search by pointer value instead? A value
> in m_user_env_list will always have a duplicate value in
> m_environ_vector, right? If so, std::remove might be a good option.
That's true. I'll do that, thanks for the tip.
>> +
>> + m_environ_vector.erase (it_env);
>> + if (it_user_env != m_user_env_list.end () - 1)
>> + m_user_env_list.erase (it_user_env);
>> + xfree (found_var);
>> }
>>
>> /* See common/environ.h. */
>> @@ -134,3 +161,11 @@ gdb_environ::envp () const
>> {
>> return const_cast<char **> (&m_environ_vector[0]);
>> }
>> +
>> +/* See common/environ.h. */
>> +
>> +const char **
>> +gdb_environ::user_envp () const
>> +{
>> + return const_cast<const char **> (&m_user_env_list[0]);
>> +}
>
> For m_environ_p, we have the envp method the way it is because we have
> to pass it to execve. We don't have such limitation for
> m_user_env_list. We don't need to add a NULL element, and user_envp
> can return a const reference to the vector instead.
I was thinking about that, but was unsure if returning a direct
reference to the private vector was a good idea. One of my first drafts
even implemented it this way, but then I changed things. But I guess
you're right; there's no harm in returning a *const* reference to the
vector. I'll change the code accordingly.
>> diff --git a/gdb/common/environ.h b/gdb/common/environ.h
>> index 0bbb191..cce7f87 100644
>> --- a/gdb/common/environ.h
>> +++ b/gdb/common/environ.h
>> @@ -32,6 +32,7 @@ public:
>> If/when we add more variables to it, NULL will always be the
>> last element. */
>> m_environ_vector.push_back (NULL);
>> + m_user_env_list.push_back (NULL);
>> }
>>
>> ~gdb_environ ()
>> @@ -41,12 +42,15 @@ public:
>>
>> /* Move constructor. */
>> gdb_environ (gdb_environ &&e)
>> - : m_environ_vector (std::move (e.m_environ_vector))
>> + : m_environ_vector (std::move (e.m_environ_vector)),
>> + m_user_env_list (std::move (e.m_user_env_list))
>> {
>> /* Make sure that the moved-from vector is left at a valid
>> state (only one NULL element). */
>> e.m_environ_vector.clear ();
>> + e.m_user_env_list.clear ();
>> e.m_environ_vector.push_back (NULL);
>> + e.m_user_env_list.push_back (NULL);
>> }
>>
>> /* Move assignment. */
>> @@ -73,9 +77,17 @@ public:
>> /* Return the environment vector represented as a 'char **'. */
>> char **envp () const;
>>
>> + /* Return the user-set environment vector represented as a 'const
>> + char **'. */
>> + const char **user_envp () const;
>> +
>> private:
>> /* A vector containing the environment variables. */
>> std::vector<char *> m_environ_vector;
>> +
>> + /* The vector containing the environment variables set by the
>> + user. */
>> + std::vector<const char *> m_user_env_list;
>> };
>>
>> #endif /* defined (ENVIRON_H) */
>> diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
>> index c167a86..d632523 100644
>> --- a/gdb/doc/gdb.texinfo
>> +++ b/gdb/doc/gdb.texinfo
>> @@ -2363,6 +2363,7 @@ print the names and values of all environment
>> variables to be given to
>> your program. You can abbreviate @code{environment} as @code{env}.
>>
>> @kindex set environment
>> +@anchor{set environment}
>> @item set environment @var{varname} @r{[}=@var{value}@r{]}
>> Set environment variable @var{varname} to @var{value}. The value
>> changes for your program (and the shell @value{GDBN} uses to launch
>> @@ -2391,6 +2392,10 @@ If necessary, you can avoid that by using the
>> @samp{env} program as a
>> wrapper instead of using @code{set environment}. @xref{set
>> exec-wrapper}, for an example doing just that.
>>
>> +Environment variables that are set by the user are also transmitted to
>> +@command{gdbserver} to be used when starting the remote inferior.
>> +@pxref{QEnvironmentHexEncoded}.
>> +
>> @kindex unset environment
>> @item unset environment @var{varname}
>> Remove variable @var{varname} from the environment to be passed to
>> your
>> @@ -20816,6 +20821,10 @@ are:
>> @tab @code{QStartupWithShell}
>> @tab @code{set startup-with-shell}
>>
>> +@item @code{environment-hex-encoded}
>> +@tab @code{QEnvironmentHexEncoded}
>> +@tab @code{set environment}
>> +
>> @item @code{conditional-breakpoints-packet}
>> @tab @code{Z0 and Z1}
>> @tab @code{Support for target-side breakpoint condition evaluation}
>> @@ -36480,6 +36489,42 @@ actually support starting the inferior
>> using a shell.
>> Use of this packet is controlled by the @code{set startup-with-shell}
>> command; @pxref{set startup-with-shell}.
>>
>> +@item QEnvironmentHexEncoded:@var{hex-value}
>> +@anchor{QEnvironmentHexEncoded}
>> +@cindex environment variable, remote request
>> +@cindex @samp{QEnvironmentHexEncoded} packet
>> +On UNIX-like targets, it is possible to set environment variables that
>> +will be passed to the inferior during the startup process. This
>> +packet is used to inform @command{gdbserver} of an environment
>> +variable that has been defined by the user on @value{GDBN} (@pxref{set
>> +environment}).
>> +
>> +The packet is composed by @var{hex-value}, an hex encoded
>> +representation of the @var{name=value} format representing an
>> +environment variable. The name of the environment variable is
>> +represented by @var{name}, and the value to be assigned to the
>> +environment variable is represented by @var{value}. If the variable
>> +has no value (i.e., the value is @code{null}), then @var{value} will
>> +not be present.
>> +
>> +This packet is only available in extended mode (@pxref{extended
>> +mode}).
>> +
>> +Reply:
>> +@table @samp
>> +@item OK
>> +The request succeeded.
>> +@end table
>> +
>> +This packet is not probed by default; the remote stub must request it,
>> +by supplying an appropriate @samp{qSupported} response
>> +(@pxref{qSupported}). This should only be done on targets that
>> +actually support passing environment variables to the starting
>> +inferior.
>> +
>> +This packet is related to the @code{set environment} command;
>> +@pxref{set environment}.
>> +
>> @item qfThreadInfo
>> @itemx qsThreadInfo
>> @cindex list active threads, remote request
>> diff --git a/gdb/gdbserver/server.c b/gdb/gdbserver/server.c
>> index 3838351..622898d 100644
>> --- a/gdb/gdbserver/server.c
>> +++ b/gdb/gdbserver/server.c
>> @@ -631,6 +631,57 @@ handle_general_set (char *own_buf)
>> return;
>> }
>>
>> + if (startswith (own_buf, "QEnvironmentHexEncoded:"))
>> + {
>> + const char *p = own_buf + sizeof ("QEnvironmentHexEncoded:")
>> -
>> 1;
>> + /* The final form of the environment variable. FINAL_VAR will
>> + hold the 'VAR=VALUE' format. */
>> + char *final_var = (char *) xcalloc (1, strlen (p) * 2);
>> + struct cleanup *c = make_cleanup (xfree, final_var);
>> + /* VAR_NAME will hold the environment variable name; VAR_VALUE
>> + will hold its value. These will be just pointers to
>> + FINAL_VAR. */
>> + char *var_name, *var_value;
>> +
>> + if (remote_debug)
>> + debug_printf (_("[QEnvironmentHexEncoded received '%s']\n"), p);
>> +
>> + /* Un-hexify the string received from the remote, which should
>> + be in the form 'VAR=VALUE'. */
>> + hex2bin (p, (gdb_byte *) final_var, strlen (p));
>> +
>> + if (remote_debug)
>> + {
>> + debug_printf (_("[Environment variable to be set: '%s']\n"),
>> + final_var);
>> + debug_flush ();
>> + }
>> +
>> + var_name = final_var;
>> + var_value = strchr (final_var, '=');
>> + /* There should always be an equal sign, even if the variable is
>> + empty. */
>> + if (var_value == NULL)
>> + {
>> + warning (_("Unknown environment variable '%s' from remote side."),
>> + final_var);
>> + write_enn (own_buf);
>> + do_cleanups (c);
>> + return;
>> + }
>> + /* Mark the end of the variable's name. */
>> + *var_value = '\0';
>> + /* And skip the '=' sign (which now is the '\0'). */
>> + ++var_value;
>> +
>> + /* Finally, set the variable to be passed to the inferior. */
>> + our_environ.set (var_name, var_value);
>> +
>> + write_ok (own_buf);
>> + do_cleanups (c);
>> + return;
>> + }
>> +
>> if (strcmp (own_buf, "QStartNoAckMode") == 0)
>> {
>> if (remote_debug)
>> @@ -2228,7 +2279,8 @@ handle_query (char *own_buf, int packet_len, int
>> *new_packet_len_p)
>> }
>>
>> sprintf (own_buf,
>> -
>> "PacketSize=%x;QPassSignals+;QProgramSignals+;QStartupWithShell+",
>> + "PacketSize=%x;QPassSignals+;QProgramSignals+;"
>> + "QStartupWithShell+;QEnvironmentHexEncoded+",
>> PBUFSIZ - 1);
>>
>> if (target_supports_catch_syscall ())
>> diff --git a/gdb/remote.c b/gdb/remote.c
>> index 8e8ee6f..8be6fd1 100644
>> --- a/gdb/remote.c
>> +++ b/gdb/remote.c
>> @@ -72,6 +72,7 @@
>> #include "btrace.h"
>> #include "record-btrace.h"
>> #include <algorithm>
>> +#include "environ.h"
>>
>> /* Temp hacks for tracepoint encoding migration. */
>> static char *target_buf;
>> @@ -1429,6 +1430,7 @@ enum {
>> PACKET_QCatchSyscalls,
>> PACKET_QProgramSignals,
>> PACKET_QStartupWithShell,
>> + PACKET_QEnvironmentHexEncoded,
>> PACKET_qCRC,
>> PACKET_qSearch_memory,
>> PACKET_vAttach,
>> @@ -4636,6 +4638,8 @@ static const struct protocol_feature
>> remote_protocol_features[] = {
>> PACKET_QProgramSignals },
>> { "QStartupWithShell", PACKET_DISABLE, remote_supported_packet,
>> PACKET_QStartupWithShell },
>> + { "QEnvironmentHexEncoded", PACKET_DISABLE, remote_supported_packet,
>> + PACKET_QEnvironmentHexEncoded },
>> { "QStartNoAckMode", PACKET_DISABLE, remote_supported_packet,
>> PACKET_QStartNoAckMode },
>> { "multiprocess", PACKET_DISABLE, remote_supported_packet,
>> @@ -9585,6 +9589,44 @@ extended_remote_run (const std::string &args)
>> }
>> }
>>
>> +/* Helper function to handle the QEnvironmentHexEncoded packet and
>> + send the user-defined environment variables to the remote. */
>> +
>> +static void
>> +extended_remote_environment_support (struct remote_state *rs)
>> +{
>> + gdb_environ *e = ¤t_inferior ()->environment;
>> + const char **user_envp = e->user_envp ();
>> +
>> + for (int i = 0; user_envp[i] != NULL; ++i)
>> + {
>> + char *encoded_fullvar;
>> + const char *fullvar = user_envp[i];
>> + struct cleanup *c;
>> +
>> + encoded_fullvar = (char *) xmalloc (get_remote_packet_size ());
>> + c = make_cleanup (xfree, encoded_fullvar);
>> +
>> + /* Convert the environment variable to an hex string, which
>> + is the best format to be transmitted over the wire. */
>> + bin2hex ((gdb_byte *) fullvar, encoded_fullvar, strlen
>> (fullvar));
>
> A bin2hex version that returns an std::string would be nice :)
Wouldn't it? ;-)
I'll see what can be done.
>> +
>> + xsnprintf (rs->buf, get_remote_packet_size (),
>> + "QEnvironmentHexEncoded:%s", encoded_fullvar);
>> +
>> + if (remote_debug)
>> + fprintf_unfiltered (gdb_stdlog, _("sending packet '%s'\n"),
>> + rs->buf);
>
> Is this useful? When "set debug remote" is active, we already have
> the packets printed out, isn't it the same thing?
Maybe change the message to print the variable name and its value
instead, then? Looking at this now, it doesn't really make sense to
print the full hexified packet.
>> +
>> + putpkt (rs->buf);
>> + getpkt (&rs->buf, &rs->buf_size, 0);
>> + if (strcmp (rs->buf, "OK") != 0)
>> + warning (_("Unable to set environment variable '%s' on remote."),
>> + fullvar);
>> + do_cleanups (c);
>> + }
>> +}
>> +
>> /* In the extended protocol we want to be able to do things like
>> "run" and have them basically work as expected. So we need
>> a special create_inferior function. We support changing the
>> @@ -9625,6 +9667,9 @@ Remote replied unexpectedly while setting
>> startup-with-shell: %s"),
>> rs->buf);
>> }
>>
>> + if (packet_support (PACKET_QEnvironmentHexEncoded) !=
>> PACKET_DISABLE)
>> + extended_remote_environment_support (rs);
>
> It doesn't make a big difference, but I would prefer the check for
> packet support in extended_remove_environment_support(), closer to the
> code its related to. If we have to re-use that function for some
> reason, the check will be already there so we can't forget it.
Sure, no problem.
> One thing that worries me about the protocol is that there doesn't
> seem to be a way to unset an environment variable (empty value is
> different than unset right?). It doesn't seem possible to start an
> inferior with just A=1 and another with just B=2. Once you set A=1,
> it's there for good.
Yeah, this was something in my mind since the beginning as well. My
plan, which I should have made clearer, is to tackle the "variable
setting" first,
> So do we need another packet for GDB to tell GDBserver "forget about
> all variables set with QEnvironmentHexEncoded previously", like
> QEnvironmentReset? GDB would send that just before setting the
> variables again with QEnvironmentHexEncoded.
Not only that, right? I think we could have another packet,
QEnvironmentUnset, which could also be used to unset specific variables.
For example, suppose that the user wants to unset an environment
variable that was set previously (i.e., before starting GDB). After
issuing an "unset env VAR" inside GDB, it makes sense to replicate this
to gdbserver as well.
I'll take a look at adding these other packets to the protocol and
extending the patch.
Comments about the testcase are also welcome, when you have a chance to
look at it.
Thanks,
--
Sergio
GPG key ID: 237A 54B1 0287 28BF 00EF 31F4 D0EB 7628 65FC 5E36
Please send encrypted e-mail if possible
http://sergiodj.net/
^ permalink raw reply [flat|nested] 35+ messages in thread
* [PATCH v2] Implement the ability to set/unset environment variables to GDBserver when starting the inferior
2017-06-29 19:41 [PATCH] Implement the ability to transmit environment variables to GDBserver when starting the inferior Sergio Durigan Junior
2017-07-10 21:32 ` Sergio Durigan Junior
2017-07-13 21:47 ` Simon Marchi
@ 2017-07-27 3:36 ` Sergio Durigan Junior
2017-07-27 17:10 ` Eli Zaretskii
` (3 more replies)
2017-08-13 6:19 ` [PATCH v3] " Sergio Durigan Junior
` (2 subsequent siblings)
5 siblings, 4 replies; 35+ messages in thread
From: Sergio Durigan Junior @ 2017-07-27 3:36 UTC (permalink / raw)
To: GDB Patches
Cc: Pedro Alves, Simon Marchi, Eli Zaretskii, Sergio Durigan Junior
Changes from v1:
- Implement suggestions by Simon.
- Improve gdb_environ::unset (use std::remove).
- Implement the ability to unset environment variables in the remote
target.
- Implement hex2str and str2hex.
- Extend unittest, testcase and documentation.
This version of the patch extends the first version/idea, which only
allowed environment variables to be set in the remote target. Now it
is also possible to unset them.
User-set environment variables are only the variables that are
explicitly set by the user, using the 'set environment' command. This
means that variables that were already present in the environment when
starting GDB/GDBserver are not transmitted/considered by this feature.
User-unset environment variables are variables that are explicitly
unset by the user, using the 'unset environment' command.
The idea behind this patch is to store user-set and user-unset
environment variables in two separate vectors, both part of
gdb_environ. Then, when extended_remote_create_inferior is preparing
to start the inferior, it will iterate over the two lists and
set/unset variables accordingly. Three new packets are introduced:
- QEnvironmentHexEncoded, which is used to set environment variables,
and contains an hex-encoded string in the format "VAR=VALUE" (VALUE
can be empty if the user set a variable with a null value, by doing
'set environment VAR=').
- QEnvironmentUnset, which is used to unset environment variables, and
contains an hex-encoded string in the format "VAR".
- QEnvironmentReset, which is always the first packet to be
transmitted, and is used to reset (i.e., unset any user-set
environment variable) the environment.
The QEnvironmentHexEncoded packet is inspired on LLDB's extensions to
the RSP. Details about it can be seen here:
<https://raw.githubusercontent.com/llvm-mirror/lldb/master/docs/lldb-gdb-remote.txt>
I decided not to implement the QEnvironment packet because it is
considered deprecated by LLDB. This packet, on LLDB, serves the same
purpose of QEnvironmentHexEncoded, but sends the information using a
plain text, non-hex-encoded string.
The other two packets are new.
This patch also includes updates to the documentation, testsuite, and
unit tests, without introducing regressions.
gdb/ChangeLog:
yyyy-mm-dd Sergio Durigan Junior <sergiodj@redhat.com>
* NEWS (Changes since GDB 8.0): Add entry mentioning new support
for setting/unsetting environment variables on the remote target.
(New remote packets): Add entries for QEnvironmentHexEncoded,
QEnvironmentUnset and QEnvironmentReset.
* common/environ.c (gdb_environ::operator=): Extend method to
handle m_user_set_env_list and m_user_unset_env_list.
(gdb_environ::clear): Likewise.
(match_var_in_string): Change type of first parameter from 'char
*' to 'const char *'.
(gdb_environ::set): Likewise.
(gdb_environ::unset): Likewise.
(gdb_environ::clear_user_set_env): New method.
(gdb_environ::user_set_envp): Likewise.
(gdb_environ::user_unset_envp): Likewise.
* common/environ.h (gdb_environ): Handle m_user_set_env_list and
m_user_unset_env_list on move constructor/assignment.
(unset): Add new default parameter 'update_unset_list = true'.
(clear_user_set_env): New method.
(user_set_envp): Likewise.
(user_unset_envp): Likewise.
(m_user_set_env_list): New vector.
(m_user_unset_env_list): Likewise.
* common/rsp-low.c (hex2str): New function.
(str2hex): Likewise.
* common/rsp-low.c (hex2str): New prototype.
(str2hex): Likewise.
* remote.c: Include "environ.h". Add QEnvironmentHexEncoded,
QEnvironmentUnset and QEnvironmentReset.
(remote_protocol_features): Add QEnvironmentHexEncoded,
QEnvironmentUnset and QEnvironmentReset packets.
(send_environment_packets): New function.
(extended_remote_environment_support): Likewise.
(extended_remote_create_inferior): Call
extended_remote_environment_support.
(_initialize_remote): Add QEnvironmentHexEncoded,
QEnvironmentUnset and QEnvironmentReset packet configs.
* unittests/environ-selftests.c (run_tests): Add tests for
m_user_set_env_list and m_user_unset_env_list.
gdb/gdbserver/ChangeLog:
yyyy-mm-dd Sergio Durigan Junior <sergiodj@redhat.com>
* server.c (handle_general_set): Handle QEnvironmentHexEncoded,
QEnvironmentUnset and QEnvironmentReset packets.
(handle_query): Inform remote that QEnvironmentHexEncoded,
QEnvironmentUnset and QEnvironmentReset are supported.
gdb/doc/ChangeLog:
yyyy-mm-dd Sergio Durigan Junior <sergiodj@redhat.com>
* gdb.texinfo (set environment): Add @anchor. Explain that
environment variables set by the user are sent to GDBserver.
(unset environment): Likewise, but for unsetting variables.
(Connecting) <Remote Packet>: Add "environment-hex-encoded",
"QEnvironmentHexEncoded", "environment-unset", "QEnvironmentUnset",
"environment-reset" and "QEnvironmentReset" to the table.
(Remote Protocol) <QEnvironmentHexEncoded, QEnvironmentUnset,
QEnvironmentReset>: New item, explaining the packet.
gdb/testsuite/ChangeLog:
yyyy-mm-dd Sergio Durigan Junior <sergiodj@redhat.com>
* gdb.base/share-env-with-gdbserver.c: New file.
* gdb.base/share-env-with-gdbserver.exp: Likewise.
---
gdb/NEWS | 24 ++
gdb/common/environ.c | 122 ++++++++--
gdb/common/environ.h | 25 +-
gdb/common/rsp-low.c | 37 +++
gdb/common/rsp-low.h | 8 +
gdb/doc/gdb.texinfo | 116 ++++++++++
gdb/gdbserver/server.c | 73 +++++-
gdb/remote.c | 82 +++++++
gdb/testsuite/gdb.base/share-env-with-gdbserver.c | 40 ++++
.../gdb.base/share-env-with-gdbserver.exp | 254 +++++++++++++++++++++
gdb/unittests/environ-selftests.c | 55 +++++
11 files changed, 819 insertions(+), 17 deletions(-)
create mode 100644 gdb/testsuite/gdb.base/share-env-with-gdbserver.c
create mode 100644 gdb/testsuite/gdb.base/share-env-with-gdbserver.exp
diff --git a/gdb/NEWS b/gdb/NEWS
index 9cd1df1..4349cce 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -3,6 +3,18 @@
*** Changes since GDB 8.0
+* On Unix systems, GDB now supports transmitting environment variables
+ that are to be set or unset to GDBserver. These variables will
+ affect the environment to be passed to the remote inferior.
+
+ To inform GDB of environment variables that are to be transmitted to
+ GDBserver, use the "set environment" command. Only user set
+ environment variables are sent to GDBserver.
+
+ To inform GDB of environment variables that are to be unset before
+ the remote inferior is started by the GDBserver, use the "unset
+ environment" command.
+
* On Unix systems, GDBserver now does globbing expansion and variable
substitution in inferior command line arguments.
@@ -14,6 +26,18 @@
* New remote packets
+QEnvironmentHexEncoded
+ Inform GDBserver of an environment variable that is to be passed to
+ the inferior when starting it.
+
+QEnvironmentUnset
+ Inform GDBserver of an environment variable that is to be unset
+ before starting the remote inferior.
+
+QEnvironmentReset
+ Inform GDBserver that the environment should be reset (i.e.,
+ user-set environment variables should be unset).
+
QStartupWithShell
Indicates whether the inferior must be started with a shell or not.
diff --git a/gdb/common/environ.c b/gdb/common/environ.c
index 698bda3..59b081f 100644
--- a/gdb/common/environ.c
+++ b/gdb/common/environ.c
@@ -30,8 +30,12 @@ gdb_environ::operator= (gdb_environ &&e)
return *this;
m_environ_vector = std::move (e.m_environ_vector);
+ m_user_set_env_list = std::move (e.m_user_set_env_list);
+ m_user_unset_env_list = std::move (e.m_user_unset_env_list);
e.m_environ_vector.clear ();
e.m_environ_vector.push_back (NULL);
+ e.m_user_set_env_list.clear ();
+ e.m_user_unset_env_list.clear ();
return *this;
}
@@ -63,6 +67,10 @@ gdb_environ::clear ()
for (char *v : m_environ_vector)
xfree (v);
m_environ_vector.clear ();
+ m_user_set_env_list.clear ();
+ for (const char *v : m_user_unset_env_list)
+ xfree ((void *) v);
+ m_user_unset_env_list.clear ();
/* Always add the NULL element. */
m_environ_vector.push_back (NULL);
}
@@ -72,7 +80,7 @@ gdb_environ::clear ()
if it contains, false otherwise. */
static bool
-match_var_in_string (char *string, const char *var, size_t var_len)
+match_var_in_string (const char *string, const char *var, size_t var_len)
{
if (strncmp (string, var, var_len) == 0 && string[var_len] == '=')
return true;
@@ -99,32 +107,104 @@ gdb_environ::get (const char *var) const
void
gdb_environ::set (const char *var, const char *value)
{
+ char *fullvar = concat (var, "=", value, NULL);
+
/* We have to unset the variable in the vector if it exists. */
- unset (var);
+ unset (var, false);
/* Insert the element before the last one, which is always NULL. */
- m_environ_vector.insert (m_environ_vector.end () - 1,
- concat (var, "=", value, NULL));
+ m_environ_vector.insert (m_environ_vector.end () - 1, fullvar);
+
+ /* Mark this environment variable as having been set by the user.
+ This will be useful when we deal with setting environment
+ variables on the remote target. */
+ m_user_set_env_list.push_back (fullvar);
+
+ /* If this environment variable is marked as unset by the user, then
+ remove it from the list, because now the user wants to set
+ it. */
+ for (std::vector<const char *>::iterator iter_user_unset
+ = m_user_unset_env_list.begin ();
+ iter_user_unset != m_user_unset_env_list.end ();
+ ++iter_user_unset)
+ if (strcmp (var, *iter_user_unset) == 0)
+ {
+ void *v = (void *) *iter_user_unset;
+
+ m_user_unset_env_list.erase (iter_user_unset);
+ xfree (v);
+ break;
+ }
}
/* See common/environ.h. */
void
-gdb_environ::unset (const char *var)
+gdb_environ::unset (const char *var, bool update_unset_list)
{
size_t len = strlen (var);
+ std::vector<char *>::iterator it_env;
/* We iterate until '.end () - 1' because the last element is
always NULL. */
- for (std::vector<char *>::iterator el = m_environ_vector.begin ();
- el != m_environ_vector.end () - 1;
- ++el)
- if (match_var_in_string (*el, var, len))
- {
- xfree (*el);
- m_environ_vector.erase (el);
- break;
- }
+ for (it_env = m_environ_vector.begin ();
+ it_env != m_environ_vector.end () - 1;
+ ++it_env)
+ if (match_var_in_string (*it_env, var, len))
+ break;
+
+ if (it_env == m_environ_vector.end () - 1)
+ {
+ /* No element has been found. */
+ return;
+ }
+
+ std::vector<const char *>::iterator it_user_set_env;
+ char *found_var = *it_env;
+
+ it_user_set_env = std::remove (m_user_set_env_list.begin (),
+ m_user_set_env_list.end (),
+ found_var);
+ if (it_user_set_env != m_user_set_env_list.end ())
+ {
+ /* We found (and removed) the element from the user_set_env
+ vector. */
+ m_user_set_env_list.erase (it_user_set_env, m_user_set_env_list.end ());
+ }
+
+ if (update_unset_list)
+ {
+ bool found_in_unset = false;
+
+ for (const char *el : m_user_unset_env_list)
+ if (strcmp (el, var) == 0)
+ {
+ found_in_unset = true;
+ break;
+ }
+
+ if (!found_in_unset)
+ m_user_unset_env_list.push_back (xstrdup (var));
+ }
+
+ m_environ_vector.erase (it_env);
+ xfree (found_var);
+}
+
+/* See common/environ.h. */
+
+void
+gdb_environ::clear_user_set_env ()
+{
+ std::vector<const char *> copy = m_user_set_env_list;
+
+ for (const char *var : copy)
+ {
+ std::string varname (var);
+
+ varname.erase (varname.find ('='), std::string::npos);
+ unset (varname.c_str (), false);
+ }
}
/* See common/environ.h. */
@@ -134,3 +214,17 @@ gdb_environ::envp () const
{
return const_cast<char **> (&m_environ_vector[0]);
}
+
+/* See common/environ.h. */
+
+const std::vector<const char *> &
+gdb_environ::user_set_envp () const
+{
+ return m_user_set_env_list;
+}
+
+const std::vector<const char *> &
+gdb_environ::user_unset_envp () const
+{
+ return m_user_unset_env_list;
+}
diff --git a/gdb/common/environ.h b/gdb/common/environ.h
index 0bbb191..2038170 100644
--- a/gdb/common/environ.h
+++ b/gdb/common/environ.h
@@ -41,12 +41,16 @@ public:
/* Move constructor. */
gdb_environ (gdb_environ &&e)
- : m_environ_vector (std::move (e.m_environ_vector))
+ : m_environ_vector (std::move (e.m_environ_vector)),
+ m_user_set_env_list (std::move (e.m_user_set_env_list)),
+ m_user_unset_env_list (std::move (e.m_user_unset_env_list))
{
/* Make sure that the moved-from vector is left at a valid
state (only one NULL element). */
e.m_environ_vector.clear ();
e.m_environ_vector.push_back (NULL);
+ e.m_user_set_env_list.clear ();
+ e.m_user_unset_env_list.clear ();
}
/* Move assignment. */
@@ -68,14 +72,31 @@ public:
void set (const char *var, const char *value);
/* Unset VAR in environment. */
- void unset (const char *var);
+ void unset (const char *var, bool update_unset_list = true);
+
+ /* Iterate through M_USER_UNSET_ENV_LIST and unset all variables. */
+ void clear_user_set_env ();
/* Return the environment vector represented as a 'char **'. */
char **envp () const;
+ /* Return the user-set environment vector. */
+ const std::vector<const char *> &user_set_envp () const;
+
+ /* Return the user-set environment vector. */
+ const std::vector<const char *> &user_unset_envp () const;
+
private:
/* A vector containing the environment variables. */
std::vector<char *> m_environ_vector;
+
+ /* The vector containing the environment variables set by the
+ user. */
+ std::vector<const char *> m_user_set_env_list;
+
+ /* The vector containing the environment variables unset by the
+ user. */
+ std::vector<const char *> m_user_unset_env_list;
};
#endif /* defined (ENVIRON_H) */
diff --git a/gdb/common/rsp-low.c b/gdb/common/rsp-low.c
index eb85ab5..522c013 100644
--- a/gdb/common/rsp-low.c
+++ b/gdb/common/rsp-low.c
@@ -133,6 +133,28 @@ hex2bin (const char *hex, gdb_byte *bin, int count)
/* See rsp-low.h. */
int
+hex2str (const std::string &hex, std::string &str)
+{
+ const char *hex_str = hex.c_str ();
+ int i;
+
+ for (i = 0; i < hex.size (); ++i)
+ {
+ if (hex_str[0] == '\0' || hex_str[1] == '\0')
+ {
+ /* Hex string is short, or of uneven length.
+ Return the count that has been converted so far. */
+ return i;
+ }
+ str += fromhex (hex_str[0]) * 16 + fromhex (hex_str[1]);
+ hex_str += 2;
+ }
+ return i;
+}
+
+/* See rsp-low.h. */
+
+int
bin2hex (const gdb_byte *bin, char *hex, int count)
{
int i;
@@ -146,6 +168,21 @@ bin2hex (const gdb_byte *bin, char *hex, int count)
return i;
}
+/* See rsp-low.h. */
+
+int
+str2hex (const std::string &str, std::string &hex)
+{
+ int i;
+
+ for (i = 0; i < str.size (); ++i)
+ {
+ hex += tohex ((str[i] >> 4) & 0xf);
+ hex += tohex (str[i] & 0xf);
+ }
+ return i;
+}
+
/* Return whether byte B needs escaping when sent as part of binary data. */
static int
diff --git a/gdb/common/rsp-low.h b/gdb/common/rsp-low.h
index b57f58b..0c22d4f 100644
--- a/gdb/common/rsp-low.h
+++ b/gdb/common/rsp-low.h
@@ -52,6 +52,10 @@ extern char *unpack_varlen_hex (char *buff, ULONGEST *result);
extern int hex2bin (const char *hex, gdb_byte *bin, int count);
+/* Like hex2bin, but work on std::string. */
+
+extern int hex2str (const std::string &hex, std::string &str);
+
/* Convert some bytes to a hexadecimal representation. BIN holds the
bytes to convert. COUNT says how many bytes to convert. The
resulting characters are stored in HEX, followed by a NUL
@@ -59,6 +63,10 @@ extern int hex2bin (const char *hex, gdb_byte *bin, int count);
extern int bin2hex (const gdb_byte *bin, char *hex, int count);
+/* Like bin2hex, but work on std::strings. */
+
+extern int str2hex (const std::string &str, std::string &hex);
+
/* Convert BUFFER, binary data at least LEN_UNITS addressable memory units
long, into escaped binary data in OUT_BUF. Only copy memory units that fit
completely in OUT_BUF. Set *OUT_LEN_UNITS to the number of units from
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index 17b4c69..e902ebb 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -2363,6 +2363,7 @@ print the names and values of all environment variables to be given to
your program. You can abbreviate @code{environment} as @code{env}.
@kindex set environment
+@anchor{set environment}
@item set environment @var{varname} @r{[}=@var{value}@r{]}
Set environment variable @var{varname} to @var{value}. The value
changes for your program (and the shell @value{GDBN} uses to launch
@@ -2391,12 +2392,21 @@ If necessary, you can avoid that by using the @samp{env} program as a
wrapper instead of using @code{set environment}. @xref{set
exec-wrapper}, for an example doing just that.
+Environment variables that are set by the user are also transmitted to
+@command{gdbserver} to be used when starting the remote inferior.
+@pxref{QEnvironmentHexEncoded}.
+
@kindex unset environment
+@anchor{unset environment}
@item unset environment @var{varname}
Remove variable @var{varname} from the environment to be passed to your
program. This is different from @samp{set env @var{varname} =};
@code{unset environment} removes the variable from the environment,
rather than assigning it an empty value.
+
+Environment variables that are unset by the user are also unset on
+@command{gdbserver} when starting the remote inferior.
+@pxref{QEnvironmentUnset}.
@end table
@emph{Warning:} On Unix systems, @value{GDBN} runs your program using
@@ -20816,6 +20826,18 @@ are:
@tab @code{QStartupWithShell}
@tab @code{set startup-with-shell}
+@item @code{environment-hex-encoded}
+@tab @code{QEnvironmentHexEncoded}
+@tab @code{set environment}
+
+@item @code{environment-unset}
+@tab @code{QEnvironmentUnset}
+@tab @code{unset environment}
+
+@item @code{environment-reset}
+@tab @code{QEnvironmentReset}
+@tab @code{Reset the inferior environment (i.e., unset user-set variables)}
+
@item @code{conditional-breakpoints-packet}
@tab @code{Z0 and Z1}
@tab @code{Support for target-side breakpoint condition evaluation}
@@ -36490,6 +36512,100 @@ actually support starting the inferior using a shell.
Use of this packet is controlled by the @code{set startup-with-shell}
command; @pxref{set startup-with-shell}.
+@item QEnvironmentHexEncoded:@var{hex-value}
+@anchor{QEnvironmentHexEncoded}
+@cindex set environment variable, remote request
+@cindex @samp{QEnvironmentHexEncoded} packet
+On UNIX-like targets, it is possible to set environment variables that
+will be passed to the inferior during the startup process. This
+packet is used to inform @command{gdbserver} of an environment
+variable that has been defined by the user on @value{GDBN} (@pxref{set
+environment}).
+
+The packet is composed by @var{hex-value}, an hex encoded
+representation of the @var{name=value} format representing an
+environment variable. The name of the environment variable is
+represented by @var{name}, and the value to be assigned to the
+environment variable is represented by @var{value}. If the variable
+has no value (i.e., the value is @code{null}), then @var{value} will
+not be present.
+
+This packet is only available in extended mode (@pxref{extended
+mode}).
+
+Reply:
+@table @samp
+@item OK
+The request succeeded.
+@end table
+
+This packet is not probed by default; the remote stub must request it,
+by supplying an appropriate @samp{qSupported} response
+(@pxref{qSupported}). This should only be done on targets that
+actually support passing environment variables to the starting
+inferior.
+
+This packet is related to the @code{set environment} command;
+@pxref{set environment}.
+
+@item QEnvironmentUnset:@var{hex-value}
+@anchor{QEnvironmentUnset}
+@cindex unset environment variable, remote request
+@cindex @samp{QEnvironmentUnset} packet
+On UNIX-like targets, it is possible to unset environment variables
+before starting the inferior in the remote target. This packet is
+used to inform @command{gdbserver} of an environment variable that has
+been unset by the user on @value{GDBN} (@pxref{unset environment}).
+
+The packet is composed by @var{hex-value}, an hex encoded
+representation of the name of the environment variable to be unset.
+
+This packet is only available in extended mode (@pxref{extended
+mode}).
+
+Reply:
+@table @samp
+@item OK
+The request succeeded.
+@end table
+
+This packet is not probed by default; the remote stub must request it,
+by supplying an appropriate @samp{qSupported} response
+(@pxref{qSupported}). This should only be done on targets that
+actually support passing environment variables to the starting
+inferior.
+
+This packet is related to the @code{unset environment} command;
+@pxref{unset environment}.
+
+@item QEnvironmentReset
+@anchor{QEnvironmentReset}
+@cindex reset environment, remote request
+@cindex @samp{QEnvironmentReset} packet
+On UNIX-like targets, this packet is used to reset the state of
+environment variables in the remote target before starting the
+inferior. In this context, reset means unsetting all environment
+variables that were previously set by the user (i.e., were not
+initially present in the environment). It is sent to
+@command{gdbserver} before the @samp{QEnvironmentHexEncoded}
+(@pxref{QEnvironmentHexEncoded}) and the @samp{QEnvironmentUnset}
+(@pxref{QEnvironmentUnset}) packets.
+
+This packet is only available in extended mode (@pxref{extended
+mode}).
+
+Reply:
+@table @samp
+@item OK
+The request succeeded.
+@end table
+
+This packet is not probed by default; the remote stub must request it,
+by supplying an appropriate @samp{qSupported} response
+(@pxref{qSupported}). This should only be done on targets that
+actually support passing environment variables to the starting
+inferior.
+
@item qfThreadInfo
@itemx qsThreadInfo
@cindex list active threads, remote request
diff --git a/gdb/gdbserver/server.c b/gdb/gdbserver/server.c
index 3838351..12dcc99 100644
--- a/gdb/gdbserver/server.c
+++ b/gdb/gdbserver/server.c
@@ -631,6 +631,75 @@ handle_general_set (char *own_buf)
return;
}
+ if (startswith (own_buf, "QEnvironmentReset"))
+ {
+ our_environ.clear_user_set_env ();
+
+ write_ok (own_buf);
+ return;
+ }
+
+ if (startswith (own_buf, "QEnvironmentHexEncoded:"))
+ {
+ const char *p = own_buf + sizeof ("QEnvironmentHexEncoded:") - 1;
+ /* The final form of the environment variable. FINAL_VAR will
+ hold the 'VAR=VALUE' format. */
+ std::string final_var;
+ std::string var_name, var_value;
+
+ if (remote_debug)
+ debug_printf (_("[QEnvironmentHexEncoded received '%s']\n"), p);
+
+ hex2str (p, final_var);
+
+ if (remote_debug)
+ {
+ debug_printf (_("[Environment variable to be set: '%s']\n"),
+ final_var.c_str ());
+ debug_flush ();
+ }
+
+ size_t pos = final_var.find ('=');
+ if (pos == std::string::npos)
+ {
+ warning (_("Unknown environment variable '%s' from remote side."),
+ final_var.c_str ());
+ write_enn (own_buf);
+ return;
+ }
+
+ var_name = final_var.substr (0, pos);
+ var_value = final_var.substr (pos + 1, std::string::npos);
+
+ our_environ.set (var_name.c_str (), var_value.c_str ());
+
+ write_ok (own_buf);
+ return;
+ }
+
+ if (startswith (own_buf, "QEnvironmentUnset:"))
+ {
+ const char *p = own_buf + sizeof ("QEnvironmentUnset:") - 1;
+ std::string varname;
+
+ if (remote_debug)
+ debug_printf (_("[QEnvironmentUnset received '%s']\n"), p);
+
+ hex2str (p, varname);
+
+ if (remote_debug)
+ {
+ debug_printf (_("[Environment variable to be unset: '%s']\n"),
+ varname.c_str ());
+ debug_flush ();
+ }
+
+ our_environ.unset (varname.c_str ());
+
+ write_ok (own_buf);
+ return;
+ }
+
if (strcmp (own_buf, "QStartNoAckMode") == 0)
{
if (remote_debug)
@@ -2228,7 +2297,9 @@ handle_query (char *own_buf, int packet_len, int *new_packet_len_p)
}
sprintf (own_buf,
- "PacketSize=%x;QPassSignals+;QProgramSignals+;QStartupWithShell+",
+ "PacketSize=%x;QPassSignals+;QProgramSignals+;"
+ "QStartupWithShell+;QEnvironmentHexEncoded+;"
+ "QEnvironmentReset+;QEnvironmentUnset+",
PBUFSIZ - 1);
if (target_supports_catch_syscall ())
diff --git a/gdb/remote.c b/gdb/remote.c
index d363a36..0eaf51c 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -72,6 +72,7 @@
#include "btrace.h"
#include "record-btrace.h"
#include <algorithm>
+#include "environ.h"
/* Temp hacks for tracepoint encoding migration. */
static char *target_buf;
@@ -1429,6 +1430,9 @@ enum {
PACKET_QCatchSyscalls,
PACKET_QProgramSignals,
PACKET_QStartupWithShell,
+ PACKET_QEnvironmentHexEncoded,
+ PACKET_QEnvironmentReset,
+ PACKET_QEnvironmentUnset,
PACKET_qCRC,
PACKET_qSearch_memory,
PACKET_vAttach,
@@ -4636,6 +4640,12 @@ static const struct protocol_feature remote_protocol_features[] = {
PACKET_QProgramSignals },
{ "QStartupWithShell", PACKET_DISABLE, remote_supported_packet,
PACKET_QStartupWithShell },
+ { "QEnvironmentHexEncoded", PACKET_DISABLE, remote_supported_packet,
+ PACKET_QEnvironmentHexEncoded },
+ { "QEnvironmentReset", PACKET_DISABLE, remote_supported_packet,
+ PACKET_QEnvironmentReset },
+ { "QEnvironmentUnset", PACKET_DISABLE, remote_supported_packet,
+ PACKET_QEnvironmentUnset },
{ "QStartNoAckMode", PACKET_DISABLE, remote_supported_packet,
PACKET_QStartNoAckMode },
{ "multiprocess", PACKET_DISABLE, remote_supported_packet,
@@ -9583,6 +9593,63 @@ extended_remote_run (const std::string &args)
}
}
+/* Helper function to send set/unset environment packets. ACTION is
+ either "set" or "unset". PACKET is either "QEnvironmentHexEncoded"
+ or "QEnvironmentUnsetVariable". VEC is the corresponding
+ std::vector (from gdb_environ) that contains the variables to be
+ sent. */
+
+static void
+send_environment_packets (struct remote_state *rs,
+ const char *action,
+ const char *packet,
+ const std::vector<const char *> &vec)
+{
+ for (const char *fullvar : vec)
+ {
+ std::string encoded_fullvar;
+
+ /* Convert the environment variable to an hex string, which
+ is the best format to be transmitted over the wire. */
+ str2hex (fullvar, encoded_fullvar);
+
+ xsnprintf (rs->buf, get_remote_packet_size (),
+ "%s:%s", packet, encoded_fullvar.c_str ());
+
+ putpkt (rs->buf);
+ getpkt (&rs->buf, &rs->buf_size, 0);
+ if (strcmp (rs->buf, "OK") != 0)
+ warning (_("Unable to %s environment variable '%s' on remote."),
+ action, fullvar);
+ }
+}
+
+/* Helper function to handle the QEnvironment* packets. */
+
+static void
+extended_remote_environment_support (struct remote_state *rs)
+{
+ if (packet_support (PACKET_QEnvironmentReset) != PACKET_DISABLE)
+ {
+ static const char qenvreset[] = "QEnvironmentReset";
+
+ putpkt (qenvreset);
+ getpkt (&rs->buf, &rs->buf_size, 0);
+ if (strcmp (rs->buf, "OK") != 0)
+ warning (_("Unable to reset environment on remote."));
+ }
+
+ gdb_environ *e = ¤t_inferior ()->environment;
+
+ if (packet_support (PACKET_QEnvironmentHexEncoded) != PACKET_DISABLE)
+ send_environment_packets (rs, "set", "QEnvironmentHexEncoded",
+ e->user_set_envp ());
+
+ if (packet_support (PACKET_QEnvironmentUnset) != PACKET_DISABLE)
+ send_environment_packets (rs, "unset", "QEnvironmentUnset",
+ e->user_unset_envp ());
+}
+
/* In the extended protocol we want to be able to do things like
"run" and have them basically work as expected. So we need
a special create_inferior function. We support changing the
@@ -9623,6 +9690,8 @@ Remote replied unexpectedly while setting startup-with-shell: %s"),
rs->buf);
}
+ extended_remote_environment_support (rs);
+
/* Now restart the remote server. */
run_worked = extended_remote_run (args) != -1;
if (!run_worked)
@@ -14116,6 +14185,19 @@ Show the maximum size of the address (in bits) in a memory packet."), NULL,
add_packet_config_cmd (&remote_protocol_packets[PACKET_QStartupWithShell],
"QStartupWithShell", "startup-with-shell", 0);
+ add_packet_config_cmd (&remote_protocol_packets
+ [PACKET_QEnvironmentHexEncoded],
+ "QEnvironmentHexEncoded", "environment-hex-encoded",
+ 0);
+
+ add_packet_config_cmd (&remote_protocol_packets[PACKET_QEnvironmentReset],
+ "QEnvironmentReset", "environment-reset",
+ 0);
+
+ add_packet_config_cmd (&remote_protocol_packets[PACKET_QEnvironmentUnset],
+ "QEnvironmentUnset", "environment-unset",
+ 0);
+
add_packet_config_cmd (&remote_protocol_packets[PACKET_qSymbol],
"qSymbol", "symbol-lookup", 0);
diff --git a/gdb/testsuite/gdb.base/share-env-with-gdbserver.c b/gdb/testsuite/gdb.base/share-env-with-gdbserver.c
new file mode 100644
index 0000000..740bd0f
--- /dev/null
+++ b/gdb/testsuite/gdb.base/share-env-with-gdbserver.c
@@ -0,0 +1,40 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+ Copyright 2017 Free Software Foundation, Inc.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+/* Wrapper around getenv for GDB. */
+
+static const char *
+my_getenv (const char *name)
+{
+ return getenv (name);
+}
+
+int
+main (int argc, char *argv[])
+{
+ const char *myvar = getenv ("GDB_TEST_VAR");
+
+ if (myvar != NULL)
+ printf ("It worked! myvar = '%s'\n", myvar);
+ else
+ printf ("It failed.");
+
+ return 0; /* break-here */
+}
diff --git a/gdb/testsuite/gdb.base/share-env-with-gdbserver.exp b/gdb/testsuite/gdb.base/share-env-with-gdbserver.exp
new file mode 100644
index 0000000..9c5ec4a
--- /dev/null
+++ b/gdb/testsuite/gdb.base/share-env-with-gdbserver.exp
@@ -0,0 +1,254 @@
+# This testcase is part of GDB, the GNU debugger.
+
+# Copyright 2017 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+# This test doesn't make sense on native-gdbserver.
+if { [use_gdb_stub] } {
+ untested "not supported"
+ return
+}
+
+standard_testfile
+
+if { [prepare_for_testing "failed to prepare" $testfile $srcfile debug] } {
+ return -1
+}
+
+set test_var_name "GDB_TEST_VAR"
+
+# Helper function that performs a check on the output of "getenv".
+#
+# - VAR_NAME is the name of the variable to be checked.
+#
+# - VAR_VALUE is the value expected.
+#
+# - TEST_MSG, if not empty, is the test message to be used by the
+# "gdb_test".
+#
+# - EMPTY_VAR_P, if non-zero, means that the variable is not expected
+# to exist. In this case, VAR_VALUE is not considered.
+
+proc check_getenv { var_name var_value { test_msg "" } { empty_var_p 0 } } {
+ global hex decimal
+
+ if { $test_msg == "" } {
+ set test_msg "print result of getenv for $var_name"
+ }
+
+ if { $empty_var_p } {
+ set var_value_match "0x0"
+ } else {
+ set var_value_match "$hex \"$var_value\""
+ }
+
+ gdb_test "print my_getenv (\"$var_name\")" "\\\$$decimal = $var_value_match" \
+ $test_msg
+}
+
+# Helper function to re-run to main and breaking at the "break-here"
+# label.
+
+proc rerun_to_main { } {
+ global decimal hex
+
+ if { ![runto_main] } {
+ return -1
+ }
+
+ gdb_breakpoint [gdb_get_line_number "break-here"]
+
+ gdb_test "continue" "Breakpoint $decimal, main \\\(argc=1, argv=$hex\\\) at.*" \
+ "continue until breakpoint"
+}
+
+# Helper function that does the actual testing.
+#
+# - VAR_VALUE is the value of the environment variable.
+#
+# - VAR_NAME is the name of the environment variable. If empty,
+# defaults to $test_var_name.
+#
+# - VAR_NAME_MATCH is the name (regex) that will be used to query the
+# environment about the variable (via getenv). This is useful when
+# we're testing variables with strange names (e.g., with an equal
+# sign in the name) and we know that the variable will actually be
+# set using another name. If empty, defatults, to $var_name.
+#
+# - VAR_VALUE_MATCH is the value (regex) that will be used to match
+# the result of getenv. The rationale is the same as explained for
+# VAR_NAME_MATCH. If empty, defaults, to $var_value.
+
+proc do_test { var_value { var_name "" } { var_name_match "" } { var_value_match "" } } {
+ global hex decimal binfile test_var_name
+
+ clean_restart $binfile
+
+ if { $var_name == "" } {
+ set var_name $test_var_name
+ }
+
+ if { $var_name_match == "" } {
+ set var_name_match $var_name
+ }
+
+ if { $var_value_match == "" } {
+ set var_value_match $var_value
+ }
+
+ if { $var_value != "" } {
+ gdb_test_no_output "set environment $var_name = $var_value" \
+ "set $var_name = $var_value"
+ } else {
+ gdb_test "set environment $var_name =" \
+ "Setting environment variable \"$var_name\" to null value." \
+ "set $var_name to null value"
+ }
+
+ rerun_to_main
+
+ check_getenv "$var_name_match" "$var_value_match" \
+ "print result of getenv for $var_name"
+}
+
+with_test_prefix "long var value" {
+ do_test "this is my test variable; testing long vars; {}"
+}
+
+with_test_prefix "empty var" {
+ do_test ""
+}
+
+with_test_prefix "strange named var" {
+ # In this test we're doing the following:
+ #
+ # (gdb) set environment 'asd =' = 123 43; asd b ### [];;;
+ #
+ # However, due to how GDB parses this line, the environment
+ # variable will end up named <'asd> (without the <>), and its
+ # value will be <' = 123 43; asd b ### [];;;> (without the <>).
+ do_test "123 43; asd b ### [];;;" "'asd ='" "'asd" "' = 123 43; asd b ### [];;;"
+}
+
+# Test setting and unsetting environment variables in various
+# fashions.
+
+proc test_set_unset_vars { } {
+ global hex decimal binfile
+
+ clean_restart $binfile
+
+ with_test_prefix "set 3 environment variables" {
+ # Set some environment variables
+ gdb_test_no_output "set environment A = 1" \
+ "set A to 1"
+ gdb_test_no_output "set environment B = 2" \
+ "set B to 2"
+ gdb_test_no_output "set environment C = 3" \
+ "set C to 3"
+
+ rerun_to_main
+
+ # Check that the variables are known by the inferior
+ check_getenv "A" "1"
+ check_getenv "B" "2"
+ check_getenv "C" "3"
+ }
+
+ with_test_prefix "unset one variable, reset one" {
+ # Now, unset/reset some values
+ gdb_test_no_output "unset environment A" \
+ "unset A"
+ gdb_test_no_output "set environment B = 4" \
+ "set B to 4"
+
+ rerun_to_main
+
+ check_getenv "A" "" "" 1
+ check_getenv "B" "4"
+ check_getenv "C" "3"
+ }
+
+ with_test_prefix "unset two variables, reset one" {
+ # Unset more values
+ gdb_test_no_output "unset environment B" \
+ "unset B"
+ gdb_test_no_output "set environment A = 1" \
+ "set A to 1 again"
+ gdb_test_no_output "unset environment C" \
+ "unset C"
+
+ rerun_to_main
+
+ check_getenv "A" "1"
+ check_getenv "B" "" "" 1
+ check_getenv "C" "" "" 1
+ }
+}
+
+with_test_prefix "test set/unset of vars" {
+ test_set_unset_vars
+}
+
+# Test that unsetting works.
+
+proc test_unset { } {
+ global hex decimal binfile gdb_prompt
+
+ clean_restart $binfile
+
+ rerun_to_main
+
+ set test_msg "check if unset works"
+ set found_home 0
+ gdb_test_multiple "print my_getenv (\"HOME\")" $test_msg {
+ -re "\\\$$decimal = $hex \".*\"\r\n$gdb_prompt $" {
+ pass $test_msg
+ set found_home 1
+ }
+ -re "\\\$$decimal = 0x0\r\n$gdb_prompt $" {
+ untested $test_msg
+ }
+ }
+
+ if { $found_home == 1 } {
+ with_test_prefix "simple unset" {
+ # We can do the test, because $HOME exists (and therefore can
+ # be unset).
+ gdb_test_no_output "unset environment HOME" "unset HOME"
+
+ rerun_to_main
+
+ # $HOME now must be empty
+ check_getenv "HOME" "" "" 1
+ }
+
+ with_test_prefix "set-then-unset" {
+ clean_restart $binfile
+
+ # Test if setting and then unsetting $HOME works.
+ gdb_test_no_output "set environment HOME = test" "set HOME as test"
+ gdb_test_no_output "unset environment HOME" "unset HOME again"
+
+ rerun_to_main
+
+ check_getenv "HOME" "" "" 1
+ }
+ }
+}
+
+with_test_prefix "test unset of vars" {
+ test_unset
+}
diff --git a/gdb/unittests/environ-selftests.c b/gdb/unittests/environ-selftests.c
index 28b16f8..7915daf 100644
--- a/gdb/unittests/environ-selftests.c
+++ b/gdb/unittests/environ-selftests.c
@@ -38,6 +38,8 @@ run_tests ()
/* When the vector is initialized, there should always be one NULL
element in it. */
SELF_CHECK (env.envp ()[0] == NULL);
+ SELF_CHECK (env.user_set_envp ().size () == 0);
+ SELF_CHECK (env.user_unset_envp ().size () == 0);
/* Make sure that there is no other element. */
SELF_CHECK (env.get ("PWD") == NULL);
@@ -45,14 +47,24 @@ run_tests ()
/* Check if unset followed by a set in an empty vector works. */
env.set ("PWD", "test");
SELF_CHECK (strcmp (env.get ("PWD"), "test") == 0);
+ SELF_CHECK (strcmp (env.user_set_envp ()[0], "PWD=test") == 0);
+ SELF_CHECK (env.user_unset_envp ().size () == 0);
/* The second element must be NULL. */
SELF_CHECK (env.envp ()[1] == NULL);
+ SELF_CHECK (env.user_set_envp ().size () == 1);
env.unset ("PWD");
SELF_CHECK (env.envp ()[0] == NULL);
+ SELF_CHECK (env.user_set_envp ().size () == 0);
+ SELF_CHECK (env.user_unset_envp ().size () == 1);
+ SELF_CHECK (strcmp (env.user_unset_envp ()[0], "PWD") == 0);
/* Initialize the environment vector using the host's environ. */
env = gdb_environ::from_host_environ ();
+ /* The user-set and user-unset lists must be empty. */
+ SELF_CHECK (env.user_set_envp ().size () == 0);
+ SELF_CHECK (env.user_unset_envp ().size () == 0);
+
/* Our test environment variable should be present at the
vector. */
SELF_CHECK (strcmp (env.get ("GDB_SELFTEST_ENVIRON"), "1") == 0);
@@ -65,6 +77,8 @@ run_tests ()
host's environment, but doesn't exist in our vector. */
env.unset ("GDB_SELFTEST_ENVIRON");
SELF_CHECK (env.get ("GDB_SELFTEST_ENVIRON") == NULL);
+ SELF_CHECK (env.user_unset_envp ().size () == 1);
+ SELF_CHECK (strcmp (env.user_unset_envp ()[0], "GDB_SELFTEST_ENVIRON") == 0);
/* Re-set the test variable. */
env.set ("GDB_SELFTEST_ENVIRON", "1");
@@ -75,6 +89,8 @@ run_tests ()
variable. */
env.clear ();
SELF_CHECK (env.envp ()[0] == NULL);
+ SELF_CHECK (env.user_set_envp ().size () == 0);
+ SELF_CHECK (env.user_unset_envp ().size () == 0);
SELF_CHECK (env.get ("GDB_SELFTEST_ENVIRON") == NULL);
/* Reinitialize our environ vector using the host environ. We
@@ -89,6 +105,21 @@ run_tests ()
++num_found;
SELF_CHECK (num_found == 1);
+ /* Before unsetting our test variable, test that user-unset works
+ fine. */
+ env.unset ("GDB_SELFTEST_ENVIRON");
+ SELF_CHECK (env.get ("GDB_SELFTEST_ENVIRON") == NULL);
+ SELF_CHECK (env.user_set_envp ().size () == 0);
+ SELF_CHECK (env.user_unset_envp ().size () == 1);
+ SELF_CHECK (strcmp (env.user_unset_envp ()[0], "GDB_SELFTEST_ENVIRON") == 0);
+ env.set ("GDB_SELFTEST_ENVIRON", "1");
+ SELF_CHECK (env.user_set_envp ().size () == 1);
+ SELF_CHECK (env.user_unset_envp ().size () == 0);
+ env.unset ("GDB_SELFTEST_ENVIRON");
+ SELF_CHECK (env.user_set_envp ().size () == 0);
+ SELF_CHECK (env.user_unset_envp ().size () == 1);
+ SELF_CHECK (strcmp (env.user_unset_envp ()[0], "GDB_SELFTEST_ENVIRON") == 0);
+
/* Get rid of our test variable. */
unsetenv ("GDB_SELFTEST_ENVIRON");
@@ -97,6 +128,10 @@ run_tests ()
vector, but can still find B. */
env.set ("GDB_SELFTEST_ENVIRON_1", "aaa");
SELF_CHECK (strcmp (env.get ("GDB_SELFTEST_ENVIRON_1"), "aaa") == 0);
+ /* User-set environ var list must contain one element. */
+ SELF_CHECK (env.user_set_envp ().size () == 1);
+ SELF_CHECK (strcmp (env.user_set_envp ()[0],
+ "GDB_SELFTEST_ENVIRON_1=aaa") == 0);
env.set ("GDB_SELFTEST_ENVIRON_2", "bbb");
SELF_CHECK (strcmp (env.get ("GDB_SELFTEST_ENVIRON_2"), "bbb") == 0);
@@ -104,6 +139,11 @@ run_tests ()
env.unset ("GDB_SELFTEST_ENVIRON_1");
SELF_CHECK (env.get ("GDB_SELFTEST_ENVIRON_1") == NULL);
SELF_CHECK (strcmp (env.get ("GDB_SELFTEST_ENVIRON_2"), "bbb") == 0);
+ /* The user-set environ var list must contain only one element
+ now. */
+ SELF_CHECK (strcmp (env.user_set_envp ()[0],
+ "GDB_SELFTEST_ENVIRON_2=bbb") == 0);
+ SELF_CHECK (env.user_set_envp ().size () == 1);
env.clear ();
@@ -111,11 +151,16 @@ run_tests ()
valid state (i.e., its only element is NULL). */
env.set ("A", "1");
SELF_CHECK (strcmp (env.get ("A"), "1") == 0);
+ SELF_CHECK (strcmp (env.user_set_envp ()[0], "A=1") == 0);
+ SELF_CHECK (env.user_set_envp ().size () == 1);
gdb_environ env2;
env2 = std::move (env);
SELF_CHECK (env.envp ()[0] == NULL);
SELF_CHECK (strcmp (env2.get ("A"), "1") == 0);
SELF_CHECK (env2.envp ()[1] == NULL);
+ SELF_CHECK (env.user_set_envp ().size () == 0);
+ SELF_CHECK (strcmp (env2.user_set_envp ()[0], "A=1") == 0);
+ SELF_CHECK (env2.user_set_envp ().size () == 1);
env.set ("B", "2");
SELF_CHECK (strcmp (env.get ("B"), "2") == 0);
SELF_CHECK (env.envp ()[1] == NULL);
@@ -125,18 +170,26 @@ run_tests ()
env.clear ();
env.set ("A", "1");
SELF_CHECK (strcmp (env.get ("A"), "1") == 0);
+ SELF_CHECK (strcmp (env.user_set_envp ()[0], "A=1") == 0);
gdb_environ env3 = std::move (env);
SELF_CHECK (env.envp ()[0] == NULL);
+ SELF_CHECK (env.user_set_envp ().size () == 0);
SELF_CHECK (strcmp (env3.get ("A"), "1") == 0);
SELF_CHECK (env3.envp ()[1] == NULL);
+ SELF_CHECK (strcmp (env3.user_set_envp ()[0], "A=1") == 0);
+ SELF_CHECK (env3.user_set_envp ().size () == 1);
env.set ("B", "2");
SELF_CHECK (strcmp (env.get ("B"), "2") == 0);
SELF_CHECK (env.envp ()[1] == NULL);
+ SELF_CHECK (strcmp (env.user_set_envp ()[0], "B=2") == 0);
+ SELF_CHECK (env.user_set_envp ().size () == 1);
/* Test self-move. */
env.clear ();
env.set ("A", "1");
SELF_CHECK (strcmp (env.get ("A"), "1") == 0);
+ SELF_CHECK (strcmp (env.user_set_envp ()[0], "A=1") == 0);
+ SELF_CHECK (env.user_set_envp ().size () == 1);
/* Some compilers warn about moving to self, but that's precisely what we want
to test here, so turn this warning off. */
@@ -148,6 +201,8 @@ run_tests ()
SELF_CHECK (strcmp (env.get ("A"), "1") == 0);
SELF_CHECK (strcmp (env.envp ()[0], "A=1") == 0);
SELF_CHECK (env.envp ()[1] == NULL);
+ SELF_CHECK (strcmp (env.user_set_envp ()[0], "A=1") == 0);
+ SELF_CHECK (env.user_set_envp ().size () == 1);
}
} /* namespace gdb_environ */
} /* namespace selftests */
--
2.9.3
^ permalink raw reply [flat|nested] 35+ messages in thread
* Re: [PATCH v2] Implement the ability to set/unset environment variables to GDBserver when starting the inferior
2017-07-27 3:36 ` [PATCH v2] Implement the ability to set/unset " Sergio Durigan Junior
@ 2017-07-27 17:10 ` Eli Zaretskii
2017-07-27 22:05 ` Simon Marchi
` (2 subsequent siblings)
3 siblings, 0 replies; 35+ messages in thread
From: Eli Zaretskii @ 2017-07-27 17:10 UTC (permalink / raw)
To: Sergio Durigan Junior; +Cc: gdb-patches, palves, simon.marchi
> From: Sergio Durigan Junior <sergiodj@redhat.com>
> Cc: Pedro Alves <palves@redhat.com>,
> Simon Marchi <simon.marchi@polymtl.ca>,
> Eli Zaretskii <eliz@gnu.org>,
> Sergio Durigan Junior <sergiodj@redhat.com>
> Date: Wed, 26 Jul 2017 23:35:31 -0400
>
> Changes from v1:
>
> - Implement suggestions by Simon.
>
> - Improve gdb_environ::unset (use std::remove).
>
> - Implement the ability to unset environment variables in the remote
> target.
>
> - Implement hex2str and str2hex.
>
> - Extend unittest, testcase and documentation.
OK for the documentation parts.
Thanks.
^ permalink raw reply [flat|nested] 35+ messages in thread
* Re: [PATCH v2] Implement the ability to set/unset environment variables to GDBserver when starting the inferior
2017-07-27 3:36 ` [PATCH v2] Implement the ability to set/unset " Sergio Durigan Junior
2017-07-27 17:10 ` Eli Zaretskii
@ 2017-07-27 22:05 ` Simon Marchi
2017-08-01 2:33 ` Sergio Durigan Junior
2017-07-29 22:36 ` Simon Marchi
[not found] ` <256083325d9f9c4cc4f5518fe6e5292d@polymtl.ca>
3 siblings, 1 reply; 35+ messages in thread
From: Simon Marchi @ 2017-07-27 22:05 UTC (permalink / raw)
To: Sergio Durigan Junior; +Cc: GDB Patches, Pedro Alves, Eli Zaretskii
Hi Sergio,
On 2017-07-27 05:35, Sergio Durigan Junior wrote:
> Changes from v1:
>
> - Implement suggestions by Simon.
>
> - Improve gdb_environ::unset (use std::remove).
>
> - Implement the ability to unset environment variables in the remote
> target.
>
> - Implement hex2str and str2hex.
>
> - Extend unittest, testcase and documentation.
>
> This version of the patch extends the first version/idea, which only
> allowed environment variables to be set in the remote target. Now it
> is also possible to unset them.
Awesome!
> User-set environment variables are only the variables that are
> explicitly set by the user, using the 'set environment' command. This
> means that variables that were already present in the environment when
> starting GDB/GDBserver are not transmitted/considered by this feature.
>
> User-unset environment variables are variables that are explicitly
> unset by the user, using the 'unset environment' command.
>
> The idea behind this patch is to store user-set and user-unset
> environment variables in two separate vectors, both part of
> gdb_environ. Then, when extended_remote_create_inferior is preparing
> to start the inferior, it will iterate over the two lists and
> set/unset variables accordingly. Three new packets are introduced:
>
> - QEnvironmentHexEncoded, which is used to set environment variables,
> and contains an hex-encoded string in the format "VAR=VALUE" (VALUE
> can be empty if the user set a variable with a null value, by doing
> 'set environment VAR=').
>
> - QEnvironmentUnset, which is used to unset environment variables, and
> contains an hex-encoded string in the format "VAR".
>
> - QEnvironmentReset, which is always the first packet to be
> transmitted, and is used to reset (i.e., unset any user-set
> environment variable) the environment.
There is this use case for which the behavior is different between
native and remote, related to unset
native:
(gdb inf1) file /usr/bin/env
(gdb inf1) unset environment DISPLAY
(gdb inf1) r # DISPLAY is not there
(gdb inf1) add-inferior -exec /usr/bin/env
(gdb inf1) inferior 2
(gdb inf2) r # DISPLAY is there
remote:
(gdb inf1) tar ext :1234
(gdb inf1) file /usr/bin/env
(gdb inf1) set remote exec-file /usr/bin/env
(gdb inf1) unset environment DISPLAY
(gdb inf1) r # DISPLAY is not there
(gdb inf1) add-inferior -exec /usr/bin/env
(gdb inf1) inferior 2
(gdb inf2) set remote exec-file /usr/bin/env
(gdb inf2) r # DISPLAY is not there
I think that's because in GDB, we make a new pristine copy of the host
environment for every inferior, which we don't in gdbserver. The way I
understand the QEnvironmentReset is that the remote agent (gdbserver)
should forget any previous modification to the environment made using
QEnvironmentHexEncoded and QEnvironmentUnset and return the environment
to its original state, when it was launched. This should allow
supporting the use case above. To implement that properly, we would
need to keep a copy of gdbserver's initial environment, which we could
revert to when receiving a QEnvironmentReset.
In any case, I just want to make sure that the packets we introduce are
not the things that limit us.
>
> The QEnvironmentHexEncoded packet is inspired on LLDB's extensions to
> the RSP. Details about it can be seen here:
>
>
> <https://raw.githubusercontent.com/llvm-mirror/lldb/master/docs/lldb-gdb-remote.txt>
>
> I decided not to implement the QEnvironment packet because it is
> considered deprecated by LLDB. This packet, on LLDB, serves the same
> purpose of QEnvironmentHexEncoded, but sends the information using a
> plain text, non-hex-encoded string.
>
> The other two packets are new.
>
> This patch also includes updates to the documentation, testsuite, and
> unit tests, without introducing regressions.
>
> gdb/ChangeLog:
> yyyy-mm-dd Sergio Durigan Junior <sergiodj@redhat.com>
>
> * NEWS (Changes since GDB 8.0): Add entry mentioning new support
> for setting/unsetting environment variables on the remote target.
> (New remote packets): Add entries for QEnvironmentHexEncoded,
> QEnvironmentUnset and QEnvironmentReset.
> * common/environ.c (gdb_environ::operator=): Extend method to
> handle m_user_set_env_list and m_user_unset_env_list.
> (gdb_environ::clear): Likewise.
> (match_var_in_string): Change type of first parameter from 'char
> *' to 'const char *'.
> (gdb_environ::set): Likewise.
> (gdb_environ::unset): Likewise.
> (gdb_environ::clear_user_set_env): New method.
> (gdb_environ::user_set_envp): Likewise.
> (gdb_environ::user_unset_envp): Likewise.
> * common/environ.h (gdb_environ): Handle m_user_set_env_list and
> m_user_unset_env_list on move constructor/assignment.
> (unset): Add new default parameter 'update_unset_list = true'.
> (clear_user_set_env): New method.
> (user_set_envp): Likewise.
> (user_unset_envp): Likewise.
> (m_user_set_env_list): New vector.
> (m_user_unset_env_list): Likewise.
> * common/rsp-low.c (hex2str): New function.
> (str2hex): Likewise.
> * common/rsp-low.c (hex2str): New prototype.
> (str2hex): Likewise.
> * remote.c: Include "environ.h". Add QEnvironmentHexEncoded,
> QEnvironmentUnset and QEnvironmentReset.
> (remote_protocol_features): Add QEnvironmentHexEncoded,
> QEnvironmentUnset and QEnvironmentReset packets.
> (send_environment_packets): New function.
> (extended_remote_environment_support): Likewise.
> (extended_remote_create_inferior): Call
> extended_remote_environment_support.
> (_initialize_remote): Add QEnvironmentHexEncoded,
> QEnvironmentUnset and QEnvironmentReset packet configs.
> * unittests/environ-selftests.c (run_tests): Add tests for
> m_user_set_env_list and m_user_unset_env_list.
>
> gdb/gdbserver/ChangeLog:
> yyyy-mm-dd Sergio Durigan Junior <sergiodj@redhat.com>
>
> * server.c (handle_general_set): Handle QEnvironmentHexEncoded,
> QEnvironmentUnset and QEnvironmentReset packets.
> (handle_query): Inform remote that QEnvironmentHexEncoded,
> QEnvironmentUnset and QEnvironmentReset are supported.
>
> gdb/doc/ChangeLog:
> yyyy-mm-dd Sergio Durigan Junior <sergiodj@redhat.com>
>
> * gdb.texinfo (set environment): Add @anchor. Explain that
> environment variables set by the user are sent to GDBserver.
> (unset environment): Likewise, but for unsetting variables.
> (Connecting) <Remote Packet>: Add "environment-hex-encoded",
> "QEnvironmentHexEncoded", "environment-unset", "QEnvironmentUnset",
> "environment-reset" and "QEnvironmentReset" to the table.
> (Remote Protocol) <QEnvironmentHexEncoded, QEnvironmentUnset,
> QEnvironmentReset>: New item, explaining the packet.
>
> gdb/testsuite/ChangeLog:
> yyyy-mm-dd Sergio Durigan Junior <sergiodj@redhat.com>
>
> * gdb.base/share-env-with-gdbserver.c: New file.
> * gdb.base/share-env-with-gdbserver.exp: Likewise.
> ---
> gdb/NEWS | 24 ++
> gdb/common/environ.c | 122 ++++++++--
> gdb/common/environ.h | 25 +-
> gdb/common/rsp-low.c | 37 +++
> gdb/common/rsp-low.h | 8 +
> gdb/doc/gdb.texinfo | 116 ++++++++++
> gdb/gdbserver/server.c | 73 +++++-
> gdb/remote.c | 82 +++++++
> gdb/testsuite/gdb.base/share-env-with-gdbserver.c | 40 ++++
> .../gdb.base/share-env-with-gdbserver.exp | 254
> +++++++++++++++++++++
> gdb/unittests/environ-selftests.c | 55 +++++
> 11 files changed, 819 insertions(+), 17 deletions(-)
> create mode 100644 gdb/testsuite/gdb.base/share-env-with-gdbserver.c
> create mode 100644 gdb/testsuite/gdb.base/share-env-with-gdbserver.exp
>
> diff --git a/gdb/NEWS b/gdb/NEWS
> index 9cd1df1..4349cce 100644
> --- a/gdb/NEWS
> +++ b/gdb/NEWS
> @@ -3,6 +3,18 @@
>
> *** Changes since GDB 8.0
>
> +* On Unix systems, GDB now supports transmitting environment variables
> + that are to be set or unset to GDBserver. These variables will
> + affect the environment to be passed to the remote inferior.
> +
> + To inform GDB of environment variables that are to be transmitted to
> + GDBserver, use the "set environment" command. Only user set
> + environment variables are sent to GDBserver.
> +
> + To inform GDB of environment variables that are to be unset before
> + the remote inferior is started by the GDBserver, use the "unset
> + environment" command.
> +
> * On Unix systems, GDBserver now does globbing expansion and variable
> substitution in inferior command line arguments.
>
> @@ -14,6 +26,18 @@
>
> * New remote packets
>
> +QEnvironmentHexEncoded
> + Inform GDBserver of an environment variable that is to be passed to
> + the inferior when starting it.
> +
> +QEnvironmentUnset
> + Inform GDBserver of an environment variable that is to be unset
> + before starting the remote inferior.
> +
> +QEnvironmentReset
> + Inform GDBserver that the environment should be reset (i.e.,
> + user-set environment variables should be unset).
> +
> QStartupWithShell
> Indicates whether the inferior must be started with a shell or not.
>
> diff --git a/gdb/common/environ.c b/gdb/common/environ.c
> index 698bda3..59b081f 100644
> --- a/gdb/common/environ.c
> +++ b/gdb/common/environ.c
> @@ -30,8 +30,12 @@ gdb_environ::operator= (gdb_environ &&e)
> return *this;
>
> m_environ_vector = std::move (e.m_environ_vector);
> + m_user_set_env_list = std::move (e.m_user_set_env_list);
> + m_user_unset_env_list = std::move (e.m_user_unset_env_list);
> e.m_environ_vector.clear ();
> e.m_environ_vector.push_back (NULL);
> + e.m_user_set_env_list.clear ();
> + e.m_user_unset_env_list.clear ();
> return *this;
> }
>
> @@ -63,6 +67,10 @@ gdb_environ::clear ()
> for (char *v : m_environ_vector)
> xfree (v);
> m_environ_vector.clear ();
> + m_user_set_env_list.clear ();
> + for (const char *v : m_user_unset_env_list)
> + xfree ((void *) v);
> + m_user_unset_env_list.clear ();
> /* Always add the NULL element. */
> m_environ_vector.push_back (NULL);
I'd keep these last two lines just after the "m_environ_vector.clear
()", since they're related.
> }
> @@ -72,7 +80,7 @@ gdb_environ::clear ()
> if it contains, false otherwise. */
>
> static bool
> -match_var_in_string (char *string, const char *var, size_t var_len)
> +match_var_in_string (const char *string, const char *var, size_t
> var_len)
> {
> if (strncmp (string, var, var_len) == 0 && string[var_len] == '=')
> return true;
> @@ -99,32 +107,104 @@ gdb_environ::get (const char *var) const
> void
> gdb_environ::set (const char *var, const char *value)
> {
> + char *fullvar = concat (var, "=", value, NULL);
> +
> /* We have to unset the variable in the vector if it exists. */
> - unset (var);
> + unset (var, false);
>
> /* Insert the element before the last one, which is always NULL. */
> - m_environ_vector.insert (m_environ_vector.end () - 1,
> - concat (var, "=", value, NULL));
> + m_environ_vector.insert (m_environ_vector.end () - 1, fullvar);
> +
> + /* Mark this environment variable as having been set by the user.
> + This will be useful when we deal with setting environment
> + variables on the remote target. */
> + m_user_set_env_list.push_back (fullvar);
> +
> + /* If this environment variable is marked as unset by the user, then
> + remove it from the list, because now the user wants to set
> + it. */
> + for (std::vector<const char *>::iterator iter_user_unset
> + = m_user_unset_env_list.begin ();
> + iter_user_unset != m_user_unset_env_list.end ();
> + ++iter_user_unset)
> + if (strcmp (var, *iter_user_unset) == 0)
> + {
> + void *v = (void *) *iter_user_unset;
> +
> + m_user_unset_env_list.erase (iter_user_unset);
> + xfree (v);
> + break;
> + }
> }
>
> /* See common/environ.h. */
>
> void
> -gdb_environ::unset (const char *var)
> +gdb_environ::unset (const char *var, bool update_unset_list)
> {
> size_t len = strlen (var);
> + std::vector<char *>::iterator it_env;
>
> /* We iterate until '.end () - 1' because the last element is
> always NULL. */
> - for (std::vector<char *>::iterator el = m_environ_vector.begin ();
> - el != m_environ_vector.end () - 1;
> - ++el)
> - if (match_var_in_string (*el, var, len))
> - {
> - xfree (*el);
> - m_environ_vector.erase (el);
> - break;
> - }
> + for (it_env = m_environ_vector.begin ();
> + it_env != m_environ_vector.end () - 1;
> + ++it_env)
> + if (match_var_in_string (*it_env, var, len))
> + break;
> +
> + if (it_env == m_environ_vector.end () - 1)
> + {
> + /* No element has been found. */
> + return;
> + }
> +
> + std::vector<const char *>::iterator it_user_set_env;
> + char *found_var = *it_env;
> +
> + it_user_set_env = std::remove (m_user_set_env_list.begin (),
> + m_user_set_env_list.end (),
> + found_var);
> + if (it_user_set_env != m_user_set_env_list.end ())
> + {
> + /* We found (and removed) the element from the user_set_env
> + vector. */
> + m_user_set_env_list.erase (it_user_set_env,
> m_user_set_env_list.end ());
> + }
> +
> + if (update_unset_list)
> + {
> + bool found_in_unset = false;
> +
> + for (const char *el : m_user_unset_env_list)
> + if (strcmp (el, var) == 0)
> + {
> + found_in_unset = true;
> + break;
> + }
> +
> + if (!found_in_unset)
> + m_user_unset_env_list.push_back (xstrdup (var));
> + }
> +
> + m_environ_vector.erase (it_env);
> + xfree (found_var);
> +}
> +
> +/* See common/environ.h. */
> +
> +void
> +gdb_environ::clear_user_set_env ()
> +{
> + std::vector<const char *> copy = m_user_set_env_list;
> +
> + for (const char *var : copy)
> + {
> + std::string varname (var);
> +
> + varname.erase (varname.find ('='), std::string::npos);
> + unset (varname.c_str (), false);
> + }
> }
>
> /* See common/environ.h. */
> @@ -134,3 +214,17 @@ gdb_environ::envp () const
> {
> return const_cast<char **> (&m_environ_vector[0]);
> }
> +
> +/* See common/environ.h. */
> +
> +const std::vector<const char *> &
> +gdb_environ::user_set_envp () const
> +{
> + return m_user_set_env_list;
> +}
> +
> +const std::vector<const char *> &
> +gdb_environ::user_unset_envp () const
> +{
> + return m_user_unset_env_list;
> +}
> diff --git a/gdb/common/environ.h b/gdb/common/environ.h
> index 0bbb191..2038170 100644
> --- a/gdb/common/environ.h
> +++ b/gdb/common/environ.h
> @@ -41,12 +41,16 @@ public:
>
> /* Move constructor. */
> gdb_environ (gdb_environ &&e)
> - : m_environ_vector (std::move (e.m_environ_vector))
> + : m_environ_vector (std::move (e.m_environ_vector)),
> + m_user_set_env_list (std::move (e.m_user_set_env_list)),
> + m_user_unset_env_list (std::move (e.m_user_unset_env_list))
> {
> /* Make sure that the moved-from vector is left at a valid
> state (only one NULL element). */
> e.m_environ_vector.clear ();
> e.m_environ_vector.push_back (NULL);
> + e.m_user_set_env_list.clear ();
> + e.m_user_unset_env_list.clear ();
> }
>
> /* Move assignment. */
> @@ -68,14 +72,31 @@ public:
> void set (const char *var, const char *value);
>
> /* Unset VAR in environment. */
> - void unset (const char *var);
> + void unset (const char *var, bool update_unset_list = true);
Can you document the new parameter?
If this parameter is only useful internally, you could make it a private
method, so that it's not exposed to the outside:
private:
void unset (const char *var, bool update_unset_list);
and have the public version with a single parameter call it:
void gdb_environ::unset (const char *var)
{
unset (var, true);
}
> +
> + /* Iterate through M_USER_UNSET_ENV_LIST and unset all variables.
> */
> + void clear_user_set_env ();
I wouldn't put the "Iterate through M_USER_UNSET_ENV_LIST" in the
comment, since that's implementation details. The function
documentation should focus on the visible effects or goals of the
function (i.e. remove the user set/unset variables in that environment).
> diff --git a/gdb/common/rsp-low.h b/gdb/common/rsp-low.h
> index b57f58b..0c22d4f 100644
> --- a/gdb/common/rsp-low.h
> +++ b/gdb/common/rsp-low.h
> @@ -52,6 +52,10 @@ extern char *unpack_varlen_hex (char *buff,
> ULONGEST *result);
>
> extern int hex2bin (const char *hex, gdb_byte *bin, int count);
>
> +/* Like hex2bin, but work on std::string. */
> +
> +extern int hex2str (const std::string &hex, std::string &str);
The variables passed to this hex parameter are char* pointing inside the
received packet in server.c. Therefore, to avoid unnecessary copies and
construction of string, I'd leave hex as a const char *. The return
value doesn't seem needed either. What about this signature?
extern std::string hex2str (const char *hex);
> +
> /* Convert some bytes to a hexadecimal representation. BIN holds the
> bytes to convert. COUNT says how many bytes to convert. The
> resulting characters are stored in HEX, followed by a NUL
> @@ -59,6 +63,10 @@ extern int hex2bin (const char *hex, gdb_byte *bin,
> int count);
>
> extern int bin2hex (const gdb_byte *bin, char *hex, int count);
>
> +/* Like bin2hex, but work on std::strings. */
> +
> +extern int str2hex (const std::string &str, std::string &hex);
> +
Similar thing here, what we pass to str is a const char*, so it leads to
an unnecessary std::string construction. Also, the interface of the
function is not well-suited to encode arbitrary binary data, which could
any byte. One could use
str2hex (std::string (p, count), hex);
but again why copy the data in the first place? So what about this:
extern std::string bin2hex (const char *bin, int count);
> --- a/gdb/gdbserver/server.c
> +++ b/gdb/gdbserver/server.c
> @@ -631,6 +631,75 @@ handle_general_set (char *own_buf)
> return;
> }
>
> + if (startswith (own_buf, "QEnvironmentReset"))
> + {
> + our_environ.clear_user_set_env ();
> +
> + write_ok (own_buf);
> + return;
> + }
> +
> + if (startswith (own_buf, "QEnvironmentHexEncoded:"))
> + {
> + const char *p = own_buf + sizeof ("QEnvironmentHexEncoded:") -
> 1;
You can also use strlen to avoid having to do -1, but either is fine
with me.
> + /* The final form of the environment variable. FINAL_VAR will
> + hold the 'VAR=VALUE' format. */
> + std::string final_var;
> + std::string var_name, var_value;
> +
> + if (remote_debug)
> + debug_printf (_("[QEnvironmentHexEncoded received '%s']\n"), p);
> +
> + hex2str (p, final_var);
> +
> + if (remote_debug)
> + {
> + debug_printf (_("[Environment variable to be set: '%s']\n"),
> + final_var.c_str ());
> + debug_flush ();
> + }
> +
> + size_t pos = final_var.find ('=');
> + if (pos == std::string::npos)
> + {
> + warning (_("Unknown environment variable '%s' from remote side."),
I don't find this error message very clear, maybe more something like
"Unexpected format"?
> +/* Helper function to handle the QEnvironment* packets. */
> +
> +static void
> +extended_remote_environment_support (struct remote_state *rs)
> +{
> + if (packet_support (PACKET_QEnvironmentReset) != PACKET_DISABLE)
> + {
> + static const char qenvreset[] = "QEnvironmentReset";
> +
> + putpkt (qenvreset);
Could this be directly
putpkt ("QEnvironmentReset")?
I haven't looked at everything yet and it's getting late here, I'll try
to continue tomorrow.
Thanks!
Simon
^ permalink raw reply [flat|nested] 35+ messages in thread
* Re: [PATCH v2] Implement the ability to set/unset environment variables to GDBserver when starting the inferior
2017-07-27 3:36 ` [PATCH v2] Implement the ability to set/unset " Sergio Durigan Junior
2017-07-27 17:10 ` Eli Zaretskii
2017-07-27 22:05 ` Simon Marchi
@ 2017-07-29 22:36 ` Simon Marchi
2017-08-01 2:43 ` Sergio Durigan Junior
[not found] ` <256083325d9f9c4cc4f5518fe6e5292d@polymtl.ca>
3 siblings, 1 reply; 35+ messages in thread
From: Simon Marchi @ 2017-07-29 22:36 UTC (permalink / raw)
To: Sergio Durigan Junior; +Cc: GDB Patches, Pedro Alves, Eli Zaretskii
Hi Sergio,
I took a closer look at set/unset, I have a few comments.
> @@ -99,32 +107,104 @@ gdb_environ::get (const char *var) const
> void
> gdb_environ::set (const char *var, const char *value)
> {
> + char *fullvar = concat (var, "=", value, NULL);
> +
> /* We have to unset the variable in the vector if it exists. */
> - unset (var);
> + unset (var, false);
>
> /* Insert the element before the last one, which is always NULL. */
> - m_environ_vector.insert (m_environ_vector.end () - 1,
> - concat (var, "=", value, NULL));
> + m_environ_vector.insert (m_environ_vector.end () - 1, fullvar);
> +
> + /* Mark this environment variable as having been set by the user.
> + This will be useful when we deal with setting environment
> + variables on the remote target. */
> + m_user_set_env_list.push_back (fullvar);
> +
> + /* If this environment variable is marked as unset by the user, then
> + remove it from the list, because now the user wants to set
> + it. */
> + for (std::vector<const char *>::iterator iter_user_unset
> + = m_user_unset_env_list.begin ();
> + iter_user_unset != m_user_unset_env_list.end ();
> + ++iter_user_unset)
> + if (strcmp (var, *iter_user_unset) == 0)
> + {
> + void *v = (void *) *iter_user_unset;
> +
> + m_user_unset_env_list.erase (iter_user_unset);
> + xfree (v);
> + break;
> + }
> }
>
> /* See common/environ.h. */
>
> void
> -gdb_environ::unset (const char *var)
> +gdb_environ::unset (const char *var, bool update_unset_list)
> {
> size_t len = strlen (var);
> + std::vector<char *>::iterator it_env;
>
> /* We iterate until '.end () - 1' because the last element is
> always NULL. */
> - for (std::vector<char *>::iterator el = m_environ_vector.begin ();
> - el != m_environ_vector.end () - 1;
> - ++el)
> - if (match_var_in_string (*el, var, len))
> - {
> - xfree (*el);
> - m_environ_vector.erase (el);
> - break;
> - }
> + for (it_env = m_environ_vector.begin ();
> + it_env != m_environ_vector.end () - 1;
> + ++it_env)
> + if (match_var_in_string (*it_env, var, len))
> + break;
> +
> + if (it_env == m_environ_vector.end () - 1)
> + {
> + /* No element has been found. */
> + return;
> + }
Do we want to support the use case to unset an environment variable that
is defined on the remote system, but not on the local system? If so,
the function should probably not return immediately if the variable is
not found in the env vector, so that the unset request ends up in the
list of variables to unset.
> +
> + std::vector<const char *>::iterator it_user_set_env;
> + char *found_var = *it_env;
> +
> + it_user_set_env = std::remove (m_user_set_env_list.begin (),
> + m_user_set_env_list.end (),
> + found_var);
> + if (it_user_set_env != m_user_set_env_list.end ())
> + {
> + /* We found (and removed) the element from the user_set_env
> + vector. */
> + m_user_set_env_list.erase (it_user_set_env,
> m_user_set_env_list.end ());
> + }
> +
> + if (update_unset_list)
> + {
> + bool found_in_unset = false;
> +
> + for (const char *el : m_user_unset_env_list)
> + if (strcmp (el, var) == 0)
> + {
> + found_in_unset = true;
> + break;
> + }
> +
> + if (!found_in_unset)
> + m_user_unset_env_list.push_back (xstrdup (var));
> + }
> +
> + m_environ_vector.erase (it_env);
> + xfree (found_var);
> +}
I have the feeling that we can reduce the amount of boilerplate code in
the set and unset methods by using std::set instead of std::vector.
Performance-wise this may not be very good, since for any reasonable
amount of variables, the vector would probably be more efficient. But
its interface makes the code clearer and lighter, in my opinion. I
suppose we could always make something with a set-like interface and
behavior implemented on top of a vector.
> +
> +/* See common/environ.h. */
> +
> +void
> +gdb_environ::clear_user_set_env ()
> +{
> + std::vector<const char *> copy = m_user_set_env_list;
> +
> + for (const char *var : copy)
> + {
> + std::string varname (var);
> +
> + varname.erase (varname.find ('='), std::string::npos);
> + unset (varname.c_str (), false);
> + }
I am not sure having a method like this is correct. It is used in
gdbserver when we want to "reset" the environment, that is forget all
user-made modifications, both set and unset. To do it correctly, it
should include restoring the variables that have been unset or
overwritten. But as I said in my other mail, I think the simplest
solution will be to restore the environment from a backup copy of the
original env. If we do that, this method won't be needed.
I have prototyped some of my suggestions to make sure they made sense.
I thought I would push them somewhere, feel free to use whatever you
want if that's useful to you:
https://github.com/simark/binutils-gdb/commits/remote-env
Thanks,
Simon
^ permalink raw reply [flat|nested] 35+ messages in thread
* Re: [PATCH v2] Implement the ability to set/unset environment variables to GDBserver when starting the inferior
2017-07-27 22:05 ` Simon Marchi
@ 2017-08-01 2:33 ` Sergio Durigan Junior
2017-08-01 9:35 ` Simon Marchi
0 siblings, 1 reply; 35+ messages in thread
From: Sergio Durigan Junior @ 2017-08-01 2:33 UTC (permalink / raw)
To: Simon Marchi; +Cc: GDB Patches, Pedro Alves, Eli Zaretskii
On Thursday, July 27 2017, Simon Marchi wrote:
> Hi Sergio,
>
> On 2017-07-27 05:35, Sergio Durigan Junior wrote:
>> User-set environment variables are only the variables that are
>> explicitly set by the user, using the 'set environment' command. This
>> means that variables that were already present in the environment when
>> starting GDB/GDBserver are not transmitted/considered by this feature.
>>
>> User-unset environment variables are variables that are explicitly
>> unset by the user, using the 'unset environment' command.
>>
>> The idea behind this patch is to store user-set and user-unset
>> environment variables in two separate vectors, both part of
>> gdb_environ. Then, when extended_remote_create_inferior is preparing
>> to start the inferior, it will iterate over the two lists and
>> set/unset variables accordingly. Three new packets are introduced:
>>
>> - QEnvironmentHexEncoded, which is used to set environment variables,
>> and contains an hex-encoded string in the format "VAR=VALUE" (VALUE
>> can be empty if the user set a variable with a null value, by doing
>> 'set environment VAR=').
>>
>> - QEnvironmentUnset, which is used to unset environment variables, and
>> contains an hex-encoded string in the format "VAR".
>>
>> - QEnvironmentReset, which is always the first packet to be
>> transmitted, and is used to reset (i.e., unset any user-set
>> environment variable) the environment.
>
> There is this use case for which the behavior is different between
> native and remote, related to unset
>
> native:
>
> (gdb inf1) file /usr/bin/env
> (gdb inf1) unset environment DISPLAY
> (gdb inf1) r # DISPLAY is not there
> (gdb inf1) add-inferior -exec /usr/bin/env
> (gdb inf1) inferior 2
> (gdb inf2) r # DISPLAY is there
>
> remote:
>
> (gdb inf1) tar ext :1234
> (gdb inf1) file /usr/bin/env
> (gdb inf1) set remote exec-file /usr/bin/env
> (gdb inf1) unset environment DISPLAY
> (gdb inf1) r # DISPLAY is not there
> (gdb inf1) add-inferior -exec /usr/bin/env
> (gdb inf1) inferior 2
> (gdb inf2) set remote exec-file /usr/bin/env
> (gdb inf2) r # DISPLAY is not there
>
> I think that's because in GDB, we make a new pristine copy of the host
> environment for every inferior, which we don't in gdbserver.
Thanks for the review, Simon.
Yes, you're right, these cases are currently different because of the
way we handle the environment on GDB and gdbserver. On gdbserver we
have 'our_environ', which is a global declared at server.c and that is
passed to all inferiors being started.
> The way I understand the QEnvironmentReset is that the remote agent
> (gdbserver) should forget any previous modification to the environment
> made using QEnvironmentHexEncoded and QEnvironmentUnset and return the
> environment to its original state, when it was launched. This should
> allow supporting the use case above. To implement that properly, we
> would need to keep a copy of gdbserver's initial environment, which we
> could revert to when receiving a QEnvironmentReset.
Yes, and we already do that on gdbserver; see the 'our_environ' global.
> In any case, I just want to make sure that the packets we introduce
> are not the things that limit us.
Sorry, I'm not sure I understood what you have in mind. Could you
explain in what ways we'd be limited by the new packets?
> [...]
>> diff --git a/gdb/common/environ.c b/gdb/common/environ.c
>> index 698bda3..59b081f 100644
>> --- a/gdb/common/environ.c
>> +++ b/gdb/common/environ.c
>> @@ -30,8 +30,12 @@ gdb_environ::operator= (gdb_environ &&e)
>> return *this;
>>
>> m_environ_vector = std::move (e.m_environ_vector);
>> + m_user_set_env_list = std::move (e.m_user_set_env_list);
>> + m_user_unset_env_list = std::move (e.m_user_unset_env_list);
>> e.m_environ_vector.clear ();
>> e.m_environ_vector.push_back (NULL);
>> + e.m_user_set_env_list.clear ();
>> + e.m_user_unset_env_list.clear ();
>> return *this;
>> }
>>
>> @@ -63,6 +67,10 @@ gdb_environ::clear ()
>> for (char *v : m_environ_vector)
>> xfree (v);
>> m_environ_vector.clear ();
>> + m_user_set_env_list.clear ();
>> + for (const char *v : m_user_unset_env_list)
>> + xfree ((void *) v);
>> + m_user_unset_env_list.clear ();
>> /* Always add the NULL element. */
>> m_environ_vector.push_back (NULL);
>
> I'd keep these last two lines just after the "m_environ_vector.clear
> ()", since they're related.
Hm, good, I was trying to keep them close but I forgot this one. Fixed.
>
>> }
>> @@ -72,7 +80,7 @@ gdb_environ::clear ()
>> if it contains, false otherwise. */
>>
>> static bool
>> -match_var_in_string (char *string, const char *var, size_t var_len)
>> +match_var_in_string (const char *string, const char *var, size_t
>> var_len)
>> {
>> if (strncmp (string, var, var_len) == 0 && string[var_len] == '=')
>> return true;
>> @@ -99,32 +107,104 @@ gdb_environ::get (const char *var) const
>> void
>> gdb_environ::set (const char *var, const char *value)
>> {
>> + char *fullvar = concat (var, "=", value, NULL);
>> +
>> /* We have to unset the variable in the vector if it exists. */
>> - unset (var);
>> + unset (var, false);
>>
>> /* Insert the element before the last one, which is always NULL. */
>> - m_environ_vector.insert (m_environ_vector.end () - 1,
>> - concat (var, "=", value, NULL));
>> + m_environ_vector.insert (m_environ_vector.end () - 1, fullvar);
>> +
>> + /* Mark this environment variable as having been set by the user.
>> + This will be useful when we deal with setting environment
>> + variables on the remote target. */
>> + m_user_set_env_list.push_back (fullvar);
>> +
>> + /* If this environment variable is marked as unset by the user, then
>> + remove it from the list, because now the user wants to set
>> + it. */
>> + for (std::vector<const char *>::iterator iter_user_unset
>> + = m_user_unset_env_list.begin ();
>> + iter_user_unset != m_user_unset_env_list.end ();
>> + ++iter_user_unset)
>> + if (strcmp (var, *iter_user_unset) == 0)
>> + {
>> + void *v = (void *) *iter_user_unset;
>> +
>> + m_user_unset_env_list.erase (iter_user_unset);
>> + xfree (v);
>> + break;
>> + }
>> }
>>
>> /* See common/environ.h. */
>>
>> void
>> -gdb_environ::unset (const char *var)
>> +gdb_environ::unset (const char *var, bool update_unset_list)
>> {
>> size_t len = strlen (var);
>> + std::vector<char *>::iterator it_env;
>>
>> /* We iterate until '.end () - 1' because the last element is
>> always NULL. */
>> - for (std::vector<char *>::iterator el = m_environ_vector.begin ();
>> - el != m_environ_vector.end () - 1;
>> - ++el)
>> - if (match_var_in_string (*el, var, len))
>> - {
>> - xfree (*el);
>> - m_environ_vector.erase (el);
>> - break;
>> - }
>> + for (it_env = m_environ_vector.begin ();
>> + it_env != m_environ_vector.end () - 1;
>> + ++it_env)
>> + if (match_var_in_string (*it_env, var, len))
>> + break;
>> +
>> + if (it_env == m_environ_vector.end () - 1)
>> + {
>> + /* No element has been found. */
>> + return;
>> + }
>> +
>> + std::vector<const char *>::iterator it_user_set_env;
>> + char *found_var = *it_env;
>> +
>> + it_user_set_env = std::remove (m_user_set_env_list.begin (),
>> + m_user_set_env_list.end (),
>> + found_var);
>> + if (it_user_set_env != m_user_set_env_list.end ())
>> + {
>> + /* We found (and removed) the element from the user_set_env
>> + vector. */
>> + m_user_set_env_list.erase (it_user_set_env,
>> m_user_set_env_list.end ());
>> + }
>> +
>> + if (update_unset_list)
>> + {
>> + bool found_in_unset = false;
>> +
>> + for (const char *el : m_user_unset_env_list)
>> + if (strcmp (el, var) == 0)
>> + {
>> + found_in_unset = true;
>> + break;
>> + }
>> +
>> + if (!found_in_unset)
>> + m_user_unset_env_list.push_back (xstrdup (var));
>> + }
>> +
>> + m_environ_vector.erase (it_env);
>> + xfree (found_var);
>> +}
>> +
>> +/* See common/environ.h. */
>> +
>> +void
>> +gdb_environ::clear_user_set_env ()
>> +{
>> + std::vector<const char *> copy = m_user_set_env_list;
>> +
>> + for (const char *var : copy)
>> + {
>> + std::string varname (var);
>> +
>> + varname.erase (varname.find ('='), std::string::npos);
>> + unset (varname.c_str (), false);
>> + }
>> }
>>
>> /* See common/environ.h. */
>> @@ -134,3 +214,17 @@ gdb_environ::envp () const
>> {
>> return const_cast<char **> (&m_environ_vector[0]);
>> }
>> +
>> +/* See common/environ.h. */
>> +
>> +const std::vector<const char *> &
>> +gdb_environ::user_set_envp () const
>> +{
>> + return m_user_set_env_list;
>> +}
>> +
>> +const std::vector<const char *> &
>> +gdb_environ::user_unset_envp () const
>> +{
>> + return m_user_unset_env_list;
>> +}
>> diff --git a/gdb/common/environ.h b/gdb/common/environ.h
>> index 0bbb191..2038170 100644
>> --- a/gdb/common/environ.h
>> +++ b/gdb/common/environ.h
>> @@ -41,12 +41,16 @@ public:
>>
>> /* Move constructor. */
>> gdb_environ (gdb_environ &&e)
>> - : m_environ_vector (std::move (e.m_environ_vector))
>> + : m_environ_vector (std::move (e.m_environ_vector)),
>> + m_user_set_env_list (std::move (e.m_user_set_env_list)),
>> + m_user_unset_env_list (std::move (e.m_user_unset_env_list))
>> {
>> /* Make sure that the moved-from vector is left at a valid
>> state (only one NULL element). */
>> e.m_environ_vector.clear ();
>> e.m_environ_vector.push_back (NULL);
>> + e.m_user_set_env_list.clear ();
>> + e.m_user_unset_env_list.clear ();
>> }
>>
>> /* Move assignment. */
>> @@ -68,14 +72,31 @@ public:
>> void set (const char *var, const char *value);
>>
>> /* Unset VAR in environment. */
>> - void unset (const char *var);
>> + void unset (const char *var, bool update_unset_list = true);
>
> Can you document the new parameter?
>
> If this parameter is only useful internally, you could make it a
> private method, so that it's not exposed to the outside:
>
> private:
> void unset (const char *var, bool update_unset_list);
>
> and have the public version with a single parameter call it:
>
> void gdb_environ::unset (const char *var)
> {
> unset (var, true);
> }
Good idea! I was trying to avoid adding a new method for that, but
overloading unset seems like a cleaner approach.
>
>> +
>> + /* Iterate through M_USER_UNSET_ENV_LIST and unset all variables.
>> */
>> + void clear_user_set_env ();
>
> I wouldn't put the "Iterate through M_USER_UNSET_ENV_LIST" in the
> comment, since that's implementation details. The function
> documentation should focus on the visible effects or goals of the
> function (i.e. remove the user set/unset variables in that
> environment).
Good point. I've updated the comment to:
/* Unset all variables that were previously set by the user, i.e.,
that were added by calling the SET method. */
void clear_user_set_env ();
>> diff --git a/gdb/common/rsp-low.h b/gdb/common/rsp-low.h
>> index b57f58b..0c22d4f 100644
>> --- a/gdb/common/rsp-low.h
>> +++ b/gdb/common/rsp-low.h
>> @@ -52,6 +52,10 @@ extern char *unpack_varlen_hex (char *buff,
>> ULONGEST *result);
>>
>> extern int hex2bin (const char *hex, gdb_byte *bin, int count);
>>
>> +/* Like hex2bin, but work on std::string. */
>> +
>> +extern int hex2str (const std::string &hex, std::string &str);
>
> The variables passed to this hex parameter are char* pointing inside
> the received packet in server.c. Therefore, to avoid unnecessary
> copies and construction of string, I'd leave hex as a const char *.
> The return value doesn't seem needed either. What about this
> signature?
>
> extern std::string hex2str (const char *hex);
Fair enough, I like the idea. Update accordingly.
>
>> +
>> /* Convert some bytes to a hexadecimal representation. BIN holds the
>> bytes to convert. COUNT says how many bytes to convert. The
>> resulting characters are stored in HEX, followed by a NUL
>> @@ -59,6 +63,10 @@ extern int hex2bin (const char *hex, gdb_byte *bin,
>> int count);
>>
>> extern int bin2hex (const gdb_byte *bin, char *hex, int count);
>>
>> +/* Like bin2hex, but work on std::strings. */
>> +
>> +extern int str2hex (const std::string &str, std::string &hex);
>> +
>
> Similar thing here, what we pass to str is a const char*, so it leads
> to an unnecessary std::string construction. Also, the interface of
> the function is not well-suited to encode arbitrary binary data, which
> could any byte. One could use
>
> str2hex (std::string (p, count), hex);
>
> but again why copy the data in the first place? So what about this:
>
> extern std::string bin2hex (const char *bin, int count);
Not sure if it was a typo or if you really meant to propose overloading
the bin2hex method and have another version of it that returns a
std::string, but I like the idea. I don't think we need to treat the
first parameter as a 'const char *'; TBH, treating it like a 'const
gdb_byte *' (just like the original version does) is more clear. So I
went ahead and implemented it this way.
>
>> --- a/gdb/gdbserver/server.c
>> +++ b/gdb/gdbserver/server.c
>> @@ -631,6 +631,75 @@ handle_general_set (char *own_buf)
>> return;
>> }
>>
>> + if (startswith (own_buf, "QEnvironmentReset"))
>> + {
>> + our_environ.clear_user_set_env ();
>> +
>> + write_ok (own_buf);
>> + return;
>> + }
>> +
>> + if (startswith (own_buf, "QEnvironmentHexEncoded:"))
>> + {
>> + const char *p = own_buf + sizeof ("QEnvironmentHexEncoded:")
>> -
>> 1;
>
> You can also use strlen to avoid having to do -1, but either is fine
> with me.
I personally like using sizeof here and avoiding the call to strlen.
>> + /* The final form of the environment variable. FINAL_VAR will
>> + hold the 'VAR=VALUE' format. */
>> + std::string final_var;
>> + std::string var_name, var_value;
>> +
>> + if (remote_debug)
>> + debug_printf (_("[QEnvironmentHexEncoded received '%s']\n"), p);
>> +
>> + hex2str (p, final_var);
>> +
>> + if (remote_debug)
>> + {
>> + debug_printf (_("[Environment variable to be set: '%s']\n"),
>> + final_var.c_str ());
>> + debug_flush ();
>> + }
>> +
>> + size_t pos = final_var.find ('=');
>> + if (pos == std::string::npos)
>> + {
>> + warning (_("Unknown environment variable '%s' from remote side."),
>
> I don't find this error message very clear, maybe more something like
> "Unexpected format"?
Sure thing.
>
>> +/* Helper function to handle the QEnvironment* packets. */
>> +
>> +static void
>> +extended_remote_environment_support (struct remote_state *rs)
>> +{
>> + if (packet_support (PACKET_QEnvironmentReset) != PACKET_DISABLE)
>> + {
>> + static const char qenvreset[] = "QEnvironmentReset";
>> +
>> + putpkt (qenvreset);
>
> Could this be directly
>
> putpkt ("QEnvironmentReset")?
Yep, totally.
> I haven't looked at everything yet and it's getting late here, I'll
> try to continue tomorrow.
Thanks for the message. I'll now reply to your other review :-).
--
Sergio
GPG key ID: 237A 54B1 0287 28BF 00EF 31F4 D0EB 7628 65FC 5E36
Please send encrypted e-mail if possible
http://sergiodj.net/
^ permalink raw reply [flat|nested] 35+ messages in thread
* Re: [PATCH v2] Implement the ability to set/unset environment variables to GDBserver when starting the inferior
2017-07-29 22:36 ` Simon Marchi
@ 2017-08-01 2:43 ` Sergio Durigan Junior
2017-08-01 9:54 ` Simon Marchi
0 siblings, 1 reply; 35+ messages in thread
From: Sergio Durigan Junior @ 2017-08-01 2:43 UTC (permalink / raw)
To: Simon Marchi; +Cc: GDB Patches, Pedro Alves, Eli Zaretskii
On Saturday, July 29 2017, Simon Marchi wrote:
> Hi Sergio,
>
> I took a closer look at set/unset, I have a few comments.
Thanks, Simon.
>> @@ -99,32 +107,104 @@ gdb_environ::get (const char *var) const
>> void
>> gdb_environ::set (const char *var, const char *value)
>> {
>> + char *fullvar = concat (var, "=", value, NULL);
>> +
>> /* We have to unset the variable in the vector if it exists. */
>> - unset (var);
>> + unset (var, false);
>>
>> /* Insert the element before the last one, which is always NULL. */
>> - m_environ_vector.insert (m_environ_vector.end () - 1,
>> - concat (var, "=", value, NULL));
>> + m_environ_vector.insert (m_environ_vector.end () - 1, fullvar);
>> +
>> + /* Mark this environment variable as having been set by the user.
>> + This will be useful when we deal with setting environment
>> + variables on the remote target. */
>> + m_user_set_env_list.push_back (fullvar);
>> +
>> + /* If this environment variable is marked as unset by the user, then
>> + remove it from the list, because now the user wants to set
>> + it. */
>> + for (std::vector<const char *>::iterator iter_user_unset
>> + = m_user_unset_env_list.begin ();
>> + iter_user_unset != m_user_unset_env_list.end ();
>> + ++iter_user_unset)
>> + if (strcmp (var, *iter_user_unset) == 0)
>> + {
>> + void *v = (void *) *iter_user_unset;
>> +
>> + m_user_unset_env_list.erase (iter_user_unset);
>> + xfree (v);
>> + break;
>> + }
>> }
>>
>> /* See common/environ.h. */
>>
>> void
>> -gdb_environ::unset (const char *var)
>> +gdb_environ::unset (const char *var, bool update_unset_list)
>> {
>> size_t len = strlen (var);
>> + std::vector<char *>::iterator it_env;
>>
>> /* We iterate until '.end () - 1' because the last element is
>> always NULL. */
>> - for (std::vector<char *>::iterator el = m_environ_vector.begin ();
>> - el != m_environ_vector.end () - 1;
>> - ++el)
>> - if (match_var_in_string (*el, var, len))
>> - {
>> - xfree (*el);
>> - m_environ_vector.erase (el);
>> - break;
>> - }
>> + for (it_env = m_environ_vector.begin ();
>> + it_env != m_environ_vector.end () - 1;
>> + ++it_env)
>> + if (match_var_in_string (*it_env, var, len))
>> + break;
>> +
>> + if (it_env == m_environ_vector.end () - 1)
>> + {
>> + /* No element has been found. */
>> + return;
>> + }
>
> Do we want to support the use case to unset an environment variable
> that is defined on the remote system, but not on the local system? If
> so, the function should probably not return immediately if the
> variable is not found in the env vector, so that the unset request
> ends up in the list of variables to unset.
I guess I hadn't thought about this use case. It makes sense to me. I
will make the proper modifications.
>> +
>> + std::vector<const char *>::iterator it_user_set_env;
>> + char *found_var = *it_env;
>> +
>> + it_user_set_env = std::remove (m_user_set_env_list.begin (),
>> + m_user_set_env_list.end (),
>> + found_var);
>> + if (it_user_set_env != m_user_set_env_list.end ())
>> + {
>> + /* We found (and removed) the element from the user_set_env
>> + vector. */
>> + m_user_set_env_list.erase (it_user_set_env,
>> m_user_set_env_list.end ());
>> + }
>> +
>> + if (update_unset_list)
>> + {
>> + bool found_in_unset = false;
>> +
>> + for (const char *el : m_user_unset_env_list)
>> + if (strcmp (el, var) == 0)
>> + {
>> + found_in_unset = true;
>> + break;
>> + }
>> +
>> + if (!found_in_unset)
>> + m_user_unset_env_list.push_back (xstrdup (var));
>> + }
>> +
>> + m_environ_vector.erase (it_env);
>> + xfree (found_var);
>> +}
>
> I have the feeling that we can reduce the amount of boilerplate code
> in the set and unset methods by using std::set instead of std::vector.
> Performance-wise this may not be very good, since for any reasonable
> amount of variables, the vector would probably be more efficient. But
> its interface makes the code clearer and lighter, in my opinion. I
> suppose we could always make something with a set-like interface and
> behavior implemented on top of a vector.
I thought about using std::set, but given that I was recently called out
for doing "premature pessimization", I chose to stick with std::vector.
I agree that for some cases std::set would make things easier to
implement/understand.
>
>> +
>> +/* See common/environ.h. */
>> +
>> +void
>> +gdb_environ::clear_user_set_env ()
>> +{
>> + std::vector<const char *> copy = m_user_set_env_list;
>> +
>> + for (const char *var : copy)
>> + {
>> + std::string varname (var);
>> +
>> + varname.erase (varname.find ('='), std::string::npos);
>> + unset (varname.c_str (), false);
>> + }
>
> I am not sure having a method like this is correct. It is used in
> gdbserver when we want to "reset" the environment, that is forget all
> user-made modifications, both set and unset. To do it correctly, it
> should include restoring the variables that have been unset or
> overwritten. But as I said in my other mail, I think the simplest
> solution will be to restore the environment from a backup copy of the
> original env. If we do that, this method won't be needed.
>
> I have prototyped some of my suggestions to make sure they made sense.
> I thought I would push them somewhere, feel free to use whatever you
> want if that's useful to you:
>
> https://github.com/simark/binutils-gdb/commits/remote-env
Thanks, that's very useful. I have code for most of what you requested,
but I'll use some ideas from your branch.
Cheers,
--
Sergio
GPG key ID: 237A 54B1 0287 28BF 00EF 31F4 D0EB 7628 65FC 5E36
Please send encrypted e-mail if possible
http://sergiodj.net/
^ permalink raw reply [flat|nested] 35+ messages in thread
* Re: [PATCH v2] Implement the ability to set/unset environment variables to GDBserver when starting the inferior
2017-08-01 2:33 ` Sergio Durigan Junior
@ 2017-08-01 9:35 ` Simon Marchi
2017-08-04 22:56 ` Sergio Durigan Junior
0 siblings, 1 reply; 35+ messages in thread
From: Simon Marchi @ 2017-08-01 9:35 UTC (permalink / raw)
To: Sergio Durigan Junior; +Cc: GDB Patches, Pedro Alves, Eli Zaretskii
Hi Sergio,
On 2017-08-01 04:33, Sergio Durigan Junior wrote:
>> There is this use case for which the behavior is different between
>> native and remote, related to unset
>>
>> native:
>>
>> (gdb inf1) file /usr/bin/env
>> (gdb inf1) unset environment DISPLAY
>> (gdb inf1) r # DISPLAY is not there
>> (gdb inf1) add-inferior -exec /usr/bin/env
>> (gdb inf1) inferior 2
>> (gdb inf2) r # DISPLAY is there
>>
>> remote:
>>
>> (gdb inf1) tar ext :1234
>> (gdb inf1) file /usr/bin/env
>> (gdb inf1) set remote exec-file /usr/bin/env
>> (gdb inf1) unset environment DISPLAY
>> (gdb inf1) r # DISPLAY is not there
>> (gdb inf1) add-inferior -exec /usr/bin/env
>> (gdb inf1) inferior 2
>> (gdb inf2) set remote exec-file /usr/bin/env
>> (gdb inf2) r # DISPLAY is not there
>>
>> I think that's because in GDB, we make a new pristine copy of the host
>> environment for every inferior, which we don't in gdbserver.
>
> Thanks for the review, Simon.
>
> Yes, you're right, these cases are currently different because of the
> way we handle the environment on GDB and gdbserver. On gdbserver we
> have 'our_environ', which is a global declared at server.c and that is
> passed to all inferiors being started.
>
>> The way I understand the QEnvironmentReset is that the remote agent
>> (gdbserver) should forget any previous modification to the environment
>> made using QEnvironmentHexEncoded and QEnvironmentUnset and return the
>> environment to its original state, when it was launched. This should
>> allow supporting the use case above. To implement that properly, we
>> would need to keep a copy of gdbserver's initial environment, which we
>> could revert to when receiving a QEnvironmentReset.
>
> Yes, and we already do that on gdbserver; see the 'our_environ' global.
Maybe I'm reading the code wrong, but that's not what I understand.
gdb_environ is never reset to gdbserver's original state. So if the
DISPLAY env var is present in the original environment and is unset with
a QEnvironmentUnset, a QEnvironmentReset won't make it reappear with the
current implementation. But we would want it to be back, to support the
scenario illustrated above, wouldn't we?
I originally talked about keeping a copy of the initial environment, but
actually when receiving a QEnvironmentReset, I think gdbserver should
simply do
our_environ = gdb_environ::from_host_environ ();
>> In any case, I just want to make sure that the packets we introduce
>> are not the things that limit us.
>
> Sorry, I'm not sure I understood what you have in mind. Could you
> explain in what ways we'd be limited by the new packets?
Oh, I just wanted to say that if the gdbserver implementation is not
perfect yet, it's not the end of the world because that can always
change. But the behavior of the RSP packets is more difficult to change
once it becomes a published interface, so we need to be careful that
their documented behavior covers all the use cases we want to support.
But I know you already know that, so I don't know why I said it in the
first place :).
>>> +
>>> + /* Iterate through M_USER_UNSET_ENV_LIST and unset all variables.
>>> */
>>> + void clear_user_set_env ();
>>
>> I wouldn't put the "Iterate through M_USER_UNSET_ENV_LIST" in the
>> comment, since that's implementation details. The function
>> documentation should focus on the visible effects or goals of the
>> function (i.e. remove the user set/unset variables in that
>> environment).
>
> Good point. I've updated the comment to:
>
> /* Unset all variables that were previously set by the user, i.e.,
> that were added by calling the SET method. */
> void clear_user_set_env ();
Sounds good.
>> Similar thing here, what we pass to str is a const char*, so it leads
>> to an unnecessary std::string construction. Also, the interface of
>> the function is not well-suited to encode arbitrary binary data, which
>> could any byte. One could use
>>
>> str2hex (std::string (p, count), hex);
>>
>> but again why copy the data in the first place? So what about this:
>>
>> extern std::string bin2hex (const char *bin, int count);
>
> Not sure if it was a typo or if you really meant to propose overloading
> the bin2hex method and have another version of it that returns a
> std::string, but I like the idea. I don't think we need to treat the
> first parameter as a 'const char *'; TBH, treating it like a 'const
> gdb_byte *' (just like the original version does) is more clear. So I
> went ahead and implemented it this way.
Sorry, I should have been more explicit. I really meant to overload
bin2hex, since there's no point in limiting this function's scope to
hex-encoding text strings, it can handle any binary data (of which a
text strings are a subset). But you are right, it should be const
gdb_byte *.
>>> --- a/gdb/gdbserver/server.c
>>> +++ b/gdb/gdbserver/server.c
>>> @@ -631,6 +631,75 @@ handle_general_set (char *own_buf)
>>> return;
>>> }
>>>
>>> + if (startswith (own_buf, "QEnvironmentReset"))
>>> + {
>>> + our_environ.clear_user_set_env ();
>>> +
>>> + write_ok (own_buf);
>>> + return;
>>> + }
>>> +
>>> + if (startswith (own_buf, "QEnvironmentHexEncoded:"))
>>> + {
>>> + const char *p = own_buf + sizeof ("QEnvironmentHexEncoded:")
>>> -
>>> 1;
>>
>> You can also use strlen to avoid having to do -1, but either is fine
>> with me.
>
> I personally like using sizeof here and avoiding the call to strlen.
Ok, but remember that the compilers optimize those calls to strlen
("literal") away to a constant.
Thanks,
Simon
^ permalink raw reply [flat|nested] 35+ messages in thread
* Re: [PATCH v2] Implement the ability to set/unset environment variables to GDBserver when starting the inferior
2017-08-01 2:43 ` Sergio Durigan Junior
@ 2017-08-01 9:54 ` Simon Marchi
2017-08-04 23:03 ` Sergio Durigan Junior
0 siblings, 1 reply; 35+ messages in thread
From: Simon Marchi @ 2017-08-01 9:54 UTC (permalink / raw)
To: Sergio Durigan Junior; +Cc: GDB Patches, Pedro Alves, Eli Zaretskii
On 2017-08-01 04:42, Sergio Durigan Junior wrote:
>> I have the feeling that we can reduce the amount of boilerplate code
>> in the set and unset methods by using std::set instead of std::vector.
>> Performance-wise this may not be very good, since for any reasonable
>> amount of variables, the vector would probably be more efficient. But
>> its interface makes the code clearer and lighter, in my opinion. I
>> suppose we could always make something with a set-like interface and
>> behavior implemented on top of a vector.
>
> I thought about using std::set, but given that I was recently called
> out
> for doing "premature pessimization", I chose to stick with std::vector.
> I agree that for some cases std::set would make things easier to
> implement/understand.
Yeah, the thing with std::set that simplifies the code is its interface,
not its implementation. And it would indeed not be a good idea
performance-wise (both CPU cycles and memory) to use std::set for
something that would typically contain a handful of elements at the
most. So that's why I think using something that has (part of) the
interface of an std::set but implemented on top of an std::vector would
be good. I'll try to prototype something soon.
Simon
^ permalink raw reply [flat|nested] 35+ messages in thread
* Re: [PATCH v2] Implement the ability to set/unset environment variables to GDBserver when starting the inferior
2017-08-01 9:35 ` Simon Marchi
@ 2017-08-04 22:56 ` Sergio Durigan Junior
0 siblings, 0 replies; 35+ messages in thread
From: Sergio Durigan Junior @ 2017-08-04 22:56 UTC (permalink / raw)
To: Simon Marchi; +Cc: GDB Patches, Pedro Alves, Eli Zaretskii
On Tuesday, August 01 2017, Simon Marchi wrote:
> Hi Sergio,
>
> On 2017-08-01 04:33, Sergio Durigan Junior wrote:
>>> There is this use case for which the behavior is different between
>>> native and remote, related to unset
>>>
>>> native:
>>>
>>> (gdb inf1) file /usr/bin/env
>>> (gdb inf1) unset environment DISPLAY
>>> (gdb inf1) r # DISPLAY is not there
>>> (gdb inf1) add-inferior -exec /usr/bin/env
>>> (gdb inf1) inferior 2
>>> (gdb inf2) r # DISPLAY is there
>>>
>>> remote:
>>>
>>> (gdb inf1) tar ext :1234
>>> (gdb inf1) file /usr/bin/env
>>> (gdb inf1) set remote exec-file /usr/bin/env
>>> (gdb inf1) unset environment DISPLAY
>>> (gdb inf1) r # DISPLAY is not there
>>> (gdb inf1) add-inferior -exec /usr/bin/env
>>> (gdb inf1) inferior 2
>>> (gdb inf2) set remote exec-file /usr/bin/env
>>> (gdb inf2) r # DISPLAY is not there
>>>
>>> I think that's because in GDB, we make a new pristine copy of the host
>>> environment for every inferior, which we don't in gdbserver.
>>
>> Thanks for the review, Simon.
>>
>> Yes, you're right, these cases are currently different because of the
>> way we handle the environment on GDB and gdbserver. On gdbserver we
>> have 'our_environ', which is a global declared at server.c and that is
>> passed to all inferiors being started.
>>
>>> The way I understand the QEnvironmentReset is that the remote agent
>>> (gdbserver) should forget any previous modification to the environment
>>> made using QEnvironmentHexEncoded and QEnvironmentUnset and return the
>>> environment to its original state, when it was launched. This should
>>> allow supporting the use case above. To implement that properly, we
>>> would need to keep a copy of gdbserver's initial environment, which we
>>> could revert to when receiving a QEnvironmentReset.
>>
>> Yes, and we already do that on gdbserver; see the 'our_environ' global.
>
> Maybe I'm reading the code wrong, but that's not what I understand.
> gdb_environ is never reset to gdbserver's original state. So if the
> DISPLAY env var is present in the original environment and is unset
> with a QEnvironmentUnset, a QEnvironmentReset won't make it reappear
> with the current implementation. But we would want it to be back, to
> support the scenario illustrated above, wouldn't we?
>
> I originally talked about keeping a copy of the initial environment,
> but actually when receiving a QEnvironmentReset, I think gdbserver
> should simply do
>
> our_environ = gdb_environ::from_host_environ ();
You're right. This specific behaviour is not implemented yet. I guess
I replied too early saying that this is working, but it's not.
>>> In any case, I just want to make sure that the packets we introduce
>>> are not the things that limit us.
>>
>> Sorry, I'm not sure I understood what you have in mind. Could you
>> explain in what ways we'd be limited by the new packets?
>
> Oh, I just wanted to say that if the gdbserver implementation is not
> perfect yet, it's not the end of the world because that can always
> change. But the behavior of the RSP packets is more difficult to
> change once it becomes a published interface, so we need to be careful
> that their documented behavior covers all the use cases we want to
> support. But I know you already know that, so I don't know why I said
> it in the first place :).
Aha, now it makes sense :-). And no worries, it's always good to
make sure that we're on the right path.
>>>> --- a/gdb/gdbserver/server.c
>>>> +++ b/gdb/gdbserver/server.c
>>>> @@ -631,6 +631,75 @@ handle_general_set (char *own_buf)
>>>> return;
>>>> }
>>>>
>>>> + if (startswith (own_buf, "QEnvironmentReset"))
>>>> + {
>>>> + our_environ.clear_user_set_env ();
>>>> +
>>>> + write_ok (own_buf);
>>>> + return;
>>>> + }
>>>> +
>>>> + if (startswith (own_buf, "QEnvironmentHexEncoded:"))
>>>> + {
>>>> + const char *p = own_buf + sizeof ("QEnvironmentHexEncoded:")
>>>> -
>>>> 1;
>>>
>>> You can also use strlen to avoid having to do -1, but either is fine
>>> with me.
>>
>> I personally like using sizeof here and avoiding the call to strlen.
>
> Ok, but remember that the compilers optimize those calls to strlen
> ("literal") away to a constant.
Fair enough. I'll use strlen then :-).
Thanks,
--
Sergio
GPG key ID: 237A 54B1 0287 28BF 00EF 31F4 D0EB 7628 65FC 5E36
Please send encrypted e-mail if possible
http://sergiodj.net/
^ permalink raw reply [flat|nested] 35+ messages in thread
* Re: [PATCH v2] Implement the ability to set/unset environment variables to GDBserver when starting the inferior
2017-08-01 9:54 ` Simon Marchi
@ 2017-08-04 23:03 ` Sergio Durigan Junior
2017-08-05 18:14 ` Simon Marchi
0 siblings, 1 reply; 35+ messages in thread
From: Sergio Durigan Junior @ 2017-08-04 23:03 UTC (permalink / raw)
To: Simon Marchi; +Cc: GDB Patches, Pedro Alves, Eli Zaretskii
On Tuesday, August 01 2017, Simon Marchi wrote:
> On 2017-08-01 04:42, Sergio Durigan Junior wrote:
>>> I have the feeling that we can reduce the amount of boilerplate code
>>> in the set and unset methods by using std::set instead of std::vector.
>>> Performance-wise this may not be very good, since for any reasonable
>>> amount of variables, the vector would probably be more efficient. But
>>> its interface makes the code clearer and lighter, in my opinion. I
>>> suppose we could always make something with a set-like interface and
>>> behavior implemented on top of a vector.
>>
>> I thought about using std::set, but given that I was recently called
>> out
>> for doing "premature pessimization", I chose to stick with std::vector.
>> I agree that for some cases std::set would make things easier to
>> implement/understand.
>
> Yeah, the thing with std::set that simplifies the code is its
> interface, not its implementation. And it would indeed not be a good
> idea performance-wise (both CPU cycles and memory) to use std::set for
> something that would typically contain a handful of elements at the
> most. So that's why I think using something that has (part of) the
> interface of an std::set but implemented on top of an std::vector
> would be good. I'll try to prototype something soon.
So, I've been thinking about this implementation over the last few days,
but it's still a bit confuse to me. My C++-foo is not so good as yours,
so maybe you can give me a hand.
From what I understood initially, your intention was to make a new class
that inherited from std::vector but overloaded/implemented methods to
mimic what a std::set would do. However, after reading a bit, it
doesn't seem like a good idea to inherit from std::vector (or any std
containers). Which made me realize that maybe you are talking about
creating a class that encapsulates a std::vector, without inheriting
from it, and that provided the regular .push_back, .insert, .empty,
etc., methods, but in an enhanced way in order to e.g. prevent the
insert of duplicated elements, which is one of the things we miss from
std::set.
Am I in the right direction here? Also, I started to think... I don't
envision the user setting thousands of user-set environment variables,
so *maybe* using std::set would be OK-ish for our use case scenario. I
don't know. While I understand the concern about premature
pessimization, I'm also not a fan of complicating the implementation
just for a little bit of optimization either.
WDYT?
--
Sergio
GPG key ID: 237A 54B1 0287 28BF 00EF 31F4 D0EB 7628 65FC 5E36
Please send encrypted e-mail if possible
http://sergiodj.net/
^ permalink raw reply [flat|nested] 35+ messages in thread
* Re: [PATCH v2] Implement the ability to set/unset environment variables to GDBserver when starting the inferior
2017-08-04 23:03 ` Sergio Durigan Junior
@ 2017-08-05 18:14 ` Simon Marchi
2017-08-12 4:34 ` Sergio Durigan Junior
0 siblings, 1 reply; 35+ messages in thread
From: Simon Marchi @ 2017-08-05 18:14 UTC (permalink / raw)
To: Sergio Durigan Junior; +Cc: GDB Patches, Pedro Alves, Eli Zaretskii
On 2017-08-05 01:03, Sergio Durigan Junior wrote:
> So, I've been thinking about this implementation over the last few
> days,
> but it's still a bit confuse to me. My C++-foo is not so good as
> yours,
> so maybe you can give me a hand.
>
> From what I understood initially, your intention was to make a new
> class
> that inherited from std::vector but overloaded/implemented methods to
> mimic what a std::set would do. However, after reading a bit, it
> doesn't seem like a good idea to inherit from std::vector (or any std
> containers). Which made me realize that maybe you are talking about
> creating a class that encapsulates a std::vector, without inheriting
> from it, and that provided the regular .push_back, .insert, .empty,
> etc., methods, but in an enhanced way in order to e.g. prevent the
> insert of duplicated elements, which is one of the things we miss from
> std::set.
Do you mean "... we miss from std::vector"?
I also read about inheriting STL containers. The danger is that they
don't have virtual destructors. So let's say we have gdb::set_vector
inheriting std::vector and try to delete a set_vector instance through a
vector pointer, the set_vector destructor won't be called. If
set_vector is simply adding some methods to the interface (no data
members, no user defined destructor), I don't know if this is really a
problem.
But now that I think of it, if we want to make sure this object
guarantees the properties of a set (like no duplicate elements), we
would have to hide most of the vector interface and only expose a
set-like interface. Otherwise, it would be possible to call push_back
and insert a duplicate element. Given that, I'd lean towards creating a
class that includes a vector and exposes a set-like interface, rather
than inheriting.
>
> Am I in the right direction here? Also, I started to think... I don't
> envision the user setting thousands of user-set environment variables,
> so *maybe* using std::set would be OK-ish for our use case scenario. I
> don't know. While I understand the concern about premature
> pessimization, I'm also not a fan of complicating the implementation
> just for a little bit of optimization either.
>
> WDYT?
Actually, if we expected the user to set thousands of environment
variables and needed it to be fast to set and unset variables, it would
be good to use std::set because of the O(log(N))
lookups/insertions/removals, which matters more when you have a lot of
elements. But when you just have a few elements, the constant cost is
more significant. A vector-based set would have O(N) complexity for
these operations (at least for insertions and removals, for lookups it
depends if it is sorted), which would be bad if we had thousands of
elements. But since we expect to have just a few, it would likely be
faster than std::set's constant cost.
But I agree with you that the important thing now is to get the
algorithm/functionality right. The typical use case is to have at the
maximum a few inferiors, which may have at the maximum a few environment
variables defined. The defined environment variables are only used when
doing a "run", which doesn't happen often. So in the end, I don't think
it would make a perceptible difference in memory usage or execution time
to use an std::set. If it helps to make the code significantly simpler,
then I think it's worth it. And we can always revisit it later by
making a drop-in replacement for std::set.
Simon
^ permalink raw reply [flat|nested] 35+ messages in thread
* Re: [PATCH v2] Implement the ability to set/unset environment variables to GDBserver when starting the inferior
[not found] ` <256083325d9f9c4cc4f5518fe6e5292d@polymtl.ca>
@ 2017-08-12 4:19 ` Sergio Durigan Junior
0 siblings, 0 replies; 35+ messages in thread
From: Sergio Durigan Junior @ 2017-08-12 4:19 UTC (permalink / raw)
To: Simon Marchi; +Cc: GDB Patches, Pedro Alves, Eli Zaretskii
On Monday, August 07 2017, Simon Marchi wrote:
> On 2017-07-27 05:35, Sergio Durigan Junior wrote:
>> +# Helper function to re-run to main and breaking at the "break-here"
>> +# label.
>> +
>> +proc rerun_to_main { } {
>> + global decimal hex
>> +
>> + if { ![runto_main] } {
>> + return -1
>> + }
>> +
>> + gdb_breakpoint [gdb_get_line_number "break-here"]
>> +
>> + gdb_test "continue" "Breakpoint $decimal, main \\\(argc=1,
>> argv=$hex\\\) at.*" \
>> + "continue until breakpoint"
>> +}
>
> Hi Sergio,
Hey Simon,
I'm back from vacation, sorry about the delay in replying these
messages.
> I just noticed something out of pure coincidence. I noticed some TCL
> backtraces when running the testsuite that I hadn't seen before, such
> as:
>
> ERROR: tcl error sourcing
> /home/emaisin/src/binutils-gdb/gdb/testsuite/gdb.base/sigbpt.exp.
> ERROR: undefined tag "break-here"
> while executing
> "error "undefined tag \"$text\"""
> (procedure "gdb_get_line_number" line 36)
> invoked from within
> "gdb_get_line_number "break-here""
> (procedure "rerun_to_main" line 8)
> invoked from within
> "rerun_to_main"
> (procedure "stepi_out" line 10)
> invoked from within
> "stepi_out "stepi""
> (file
> "/home/emaisin/src/binutils-gdb/gdb/testsuite/gdb.base/sigbpt.exp"
> line 256)
> invoked from within
> "source
> /home/emaisin/src/binutils-gdb/gdb/testsuite/gdb.base/sigbpt.exp"
> ("uplevel" body line 1)
> invoked from within
> "uplevel #0 source
> /home/emaisin/src/binutils-gdb/gdb/testsuite/gdb.base/sigbpt.exp"
> invoked from within
> "catch "uplevel #0 source $test_file_name""
>
> It turns out I had your test file (share-env-with-gdbserver.exp) still
> lying around in my repo. The procedure defined above (rerun_to_main)
> overrides one with the same name defined in lib/gdb.exp, breaking the
> tests executed after, that use this proc.
Ops. I'll redefine the proc and use another name. Thanks for catching
that.
Cheers,
--
Sergio
GPG key ID: 237A 54B1 0287 28BF 00EF 31F4 D0EB 7628 65FC 5E36
Please send encrypted e-mail if possible
http://sergiodj.net/
^ permalink raw reply [flat|nested] 35+ messages in thread
* Re: [PATCH v2] Implement the ability to set/unset environment variables to GDBserver when starting the inferior
2017-08-05 18:14 ` Simon Marchi
@ 2017-08-12 4:34 ` Sergio Durigan Junior
2017-08-12 8:11 ` Simon Marchi
0 siblings, 1 reply; 35+ messages in thread
From: Sergio Durigan Junior @ 2017-08-12 4:34 UTC (permalink / raw)
To: Simon Marchi; +Cc: GDB Patches, Pedro Alves, Eli Zaretskii
On Saturday, August 05 2017, Simon Marchi wrote:
> On 2017-08-05 01:03, Sergio Durigan Junior wrote:
>> So, I've been thinking about this implementation over the last few
>> days,
>> but it's still a bit confuse to me. My C++-foo is not so good as
>> yours,
>> so maybe you can give me a hand.
>>
>> From what I understood initially, your intention was to make a new
>> class
>> that inherited from std::vector but overloaded/implemented methods to
>> mimic what a std::set would do. However, after reading a bit, it
>> doesn't seem like a good idea to inherit from std::vector (or any std
>> containers). Which made me realize that maybe you are talking about
>> creating a class that encapsulates a std::vector, without inheriting
>> from it, and that provided the regular .push_back, .insert, .empty,
>> etc., methods, but in an enhanced way in order to e.g. prevent the
>> insert of duplicated elements, which is one of the things we miss from
>> std::set.
>
> Do you mean "... we miss from std::vector"?
I meant that we miss a few features that std::set has, and we'd like to
have them implemented alongside with what std::vector offers.
> I also read about inheriting STL containers. The danger is that they
> don't have virtual destructors. So let's say we have gdb::set_vector
> inheriting std::vector and try to delete a set_vector instance through
> a vector pointer, the set_vector destructor won't be called. If
> set_vector is simply adding some methods to the interface (no data
> members, no user defined destructor), I don't know if this is really a
> problem.
Right, that's the main issue I've found, too. As I was/am not sure this
is really an issue for this specific case, I decided to consult you
first.
> But now that I think of it, if we want to make sure this object
> guarantees the properties of a set (like no duplicate elements), we
> would have to hide most of the vector interface and only expose a
> set-like interface. Otherwise, it would be possible to call push_back
> and insert a duplicate element. Given that, I'd lean towards creating
> a class that includes a vector and exposes a set-like interface,
> rather than inheriting.
Hm, OK, it's nice to know that I was leaning towards a solution that
crossed your mind as well.
>>
>> Am I in the right direction here? Also, I started to think... I don't
>> envision the user setting thousands of user-set environment variables,
>> so *maybe* using std::set would be OK-ish for our use case scenario. I
>> don't know. While I understand the concern about premature
>> pessimization, I'm also not a fan of complicating the implementation
>> just for a little bit of optimization either.
>>
>> WDYT?
>
> Actually, if we expected the user to set thousands of environment
> variables and needed it to be fast to set and unset variables, it
> would be good to use std::set because of the O(log(N))
> lookups/insertions/removals, which matters more when you have a lot of
> elements. But when you just have a few elements, the constant cost is
> more significant. A vector-based set would have O(N) complexity for
> these operations (at least for insertions and removals, for lookups it
> depends if it is sorted), which would be bad if we had thousands of
> elements. But since we expect to have just a few, it would likely be
> faster than std::set's constant cost.
You mean std::vector's constant cost, right?
> But I agree with you that the important thing now is to get the
> algorithm/functionality right. The typical use case is to have at the
> maximum a few inferiors, which may have at the maximum a few
> environment variables defined. The defined environment variables are
> only used when doing a "run", which doesn't happen often. So in the
> end, I don't think it would make a perceptible difference in memory
> usage or execution time to use an std::set. If it helps to make the
> code significantly simpler, then I think it's worth it. And we can
> always revisit it later by making a drop-in replacement for std::set.
It indeed helps make the code simpler if I use std::set, which is what
I'm using in the last version of the patch. I'm glad that you
understood and agreed with my point, so I'll go ahead and submit this
version soon (I'll fix the testcase problem first, and run more tests to
make sure everything is OK).
Thanks,
--
Sergio
GPG key ID: 237A 54B1 0287 28BF 00EF 31F4 D0EB 7628 65FC 5E36
Please send encrypted e-mail if possible
http://sergiodj.net/
^ permalink raw reply [flat|nested] 35+ messages in thread
* Re: [PATCH v2] Implement the ability to set/unset environment variables to GDBserver when starting the inferior
2017-08-12 4:34 ` Sergio Durigan Junior
@ 2017-08-12 8:11 ` Simon Marchi
0 siblings, 0 replies; 35+ messages in thread
From: Simon Marchi @ 2017-08-12 8:11 UTC (permalink / raw)
To: Sergio Durigan Junior; +Cc: GDB Patches, Pedro Alves, Eli Zaretskii
On 2017-08-12 06:33, Sergio Durigan Junior wrote:
>> Actually, if we expected the user to set thousands of environment
>> variables and needed it to be fast to set and unset variables, it
>> would be good to use std::set because of the O(log(N))
>> lookups/insertions/removals, which matters more when you have a lot of
>> elements. But when you just have a few elements, the constant cost is
>> more significant. A vector-based set would have O(N) complexity for
>> these operations (at least for insertions and removals, for lookups it
>> depends if it is sorted), which would be bad if we had thousands of
>> elements. But since we expect to have just a few, it would likely be
>> faster than std::set's constant cost.
>
> You mean std::vector's constant cost, right?
No, I meant std::set's constant cost, although it wasn't clear. I meant
the constant hidden by the big-O notation. The complexity of removing
from a set may be O(log(N)), but we could also write it as "C1 *
log(N)", where C1 is a constant. For a vector, it would take "C2 * N",
where C2 is a constant. If C1 is much larger than C2, then using a set
only starts being interesting with large Ns. That does a much better
job at explaining than I do:
http://lafstern.org/matt/col1.pdf
Thanks,
Simon
^ permalink raw reply [flat|nested] 35+ messages in thread
* [PATCH v3] Implement the ability to set/unset environment variables to GDBserver when starting the inferior
2017-06-29 19:41 [PATCH] Implement the ability to transmit environment variables to GDBserver when starting the inferior Sergio Durigan Junior
` (2 preceding siblings ...)
2017-07-27 3:36 ` [PATCH v2] Implement the ability to set/unset " Sergio Durigan Junior
@ 2017-08-13 6:19 ` Sergio Durigan Junior
2017-08-21 19:11 ` Sergio Durigan Junior
2017-08-24 19:25 ` Simon Marchi
2017-08-28 23:13 ` [PATCH v4] " Sergio Durigan Junior
2017-08-31 20:49 ` [PATCH v5] " Sergio Durigan Junior
5 siblings, 2 replies; 35+ messages in thread
From: Sergio Durigan Junior @ 2017-08-13 6:19 UTC (permalink / raw)
To: GDB Patches
Cc: Pedro Alves, Simon Marchi, Eli Zaretskii, Sergio Durigan Junior
Changes from v2 (addressing comments by Simon):
- Make m_user_set_env_list and m_user_unset_env_list std::set's,
instead of std::vector's. This simplified the code and had the nice
side effect of "automatically" avoid duplicates in the sets (at the
expense of a bit more processing). This involved the adjustment of
several parts of the code.
- Removed automatic parameter from unset method; now the code has two
unset's which are called depending on the case.
- Implement bin2hex overload, and get rid of str2hex.
- Nits and small mistakes fixed.
- s/rerun_to_main/do_prepare_inferior/g in the testcase, in order to
avoid accidentally overwriting the top rerun_to_main proc (and mess
up with subsequent tests).
This patch implements the ability to set/unset environment variables
on the remote target, mimicking what GDB already offers to the user.
There are two features present here: user-set and user-unset
environment variables.
User-set environment variables are only the variables that are
explicitly set by the user, using the 'set environment' command. This
means that variables that were already present in the environment when
starting GDB/GDBserver are not transmitted/considered by this feature.
User-unset environment variables are variables that are explicitly
unset by the user, using the 'unset environment' command.
The idea behind this patch is to store user-set and user-unset
environment variables in two separate sets, both part of gdb_environ.
Then, when extended_remote_create_inferior is preparing to start the
inferior, it will iterate over the two sets and set/unset variables
accordingly. Three new packets are introduced:
- QEnvironmentHexEncoded, which is used to set environment variables,
and contains an hex-encoded string in the format "VAR=VALUE" (VALUE
can be empty if the user set a variable with a null value, by doing
'set environment VAR=').
- QEnvironmentUnset, which is used to unset environment variables, and
contains an hex-encoded string in the format "VAR".
- QEnvironmentReset, which is always the first packet to be
transmitted, and is used to reset the environment, i.e., discard any
changes made by the user on previous runs.
The QEnvironmentHexEncoded packet is inspired on LLDB's extensions to
the RSP. Details about it can be seen here:
<https://raw.githubusercontent.com/llvm-mirror/lldb/master/docs/lldb-gdb-remote.txt>
I decided not to implement the QEnvironment packet because it is
considered deprecated by LLDB. This packet, on LLDB, serves the same
purpose of QEnvironmentHexEncoded, but sends the information using a
plain text, non-hex-encoded string.
The other two packets are new.
This patch also includes updates to the documentation, testsuite, and
unit tests, without introducing regressions.
gdb/ChangeLog:
yyyy-mm-dd Sergio Durigan Junior <sergiodj@redhat.com>
* NEWS (Changes since GDB 8.0): Add entry mentioning new support
for setting/unsetting environment variables on the remote target.
(New remote packets): Add entries for QEnvironmentHexEncoded,
QEnvironmentUnset and QEnvironmentReset.
* common/environ.c (gdb_environ::operator=): Extend method to
handle m_user_set_env_list and m_user_unset_env_list.
(gdb_environ::clear): Likewise.
(match_var_in_string): Change type of first parameter from 'char
*' to 'const char *'.
(gdb_environ::set): Extend method to handle
m_user_set_env_list and m_user_unset_env_list.
(gdb_environ::unset): Likewise.
(gdb_environ::clear_user_set_env): New method.
(gdb_environ::user_set_envp): Likewise.
(gdb_environ::user_unset_envp): Likewise.
* common/environ.h (gdb_environ): Handle m_user_set_env_list and
m_user_unset_env_list on move constructor/assignment.
(unset): Add new default parameter 'update_unset_list = true'.
(clear_user_set_env): New method.
(user_set_envp): Likewise.
(user_unset_envp): Likewise.
(m_user_set_env_list): New std::set.
(m_user_unset_env_list): Likewise.
* common/rsp-low.c (hex2str): New function.
(bin2hex): New overload for bin2hex function.
* common/rsp-low.c (hex2str): New prototype.
(str2hex): New overload prototype.
* remote.c: Include "environ.h". Add QEnvironmentHexEncoded,
QEnvironmentUnset and QEnvironmentReset.
(remote_protocol_features): Add QEnvironmentHexEncoded,
QEnvironmentUnset and QEnvironmentReset packets.
(send_environment_packet): New function.
(extended_remote_environment_support): Likewise.
(extended_remote_create_inferior): Call
extended_remote_environment_support.
(_initialize_remote): Add QEnvironmentHexEncoded,
QEnvironmentUnset and QEnvironmentReset packet configs.
* unittests/environ-selftests.c (run_tests): Add tests for
m_user_set_env_list and m_user_unset_env_list.
gdb/gdbserver/ChangeLog:
yyyy-mm-dd Sergio Durigan Junior <sergiodj@redhat.com>
* server.c (handle_general_set): Handle QEnvironmentHexEncoded,
QEnvironmentUnset and QEnvironmentReset packets.
(handle_query): Inform remote that QEnvironmentHexEncoded,
QEnvironmentUnset and QEnvironmentReset are supported.
gdb/doc/ChangeLog:
yyyy-mm-dd Sergio Durigan Junior <sergiodj@redhat.com>
* gdb.texinfo (set environment): Add @anchor. Explain that
environment variables set by the user are sent to GDBserver.
(unset environment): Likewise, but for unsetting variables.
(Connecting) <Remote Packet>: Add "environment-hex-encoded",
"QEnvironmentHexEncoded", "environment-unset", "QEnvironmentUnset",
"environment-reset" and "QEnvironmentReset" to the table.
(Remote Protocol) <QEnvironmentHexEncoded, QEnvironmentUnset,
QEnvironmentReset>: New item, explaining the packet.
gdb/testsuite/ChangeLog:
yyyy-mm-dd Sergio Durigan Junior <sergiodj@redhat.com>
* gdb.base/share-env-with-gdbserver.c: New file.
* gdb.base/share-env-with-gdbserver.exp: Likewise.
---
gdb/NEWS | 24 ++
gdb/common/environ.c | 75 ++++--
gdb/common/environ.h | 24 +-
gdb/common/rsp-low.c | 39 ++++
gdb/common/rsp-low.h | 8 +
gdb/doc/gdb.texinfo | 116 ++++++++++
gdb/gdbserver/server.c | 65 +++++-
gdb/remote.c | 76 ++++++
gdb/testsuite/gdb.base/share-env-with-gdbserver.c | 40 ++++
.../gdb.base/share-env-with-gdbserver.exp | 254 +++++++++++++++++++++
gdb/unittests/environ-selftests.c | 64 ++++++
11 files changed, 769 insertions(+), 16 deletions(-)
create mode 100644 gdb/testsuite/gdb.base/share-env-with-gdbserver.c
create mode 100644 gdb/testsuite/gdb.base/share-env-with-gdbserver.exp
diff --git a/gdb/NEWS b/gdb/NEWS
index 9cd1df1cfe..4349cce6e2 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -3,6 +3,18 @@
*** Changes since GDB 8.0
+* On Unix systems, GDB now supports transmitting environment variables
+ that are to be set or unset to GDBserver. These variables will
+ affect the environment to be passed to the remote inferior.
+
+ To inform GDB of environment variables that are to be transmitted to
+ GDBserver, use the "set environment" command. Only user set
+ environment variables are sent to GDBserver.
+
+ To inform GDB of environment variables that are to be unset before
+ the remote inferior is started by the GDBserver, use the "unset
+ environment" command.
+
* On Unix systems, GDBserver now does globbing expansion and variable
substitution in inferior command line arguments.
@@ -14,6 +26,18 @@
* New remote packets
+QEnvironmentHexEncoded
+ Inform GDBserver of an environment variable that is to be passed to
+ the inferior when starting it.
+
+QEnvironmentUnset
+ Inform GDBserver of an environment variable that is to be unset
+ before starting the remote inferior.
+
+QEnvironmentReset
+ Inform GDBserver that the environment should be reset (i.e.,
+ user-set environment variables should be unset).
+
QStartupWithShell
Indicates whether the inferior must be started with a shell or not.
diff --git a/gdb/common/environ.c b/gdb/common/environ.c
index 698bda3ac1..7753f6ebca 100644
--- a/gdb/common/environ.c
+++ b/gdb/common/environ.c
@@ -30,8 +30,12 @@ gdb_environ::operator= (gdb_environ &&e)
return *this;
m_environ_vector = std::move (e.m_environ_vector);
+ m_user_set_env = std::move (e.m_user_set_env);
+ m_user_unset_env = std::move (e.m_user_unset_env);
e.m_environ_vector.clear ();
e.m_environ_vector.push_back (NULL);
+ e.m_user_set_env.clear ();
+ e.m_user_unset_env.clear ();
return *this;
}
@@ -65,6 +69,8 @@ gdb_environ::clear ()
m_environ_vector.clear ();
/* Always add the NULL element. */
m_environ_vector.push_back (NULL);
+ m_user_set_env.clear ();
+ m_user_unset_env.clear ();
}
/* Helper function to check if STRING contains an environment variable
@@ -72,7 +78,7 @@ gdb_environ::clear ()
if it contains, false otherwise. */
static bool
-match_var_in_string (char *string, const char *var, size_t var_len)
+match_var_in_string (const char *string, const char *var, size_t var_len)
{
if (strncmp (string, var, var_len) == 0 && string[var_len] == '=')
return true;
@@ -99,32 +105,59 @@ gdb_environ::get (const char *var) const
void
gdb_environ::set (const char *var, const char *value)
{
+ char *fullvar = concat (var, "=", value, NULL);
+
/* We have to unset the variable in the vector if it exists. */
- unset (var);
+ unset (var, false);
/* Insert the element before the last one, which is always NULL. */
- m_environ_vector.insert (m_environ_vector.end () - 1,
- concat (var, "=", value, NULL));
+ m_environ_vector.insert (m_environ_vector.end () - 1, fullvar);
+
+ /* Mark this environment variable as having been set by the user.
+ This will be useful when we deal with setting environment
+ variables on the remote target. */
+ m_user_set_env.insert (std::string (fullvar));
+
+ /* If this environment variable is marked as unset by the user, then
+ remove it from the list, because now the user wants to set
+ it. */
+ m_user_unset_env.erase (std::string (var));
}
/* See common/environ.h. */
void
-gdb_environ::unset (const char *var)
+gdb_environ::unset (const char *var, bool update_unset_list)
{
size_t len = strlen (var);
+ std::vector<char *>::iterator it_env;
/* We iterate until '.end () - 1' because the last element is
always NULL. */
- for (std::vector<char *>::iterator el = m_environ_vector.begin ();
- el != m_environ_vector.end () - 1;
- ++el)
- if (match_var_in_string (*el, var, len))
- {
- xfree (*el);
- m_environ_vector.erase (el);
- break;
- }
+ for (it_env = m_environ_vector.begin ();
+ it_env != m_environ_vector.end () - 1;
+ ++it_env)
+ if (match_var_in_string (*it_env, var, len))
+ break;
+
+ if (it_env != m_environ_vector.end () - 1)
+ {
+ m_user_set_env.erase (std::string (*it_env));
+ xfree (*it_env);
+
+ m_environ_vector.erase (it_env);
+ }
+
+ if (update_unset_list)
+ m_user_unset_env.insert (std::string (var));
+}
+
+/* See common/environ.h. */
+
+void
+gdb_environ::unset (const char *var)
+{
+ unset (var, true);
}
/* See common/environ.h. */
@@ -134,3 +167,17 @@ gdb_environ::envp () const
{
return const_cast<char **> (&m_environ_vector[0]);
}
+
+/* See common/environ.h. */
+
+const std::set<std::string> &
+gdb_environ::user_set_env () const
+{
+ return m_user_set_env;
+}
+
+const std::set<std::string> &
+gdb_environ::user_unset_env () const
+{
+ return m_user_unset_env;
+}
diff --git a/gdb/common/environ.h b/gdb/common/environ.h
index 0bbb191361..5ac27fe761 100644
--- a/gdb/common/environ.h
+++ b/gdb/common/environ.h
@@ -18,6 +18,7 @@
#define ENVIRON_H 1
#include <vector>
+#include <set>
/* Class that represents the environment variables as seen by the
inferior. */
@@ -41,12 +42,16 @@ public:
/* Move constructor. */
gdb_environ (gdb_environ &&e)
- : m_environ_vector (std::move (e.m_environ_vector))
+ : m_environ_vector (std::move (e.m_environ_vector)),
+ m_user_set_env (std::move (e.m_user_set_env)),
+ m_user_unset_env (std::move (e.m_user_unset_env))
{
/* Make sure that the moved-from vector is left at a valid
state (only one NULL element). */
e.m_environ_vector.clear ();
e.m_environ_vector.push_back (NULL);
+ e.m_user_set_env.clear ();
+ e.m_user_unset_env.clear ();
}
/* Move assignment. */
@@ -73,9 +78,26 @@ public:
/* Return the environment vector represented as a 'char **'. */
char **envp () const;
+ /* Return the user-set environment vector. */
+ const std::set<std::string> &user_set_env () const;
+
+ /* Return the user-set environment vector. */
+ const std::set<std::string> &user_unset_env () const;
+
private:
+ /* Unset VAR in environment. If UPDATE_UNSET_LIST is true, then
+ also update M_USER_UNSET_ENV to reflect the unsetting of the
+ environment variable. */
+ void unset (const char *var, bool update_unset_list);
+
/* A vector containing the environment variables. */
std::vector<char *> m_environ_vector;
+
+ /* The environment variables explicitly set by the user. */
+ std::set<std::string> m_user_set_env;
+
+ /* The environment variables explicitly unset by the user. */
+ std::set<std::string> m_user_unset_env;
};
#endif /* defined (ENVIRON_H) */
diff --git a/gdb/common/rsp-low.c b/gdb/common/rsp-low.c
index eb85ab5701..9f71699144 100644
--- a/gdb/common/rsp-low.c
+++ b/gdb/common/rsp-low.c
@@ -132,6 +132,29 @@ hex2bin (const char *hex, gdb_byte *bin, int count)
/* See rsp-low.h. */
+std::string
+hex2str (const char *hex)
+{
+ std::string ret;
+ size_t len = strlen (hex);
+
+ for (size_t i = 0; i < len; ++i)
+ {
+ if (hex[0] == '\0' || hex[1] == '\0')
+ {
+ /* Hex string is short, or of uneven length. Return what we
+ have so far. */
+ return ret;
+ }
+ ret += fromhex (hex[0]) * 16 + fromhex (hex[1]);
+ hex += 2;
+ }
+
+ return ret;
+}
+
+/* See rsp-low.h. */
+
int
bin2hex (const gdb_byte *bin, char *hex, int count)
{
@@ -146,6 +169,22 @@ bin2hex (const gdb_byte *bin, char *hex, int count)
return i;
}
+/* See rsp-low.h. */
+
+std::string
+bin2hex (const gdb_byte *bin, int count)
+{
+ std::string ret;
+
+ for (int i = 0; i < count; ++i)
+ {
+ ret += tohex ((*bin >> 4) & 0xf);
+ ret += tohex (*bin++ & 0xf);
+ }
+
+ return ret;
+}
+
/* Return whether byte B needs escaping when sent as part of binary data. */
static int
diff --git a/gdb/common/rsp-low.h b/gdb/common/rsp-low.h
index b57f58bc75..91cff4dac5 100644
--- a/gdb/common/rsp-low.h
+++ b/gdb/common/rsp-low.h
@@ -52,6 +52,10 @@ extern char *unpack_varlen_hex (char *buff, ULONGEST *result);
extern int hex2bin (const char *hex, gdb_byte *bin, int count);
+/* Like hex2bin, but return a std::string. */
+
+extern std::string hex2str (const char *hex);
+
/* Convert some bytes to a hexadecimal representation. BIN holds the
bytes to convert. COUNT says how many bytes to convert. The
resulting characters are stored in HEX, followed by a NUL
@@ -59,6 +63,10 @@ extern int hex2bin (const char *hex, gdb_byte *bin, int count);
extern int bin2hex (const gdb_byte *bin, char *hex, int count);
+/* Overloaded version of bin2hex that return a std::string. */
+
+extern std::string bin2hex (const gdb_byte *bin, int count);
+
/* Convert BUFFER, binary data at least LEN_UNITS addressable memory units
long, into escaped binary data in OUT_BUF. Only copy memory units that fit
completely in OUT_BUF. Set *OUT_LEN_UNITS to the number of units from
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index 7528183506..1c871cb34c 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -2363,6 +2363,7 @@ print the names and values of all environment variables to be given to
your program. You can abbreviate @code{environment} as @code{env}.
@kindex set environment
+@anchor{set environment}
@item set environment @var{varname} @r{[}=@var{value}@r{]}
Set environment variable @var{varname} to @var{value}. The value
changes for your program (and the shell @value{GDBN} uses to launch
@@ -2391,12 +2392,21 @@ If necessary, you can avoid that by using the @samp{env} program as a
wrapper instead of using @code{set environment}. @xref{set
exec-wrapper}, for an example doing just that.
+Environment variables that are set by the user are also transmitted to
+@command{gdbserver} to be used when starting the remote inferior.
+@pxref{QEnvironmentHexEncoded}.
+
@kindex unset environment
+@anchor{unset environment}
@item unset environment @var{varname}
Remove variable @var{varname} from the environment to be passed to your
program. This is different from @samp{set env @var{varname} =};
@code{unset environment} removes the variable from the environment,
rather than assigning it an empty value.
+
+Environment variables that are unset by the user are also unset on
+@command{gdbserver} when starting the remote inferior.
+@pxref{QEnvironmentUnset}.
@end table
@emph{Warning:} On Unix systems, @value{GDBN} runs your program using
@@ -20816,6 +20826,18 @@ are:
@tab @code{QStartupWithShell}
@tab @code{set startup-with-shell}
+@item @code{environment-hex-encoded}
+@tab @code{QEnvironmentHexEncoded}
+@tab @code{set environment}
+
+@item @code{environment-unset}
+@tab @code{QEnvironmentUnset}
+@tab @code{unset environment}
+
+@item @code{environment-reset}
+@tab @code{QEnvironmentReset}
+@tab @code{Reset the inferior environment (i.e., unset user-set variables)}
+
@item @code{conditional-breakpoints-packet}
@tab @code{Z0 and Z1}
@tab @code{Support for target-side breakpoint condition evaluation}
@@ -36490,6 +36512,100 @@ actually support starting the inferior using a shell.
Use of this packet is controlled by the @code{set startup-with-shell}
command; @pxref{set startup-with-shell}.
+@item QEnvironmentHexEncoded:@var{hex-value}
+@anchor{QEnvironmentHexEncoded}
+@cindex set environment variable, remote request
+@cindex @samp{QEnvironmentHexEncoded} packet
+On UNIX-like targets, it is possible to set environment variables that
+will be passed to the inferior during the startup process. This
+packet is used to inform @command{gdbserver} of an environment
+variable that has been defined by the user on @value{GDBN} (@pxref{set
+environment}).
+
+The packet is composed by @var{hex-value}, an hex encoded
+representation of the @var{name=value} format representing an
+environment variable. The name of the environment variable is
+represented by @var{name}, and the value to be assigned to the
+environment variable is represented by @var{value}. If the variable
+has no value (i.e., the value is @code{null}), then @var{value} will
+not be present.
+
+This packet is only available in extended mode (@pxref{extended
+mode}).
+
+Reply:
+@table @samp
+@item OK
+The request succeeded.
+@end table
+
+This packet is not probed by default; the remote stub must request it,
+by supplying an appropriate @samp{qSupported} response
+(@pxref{qSupported}). This should only be done on targets that
+actually support passing environment variables to the starting
+inferior.
+
+This packet is related to the @code{set environment} command;
+@pxref{set environment}.
+
+@item QEnvironmentUnset:@var{hex-value}
+@anchor{QEnvironmentUnset}
+@cindex unset environment variable, remote request
+@cindex @samp{QEnvironmentUnset} packet
+On UNIX-like targets, it is possible to unset environment variables
+before starting the inferior in the remote target. This packet is
+used to inform @command{gdbserver} of an environment variable that has
+been unset by the user on @value{GDBN} (@pxref{unset environment}).
+
+The packet is composed by @var{hex-value}, an hex encoded
+representation of the name of the environment variable to be unset.
+
+This packet is only available in extended mode (@pxref{extended
+mode}).
+
+Reply:
+@table @samp
+@item OK
+The request succeeded.
+@end table
+
+This packet is not probed by default; the remote stub must request it,
+by supplying an appropriate @samp{qSupported} response
+(@pxref{qSupported}). This should only be done on targets that
+actually support passing environment variables to the starting
+inferior.
+
+This packet is related to the @code{unset environment} command;
+@pxref{unset environment}.
+
+@item QEnvironmentReset
+@anchor{QEnvironmentReset}
+@cindex reset environment, remote request
+@cindex @samp{QEnvironmentReset} packet
+On UNIX-like targets, this packet is used to reset the state of
+environment variables in the remote target before starting the
+inferior. In this context, reset means unsetting all environment
+variables that were previously set by the user (i.e., were not
+initially present in the environment). It is sent to
+@command{gdbserver} before the @samp{QEnvironmentHexEncoded}
+(@pxref{QEnvironmentHexEncoded}) and the @samp{QEnvironmentUnset}
+(@pxref{QEnvironmentUnset}) packets.
+
+This packet is only available in extended mode (@pxref{extended
+mode}).
+
+Reply:
+@table @samp
+@item OK
+The request succeeded.
+@end table
+
+This packet is not probed by default; the remote stub must request it,
+by supplying an appropriate @samp{qSupported} response
+(@pxref{qSupported}). This should only be done on targets that
+actually support passing environment variables to the starting
+inferior.
+
@item qfThreadInfo
@itemx qsThreadInfo
@cindex list active threads, remote request
diff --git a/gdb/gdbserver/server.c b/gdb/gdbserver/server.c
index 38383510e8..fa1116a5ee 100644
--- a/gdb/gdbserver/server.c
+++ b/gdb/gdbserver/server.c
@@ -631,6 +631,67 @@ handle_general_set (char *own_buf)
return;
}
+ if (startswith (own_buf, "QEnvironmentReset"))
+ {
+ our_environ = gdb_environ::from_host_environ ();
+
+ write_ok (own_buf);
+ return;
+ }
+
+ if (startswith (own_buf, "QEnvironmentHexEncoded:"))
+ {
+ const char *p = own_buf + sizeof ("QEnvironmentHexEncoded:") - 1;
+ /* The final form of the environment variable. FINAL_VAR will
+ hold the 'VAR=VALUE' format. */
+ std::string final_var = hex2str (p);
+ std::string var_name, var_value;
+
+ if (remote_debug)
+ {
+ debug_printf (_("[QEnvironmentHexEncoded received '%s']\n"), p);
+ debug_printf (_("[Environment variable to be set: '%s']\n"),
+ final_var.c_str ());
+ debug_flush ();
+ }
+
+ size_t pos = final_var.find ('=');
+ if (pos == std::string::npos)
+ {
+ warning (_("Unexpected format for environment variable: '%s'"),
+ final_var.c_str ());
+ write_enn (own_buf);
+ return;
+ }
+
+ var_name = final_var.substr (0, pos);
+ var_value = final_var.substr (pos + 1, std::string::npos);
+
+ our_environ.set (var_name.c_str (), var_value.c_str ());
+
+ write_ok (own_buf);
+ return;
+ }
+
+ if (startswith (own_buf, "QEnvironmentUnset:"))
+ {
+ const char *p = own_buf + sizeof ("QEnvironmentUnset:") - 1;
+ std::string varname = hex2str (p);
+
+ if (remote_debug)
+ {
+ debug_printf (_("[QEnvironmentUnset received '%s']\n"), p);
+ debug_printf (_("[Environment variable to be unset: '%s']\n"),
+ varname.c_str ());
+ debug_flush ();
+ }
+
+ our_environ.unset (varname.c_str ());
+
+ write_ok (own_buf);
+ return;
+ }
+
if (strcmp (own_buf, "QStartNoAckMode") == 0)
{
if (remote_debug)
@@ -2228,7 +2289,9 @@ handle_query (char *own_buf, int packet_len, int *new_packet_len_p)
}
sprintf (own_buf,
- "PacketSize=%x;QPassSignals+;QProgramSignals+;QStartupWithShell+",
+ "PacketSize=%x;QPassSignals+;QProgramSignals+;"
+ "QStartupWithShell+;QEnvironmentHexEncoded+;"
+ "QEnvironmentReset+;QEnvironmentUnset+",
PBUFSIZ - 1);
if (target_supports_catch_syscall ())
diff --git a/gdb/remote.c b/gdb/remote.c
index ff59a0f81f..8c275fcf4f 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -73,6 +73,7 @@
#include "record-btrace.h"
#include <algorithm>
#include "common/scoped_restore.h"
+#include "environ.h"
/* Temp hacks for tracepoint encoding migration. */
static char *target_buf;
@@ -1430,6 +1431,9 @@ enum {
PACKET_QCatchSyscalls,
PACKET_QProgramSignals,
PACKET_QStartupWithShell,
+ PACKET_QEnvironmentHexEncoded,
+ PACKET_QEnvironmentReset,
+ PACKET_QEnvironmentUnset,
PACKET_qCRC,
PACKET_qSearch_memory,
PACKET_vAttach,
@@ -4637,6 +4641,12 @@ static const struct protocol_feature remote_protocol_features[] = {
PACKET_QProgramSignals },
{ "QStartupWithShell", PACKET_DISABLE, remote_supported_packet,
PACKET_QStartupWithShell },
+ { "QEnvironmentHexEncoded", PACKET_DISABLE, remote_supported_packet,
+ PACKET_QEnvironmentHexEncoded },
+ { "QEnvironmentReset", PACKET_DISABLE, remote_supported_packet,
+ PACKET_QEnvironmentReset },
+ { "QEnvironmentUnset", PACKET_DISABLE, remote_supported_packet,
+ PACKET_QEnvironmentUnset },
{ "QStartNoAckMode", PACKET_DISABLE, remote_supported_packet,
PACKET_QStartNoAckMode },
{ "multiprocess", PACKET_DISABLE, remote_supported_packet,
@@ -9556,6 +9566,57 @@ extended_remote_run (const std::string &args)
}
}
+/* Helper function to send set/unset environment packets. ACTION is
+ either "set" or "unset". PACKET is either "QEnvironmentHexEncoded"
+ or "QEnvironmentUnsetVariable". VALUE is the variable to be
+ sent. */
+
+static void
+send_environment_packet (struct remote_state *rs,
+ const char *action,
+ const char *packet,
+ const char *value)
+{
+ /* Convert the environment variable to an hex string, which
+ is the best format to be transmitted over the wire. */
+ std::string encoded_value = bin2hex ((const gdb_byte *) value,
+ strlen (value));
+
+ xsnprintf (rs->buf, get_remote_packet_size (),
+ "%s:%s", packet, encoded_value.c_str ());
+
+ putpkt (rs->buf);
+ getpkt (&rs->buf, &rs->buf_size, 0);
+ if (strcmp (rs->buf, "OK") != 0)
+ warning (_("Unable to %s environment variable '%s' on remote."),
+ action, value);
+}
+
+/* Helper function to handle the QEnvironment* packets. */
+
+static void
+extended_remote_environment_support (struct remote_state *rs)
+{
+ if (packet_support (PACKET_QEnvironmentReset) != PACKET_DISABLE)
+ {
+ putpkt ("QEnvironmentReset");
+ getpkt (&rs->buf, &rs->buf_size, 0);
+ if (strcmp (rs->buf, "OK") != 0)
+ warning (_("Unable to reset environment on remote."));
+ }
+
+ gdb_environ *e = ¤t_inferior ()->environment;
+
+ if (packet_support (PACKET_QEnvironmentHexEncoded) != PACKET_DISABLE)
+ for (const std::string &el : e->user_set_env ())
+ send_environment_packet (rs, "set", "QEnvironmentHexEncoded",
+ el.c_str ());
+
+ if (packet_support (PACKET_QEnvironmentUnset) != PACKET_DISABLE)
+ for (const std::string &el : e->user_unset_env ())
+ send_environment_packet (rs, "unset", "QEnvironmentUnset", el.c_str ());
+}
+
/* In the extended protocol we want to be able to do things like
"run" and have them basically work as expected. So we need
a special create_inferior function. We support changing the
@@ -9596,6 +9657,8 @@ Remote replied unexpectedly while setting startup-with-shell: %s"),
rs->buf);
}
+ extended_remote_environment_support (rs);
+
/* Now restart the remote server. */
run_worked = extended_remote_run (args) != -1;
if (!run_worked)
@@ -14067,6 +14130,19 @@ Show the maximum size of the address (in bits) in a memory packet."), NULL,
add_packet_config_cmd (&remote_protocol_packets[PACKET_QStartupWithShell],
"QStartupWithShell", "startup-with-shell", 0);
+ add_packet_config_cmd (&remote_protocol_packets
+ [PACKET_QEnvironmentHexEncoded],
+ "QEnvironmentHexEncoded", "environment-hex-encoded",
+ 0);
+
+ add_packet_config_cmd (&remote_protocol_packets[PACKET_QEnvironmentReset],
+ "QEnvironmentReset", "environment-reset",
+ 0);
+
+ add_packet_config_cmd (&remote_protocol_packets[PACKET_QEnvironmentUnset],
+ "QEnvironmentUnset", "environment-unset",
+ 0);
+
add_packet_config_cmd (&remote_protocol_packets[PACKET_qSymbol],
"qSymbol", "symbol-lookup", 0);
diff --git a/gdb/testsuite/gdb.base/share-env-with-gdbserver.c b/gdb/testsuite/gdb.base/share-env-with-gdbserver.c
new file mode 100644
index 0000000000..740bd0fb52
--- /dev/null
+++ b/gdb/testsuite/gdb.base/share-env-with-gdbserver.c
@@ -0,0 +1,40 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+ Copyright 2017 Free Software Foundation, Inc.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+/* Wrapper around getenv for GDB. */
+
+static const char *
+my_getenv (const char *name)
+{
+ return getenv (name);
+}
+
+int
+main (int argc, char *argv[])
+{
+ const char *myvar = getenv ("GDB_TEST_VAR");
+
+ if (myvar != NULL)
+ printf ("It worked! myvar = '%s'\n", myvar);
+ else
+ printf ("It failed.");
+
+ return 0; /* break-here */
+}
diff --git a/gdb/testsuite/gdb.base/share-env-with-gdbserver.exp b/gdb/testsuite/gdb.base/share-env-with-gdbserver.exp
new file mode 100644
index 0000000000..0aca8f6b5e
--- /dev/null
+++ b/gdb/testsuite/gdb.base/share-env-with-gdbserver.exp
@@ -0,0 +1,254 @@
+# This testcase is part of GDB, the GNU debugger.
+
+# Copyright 2017 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+# This test doesn't make sense on native-gdbserver.
+if { [use_gdb_stub] } {
+ untested "not supported"
+ return
+}
+
+standard_testfile
+
+if { [prepare_for_testing "failed to prepare" $testfile $srcfile debug] } {
+ return -1
+}
+
+set test_var_name "GDB_TEST_VAR"
+
+# Helper function that performs a check on the output of "getenv".
+#
+# - VAR_NAME is the name of the variable to be checked.
+#
+# - VAR_VALUE is the value expected.
+#
+# - TEST_MSG, if not empty, is the test message to be used by the
+# "gdb_test".
+#
+# - EMPTY_VAR_P, if non-zero, means that the variable is not expected
+# to exist. In this case, VAR_VALUE is not considered.
+
+proc check_getenv { var_name var_value { test_msg "" } { empty_var_p 0 } } {
+ global hex decimal
+
+ if { $test_msg == "" } {
+ set test_msg "print result of getenv for $var_name"
+ }
+
+ if { $empty_var_p } {
+ set var_value_match "0x0"
+ } else {
+ set var_value_match "$hex \"$var_value\""
+ }
+
+ gdb_test "print my_getenv (\"$var_name\")" "\\\$$decimal = $var_value_match" \
+ $test_msg
+}
+
+# Helper function to re-run to main and breaking at the "break-here"
+# label.
+
+proc do_prepare_inferior { } {
+ global decimal hex
+
+ if { ![runto_main] } {
+ return -1
+ }
+
+ gdb_breakpoint [gdb_get_line_number "break-here"]
+
+ gdb_test "continue" "Breakpoint $decimal, main \\\(argc=1, argv=$hex\\\) at.*" \
+ "continue until breakpoint"
+}
+
+# Helper function that does the actual testing.
+#
+# - VAR_VALUE is the value of the environment variable.
+#
+# - VAR_NAME is the name of the environment variable. If empty,
+# defaults to $test_var_name.
+#
+# - VAR_NAME_MATCH is the name (regex) that will be used to query the
+# environment about the variable (via getenv). This is useful when
+# we're testing variables with strange names (e.g., with an equal
+# sign in the name) and we know that the variable will actually be
+# set using another name. If empty, defatults, to $var_name.
+#
+# - VAR_VALUE_MATCH is the value (regex) that will be used to match
+# the result of getenv. The rationale is the same as explained for
+# VAR_NAME_MATCH. If empty, defaults, to $var_value.
+
+proc do_test { var_value { var_name "" } { var_name_match "" } { var_value_match "" } } {
+ global hex decimal binfile test_var_name
+
+ clean_restart $binfile
+
+ if { $var_name == "" } {
+ set var_name $test_var_name
+ }
+
+ if { $var_name_match == "" } {
+ set var_name_match $var_name
+ }
+
+ if { $var_value_match == "" } {
+ set var_value_match $var_value
+ }
+
+ if { $var_value != "" } {
+ gdb_test_no_output "set environment $var_name = $var_value" \
+ "set $var_name = $var_value"
+ } else {
+ gdb_test "set environment $var_name =" \
+ "Setting environment variable \"$var_name\" to null value." \
+ "set $var_name to null value"
+ }
+
+ do_prepare_inferior
+
+ check_getenv "$var_name_match" "$var_value_match" \
+ "print result of getenv for $var_name"
+}
+
+with_test_prefix "long var value" {
+ do_test "this is my test variable; testing long vars; {}"
+}
+
+with_test_prefix "empty var" {
+ do_test ""
+}
+
+with_test_prefix "strange named var" {
+ # In this test we're doing the following:
+ #
+ # (gdb) set environment 'asd =' = 123 43; asd b ### [];;;
+ #
+ # However, due to how GDB parses this line, the environment
+ # variable will end up named <'asd> (without the <>), and its
+ # value will be <' = 123 43; asd b ### [];;;> (without the <>).
+ do_test "123 43; asd b ### [];;;" "'asd ='" "'asd" "' = 123 43; asd b ### [];;;"
+}
+
+# Test setting and unsetting environment variables in various
+# fashions.
+
+proc test_set_unset_vars { } {
+ global hex decimal binfile
+
+ clean_restart $binfile
+
+ with_test_prefix "set 3 environment variables" {
+ # Set some environment variables
+ gdb_test_no_output "set environment A = 1" \
+ "set A to 1"
+ gdb_test_no_output "set environment B = 2" \
+ "set B to 2"
+ gdb_test_no_output "set environment C = 3" \
+ "set C to 3"
+
+ do_prepare_inferior
+
+ # Check that the variables are known by the inferior
+ check_getenv "A" "1"
+ check_getenv "B" "2"
+ check_getenv "C" "3"
+ }
+
+ with_test_prefix "unset one variable, reset one" {
+ # Now, unset/reset some values
+ gdb_test_no_output "unset environment A" \
+ "unset A"
+ gdb_test_no_output "set environment B = 4" \
+ "set B to 4"
+
+ do_prepare_inferior
+
+ check_getenv "A" "" "" 1
+ check_getenv "B" "4"
+ check_getenv "C" "3"
+ }
+
+ with_test_prefix "unset two variables, reset one" {
+ # Unset more values
+ gdb_test_no_output "unset environment B" \
+ "unset B"
+ gdb_test_no_output "set environment A = 1" \
+ "set A to 1 again"
+ gdb_test_no_output "unset environment C" \
+ "unset C"
+
+ do_prepare_inferior
+
+ check_getenv "A" "1"
+ check_getenv "B" "" "" 1
+ check_getenv "C" "" "" 1
+ }
+}
+
+with_test_prefix "test set/unset of vars" {
+ test_set_unset_vars
+}
+
+# Test that unsetting works.
+
+proc test_unset { } {
+ global hex decimal binfile gdb_prompt
+
+ clean_restart $binfile
+
+ do_prepare_inferior
+
+ set test_msg "check if unset works"
+ set found_home 0
+ gdb_test_multiple "print my_getenv (\"HOME\")" $test_msg {
+ -re "\\\$$decimal = $hex \".*\"\r\n$gdb_prompt $" {
+ pass $test_msg
+ set found_home 1
+ }
+ -re "\\\$$decimal = 0x0\r\n$gdb_prompt $" {
+ untested $test_msg
+ }
+ }
+
+ if { $found_home == 1 } {
+ with_test_prefix "simple unset" {
+ # We can do the test, because $HOME exists (and therefore can
+ # be unset).
+ gdb_test_no_output "unset environment HOME" "unset HOME"
+
+ do_prepare_inferior
+
+ # $HOME now must be empty
+ check_getenv "HOME" "" "" 1
+ }
+
+ with_test_prefix "set-then-unset" {
+ clean_restart $binfile
+
+ # Test if setting and then unsetting $HOME works.
+ gdb_test_no_output "set environment HOME = test" "set HOME as test"
+ gdb_test_no_output "unset environment HOME" "unset HOME again"
+
+ do_prepare_inferior
+
+ check_getenv "HOME" "" "" 1
+ }
+ }
+}
+
+with_test_prefix "test unset of vars" {
+ test_unset
+}
diff --git a/gdb/unittests/environ-selftests.c b/gdb/unittests/environ-selftests.c
index 28b16f828f..e17715b43c 100644
--- a/gdb/unittests/environ-selftests.c
+++ b/gdb/unittests/environ-selftests.c
@@ -22,6 +22,12 @@
#include "common/environ.h"
#include "common/diagnostics.h"
+static bool
+set_contains (const std::set<std::string> &set, std::string key)
+{
+ return set.find (key) != set.end ();
+}
+
namespace selftests {
namespace gdb_environ_tests {
@@ -38,6 +44,8 @@ run_tests ()
/* When the vector is initialized, there should always be one NULL
element in it. */
SELF_CHECK (env.envp ()[0] == NULL);
+ SELF_CHECK (env.user_set_env ().size () == 0);
+ SELF_CHECK (env.user_unset_env ().size () == 0);
/* Make sure that there is no other element. */
SELF_CHECK (env.get ("PWD") == NULL);
@@ -45,14 +53,24 @@ run_tests ()
/* Check if unset followed by a set in an empty vector works. */
env.set ("PWD", "test");
SELF_CHECK (strcmp (env.get ("PWD"), "test") == 0);
+ SELF_CHECK (set_contains (env.user_set_env (), std::string ("PWD=test")));
+ SELF_CHECK (env.user_unset_env ().size () == 0);
/* The second element must be NULL. */
SELF_CHECK (env.envp ()[1] == NULL);
+ SELF_CHECK (env.user_set_env ().size () == 1);
env.unset ("PWD");
SELF_CHECK (env.envp ()[0] == NULL);
+ SELF_CHECK (env.user_set_env ().size () == 0);
+ SELF_CHECK (env.user_unset_env ().size () == 1);
+ SELF_CHECK (set_contains (env.user_unset_env (), std::string ("PWD")));
/* Initialize the environment vector using the host's environ. */
env = gdb_environ::from_host_environ ();
+ /* The user-set and user-unset lists must be empty. */
+ SELF_CHECK (env.user_set_env ().size () == 0);
+ SELF_CHECK (env.user_unset_env ().size () == 0);
+
/* Our test environment variable should be present at the
vector. */
SELF_CHECK (strcmp (env.get ("GDB_SELFTEST_ENVIRON"), "1") == 0);
@@ -65,6 +83,9 @@ run_tests ()
host's environment, but doesn't exist in our vector. */
env.unset ("GDB_SELFTEST_ENVIRON");
SELF_CHECK (env.get ("GDB_SELFTEST_ENVIRON") == NULL);
+ SELF_CHECK (env.user_unset_env ().size () == 1);
+ SELF_CHECK (set_contains (env.user_unset_env (),
+ std::string ("GDB_SELFTEST_ENVIRON")));
/* Re-set the test variable. */
env.set ("GDB_SELFTEST_ENVIRON", "1");
@@ -75,6 +96,8 @@ run_tests ()
variable. */
env.clear ();
SELF_CHECK (env.envp ()[0] == NULL);
+ SELF_CHECK (env.user_set_env ().size () == 0);
+ SELF_CHECK (env.user_unset_env ().size () == 0);
SELF_CHECK (env.get ("GDB_SELFTEST_ENVIRON") == NULL);
/* Reinitialize our environ vector using the host environ. We
@@ -89,6 +112,23 @@ run_tests ()
++num_found;
SELF_CHECK (num_found == 1);
+ /* Before unsetting our test variable, test that user-unset works
+ fine. */
+ env.unset ("GDB_SELFTEST_ENVIRON");
+ SELF_CHECK (env.get ("GDB_SELFTEST_ENVIRON") == NULL);
+ SELF_CHECK (env.user_set_env ().size () == 0);
+ SELF_CHECK (env.user_unset_env ().size () == 1);
+ SELF_CHECK (set_contains (env.user_unset_env (),
+ std::string ("GDB_SELFTEST_ENVIRON")));
+ env.set ("GDB_SELFTEST_ENVIRON", "1");
+ SELF_CHECK (env.user_set_env ().size () == 1);
+ SELF_CHECK (env.user_unset_env ().size () == 0);
+ env.unset ("GDB_SELFTEST_ENVIRON");
+ SELF_CHECK (env.user_set_env ().size () == 0);
+ SELF_CHECK (env.user_unset_env ().size () == 1);
+ SELF_CHECK (set_contains (env.user_unset_env (),
+ std::string ("GDB_SELFTEST_ENVIRON")));
+
/* Get rid of our test variable. */
unsetenv ("GDB_SELFTEST_ENVIRON");
@@ -97,6 +137,10 @@ run_tests ()
vector, but can still find B. */
env.set ("GDB_SELFTEST_ENVIRON_1", "aaa");
SELF_CHECK (strcmp (env.get ("GDB_SELFTEST_ENVIRON_1"), "aaa") == 0);
+ /* User-set environ var list must contain one element. */
+ SELF_CHECK (env.user_set_env ().size () == 1);
+ SELF_CHECK (set_contains (env.user_set_env (),
+ std::string ("GDB_SELFTEST_ENVIRON_1=aaa")));
env.set ("GDB_SELFTEST_ENVIRON_2", "bbb");
SELF_CHECK (strcmp (env.get ("GDB_SELFTEST_ENVIRON_2"), "bbb") == 0);
@@ -104,6 +148,11 @@ run_tests ()
env.unset ("GDB_SELFTEST_ENVIRON_1");
SELF_CHECK (env.get ("GDB_SELFTEST_ENVIRON_1") == NULL);
SELF_CHECK (strcmp (env.get ("GDB_SELFTEST_ENVIRON_2"), "bbb") == 0);
+ /* The user-set environ var list must contain only one element
+ now. */
+ SELF_CHECK (set_contains (env.user_set_env (),
+ std::string ("GDB_SELFTEST_ENVIRON_2=bbb")));
+ SELF_CHECK (env.user_set_env ().size () == 1);
env.clear ();
@@ -111,11 +160,16 @@ run_tests ()
valid state (i.e., its only element is NULL). */
env.set ("A", "1");
SELF_CHECK (strcmp (env.get ("A"), "1") == 0);
+ SELF_CHECK (set_contains (env.user_set_env (), std::string ("A=1")));
+ SELF_CHECK (env.user_set_env ().size () == 1);
gdb_environ env2;
env2 = std::move (env);
SELF_CHECK (env.envp ()[0] == NULL);
SELF_CHECK (strcmp (env2.get ("A"), "1") == 0);
SELF_CHECK (env2.envp ()[1] == NULL);
+ SELF_CHECK (env.user_set_env ().size () == 0);
+ SELF_CHECK (set_contains (env2.user_set_env (), std::string ("A=1")));
+ SELF_CHECK (env2.user_set_env ().size () == 1);
env.set ("B", "2");
SELF_CHECK (strcmp (env.get ("B"), "2") == 0);
SELF_CHECK (env.envp ()[1] == NULL);
@@ -125,18 +179,26 @@ run_tests ()
env.clear ();
env.set ("A", "1");
SELF_CHECK (strcmp (env.get ("A"), "1") == 0);
+ SELF_CHECK (set_contains (env.user_set_env (), std::string ("A=1")));
gdb_environ env3 = std::move (env);
SELF_CHECK (env.envp ()[0] == NULL);
+ SELF_CHECK (env.user_set_env ().size () == 0);
SELF_CHECK (strcmp (env3.get ("A"), "1") == 0);
SELF_CHECK (env3.envp ()[1] == NULL);
+ SELF_CHECK (set_contains (env3.user_set_env (), std::string ("A=1")));
+ SELF_CHECK (env3.user_set_env ().size () == 1);
env.set ("B", "2");
SELF_CHECK (strcmp (env.get ("B"), "2") == 0);
SELF_CHECK (env.envp ()[1] == NULL);
+ SELF_CHECK (set_contains (env.user_set_env (), std::string ("B=2")));
+ SELF_CHECK (env.user_set_env ().size () == 1);
/* Test self-move. */
env.clear ();
env.set ("A", "1");
SELF_CHECK (strcmp (env.get ("A"), "1") == 0);
+ SELF_CHECK (set_contains (env.user_set_env (), std::string ("A=1")));
+ SELF_CHECK (env.user_set_env ().size () == 1);
/* Some compilers warn about moving to self, but that's precisely what we want
to test here, so turn this warning off. */
@@ -148,6 +210,8 @@ run_tests ()
SELF_CHECK (strcmp (env.get ("A"), "1") == 0);
SELF_CHECK (strcmp (env.envp ()[0], "A=1") == 0);
SELF_CHECK (env.envp ()[1] == NULL);
+ SELF_CHECK (set_contains (env.user_set_env (), std::string ("A=1")));
+ SELF_CHECK (env.user_set_env ().size () == 1);
}
} /* namespace gdb_environ */
} /* namespace selftests */
--
2.13.3
^ permalink raw reply [flat|nested] 35+ messages in thread
* Re: [PATCH v3] Implement the ability to set/unset environment variables to GDBserver when starting the inferior
2017-08-13 6:19 ` [PATCH v3] " Sergio Durigan Junior
@ 2017-08-21 19:11 ` Sergio Durigan Junior
2017-08-24 19:25 ` Simon Marchi
1 sibling, 0 replies; 35+ messages in thread
From: Sergio Durigan Junior @ 2017-08-21 19:11 UTC (permalink / raw)
To: GDB Patches; +Cc: Pedro Alves, Simon Marchi, Eli Zaretskii
Ping.
On Sunday, August 13 2017, I wrote:
> Changes from v2 (addressing comments by Simon):
>
> - Make m_user_set_env_list and m_user_unset_env_list std::set's,
> instead of std::vector's. This simplified the code and had the nice
> side effect of "automatically" avoid duplicates in the sets (at the
> expense of a bit more processing). This involved the adjustment of
> several parts of the code.
>
> - Removed automatic parameter from unset method; now the code has two
> unset's which are called depending on the case.
>
> - Implement bin2hex overload, and get rid of str2hex.
>
> - Nits and small mistakes fixed.
>
> - s/rerun_to_main/do_prepare_inferior/g in the testcase, in order to
> avoid accidentally overwriting the top rerun_to_main proc (and mess
> up with subsequent tests).
>
>
> This patch implements the ability to set/unset environment variables
> on the remote target, mimicking what GDB already offers to the user.
> There are two features present here: user-set and user-unset
> environment variables.
>
> User-set environment variables are only the variables that are
> explicitly set by the user, using the 'set environment' command. This
> means that variables that were already present in the environment when
> starting GDB/GDBserver are not transmitted/considered by this feature.
>
> User-unset environment variables are variables that are explicitly
> unset by the user, using the 'unset environment' command.
>
> The idea behind this patch is to store user-set and user-unset
> environment variables in two separate sets, both part of gdb_environ.
> Then, when extended_remote_create_inferior is preparing to start the
> inferior, it will iterate over the two sets and set/unset variables
> accordingly. Three new packets are introduced:
>
> - QEnvironmentHexEncoded, which is used to set environment variables,
> and contains an hex-encoded string in the format "VAR=VALUE" (VALUE
> can be empty if the user set a variable with a null value, by doing
> 'set environment VAR=').
>
> - QEnvironmentUnset, which is used to unset environment variables, and
> contains an hex-encoded string in the format "VAR".
>
> - QEnvironmentReset, which is always the first packet to be
> transmitted, and is used to reset the environment, i.e., discard any
> changes made by the user on previous runs.
>
> The QEnvironmentHexEncoded packet is inspired on LLDB's extensions to
> the RSP. Details about it can be seen here:
>
> <https://raw.githubusercontent.com/llvm-mirror/lldb/master/docs/lldb-gdb-remote.txt>
>
> I decided not to implement the QEnvironment packet because it is
> considered deprecated by LLDB. This packet, on LLDB, serves the same
> purpose of QEnvironmentHexEncoded, but sends the information using a
> plain text, non-hex-encoded string.
>
> The other two packets are new.
>
> This patch also includes updates to the documentation, testsuite, and
> unit tests, without introducing regressions.
>
> gdb/ChangeLog:
> yyyy-mm-dd Sergio Durigan Junior <sergiodj@redhat.com>
>
> * NEWS (Changes since GDB 8.0): Add entry mentioning new support
> for setting/unsetting environment variables on the remote target.
> (New remote packets): Add entries for QEnvironmentHexEncoded,
> QEnvironmentUnset and QEnvironmentReset.
> * common/environ.c (gdb_environ::operator=): Extend method to
> handle m_user_set_env_list and m_user_unset_env_list.
> (gdb_environ::clear): Likewise.
> (match_var_in_string): Change type of first parameter from 'char
> *' to 'const char *'.
> (gdb_environ::set): Extend method to handle
> m_user_set_env_list and m_user_unset_env_list.
> (gdb_environ::unset): Likewise.
> (gdb_environ::clear_user_set_env): New method.
> (gdb_environ::user_set_envp): Likewise.
> (gdb_environ::user_unset_envp): Likewise.
> * common/environ.h (gdb_environ): Handle m_user_set_env_list and
> m_user_unset_env_list on move constructor/assignment.
> (unset): Add new default parameter 'update_unset_list = true'.
> (clear_user_set_env): New method.
> (user_set_envp): Likewise.
> (user_unset_envp): Likewise.
> (m_user_set_env_list): New std::set.
> (m_user_unset_env_list): Likewise.
> * common/rsp-low.c (hex2str): New function.
> (bin2hex): New overload for bin2hex function.
> * common/rsp-low.c (hex2str): New prototype.
> (str2hex): New overload prototype.
> * remote.c: Include "environ.h". Add QEnvironmentHexEncoded,
> QEnvironmentUnset and QEnvironmentReset.
> (remote_protocol_features): Add QEnvironmentHexEncoded,
> QEnvironmentUnset and QEnvironmentReset packets.
> (send_environment_packet): New function.
> (extended_remote_environment_support): Likewise.
> (extended_remote_create_inferior): Call
> extended_remote_environment_support.
> (_initialize_remote): Add QEnvironmentHexEncoded,
> QEnvironmentUnset and QEnvironmentReset packet configs.
> * unittests/environ-selftests.c (run_tests): Add tests for
> m_user_set_env_list and m_user_unset_env_list.
>
> gdb/gdbserver/ChangeLog:
> yyyy-mm-dd Sergio Durigan Junior <sergiodj@redhat.com>
>
> * server.c (handle_general_set): Handle QEnvironmentHexEncoded,
> QEnvironmentUnset and QEnvironmentReset packets.
> (handle_query): Inform remote that QEnvironmentHexEncoded,
> QEnvironmentUnset and QEnvironmentReset are supported.
>
> gdb/doc/ChangeLog:
> yyyy-mm-dd Sergio Durigan Junior <sergiodj@redhat.com>
>
> * gdb.texinfo (set environment): Add @anchor. Explain that
> environment variables set by the user are sent to GDBserver.
> (unset environment): Likewise, but for unsetting variables.
> (Connecting) <Remote Packet>: Add "environment-hex-encoded",
> "QEnvironmentHexEncoded", "environment-unset", "QEnvironmentUnset",
> "environment-reset" and "QEnvironmentReset" to the table.
> (Remote Protocol) <QEnvironmentHexEncoded, QEnvironmentUnset,
> QEnvironmentReset>: New item, explaining the packet.
>
> gdb/testsuite/ChangeLog:
> yyyy-mm-dd Sergio Durigan Junior <sergiodj@redhat.com>
>
> * gdb.base/share-env-with-gdbserver.c: New file.
> * gdb.base/share-env-with-gdbserver.exp: Likewise.
> ---
> gdb/NEWS | 24 ++
> gdb/common/environ.c | 75 ++++--
> gdb/common/environ.h | 24 +-
> gdb/common/rsp-low.c | 39 ++++
> gdb/common/rsp-low.h | 8 +
> gdb/doc/gdb.texinfo | 116 ++++++++++
> gdb/gdbserver/server.c | 65 +++++-
> gdb/remote.c | 76 ++++++
> gdb/testsuite/gdb.base/share-env-with-gdbserver.c | 40 ++++
> .../gdb.base/share-env-with-gdbserver.exp | 254 +++++++++++++++++++++
> gdb/unittests/environ-selftests.c | 64 ++++++
> 11 files changed, 769 insertions(+), 16 deletions(-)
> create mode 100644 gdb/testsuite/gdb.base/share-env-with-gdbserver.c
> create mode 100644 gdb/testsuite/gdb.base/share-env-with-gdbserver.exp
>
> diff --git a/gdb/NEWS b/gdb/NEWS
> index 9cd1df1cfe..4349cce6e2 100644
> --- a/gdb/NEWS
> +++ b/gdb/NEWS
> @@ -3,6 +3,18 @@
>
> *** Changes since GDB 8.0
>
> +* On Unix systems, GDB now supports transmitting environment variables
> + that are to be set or unset to GDBserver. These variables will
> + affect the environment to be passed to the remote inferior.
> +
> + To inform GDB of environment variables that are to be transmitted to
> + GDBserver, use the "set environment" command. Only user set
> + environment variables are sent to GDBserver.
> +
> + To inform GDB of environment variables that are to be unset before
> + the remote inferior is started by the GDBserver, use the "unset
> + environment" command.
> +
> * On Unix systems, GDBserver now does globbing expansion and variable
> substitution in inferior command line arguments.
>
> @@ -14,6 +26,18 @@
>
> * New remote packets
>
> +QEnvironmentHexEncoded
> + Inform GDBserver of an environment variable that is to be passed to
> + the inferior when starting it.
> +
> +QEnvironmentUnset
> + Inform GDBserver of an environment variable that is to be unset
> + before starting the remote inferior.
> +
> +QEnvironmentReset
> + Inform GDBserver that the environment should be reset (i.e.,
> + user-set environment variables should be unset).
> +
> QStartupWithShell
> Indicates whether the inferior must be started with a shell or not.
>
> diff --git a/gdb/common/environ.c b/gdb/common/environ.c
> index 698bda3ac1..7753f6ebca 100644
> --- a/gdb/common/environ.c
> +++ b/gdb/common/environ.c
> @@ -30,8 +30,12 @@ gdb_environ::operator= (gdb_environ &&e)
> return *this;
>
> m_environ_vector = std::move (e.m_environ_vector);
> + m_user_set_env = std::move (e.m_user_set_env);
> + m_user_unset_env = std::move (e.m_user_unset_env);
> e.m_environ_vector.clear ();
> e.m_environ_vector.push_back (NULL);
> + e.m_user_set_env.clear ();
> + e.m_user_unset_env.clear ();
> return *this;
> }
>
> @@ -65,6 +69,8 @@ gdb_environ::clear ()
> m_environ_vector.clear ();
> /* Always add the NULL element. */
> m_environ_vector.push_back (NULL);
> + m_user_set_env.clear ();
> + m_user_unset_env.clear ();
> }
>
> /* Helper function to check if STRING contains an environment variable
> @@ -72,7 +78,7 @@ gdb_environ::clear ()
> if it contains, false otherwise. */
>
> static bool
> -match_var_in_string (char *string, const char *var, size_t var_len)
> +match_var_in_string (const char *string, const char *var, size_t var_len)
> {
> if (strncmp (string, var, var_len) == 0 && string[var_len] == '=')
> return true;
> @@ -99,32 +105,59 @@ gdb_environ::get (const char *var) const
> void
> gdb_environ::set (const char *var, const char *value)
> {
> + char *fullvar = concat (var, "=", value, NULL);
> +
> /* We have to unset the variable in the vector if it exists. */
> - unset (var);
> + unset (var, false);
>
> /* Insert the element before the last one, which is always NULL. */
> - m_environ_vector.insert (m_environ_vector.end () - 1,
> - concat (var, "=", value, NULL));
> + m_environ_vector.insert (m_environ_vector.end () - 1, fullvar);
> +
> + /* Mark this environment variable as having been set by the user.
> + This will be useful when we deal with setting environment
> + variables on the remote target. */
> + m_user_set_env.insert (std::string (fullvar));
> +
> + /* If this environment variable is marked as unset by the user, then
> + remove it from the list, because now the user wants to set
> + it. */
> + m_user_unset_env.erase (std::string (var));
> }
>
> /* See common/environ.h. */
>
> void
> -gdb_environ::unset (const char *var)
> +gdb_environ::unset (const char *var, bool update_unset_list)
> {
> size_t len = strlen (var);
> + std::vector<char *>::iterator it_env;
>
> /* We iterate until '.end () - 1' because the last element is
> always NULL. */
> - for (std::vector<char *>::iterator el = m_environ_vector.begin ();
> - el != m_environ_vector.end () - 1;
> - ++el)
> - if (match_var_in_string (*el, var, len))
> - {
> - xfree (*el);
> - m_environ_vector.erase (el);
> - break;
> - }
> + for (it_env = m_environ_vector.begin ();
> + it_env != m_environ_vector.end () - 1;
> + ++it_env)
> + if (match_var_in_string (*it_env, var, len))
> + break;
> +
> + if (it_env != m_environ_vector.end () - 1)
> + {
> + m_user_set_env.erase (std::string (*it_env));
> + xfree (*it_env);
> +
> + m_environ_vector.erase (it_env);
> + }
> +
> + if (update_unset_list)
> + m_user_unset_env.insert (std::string (var));
> +}
> +
> +/* See common/environ.h. */
> +
> +void
> +gdb_environ::unset (const char *var)
> +{
> + unset (var, true);
> }
>
> /* See common/environ.h. */
> @@ -134,3 +167,17 @@ gdb_environ::envp () const
> {
> return const_cast<char **> (&m_environ_vector[0]);
> }
> +
> +/* See common/environ.h. */
> +
> +const std::set<std::string> &
> +gdb_environ::user_set_env () const
> +{
> + return m_user_set_env;
> +}
> +
> +const std::set<std::string> &
> +gdb_environ::user_unset_env () const
> +{
> + return m_user_unset_env;
> +}
> diff --git a/gdb/common/environ.h b/gdb/common/environ.h
> index 0bbb191361..5ac27fe761 100644
> --- a/gdb/common/environ.h
> +++ b/gdb/common/environ.h
> @@ -18,6 +18,7 @@
> #define ENVIRON_H 1
>
> #include <vector>
> +#include <set>
>
> /* Class that represents the environment variables as seen by the
> inferior. */
> @@ -41,12 +42,16 @@ public:
>
> /* Move constructor. */
> gdb_environ (gdb_environ &&e)
> - : m_environ_vector (std::move (e.m_environ_vector))
> + : m_environ_vector (std::move (e.m_environ_vector)),
> + m_user_set_env (std::move (e.m_user_set_env)),
> + m_user_unset_env (std::move (e.m_user_unset_env))
> {
> /* Make sure that the moved-from vector is left at a valid
> state (only one NULL element). */
> e.m_environ_vector.clear ();
> e.m_environ_vector.push_back (NULL);
> + e.m_user_set_env.clear ();
> + e.m_user_unset_env.clear ();
> }
>
> /* Move assignment. */
> @@ -73,9 +78,26 @@ public:
> /* Return the environment vector represented as a 'char **'. */
> char **envp () const;
>
> + /* Return the user-set environment vector. */
> + const std::set<std::string> &user_set_env () const;
> +
> + /* Return the user-set environment vector. */
> + const std::set<std::string> &user_unset_env () const;
> +
> private:
> + /* Unset VAR in environment. If UPDATE_UNSET_LIST is true, then
> + also update M_USER_UNSET_ENV to reflect the unsetting of the
> + environment variable. */
> + void unset (const char *var, bool update_unset_list);
> +
> /* A vector containing the environment variables. */
> std::vector<char *> m_environ_vector;
> +
> + /* The environment variables explicitly set by the user. */
> + std::set<std::string> m_user_set_env;
> +
> + /* The environment variables explicitly unset by the user. */
> + std::set<std::string> m_user_unset_env;
> };
>
> #endif /* defined (ENVIRON_H) */
> diff --git a/gdb/common/rsp-low.c b/gdb/common/rsp-low.c
> index eb85ab5701..9f71699144 100644
> --- a/gdb/common/rsp-low.c
> +++ b/gdb/common/rsp-low.c
> @@ -132,6 +132,29 @@ hex2bin (const char *hex, gdb_byte *bin, int count)
>
> /* See rsp-low.h. */
>
> +std::string
> +hex2str (const char *hex)
> +{
> + std::string ret;
> + size_t len = strlen (hex);
> +
> + for (size_t i = 0; i < len; ++i)
> + {
> + if (hex[0] == '\0' || hex[1] == '\0')
> + {
> + /* Hex string is short, or of uneven length. Return what we
> + have so far. */
> + return ret;
> + }
> + ret += fromhex (hex[0]) * 16 + fromhex (hex[1]);
> + hex += 2;
> + }
> +
> + return ret;
> +}
> +
> +/* See rsp-low.h. */
> +
> int
> bin2hex (const gdb_byte *bin, char *hex, int count)
> {
> @@ -146,6 +169,22 @@ bin2hex (const gdb_byte *bin, char *hex, int count)
> return i;
> }
>
> +/* See rsp-low.h. */
> +
> +std::string
> +bin2hex (const gdb_byte *bin, int count)
> +{
> + std::string ret;
> +
> + for (int i = 0; i < count; ++i)
> + {
> + ret += tohex ((*bin >> 4) & 0xf);
> + ret += tohex (*bin++ & 0xf);
> + }
> +
> + return ret;
> +}
> +
> /* Return whether byte B needs escaping when sent as part of binary data. */
>
> static int
> diff --git a/gdb/common/rsp-low.h b/gdb/common/rsp-low.h
> index b57f58bc75..91cff4dac5 100644
> --- a/gdb/common/rsp-low.h
> +++ b/gdb/common/rsp-low.h
> @@ -52,6 +52,10 @@ extern char *unpack_varlen_hex (char *buff, ULONGEST *result);
>
> extern int hex2bin (const char *hex, gdb_byte *bin, int count);
>
> +/* Like hex2bin, but return a std::string. */
> +
> +extern std::string hex2str (const char *hex);
> +
> /* Convert some bytes to a hexadecimal representation. BIN holds the
> bytes to convert. COUNT says how many bytes to convert. The
> resulting characters are stored in HEX, followed by a NUL
> @@ -59,6 +63,10 @@ extern int hex2bin (const char *hex, gdb_byte *bin, int count);
>
> extern int bin2hex (const gdb_byte *bin, char *hex, int count);
>
> +/* Overloaded version of bin2hex that return a std::string. */
> +
> +extern std::string bin2hex (const gdb_byte *bin, int count);
> +
> /* Convert BUFFER, binary data at least LEN_UNITS addressable memory units
> long, into escaped binary data in OUT_BUF. Only copy memory units that fit
> completely in OUT_BUF. Set *OUT_LEN_UNITS to the number of units from
> diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
> index 7528183506..1c871cb34c 100644
> --- a/gdb/doc/gdb.texinfo
> +++ b/gdb/doc/gdb.texinfo
> @@ -2363,6 +2363,7 @@ print the names and values of all environment variables to be given to
> your program. You can abbreviate @code{environment} as @code{env}.
>
> @kindex set environment
> +@anchor{set environment}
> @item set environment @var{varname} @r{[}=@var{value}@r{]}
> Set environment variable @var{varname} to @var{value}. The value
> changes for your program (and the shell @value{GDBN} uses to launch
> @@ -2391,12 +2392,21 @@ If necessary, you can avoid that by using the @samp{env} program as a
> wrapper instead of using @code{set environment}. @xref{set
> exec-wrapper}, for an example doing just that.
>
> +Environment variables that are set by the user are also transmitted to
> +@command{gdbserver} to be used when starting the remote inferior.
> +@pxref{QEnvironmentHexEncoded}.
> +
> @kindex unset environment
> +@anchor{unset environment}
> @item unset environment @var{varname}
> Remove variable @var{varname} from the environment to be passed to your
> program. This is different from @samp{set env @var{varname} =};
> @code{unset environment} removes the variable from the environment,
> rather than assigning it an empty value.
> +
> +Environment variables that are unset by the user are also unset on
> +@command{gdbserver} when starting the remote inferior.
> +@pxref{QEnvironmentUnset}.
> @end table
>
> @emph{Warning:} On Unix systems, @value{GDBN} runs your program using
> @@ -20816,6 +20826,18 @@ are:
> @tab @code{QStartupWithShell}
> @tab @code{set startup-with-shell}
>
> +@item @code{environment-hex-encoded}
> +@tab @code{QEnvironmentHexEncoded}
> +@tab @code{set environment}
> +
> +@item @code{environment-unset}
> +@tab @code{QEnvironmentUnset}
> +@tab @code{unset environment}
> +
> +@item @code{environment-reset}
> +@tab @code{QEnvironmentReset}
> +@tab @code{Reset the inferior environment (i.e., unset user-set variables)}
> +
> @item @code{conditional-breakpoints-packet}
> @tab @code{Z0 and Z1}
> @tab @code{Support for target-side breakpoint condition evaluation}
> @@ -36490,6 +36512,100 @@ actually support starting the inferior using a shell.
> Use of this packet is controlled by the @code{set startup-with-shell}
> command; @pxref{set startup-with-shell}.
>
> +@item QEnvironmentHexEncoded:@var{hex-value}
> +@anchor{QEnvironmentHexEncoded}
> +@cindex set environment variable, remote request
> +@cindex @samp{QEnvironmentHexEncoded} packet
> +On UNIX-like targets, it is possible to set environment variables that
> +will be passed to the inferior during the startup process. This
> +packet is used to inform @command{gdbserver} of an environment
> +variable that has been defined by the user on @value{GDBN} (@pxref{set
> +environment}).
> +
> +The packet is composed by @var{hex-value}, an hex encoded
> +representation of the @var{name=value} format representing an
> +environment variable. The name of the environment variable is
> +represented by @var{name}, and the value to be assigned to the
> +environment variable is represented by @var{value}. If the variable
> +has no value (i.e., the value is @code{null}), then @var{value} will
> +not be present.
> +
> +This packet is only available in extended mode (@pxref{extended
> +mode}).
> +
> +Reply:
> +@table @samp
> +@item OK
> +The request succeeded.
> +@end table
> +
> +This packet is not probed by default; the remote stub must request it,
> +by supplying an appropriate @samp{qSupported} response
> +(@pxref{qSupported}). This should only be done on targets that
> +actually support passing environment variables to the starting
> +inferior.
> +
> +This packet is related to the @code{set environment} command;
> +@pxref{set environment}.
> +
> +@item QEnvironmentUnset:@var{hex-value}
> +@anchor{QEnvironmentUnset}
> +@cindex unset environment variable, remote request
> +@cindex @samp{QEnvironmentUnset} packet
> +On UNIX-like targets, it is possible to unset environment variables
> +before starting the inferior in the remote target. This packet is
> +used to inform @command{gdbserver} of an environment variable that has
> +been unset by the user on @value{GDBN} (@pxref{unset environment}).
> +
> +The packet is composed by @var{hex-value}, an hex encoded
> +representation of the name of the environment variable to be unset.
> +
> +This packet is only available in extended mode (@pxref{extended
> +mode}).
> +
> +Reply:
> +@table @samp
> +@item OK
> +The request succeeded.
> +@end table
> +
> +This packet is not probed by default; the remote stub must request it,
> +by supplying an appropriate @samp{qSupported} response
> +(@pxref{qSupported}). This should only be done on targets that
> +actually support passing environment variables to the starting
> +inferior.
> +
> +This packet is related to the @code{unset environment} command;
> +@pxref{unset environment}.
> +
> +@item QEnvironmentReset
> +@anchor{QEnvironmentReset}
> +@cindex reset environment, remote request
> +@cindex @samp{QEnvironmentReset} packet
> +On UNIX-like targets, this packet is used to reset the state of
> +environment variables in the remote target before starting the
> +inferior. In this context, reset means unsetting all environment
> +variables that were previously set by the user (i.e., were not
> +initially present in the environment). It is sent to
> +@command{gdbserver} before the @samp{QEnvironmentHexEncoded}
> +(@pxref{QEnvironmentHexEncoded}) and the @samp{QEnvironmentUnset}
> +(@pxref{QEnvironmentUnset}) packets.
> +
> +This packet is only available in extended mode (@pxref{extended
> +mode}).
> +
> +Reply:
> +@table @samp
> +@item OK
> +The request succeeded.
> +@end table
> +
> +This packet is not probed by default; the remote stub must request it,
> +by supplying an appropriate @samp{qSupported} response
> +(@pxref{qSupported}). This should only be done on targets that
> +actually support passing environment variables to the starting
> +inferior.
> +
> @item qfThreadInfo
> @itemx qsThreadInfo
> @cindex list active threads, remote request
> diff --git a/gdb/gdbserver/server.c b/gdb/gdbserver/server.c
> index 38383510e8..fa1116a5ee 100644
> --- a/gdb/gdbserver/server.c
> +++ b/gdb/gdbserver/server.c
> @@ -631,6 +631,67 @@ handle_general_set (char *own_buf)
> return;
> }
>
> + if (startswith (own_buf, "QEnvironmentReset"))
> + {
> + our_environ = gdb_environ::from_host_environ ();
> +
> + write_ok (own_buf);
> + return;
> + }
> +
> + if (startswith (own_buf, "QEnvironmentHexEncoded:"))
> + {
> + const char *p = own_buf + sizeof ("QEnvironmentHexEncoded:") - 1;
> + /* The final form of the environment variable. FINAL_VAR will
> + hold the 'VAR=VALUE' format. */
> + std::string final_var = hex2str (p);
> + std::string var_name, var_value;
> +
> + if (remote_debug)
> + {
> + debug_printf (_("[QEnvironmentHexEncoded received '%s']\n"), p);
> + debug_printf (_("[Environment variable to be set: '%s']\n"),
> + final_var.c_str ());
> + debug_flush ();
> + }
> +
> + size_t pos = final_var.find ('=');
> + if (pos == std::string::npos)
> + {
> + warning (_("Unexpected format for environment variable: '%s'"),
> + final_var.c_str ());
> + write_enn (own_buf);
> + return;
> + }
> +
> + var_name = final_var.substr (0, pos);
> + var_value = final_var.substr (pos + 1, std::string::npos);
> +
> + our_environ.set (var_name.c_str (), var_value.c_str ());
> +
> + write_ok (own_buf);
> + return;
> + }
> +
> + if (startswith (own_buf, "QEnvironmentUnset:"))
> + {
> + const char *p = own_buf + sizeof ("QEnvironmentUnset:") - 1;
> + std::string varname = hex2str (p);
> +
> + if (remote_debug)
> + {
> + debug_printf (_("[QEnvironmentUnset received '%s']\n"), p);
> + debug_printf (_("[Environment variable to be unset: '%s']\n"),
> + varname.c_str ());
> + debug_flush ();
> + }
> +
> + our_environ.unset (varname.c_str ());
> +
> + write_ok (own_buf);
> + return;
> + }
> +
> if (strcmp (own_buf, "QStartNoAckMode") == 0)
> {
> if (remote_debug)
> @@ -2228,7 +2289,9 @@ handle_query (char *own_buf, int packet_len, int *new_packet_len_p)
> }
>
> sprintf (own_buf,
> - "PacketSize=%x;QPassSignals+;QProgramSignals+;QStartupWithShell+",
> + "PacketSize=%x;QPassSignals+;QProgramSignals+;"
> + "QStartupWithShell+;QEnvironmentHexEncoded+;"
> + "QEnvironmentReset+;QEnvironmentUnset+",
> PBUFSIZ - 1);
>
> if (target_supports_catch_syscall ())
> diff --git a/gdb/remote.c b/gdb/remote.c
> index ff59a0f81f..8c275fcf4f 100644
> --- a/gdb/remote.c
> +++ b/gdb/remote.c
> @@ -73,6 +73,7 @@
> #include "record-btrace.h"
> #include <algorithm>
> #include "common/scoped_restore.h"
> +#include "environ.h"
>
> /* Temp hacks for tracepoint encoding migration. */
> static char *target_buf;
> @@ -1430,6 +1431,9 @@ enum {
> PACKET_QCatchSyscalls,
> PACKET_QProgramSignals,
> PACKET_QStartupWithShell,
> + PACKET_QEnvironmentHexEncoded,
> + PACKET_QEnvironmentReset,
> + PACKET_QEnvironmentUnset,
> PACKET_qCRC,
> PACKET_qSearch_memory,
> PACKET_vAttach,
> @@ -4637,6 +4641,12 @@ static const struct protocol_feature remote_protocol_features[] = {
> PACKET_QProgramSignals },
> { "QStartupWithShell", PACKET_DISABLE, remote_supported_packet,
> PACKET_QStartupWithShell },
> + { "QEnvironmentHexEncoded", PACKET_DISABLE, remote_supported_packet,
> + PACKET_QEnvironmentHexEncoded },
> + { "QEnvironmentReset", PACKET_DISABLE, remote_supported_packet,
> + PACKET_QEnvironmentReset },
> + { "QEnvironmentUnset", PACKET_DISABLE, remote_supported_packet,
> + PACKET_QEnvironmentUnset },
> { "QStartNoAckMode", PACKET_DISABLE, remote_supported_packet,
> PACKET_QStartNoAckMode },
> { "multiprocess", PACKET_DISABLE, remote_supported_packet,
> @@ -9556,6 +9566,57 @@ extended_remote_run (const std::string &args)
> }
> }
>
> +/* Helper function to send set/unset environment packets. ACTION is
> + either "set" or "unset". PACKET is either "QEnvironmentHexEncoded"
> + or "QEnvironmentUnsetVariable". VALUE is the variable to be
> + sent. */
> +
> +static void
> +send_environment_packet (struct remote_state *rs,
> + const char *action,
> + const char *packet,
> + const char *value)
> +{
> + /* Convert the environment variable to an hex string, which
> + is the best format to be transmitted over the wire. */
> + std::string encoded_value = bin2hex ((const gdb_byte *) value,
> + strlen (value));
> +
> + xsnprintf (rs->buf, get_remote_packet_size (),
> + "%s:%s", packet, encoded_value.c_str ());
> +
> + putpkt (rs->buf);
> + getpkt (&rs->buf, &rs->buf_size, 0);
> + if (strcmp (rs->buf, "OK") != 0)
> + warning (_("Unable to %s environment variable '%s' on remote."),
> + action, value);
> +}
> +
> +/* Helper function to handle the QEnvironment* packets. */
> +
> +static void
> +extended_remote_environment_support (struct remote_state *rs)
> +{
> + if (packet_support (PACKET_QEnvironmentReset) != PACKET_DISABLE)
> + {
> + putpkt ("QEnvironmentReset");
> + getpkt (&rs->buf, &rs->buf_size, 0);
> + if (strcmp (rs->buf, "OK") != 0)
> + warning (_("Unable to reset environment on remote."));
> + }
> +
> + gdb_environ *e = ¤t_inferior ()->environment;
> +
> + if (packet_support (PACKET_QEnvironmentHexEncoded) != PACKET_DISABLE)
> + for (const std::string &el : e->user_set_env ())
> + send_environment_packet (rs, "set", "QEnvironmentHexEncoded",
> + el.c_str ());
> +
> + if (packet_support (PACKET_QEnvironmentUnset) != PACKET_DISABLE)
> + for (const std::string &el : e->user_unset_env ())
> + send_environment_packet (rs, "unset", "QEnvironmentUnset", el.c_str ());
> +}
> +
> /* In the extended protocol we want to be able to do things like
> "run" and have them basically work as expected. So we need
> a special create_inferior function. We support changing the
> @@ -9596,6 +9657,8 @@ Remote replied unexpectedly while setting startup-with-shell: %s"),
> rs->buf);
> }
>
> + extended_remote_environment_support (rs);
> +
> /* Now restart the remote server. */
> run_worked = extended_remote_run (args) != -1;
> if (!run_worked)
> @@ -14067,6 +14130,19 @@ Show the maximum size of the address (in bits) in a memory packet."), NULL,
> add_packet_config_cmd (&remote_protocol_packets[PACKET_QStartupWithShell],
> "QStartupWithShell", "startup-with-shell", 0);
>
> + add_packet_config_cmd (&remote_protocol_packets
> + [PACKET_QEnvironmentHexEncoded],
> + "QEnvironmentHexEncoded", "environment-hex-encoded",
> + 0);
> +
> + add_packet_config_cmd (&remote_protocol_packets[PACKET_QEnvironmentReset],
> + "QEnvironmentReset", "environment-reset",
> + 0);
> +
> + add_packet_config_cmd (&remote_protocol_packets[PACKET_QEnvironmentUnset],
> + "QEnvironmentUnset", "environment-unset",
> + 0);
> +
> add_packet_config_cmd (&remote_protocol_packets[PACKET_qSymbol],
> "qSymbol", "symbol-lookup", 0);
>
> diff --git a/gdb/testsuite/gdb.base/share-env-with-gdbserver.c b/gdb/testsuite/gdb.base/share-env-with-gdbserver.c
> new file mode 100644
> index 0000000000..740bd0fb52
> --- /dev/null
> +++ b/gdb/testsuite/gdb.base/share-env-with-gdbserver.c
> @@ -0,0 +1,40 @@
> +/* This testcase is part of GDB, the GNU debugger.
> +
> + Copyright 2017 Free Software Foundation, Inc.
> +
> + This program is free software; you can redistribute it and/or modify
> + it under the terms of the GNU General Public License as published by
> + the Free Software Foundation; either version 3 of the License, or
> + (at your option) any later version.
> +
> + This program is distributed in the hope that it will be useful,
> + but WITHOUT ANY WARRANTY; without even the implied warranty of
> + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
> + GNU General Public License for more details.
> +
> + You should have received a copy of the GNU General Public License
> + along with this program. If not, see <http://www.gnu.org/licenses/>. */
> +
> +#include <stdio.h>
> +#include <stdlib.h>
> +
> +/* Wrapper around getenv for GDB. */
> +
> +static const char *
> +my_getenv (const char *name)
> +{
> + return getenv (name);
> +}
> +
> +int
> +main (int argc, char *argv[])
> +{
> + const char *myvar = getenv ("GDB_TEST_VAR");
> +
> + if (myvar != NULL)
> + printf ("It worked! myvar = '%s'\n", myvar);
> + else
> + printf ("It failed.");
> +
> + return 0; /* break-here */
> +}
> diff --git a/gdb/testsuite/gdb.base/share-env-with-gdbserver.exp b/gdb/testsuite/gdb.base/share-env-with-gdbserver.exp
> new file mode 100644
> index 0000000000..0aca8f6b5e
> --- /dev/null
> +++ b/gdb/testsuite/gdb.base/share-env-with-gdbserver.exp
> @@ -0,0 +1,254 @@
> +# This testcase is part of GDB, the GNU debugger.
> +
> +# Copyright 2017 Free Software Foundation, Inc.
> +
> +# This program is free software; you can redistribute it and/or modify
> +# it under the terms of the GNU General Public License as published by
> +# the Free Software Foundation; either version 3 of the License, or
> +# (at your option) any later version.
> +#
> +# This program is distributed in the hope that it will be useful,
> +# but WITHOUT ANY WARRANTY; without even the implied warranty of
> +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
> +# GNU General Public License for more details.
> +#
> +# You should have received a copy of the GNU General Public License
> +# along with this program. If not, see <http://www.gnu.org/licenses/>.
> +
> +# This test doesn't make sense on native-gdbserver.
> +if { [use_gdb_stub] } {
> + untested "not supported"
> + return
> +}
> +
> +standard_testfile
> +
> +if { [prepare_for_testing "failed to prepare" $testfile $srcfile debug] } {
> + return -1
> +}
> +
> +set test_var_name "GDB_TEST_VAR"
> +
> +# Helper function that performs a check on the output of "getenv".
> +#
> +# - VAR_NAME is the name of the variable to be checked.
> +#
> +# - VAR_VALUE is the value expected.
> +#
> +# - TEST_MSG, if not empty, is the test message to be used by the
> +# "gdb_test".
> +#
> +# - EMPTY_VAR_P, if non-zero, means that the variable is not expected
> +# to exist. In this case, VAR_VALUE is not considered.
> +
> +proc check_getenv { var_name var_value { test_msg "" } { empty_var_p 0 } } {
> + global hex decimal
> +
> + if { $test_msg == "" } {
> + set test_msg "print result of getenv for $var_name"
> + }
> +
> + if { $empty_var_p } {
> + set var_value_match "0x0"
> + } else {
> + set var_value_match "$hex \"$var_value\""
> + }
> +
> + gdb_test "print my_getenv (\"$var_name\")" "\\\$$decimal = $var_value_match" \
> + $test_msg
> +}
> +
> +# Helper function to re-run to main and breaking at the "break-here"
> +# label.
> +
> +proc do_prepare_inferior { } {
> + global decimal hex
> +
> + if { ![runto_main] } {
> + return -1
> + }
> +
> + gdb_breakpoint [gdb_get_line_number "break-here"]
> +
> + gdb_test "continue" "Breakpoint $decimal, main \\\(argc=1, argv=$hex\\\) at.*" \
> + "continue until breakpoint"
> +}
> +
> +# Helper function that does the actual testing.
> +#
> +# - VAR_VALUE is the value of the environment variable.
> +#
> +# - VAR_NAME is the name of the environment variable. If empty,
> +# defaults to $test_var_name.
> +#
> +# - VAR_NAME_MATCH is the name (regex) that will be used to query the
> +# environment about the variable (via getenv). This is useful when
> +# we're testing variables with strange names (e.g., with an equal
> +# sign in the name) and we know that the variable will actually be
> +# set using another name. If empty, defatults, to $var_name.
> +#
> +# - VAR_VALUE_MATCH is the value (regex) that will be used to match
> +# the result of getenv. The rationale is the same as explained for
> +# VAR_NAME_MATCH. If empty, defaults, to $var_value.
> +
> +proc do_test { var_value { var_name "" } { var_name_match "" } { var_value_match "" } } {
> + global hex decimal binfile test_var_name
> +
> + clean_restart $binfile
> +
> + if { $var_name == "" } {
> + set var_name $test_var_name
> + }
> +
> + if { $var_name_match == "" } {
> + set var_name_match $var_name
> + }
> +
> + if { $var_value_match == "" } {
> + set var_value_match $var_value
> + }
> +
> + if { $var_value != "" } {
> + gdb_test_no_output "set environment $var_name = $var_value" \
> + "set $var_name = $var_value"
> + } else {
> + gdb_test "set environment $var_name =" \
> + "Setting environment variable \"$var_name\" to null value." \
> + "set $var_name to null value"
> + }
> +
> + do_prepare_inferior
> +
> + check_getenv "$var_name_match" "$var_value_match" \
> + "print result of getenv for $var_name"
> +}
> +
> +with_test_prefix "long var value" {
> + do_test "this is my test variable; testing long vars; {}"
> +}
> +
> +with_test_prefix "empty var" {
> + do_test ""
> +}
> +
> +with_test_prefix "strange named var" {
> + # In this test we're doing the following:
> + #
> + # (gdb) set environment 'asd =' = 123 43; asd b ### [];;;
> + #
> + # However, due to how GDB parses this line, the environment
> + # variable will end up named <'asd> (without the <>), and its
> + # value will be <' = 123 43; asd b ### [];;;> (without the <>).
> + do_test "123 43; asd b ### [];;;" "'asd ='" "'asd" "' = 123 43; asd b ### [];;;"
> +}
> +
> +# Test setting and unsetting environment variables in various
> +# fashions.
> +
> +proc test_set_unset_vars { } {
> + global hex decimal binfile
> +
> + clean_restart $binfile
> +
> + with_test_prefix "set 3 environment variables" {
> + # Set some environment variables
> + gdb_test_no_output "set environment A = 1" \
> + "set A to 1"
> + gdb_test_no_output "set environment B = 2" \
> + "set B to 2"
> + gdb_test_no_output "set environment C = 3" \
> + "set C to 3"
> +
> + do_prepare_inferior
> +
> + # Check that the variables are known by the inferior
> + check_getenv "A" "1"
> + check_getenv "B" "2"
> + check_getenv "C" "3"
> + }
> +
> + with_test_prefix "unset one variable, reset one" {
> + # Now, unset/reset some values
> + gdb_test_no_output "unset environment A" \
> + "unset A"
> + gdb_test_no_output "set environment B = 4" \
> + "set B to 4"
> +
> + do_prepare_inferior
> +
> + check_getenv "A" "" "" 1
> + check_getenv "B" "4"
> + check_getenv "C" "3"
> + }
> +
> + with_test_prefix "unset two variables, reset one" {
> + # Unset more values
> + gdb_test_no_output "unset environment B" \
> + "unset B"
> + gdb_test_no_output "set environment A = 1" \
> + "set A to 1 again"
> + gdb_test_no_output "unset environment C" \
> + "unset C"
> +
> + do_prepare_inferior
> +
> + check_getenv "A" "1"
> + check_getenv "B" "" "" 1
> + check_getenv "C" "" "" 1
> + }
> +}
> +
> +with_test_prefix "test set/unset of vars" {
> + test_set_unset_vars
> +}
> +
> +# Test that unsetting works.
> +
> +proc test_unset { } {
> + global hex decimal binfile gdb_prompt
> +
> + clean_restart $binfile
> +
> + do_prepare_inferior
> +
> + set test_msg "check if unset works"
> + set found_home 0
> + gdb_test_multiple "print my_getenv (\"HOME\")" $test_msg {
> + -re "\\\$$decimal = $hex \".*\"\r\n$gdb_prompt $" {
> + pass $test_msg
> + set found_home 1
> + }
> + -re "\\\$$decimal = 0x0\r\n$gdb_prompt $" {
> + untested $test_msg
> + }
> + }
> +
> + if { $found_home == 1 } {
> + with_test_prefix "simple unset" {
> + # We can do the test, because $HOME exists (and therefore can
> + # be unset).
> + gdb_test_no_output "unset environment HOME" "unset HOME"
> +
> + do_prepare_inferior
> +
> + # $HOME now must be empty
> + check_getenv "HOME" "" "" 1
> + }
> +
> + with_test_prefix "set-then-unset" {
> + clean_restart $binfile
> +
> + # Test if setting and then unsetting $HOME works.
> + gdb_test_no_output "set environment HOME = test" "set HOME as test"
> + gdb_test_no_output "unset environment HOME" "unset HOME again"
> +
> + do_prepare_inferior
> +
> + check_getenv "HOME" "" "" 1
> + }
> + }
> +}
> +
> +with_test_prefix "test unset of vars" {
> + test_unset
> +}
> diff --git a/gdb/unittests/environ-selftests.c b/gdb/unittests/environ-selftests.c
> index 28b16f828f..e17715b43c 100644
> --- a/gdb/unittests/environ-selftests.c
> +++ b/gdb/unittests/environ-selftests.c
> @@ -22,6 +22,12 @@
> #include "common/environ.h"
> #include "common/diagnostics.h"
>
> +static bool
> +set_contains (const std::set<std::string> &set, std::string key)
> +{
> + return set.find (key) != set.end ();
> +}
> +
> namespace selftests {
> namespace gdb_environ_tests {
>
> @@ -38,6 +44,8 @@ run_tests ()
> /* When the vector is initialized, there should always be one NULL
> element in it. */
> SELF_CHECK (env.envp ()[0] == NULL);
> + SELF_CHECK (env.user_set_env ().size () == 0);
> + SELF_CHECK (env.user_unset_env ().size () == 0);
>
> /* Make sure that there is no other element. */
> SELF_CHECK (env.get ("PWD") == NULL);
> @@ -45,14 +53,24 @@ run_tests ()
> /* Check if unset followed by a set in an empty vector works. */
> env.set ("PWD", "test");
> SELF_CHECK (strcmp (env.get ("PWD"), "test") == 0);
> + SELF_CHECK (set_contains (env.user_set_env (), std::string ("PWD=test")));
> + SELF_CHECK (env.user_unset_env ().size () == 0);
> /* The second element must be NULL. */
> SELF_CHECK (env.envp ()[1] == NULL);
> + SELF_CHECK (env.user_set_env ().size () == 1);
> env.unset ("PWD");
> SELF_CHECK (env.envp ()[0] == NULL);
> + SELF_CHECK (env.user_set_env ().size () == 0);
> + SELF_CHECK (env.user_unset_env ().size () == 1);
> + SELF_CHECK (set_contains (env.user_unset_env (), std::string ("PWD")));
>
> /* Initialize the environment vector using the host's environ. */
> env = gdb_environ::from_host_environ ();
>
> + /* The user-set and user-unset lists must be empty. */
> + SELF_CHECK (env.user_set_env ().size () == 0);
> + SELF_CHECK (env.user_unset_env ().size () == 0);
> +
> /* Our test environment variable should be present at the
> vector. */
> SELF_CHECK (strcmp (env.get ("GDB_SELFTEST_ENVIRON"), "1") == 0);
> @@ -65,6 +83,9 @@ run_tests ()
> host's environment, but doesn't exist in our vector. */
> env.unset ("GDB_SELFTEST_ENVIRON");
> SELF_CHECK (env.get ("GDB_SELFTEST_ENVIRON") == NULL);
> + SELF_CHECK (env.user_unset_env ().size () == 1);
> + SELF_CHECK (set_contains (env.user_unset_env (),
> + std::string ("GDB_SELFTEST_ENVIRON")));
>
> /* Re-set the test variable. */
> env.set ("GDB_SELFTEST_ENVIRON", "1");
> @@ -75,6 +96,8 @@ run_tests ()
> variable. */
> env.clear ();
> SELF_CHECK (env.envp ()[0] == NULL);
> + SELF_CHECK (env.user_set_env ().size () == 0);
> + SELF_CHECK (env.user_unset_env ().size () == 0);
> SELF_CHECK (env.get ("GDB_SELFTEST_ENVIRON") == NULL);
>
> /* Reinitialize our environ vector using the host environ. We
> @@ -89,6 +112,23 @@ run_tests ()
> ++num_found;
> SELF_CHECK (num_found == 1);
>
> + /* Before unsetting our test variable, test that user-unset works
> + fine. */
> + env.unset ("GDB_SELFTEST_ENVIRON");
> + SELF_CHECK (env.get ("GDB_SELFTEST_ENVIRON") == NULL);
> + SELF_CHECK (env.user_set_env ().size () == 0);
> + SELF_CHECK (env.user_unset_env ().size () == 1);
> + SELF_CHECK (set_contains (env.user_unset_env (),
> + std::string ("GDB_SELFTEST_ENVIRON")));
> + env.set ("GDB_SELFTEST_ENVIRON", "1");
> + SELF_CHECK (env.user_set_env ().size () == 1);
> + SELF_CHECK (env.user_unset_env ().size () == 0);
> + env.unset ("GDB_SELFTEST_ENVIRON");
> + SELF_CHECK (env.user_set_env ().size () == 0);
> + SELF_CHECK (env.user_unset_env ().size () == 1);
> + SELF_CHECK (set_contains (env.user_unset_env (),
> + std::string ("GDB_SELFTEST_ENVIRON")));
> +
> /* Get rid of our test variable. */
> unsetenv ("GDB_SELFTEST_ENVIRON");
>
> @@ -97,6 +137,10 @@ run_tests ()
> vector, but can still find B. */
> env.set ("GDB_SELFTEST_ENVIRON_1", "aaa");
> SELF_CHECK (strcmp (env.get ("GDB_SELFTEST_ENVIRON_1"), "aaa") == 0);
> + /* User-set environ var list must contain one element. */
> + SELF_CHECK (env.user_set_env ().size () == 1);
> + SELF_CHECK (set_contains (env.user_set_env (),
> + std::string ("GDB_SELFTEST_ENVIRON_1=aaa")));
>
> env.set ("GDB_SELFTEST_ENVIRON_2", "bbb");
> SELF_CHECK (strcmp (env.get ("GDB_SELFTEST_ENVIRON_2"), "bbb") == 0);
> @@ -104,6 +148,11 @@ run_tests ()
> env.unset ("GDB_SELFTEST_ENVIRON_1");
> SELF_CHECK (env.get ("GDB_SELFTEST_ENVIRON_1") == NULL);
> SELF_CHECK (strcmp (env.get ("GDB_SELFTEST_ENVIRON_2"), "bbb") == 0);
> + /* The user-set environ var list must contain only one element
> + now. */
> + SELF_CHECK (set_contains (env.user_set_env (),
> + std::string ("GDB_SELFTEST_ENVIRON_2=bbb")));
> + SELF_CHECK (env.user_set_env ().size () == 1);
>
> env.clear ();
>
> @@ -111,11 +160,16 @@ run_tests ()
> valid state (i.e., its only element is NULL). */
> env.set ("A", "1");
> SELF_CHECK (strcmp (env.get ("A"), "1") == 0);
> + SELF_CHECK (set_contains (env.user_set_env (), std::string ("A=1")));
> + SELF_CHECK (env.user_set_env ().size () == 1);
> gdb_environ env2;
> env2 = std::move (env);
> SELF_CHECK (env.envp ()[0] == NULL);
> SELF_CHECK (strcmp (env2.get ("A"), "1") == 0);
> SELF_CHECK (env2.envp ()[1] == NULL);
> + SELF_CHECK (env.user_set_env ().size () == 0);
> + SELF_CHECK (set_contains (env2.user_set_env (), std::string ("A=1")));
> + SELF_CHECK (env2.user_set_env ().size () == 1);
> env.set ("B", "2");
> SELF_CHECK (strcmp (env.get ("B"), "2") == 0);
> SELF_CHECK (env.envp ()[1] == NULL);
> @@ -125,18 +179,26 @@ run_tests ()
> env.clear ();
> env.set ("A", "1");
> SELF_CHECK (strcmp (env.get ("A"), "1") == 0);
> + SELF_CHECK (set_contains (env.user_set_env (), std::string ("A=1")));
> gdb_environ env3 = std::move (env);
> SELF_CHECK (env.envp ()[0] == NULL);
> + SELF_CHECK (env.user_set_env ().size () == 0);
> SELF_CHECK (strcmp (env3.get ("A"), "1") == 0);
> SELF_CHECK (env3.envp ()[1] == NULL);
> + SELF_CHECK (set_contains (env3.user_set_env (), std::string ("A=1")));
> + SELF_CHECK (env3.user_set_env ().size () == 1);
> env.set ("B", "2");
> SELF_CHECK (strcmp (env.get ("B"), "2") == 0);
> SELF_CHECK (env.envp ()[1] == NULL);
> + SELF_CHECK (set_contains (env.user_set_env (), std::string ("B=2")));
> + SELF_CHECK (env.user_set_env ().size () == 1);
>
> /* Test self-move. */
> env.clear ();
> env.set ("A", "1");
> SELF_CHECK (strcmp (env.get ("A"), "1") == 0);
> + SELF_CHECK (set_contains (env.user_set_env (), std::string ("A=1")));
> + SELF_CHECK (env.user_set_env ().size () == 1);
>
> /* Some compilers warn about moving to self, but that's precisely what we want
> to test here, so turn this warning off. */
> @@ -148,6 +210,8 @@ run_tests ()
> SELF_CHECK (strcmp (env.get ("A"), "1") == 0);
> SELF_CHECK (strcmp (env.envp ()[0], "A=1") == 0);
> SELF_CHECK (env.envp ()[1] == NULL);
> + SELF_CHECK (set_contains (env.user_set_env (), std::string ("A=1")));
> + SELF_CHECK (env.user_set_env ().size () == 1);
> }
> } /* namespace gdb_environ */
> } /* namespace selftests */
> --
> 2.13.3
--
Sergio
GPG key ID: 237A 54B1 0287 28BF 00EF 31F4 D0EB 7628 65FC 5E36
Please send encrypted e-mail if possible
http://sergiodj.net/
^ permalink raw reply [flat|nested] 35+ messages in thread
* Re: [PATCH v3] Implement the ability to set/unset environment variables to GDBserver when starting the inferior
2017-08-13 6:19 ` [PATCH v3] " Sergio Durigan Junior
2017-08-21 19:11 ` Sergio Durigan Junior
@ 2017-08-24 19:25 ` Simon Marchi
2017-08-28 21:25 ` Sergio Durigan Junior
1 sibling, 1 reply; 35+ messages in thread
From: Simon Marchi @ 2017-08-24 19:25 UTC (permalink / raw)
To: Sergio Durigan Junior; +Cc: GDB Patches, Pedro Alves, Eli Zaretskii
Hi Sergio,
Just some nits about the code, and I also looked at the tests, which I
hadn't looked at before.
On 2017-08-13 08:19, Sergio Durigan Junior wrote:
> @@ -73,9 +78,26 @@ public:
> /* Return the environment vector represented as a 'char **'. */
> char **envp () const;
>
> + /* Return the user-set environment vector. */
> + const std::set<std::string> &user_set_env () const;
> +
> + /* Return the user-set environment vector. */
user-set -> user-unset ?
> + const std::set<std::string> &user_unset_env () const;
> +
> private:
> + /* Unset VAR in environment. If UPDATE_UNSET_LIST is true, then
> + also update M_USER_UNSET_ENV to reflect the unsetting of the
> + environment variable. */
> + void unset (const char *var, bool update_unset_list);
> +
> /* A vector containing the environment variables. */
> std::vector<char *> m_environ_vector;
> +
> + /* The environment variables explicitly set by the user. */
> + std::set<std::string> m_user_set_env;
> +
> + /* The environment variables explicitly unset by the user. */
> + std::set<std::string> m_user_unset_env;
> };
>
> #endif /* defined (ENVIRON_H) */
> diff --git a/gdb/common/rsp-low.c b/gdb/common/rsp-low.c
> index eb85ab5701..9f71699144 100644
> --- a/gdb/common/rsp-low.c
> +++ b/gdb/common/rsp-low.c
> @@ -132,6 +132,29 @@ hex2bin (const char *hex, gdb_byte *bin, int
> count)
>
> /* See rsp-low.h. */
>
> +std::string
> +hex2str (const char *hex)
> +{
> + std::string ret;
> + size_t len = strlen (hex);
We could call ret.reserve (len / 2) to avoid reallocations when
appending to the string.
> +
> + for (size_t i = 0; i < len; ++i)
> + {
> + if (hex[0] == '\0' || hex[1] == '\0')
> + {
> + /* Hex string is short, or of uneven length. Return what we
> + have so far. */
> + return ret;
> + }
> + ret += fromhex (hex[0]) * 16 + fromhex (hex[1]);
> + hex += 2;
> + }
> +
> + return ret;
> +}
> +
> +/* See rsp-low.h. */
> +
> int
> bin2hex (const gdb_byte *bin, char *hex, int count)
> {
> @@ -146,6 +169,22 @@ bin2hex (const gdb_byte *bin, char *hex, int
> count)
> return i;
> }
>
> +/* See rsp-low.h. */
> +
> +std::string
> +bin2hex (const gdb_byte *bin, int count)
> +{
> + std::string ret;
Here too (but with count * 2).
> +
> + for (int i = 0; i < count; ++i)
> + {
> + ret += tohex ((*bin >> 4) & 0xf);
> + ret += tohex (*bin++ & 0xf);
> + }
> +
> + return ret;
> +}
> +
> /* Return whether byte B needs escaping when sent as part of binary
> data. */
>
> static int
> diff --git a/gdb/common/rsp-low.h b/gdb/common/rsp-low.h
> index b57f58bc75..91cff4dac5 100644
> --- a/gdb/common/rsp-low.h
> +++ b/gdb/common/rsp-low.h
> @@ -52,6 +52,10 @@ extern char *unpack_varlen_hex (char *buff,
> ULONGEST *result);
>
> extern int hex2bin (const char *hex, gdb_byte *bin, int count);
>
> +/* Like hex2bin, but return a std::string. */
> +
> +extern std::string hex2str (const char *hex);
> +
> /* Convert some bytes to a hexadecimal representation. BIN holds the
> bytes to convert. COUNT says how many bytes to convert. The
> resulting characters are stored in HEX, followed by a NUL
> @@ -59,6 +63,10 @@ extern int hex2bin (const char *hex, gdb_byte *bin,
> int count);
>
> extern int bin2hex (const gdb_byte *bin, char *hex, int count);
>
> +/* Overloaded version of bin2hex that return a std::string. */
"returns"
> diff --git a/gdb/gdbserver/server.c b/gdb/gdbserver/server.c
> index 38383510e8..fa1116a5ee 100644
> --- a/gdb/gdbserver/server.c
> +++ b/gdb/gdbserver/server.c
> @@ -631,6 +631,67 @@ handle_general_set (char *own_buf)
> return;
> }
>
> + if (startswith (own_buf, "QEnvironmentReset"))
I would use strcmp here. Otherwise, it would also match if we sent a
packet "QEnvironmentResetFoo" (that could conflict with some packet we
add in the future).
> diff --git a/gdb/testsuite/gdb.base/share-env-with-gdbserver.exp
> b/gdb/testsuite/gdb.base/share-env-with-gdbserver.exp
> new file mode 100644
> index 0000000000..0aca8f6b5e
> --- /dev/null
> +++ b/gdb/testsuite/gdb.base/share-env-with-gdbserver.exp
> @@ -0,0 +1,254 @@
> +# This testcase is part of GDB, the GNU debugger.
> +
> +# Copyright 2017 Free Software Foundation, Inc.
> +
> +# This program is free software; you can redistribute it and/or modify
> +# it under the terms of the GNU General Public License as published by
> +# the Free Software Foundation; either version 3 of the License, or
> +# (at your option) any later version.
> +#
> +# This program is distributed in the hope that it will be useful,
> +# but WITHOUT ANY WARRANTY; without even the implied warranty of
> +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
> +# GNU General Public License for more details.
> +#
> +# You should have received a copy of the GNU General Public License
> +# along with this program. If not, see
> <http://www.gnu.org/licenses/>.
> +
> +# This test doesn't make sense on native-gdbserver.
> +if { [use_gdb_stub] } {
> + untested "not supported"
> + return
> +}
> +
> +standard_testfile
> +
> +if { [prepare_for_testing "failed to prepare" $testfile $srcfile
> debug] } {
> + return -1
> +}
> +
> +set test_var_name "GDB_TEST_VAR"
> +
> +# Helper function that performs a check on the output of "getenv".
> +#
> +# - VAR_NAME is the name of the variable to be checked.
> +#
> +# - VAR_VALUE is the value expected.
> +#
> +# - TEST_MSG, if not empty, is the test message to be used by the
> +# "gdb_test".
> +#
> +# - EMPTY_VAR_P, if non-zero, means that the variable is not expected
> +# to exist. In this case, VAR_VALUE is not considered.
> +
> +proc check_getenv { var_name var_value { test_msg "" } { empty_var_p 0
> } } {
> + global hex decimal
> +
> + if { $test_msg == "" } {
> + set test_msg "print result of getenv for $var_name"
> + }
> +
> + if { $empty_var_p } {
> + set var_value_match "0x0"
> + } else {
> + set var_value_match "$hex \"$var_value\""
> + }
> +
> + gdb_test "print my_getenv (\"$var_name\")" "\\\$$decimal =
> $var_value_match" \
> + $test_msg
> +}
> +
> +# Helper function to re-run to main and breaking at the "break-here"
> +# label.
> +
> +proc do_prepare_inferior { } {
> + global decimal hex
> +
> + if { ![runto_main] } {
> + return -1
> + }
> +
> + gdb_breakpoint [gdb_get_line_number "break-here"]
> +
> + gdb_test "continue" "Breakpoint $decimal, main \\\(argc=1,
> argv=$hex\\\) at.*" \
> + "continue until breakpoint"
> +}
> +
> +# Helper function that does the actual testing.
> +#
> +# - VAR_VALUE is the value of the environment variable.
> +#
> +# - VAR_NAME is the name of the environment variable. If empty,
> +# defaults to $test_var_name.
> +#
> +# - VAR_NAME_MATCH is the name (regex) that will be used to query the
> +# environment about the variable (via getenv). This is useful when
> +# we're testing variables with strange names (e.g., with an equal
> +# sign in the name) and we know that the variable will actually be
> +# set using another name. If empty, defatults, to $var_name.
> +#
> +# - VAR_VALUE_MATCH is the value (regex) that will be used to match
> +# the result of getenv. The rationale is the same as explained for
> +# VAR_NAME_MATCH. If empty, defaults, to $var_value.
> +
> +proc do_test { var_value { var_name "" } { var_name_match "" } {
> var_value_match "" } } {
> + global hex decimal binfile test_var_name
hex and decimal are unused
> +
> + clean_restart $binfile
> +
> + if { $var_name == "" } {
> + set var_name $test_var_name
> + }
> +
> + if { $var_name_match == "" } {
> + set var_name_match $var_name
> + }
> +
> + if { $var_value_match == "" } {
> + set var_value_match $var_value
> + }
> +
> + if { $var_value != "" } {
> + gdb_test_no_output "set environment $var_name = $var_value" \
> + "set $var_name = $var_value"
> + } else {
> + gdb_test "set environment $var_name =" \
> + "Setting environment variable \"$var_name\" to null value." \
> + "set $var_name to null value"
> + }
> +
> + do_prepare_inferior
> +
> + check_getenv "$var_name_match" "$var_value_match" \
> + "print result of getenv for $var_name"
> +}
> +
> +with_test_prefix "long var value" {
> + do_test "this is my test variable; testing long vars; {}"
> +}
> +
> +with_test_prefix "empty var" {
> + do_test ""
> +}
> +
> +with_test_prefix "strange named var" {
> + # In this test we're doing the following:
> + #
git am complains about a trailing whitespace here.
> + # (gdb) set environment 'asd =' = 123 43; asd b ### [];;;
> + #
> + # However, due to how GDB parses this line, the environment
> + # variable will end up named <'asd> (without the <>), and its
> + # value will be <' = 123 43; asd b ### [];;;> (without the <>).
> + do_test "123 43; asd b ### [];;;" "'asd ='" "'asd" "' = 123 43;
> asd b ### [];;;"
Watch out, these square brackets are evaluated by tcl and replaced with
an empty string.
> +}
> +
> +# Test setting and unsetting environment variables in various
> +# fashions.
> +
> +proc test_set_unset_vars { } {
> + global hex decimal binfile
hex and decimal are unused.
> @@ -89,6 +112,23 @@ run_tests ()
> ++num_found;
> SELF_CHECK (num_found == 1);
>
> + /* Before unsetting our test variable, test that user-unset works
> + fine. */
> + env.unset ("GDB_SELFTEST_ENVIRON");
> + SELF_CHECK (env.get ("GDB_SELFTEST_ENVIRON") == NULL);
> + SELF_CHECK (env.user_set_env ().size () == 0);
> + SELF_CHECK (env.user_unset_env ().size () == 1);
> + SELF_CHECK (set_contains (env.user_unset_env (),
> + std::string ("GDB_SELFTEST_ENVIRON")));
It seems to me that the same thing has been tested a few lines higher.
Can we keep just one? But in general, I find it quite confusing to find
out what is tested and see if anything it missing. I think it would be
better to have several functions (run_tests can call sub-functions),
where each function tests one particular behavior. Not only would it
help readability, but it would also make it easier to add tests without
fear of breaking the existing tests.
Simon
^ permalink raw reply [flat|nested] 35+ messages in thread
* Re: [PATCH v3] Implement the ability to set/unset environment variables to GDBserver when starting the inferior
2017-08-24 19:25 ` Simon Marchi
@ 2017-08-28 21:25 ` Sergio Durigan Junior
0 siblings, 0 replies; 35+ messages in thread
From: Sergio Durigan Junior @ 2017-08-28 21:25 UTC (permalink / raw)
To: Simon Marchi; +Cc: GDB Patches, Pedro Alves, Eli Zaretskii
On Thursday, August 24 2017, Simon Marchi wrote:
> Hi Sergio,
>
> Just some nits about the code, and I also looked at the tests, which I
> hadn't looked at before.
Thanks for the review, Simon!
> On 2017-08-13 08:19, Sergio Durigan Junior wrote:
>> @@ -73,9 +78,26 @@ public:
>> /* Return the environment vector represented as a 'char **'. */
>> char **envp () const;
>>
>> + /* Return the user-set environment vector. */
>> + const std::set<std::string> &user_set_env () const;
>> +
>> + /* Return the user-set environment vector. */
>
> user-set -> user-unset ?
Fixed.
>> + const std::set<std::string> &user_unset_env () const;
>> +
>> private:
>> + /* Unset VAR in environment. If UPDATE_UNSET_LIST is true, then
>> + also update M_USER_UNSET_ENV to reflect the unsetting of the
>> + environment variable. */
>> + void unset (const char *var, bool update_unset_list);
>> +
>> /* A vector containing the environment variables. */
>> std::vector<char *> m_environ_vector;
>> +
>> + /* The environment variables explicitly set by the user. */
>> + std::set<std::string> m_user_set_env;
>> +
>> + /* The environment variables explicitly unset by the user. */
>> + std::set<std::string> m_user_unset_env;
>> };
>>
>> #endif /* defined (ENVIRON_H) */
>> diff --git a/gdb/common/rsp-low.c b/gdb/common/rsp-low.c
>> index eb85ab5701..9f71699144 100644
>> --- a/gdb/common/rsp-low.c
>> +++ b/gdb/common/rsp-low.c
>> @@ -132,6 +132,29 @@ hex2bin (const char *hex, gdb_byte *bin, int
>> count)
>>
>> /* See rsp-low.h. */
>>
>> +std::string
>> +hex2str (const char *hex)
>> +{
>> + std::string ret;
>> + size_t len = strlen (hex);
>
> We could call ret.reserve (len / 2) to avoid reallocations when
> appending to the string.
Done.
>> +
>> + for (size_t i = 0; i < len; ++i)
>> + {
>> + if (hex[0] == '\0' || hex[1] == '\0')
>> + {
>> + /* Hex string is short, or of uneven length. Return what we
>> + have so far. */
>> + return ret;
>> + }
>> + ret += fromhex (hex[0]) * 16 + fromhex (hex[1]);
>> + hex += 2;
>> + }
>> +
>> + return ret;
>> +}
>> +
>> +/* See rsp-low.h. */
>> +
>> int
>> bin2hex (const gdb_byte *bin, char *hex, int count)
>> {
>> @@ -146,6 +169,22 @@ bin2hex (const gdb_byte *bin, char *hex, int
>> count)
>> return i;
>> }
>>
>> +/* See rsp-low.h. */
>> +
>> +std::string
>> +bin2hex (const gdb_byte *bin, int count)
>> +{
>> + std::string ret;
>
> Here too (but with count * 2).
Done.
>> +
>> + for (int i = 0; i < count; ++i)
>> + {
>> + ret += tohex ((*bin >> 4) & 0xf);
>> + ret += tohex (*bin++ & 0xf);
>> + }
>> +
>> + return ret;
>> +}
>> +
>> /* Return whether byte B needs escaping when sent as part of binary
>> data. */
>>
>> static int
>> diff --git a/gdb/common/rsp-low.h b/gdb/common/rsp-low.h
>> index b57f58bc75..91cff4dac5 100644
>> --- a/gdb/common/rsp-low.h
>> +++ b/gdb/common/rsp-low.h
>> @@ -52,6 +52,10 @@ extern char *unpack_varlen_hex (char *buff,
>> ULONGEST *result);
>>
>> extern int hex2bin (const char *hex, gdb_byte *bin, int count);
>>
>> +/* Like hex2bin, but return a std::string. */
>> +
>> +extern std::string hex2str (const char *hex);
>> +
>> /* Convert some bytes to a hexadecimal representation. BIN holds the
>> bytes to convert. COUNT says how many bytes to convert. The
>> resulting characters are stored in HEX, followed by a NUL
>> @@ -59,6 +63,10 @@ extern int hex2bin (const char *hex, gdb_byte *bin,
>> int count);
>>
>> extern int bin2hex (const gdb_byte *bin, char *hex, int count);
>>
>> +/* Overloaded version of bin2hex that return a std::string. */
>
> "returns"
Fixed.
>> diff --git a/gdb/gdbserver/server.c b/gdb/gdbserver/server.c
>> index 38383510e8..fa1116a5ee 100644
>> --- a/gdb/gdbserver/server.c
>> +++ b/gdb/gdbserver/server.c
>> @@ -631,6 +631,67 @@ handle_general_set (char *own_buf)
>> return;
>> }
>>
>> + if (startswith (own_buf, "QEnvironmentReset"))
>
> I would use strcmp here. Otherwise, it would also match if we sent a
> packet "QEnvironmentResetFoo" (that could conflict with some packet we
> add in the future).
Hm, good point. Fixed.
>> diff --git a/gdb/testsuite/gdb.base/share-env-with-gdbserver.exp
>> b/gdb/testsuite/gdb.base/share-env-with-gdbserver.exp
>> new file mode 100644
>> index 0000000000..0aca8f6b5e
>> --- /dev/null
>> +++ b/gdb/testsuite/gdb.base/share-env-with-gdbserver.exp
>> @@ -0,0 +1,254 @@
>> +# This testcase is part of GDB, the GNU debugger.
>> +
>> +# Copyright 2017 Free Software Foundation, Inc.
>> +
>> +# This program is free software; you can redistribute it and/or modify
>> +# it under the terms of the GNU General Public License as published by
>> +# the Free Software Foundation; either version 3 of the License, or
>> +# (at your option) any later version.
>> +#
>> +# This program is distributed in the hope that it will be useful,
>> +# but WITHOUT ANY WARRANTY; without even the implied warranty of
>> +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
>> +# GNU General Public License for more details.
>> +#
>> +# You should have received a copy of the GNU General Public License
>> +# along with this program. If not, see
>> <http://www.gnu.org/licenses/>.
>> +
>> +# This test doesn't make sense on native-gdbserver.
>> +if { [use_gdb_stub] } {
>> + untested "not supported"
>> + return
>> +}
>> +
>> +standard_testfile
>> +
>> +if { [prepare_for_testing "failed to prepare" $testfile $srcfile
>> debug] } {
>> + return -1
>> +}
>> +
>> +set test_var_name "GDB_TEST_VAR"
>> +
>> +# Helper function that performs a check on the output of "getenv".
>> +#
>> +# - VAR_NAME is the name of the variable to be checked.
>> +#
>> +# - VAR_VALUE is the value expected.
>> +#
>> +# - TEST_MSG, if not empty, is the test message to be used by the
>> +# "gdb_test".
>> +#
>> +# - EMPTY_VAR_P, if non-zero, means that the variable is not expected
>> +# to exist. In this case, VAR_VALUE is not considered.
>> +
>> +proc check_getenv { var_name var_value { test_msg "" } {
>> empty_var_p 0 } } {
>> + global hex decimal
>> +
>> + if { $test_msg == "" } {
>> + set test_msg "print result of getenv for $var_name"
>> + }
>> +
>> + if { $empty_var_p } {
>> + set var_value_match "0x0"
>> + } else {
>> + set var_value_match "$hex \"$var_value\""
>> + }
>> +
>> + gdb_test "print my_getenv (\"$var_name\")" "\\\$$decimal =
>> $var_value_match" \
>> + $test_msg
>> +}
>> +
>> +# Helper function to re-run to main and breaking at the "break-here"
>> +# label.
>> +
>> +proc do_prepare_inferior { } {
>> + global decimal hex
>> +
>> + if { ![runto_main] } {
>> + return -1
>> + }
>> +
>> + gdb_breakpoint [gdb_get_line_number "break-here"]
>> +
>> + gdb_test "continue" "Breakpoint $decimal, main \\\(argc=1,
>> argv=$hex\\\) at.*" \
>> + "continue until breakpoint"
>> +}
>> +
>> +# Helper function that does the actual testing.
>> +#
>> +# - VAR_VALUE is the value of the environment variable.
>> +#
>> +# - VAR_NAME is the name of the environment variable. If empty,
>> +# defaults to $test_var_name.
>> +#
>> +# - VAR_NAME_MATCH is the name (regex) that will be used to query the
>> +# environment about the variable (via getenv). This is useful when
>> +# we're testing variables with strange names (e.g., with an equal
>> +# sign in the name) and we know that the variable will actually be
>> +# set using another name. If empty, defatults, to $var_name.
>> +#
>> +# - VAR_VALUE_MATCH is the value (regex) that will be used to match
>> +# the result of getenv. The rationale is the same as explained for
>> +# VAR_NAME_MATCH. If empty, defaults, to $var_value.
>> +
>> +proc do_test { var_value { var_name "" } { var_name_match "" } {
>> var_value_match "" } } {
>> + global hex decimal binfile test_var_name
>
> hex and decimal are unused
Removed.
>> +
>> + clean_restart $binfile
>> +
>> + if { $var_name == "" } {
>> + set var_name $test_var_name
>> + }
>> +
>> + if { $var_name_match == "" } {
>> + set var_name_match $var_name
>> + }
>> +
>> + if { $var_value_match == "" } {
>> + set var_value_match $var_value
>> + }
>> +
>> + if { $var_value != "" } {
>> + gdb_test_no_output "set environment $var_name = $var_value" \
>> + "set $var_name = $var_value"
>> + } else {
>> + gdb_test "set environment $var_name =" \
>> + "Setting environment variable \"$var_name\" to null value." \
>> + "set $var_name to null value"
>> + }
>> +
>> + do_prepare_inferior
>> +
>> + check_getenv "$var_name_match" "$var_value_match" \
>> + "print result of getenv for $var_name"
>> +}
>> +
>> +with_test_prefix "long var value" {
>> + do_test "this is my test variable; testing long vars; {}"
>> +}
>> +
>> +with_test_prefix "empty var" {
>> + do_test ""
>> +}
>> +
>> +with_test_prefix "strange named var" {
>> + # In this test we're doing the following:
>> + #
>
> git am complains about a trailing whitespace here.
Removed.
>> + # (gdb) set environment 'asd =' = 123 43; asd b ### [];;;
>> + #
>> + # However, due to how GDB parses this line, the environment
>> + # variable will end up named <'asd> (without the <>), and its
>> + # value will be <' = 123 43; asd b ### [];;;> (without the <>).
>> + do_test "123 43; asd b ### [];;;" "'asd ='" "'asd" "' = 123 43;
>> asd b ### [];;;"
>
> Watch out, these square brackets are evaluated by tcl and replaced
> with an empty string.
Hm, OK. I'll make sure to properly quote them.
>> +}
>> +
>> +# Test setting and unsetting environment variables in various
>> +# fashions.
>> +
>> +proc test_set_unset_vars { } {
>> + global hex decimal binfile
>
> hex and decimal are unused.
Removed.
>> @@ -89,6 +112,23 @@ run_tests ()
>> ++num_found;
>> SELF_CHECK (num_found == 1);
>>
>> + /* Before unsetting our test variable, test that user-unset works
>> + fine. */
>> + env.unset ("GDB_SELFTEST_ENVIRON");
>> + SELF_CHECK (env.get ("GDB_SELFTEST_ENVIRON") == NULL);
>> + SELF_CHECK (env.user_set_env ().size () == 0);
>> + SELF_CHECK (env.user_unset_env ().size () == 1);
>> + SELF_CHECK (set_contains (env.user_unset_env (),
>> + std::string ("GDB_SELFTEST_ENVIRON")));
>
> It seems to me that the same thing has been tested a few lines higher.
> Can we keep just one? But in general, I find it quite confusing to
> find out what is tested and see if anything it missing. I think it
> would be better to have several functions (run_tests can call
> sub-functions), where each function tests one particular behavior.
> Not only would it help readability, but it would also make it easier
> to add tests without fear of breaking the existing tests.
Good point. I'll make some adjustments to the way things are tested
here.
I'll submit v4 soon.
Thanks,
--
Sergio
GPG key ID: 237A 54B1 0287 28BF 00EF 31F4 D0EB 7628 65FC 5E36
Please send encrypted e-mail if possible
http://sergiodj.net/
^ permalink raw reply [flat|nested] 35+ messages in thread
* [PATCH v4] Implement the ability to set/unset environment variables to GDBserver when starting the inferior
2017-06-29 19:41 [PATCH] Implement the ability to transmit environment variables to GDBserver when starting the inferior Sergio Durigan Junior
` (3 preceding siblings ...)
2017-08-13 6:19 ` [PATCH v3] " Sergio Durigan Junior
@ 2017-08-28 23:13 ` Sergio Durigan Junior
2017-08-31 19:37 ` Simon Marchi
2017-08-31 20:49 ` [PATCH v5] " Sergio Durigan Junior
5 siblings, 1 reply; 35+ messages in thread
From: Sergio Durigan Junior @ 2017-08-28 23:13 UTC (permalink / raw)
To: GDB Patches; +Cc: Pedro Alves, Simon Marchi, Sergio Durigan Junior
Changes from v3 (addressing Simon's comments):
- Fix typos/thinkos.
- Use 'reserve' method on std::string's to be returned at 'hex2str'
and 'bin2hex'.
- Use 'strcmp' instead of 'startswith' to detect 'QEnvironmentReset'.
- Fix regexp on testcase (prevent TCL from evaluating '[]').
- Improve readability of the unittests.
This patch implements the ability to set/unset environment variables
on the remote target, mimicking what GDB already offers to the user.
There are two features present here: user-set and user-unset
environment variables.
User-set environment variables are only the variables that are
explicitly set by the user, using the 'set environment' command. This
means that variables that were already present in the environment when
starting GDB/GDBserver are not transmitted/considered by this feature.
User-unset environment variables are variables that are explicitly
unset by the user, using the 'unset environment' command.
The idea behind this patch is to store user-set and user-unset
environment variables in two separate sets, both part of gdb_environ.
Then, when extended_remote_create_inferior is preparing to start the
inferior, it will iterate over the two sets and set/unset variables
accordingly. Three new packets are introduced:
- QEnvironmentHexEncoded, which is used to set environment variables,
and contains an hex-encoded string in the format "VAR=VALUE" (VALUE
can be empty if the user set a variable with a null value, by doing
'set environment VAR=').
- QEnvironmentUnset, which is used to unset environment variables, and
contains an hex-encoded string in the format "VAR".
- QEnvironmentReset, which is always the first packet to be
transmitted, and is used to reset the environment, i.e., discard any
changes made by the user on previous runs.
The QEnvironmentHexEncoded packet is inspired on LLDB's extensions to
the RSP. Details about it can be seen here:
<https://raw.githubusercontent.com/llvm-mirror/lldb/master/docs/lldb-gdb-remote.txt>
I decided not to implement the QEnvironment packet because it is
considered deprecated by LLDB. This packet, on LLDB, serves the same
purpose of QEnvironmentHexEncoded, but sends the information using a
plain text, non-hex-encoded string.
The other two packets are new.
This patch also includes updates to the documentation, testsuite, and
unit tests, without introducing regressions.
gdb/ChangeLog:
yyyy-mm-dd Sergio Durigan Junior <sergiodj@redhat.com>
* NEWS (Changes since GDB 8.0): Add entry mentioning new support
for setting/unsetting environment variables on the remote target.
(New remote packets): Add entries for QEnvironmentHexEncoded,
QEnvironmentUnset and QEnvironmentReset.
* common/environ.c (gdb_environ::operator=): Extend method to
handle m_user_set_env_list and m_user_unset_env_list.
(gdb_environ::clear): Likewise.
(match_var_in_string): Change type of first parameter from 'char
*' to 'const char *'.
(gdb_environ::set): Extend method to handle
m_user_set_env_list and m_user_unset_env_list.
(gdb_environ::unset): Likewise.
(gdb_environ::clear_user_set_env): New method.
(gdb_environ::user_set_envp): Likewise.
(gdb_environ::user_unset_envp): Likewise.
* common/environ.h (gdb_environ): Handle m_user_set_env_list and
m_user_unset_env_list on move constructor/assignment.
(unset): Add new default parameter 'update_unset_list = true'.
(clear_user_set_env): New method.
(user_set_envp): Likewise.
(user_unset_envp): Likewise.
(m_user_set_env_list): New std::set.
(m_user_unset_env_list): Likewise.
* common/rsp-low.c (hex2str): New function.
(bin2hex): New overload for bin2hex function.
* common/rsp-low.c (hex2str): New prototype.
(str2hex): New overload prototype.
* remote.c: Include "environ.h". Add QEnvironmentHexEncoded,
QEnvironmentUnset and QEnvironmentReset.
(remote_protocol_features): Add QEnvironmentHexEncoded,
QEnvironmentUnset and QEnvironmentReset packets.
(send_environment_packet): New function.
(extended_remote_environment_support): Likewise.
(extended_remote_create_inferior): Call
extended_remote_environment_support.
(_initialize_remote): Add QEnvironmentHexEncoded,
QEnvironmentUnset and QEnvironmentReset packet configs.
* unittests/environ-selftests.c (test_vector_initialization):
New function.
(test_init_from_host_environ): Likewise.
(test_reinit_from_host_environ): Likewise.
(test_set_A_unset_B_unset_A_cannot_find_A_can_find_B):
Likewise.
(test_unset_set_empty_vector): Likewise.
(test_vector_clear): Likewise.
(test_std_move): Likewise.
(test_move_constructor):
(test_self_move): Likewise.
(test_set_unset_reset): Likewise.
(run_tests): Rewrite in terms of the functions above.
gdb/gdbserver/ChangeLog:
yyyy-mm-dd Sergio Durigan Junior <sergiodj@redhat.com>
* server.c (handle_general_set): Handle QEnvironmentHexEncoded,
QEnvironmentUnset and QEnvironmentReset packets.
(handle_query): Inform remote that QEnvironmentHexEncoded,
QEnvironmentUnset and QEnvironmentReset are supported.
gdb/doc/ChangeLog:
yyyy-mm-dd Sergio Durigan Junior <sergiodj@redhat.com>
* gdb.texinfo (set environment): Add @anchor. Explain that
environment variables set by the user are sent to GDBserver.
(unset environment): Likewise, but for unsetting variables.
(Connecting) <Remote Packet>: Add "environment-hex-encoded",
"QEnvironmentHexEncoded", "environment-unset", "QEnvironmentUnset",
"environment-reset" and "QEnvironmentReset" to the table.
(Remote Protocol) <QEnvironmentHexEncoded, QEnvironmentUnset,
QEnvironmentReset>: New item, explaining the packet.
gdb/testsuite/ChangeLog:
yyyy-mm-dd Sergio Durigan Junior <sergiodj@redhat.com>
* gdb.base/share-env-with-gdbserver.c: New file.
* gdb.base/share-env-with-gdbserver.exp: Likewise.
---
gdb/NEWS | 28 ++
gdb/common/environ.c | 75 +++++-
gdb/common/environ.h | 24 +-
gdb/common/rsp-low.c | 41 +++
gdb/common/rsp-low.h | 8 +
gdb/doc/gdb.texinfo | 116 +++++++++
gdb/gdbserver/server.c | 65 ++++-
gdb/remote.c | 76 ++++++
gdb/testsuite/gdb.base/share-env-with-gdbserver.c | 40 +++
.../gdb.base/share-env-with-gdbserver.exp | 255 +++++++++++++++++++
gdb/unittests/environ-selftests.c | 283 +++++++++++++++------
11 files changed, 916 insertions(+), 95 deletions(-)
create mode 100644 gdb/testsuite/gdb.base/share-env-with-gdbserver.c
create mode 100644 gdb/testsuite/gdb.base/share-env-with-gdbserver.exp
diff --git a/gdb/NEWS b/gdb/NEWS
index 735415495e..fda3064605 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -3,6 +3,18 @@
*** Changes since GDB 8.0
+* On Unix systems, GDB now supports transmitting environment variables
+ that are to be set or unset to GDBserver. These variables will
+ affect the environment to be passed to the remote inferior.
+
+ To inform GDB of environment variables that are to be transmitted to
+ GDBserver, use the "set environment" command. Only user set
+ environment variables are sent to GDBserver.
+
+ To inform GDB of environment variables that are to be unset before
+ the remote inferior is started by the GDBserver, use the "unset
+ environment" command.
+
* New features in the GDB remote stub, GDBserver
** New "--selftest" command line option runs some GDBserver self
@@ -17,8 +29,24 @@
"target remote", you can disable the startup with shell by using the
new "--no-startup-with-shell" GDBserver command line option.
+ ** On Unix systems, GDBserver now supports receiving environment
+ variables that are to be set or unset from GDB. These variables
+ will affect the environment to be passed to the inferior.
+
* New remote packets
+QEnvironmentHexEncoded
+ Inform GDBserver of an environment variable that is to be passed to
+ the inferior when starting it.
+
+QEnvironmentUnset
+ Inform GDBserver of an environment variable that is to be unset
+ before starting the remote inferior.
+
+QEnvironmentReset
+ Inform GDBserver that the environment should be reset (i.e.,
+ user-set environment variables should be unset).
+
QStartupWithShell
Indicates whether the inferior must be started with a shell or not.
diff --git a/gdb/common/environ.c b/gdb/common/environ.c
index 698bda3ac1..7753f6ebca 100644
--- a/gdb/common/environ.c
+++ b/gdb/common/environ.c
@@ -30,8 +30,12 @@ gdb_environ::operator= (gdb_environ &&e)
return *this;
m_environ_vector = std::move (e.m_environ_vector);
+ m_user_set_env = std::move (e.m_user_set_env);
+ m_user_unset_env = std::move (e.m_user_unset_env);
e.m_environ_vector.clear ();
e.m_environ_vector.push_back (NULL);
+ e.m_user_set_env.clear ();
+ e.m_user_unset_env.clear ();
return *this;
}
@@ -65,6 +69,8 @@ gdb_environ::clear ()
m_environ_vector.clear ();
/* Always add the NULL element. */
m_environ_vector.push_back (NULL);
+ m_user_set_env.clear ();
+ m_user_unset_env.clear ();
}
/* Helper function to check if STRING contains an environment variable
@@ -72,7 +78,7 @@ gdb_environ::clear ()
if it contains, false otherwise. */
static bool
-match_var_in_string (char *string, const char *var, size_t var_len)
+match_var_in_string (const char *string, const char *var, size_t var_len)
{
if (strncmp (string, var, var_len) == 0 && string[var_len] == '=')
return true;
@@ -99,32 +105,59 @@ gdb_environ::get (const char *var) const
void
gdb_environ::set (const char *var, const char *value)
{
+ char *fullvar = concat (var, "=", value, NULL);
+
/* We have to unset the variable in the vector if it exists. */
- unset (var);
+ unset (var, false);
/* Insert the element before the last one, which is always NULL. */
- m_environ_vector.insert (m_environ_vector.end () - 1,
- concat (var, "=", value, NULL));
+ m_environ_vector.insert (m_environ_vector.end () - 1, fullvar);
+
+ /* Mark this environment variable as having been set by the user.
+ This will be useful when we deal with setting environment
+ variables on the remote target. */
+ m_user_set_env.insert (std::string (fullvar));
+
+ /* If this environment variable is marked as unset by the user, then
+ remove it from the list, because now the user wants to set
+ it. */
+ m_user_unset_env.erase (std::string (var));
}
/* See common/environ.h. */
void
-gdb_environ::unset (const char *var)
+gdb_environ::unset (const char *var, bool update_unset_list)
{
size_t len = strlen (var);
+ std::vector<char *>::iterator it_env;
/* We iterate until '.end () - 1' because the last element is
always NULL. */
- for (std::vector<char *>::iterator el = m_environ_vector.begin ();
- el != m_environ_vector.end () - 1;
- ++el)
- if (match_var_in_string (*el, var, len))
- {
- xfree (*el);
- m_environ_vector.erase (el);
- break;
- }
+ for (it_env = m_environ_vector.begin ();
+ it_env != m_environ_vector.end () - 1;
+ ++it_env)
+ if (match_var_in_string (*it_env, var, len))
+ break;
+
+ if (it_env != m_environ_vector.end () - 1)
+ {
+ m_user_set_env.erase (std::string (*it_env));
+ xfree (*it_env);
+
+ m_environ_vector.erase (it_env);
+ }
+
+ if (update_unset_list)
+ m_user_unset_env.insert (std::string (var));
+}
+
+/* See common/environ.h. */
+
+void
+gdb_environ::unset (const char *var)
+{
+ unset (var, true);
}
/* See common/environ.h. */
@@ -134,3 +167,17 @@ gdb_environ::envp () const
{
return const_cast<char **> (&m_environ_vector[0]);
}
+
+/* See common/environ.h. */
+
+const std::set<std::string> &
+gdb_environ::user_set_env () const
+{
+ return m_user_set_env;
+}
+
+const std::set<std::string> &
+gdb_environ::user_unset_env () const
+{
+ return m_user_unset_env;
+}
diff --git a/gdb/common/environ.h b/gdb/common/environ.h
index 0bbb191361..4003f4eee8 100644
--- a/gdb/common/environ.h
+++ b/gdb/common/environ.h
@@ -18,6 +18,7 @@
#define ENVIRON_H 1
#include <vector>
+#include <set>
/* Class that represents the environment variables as seen by the
inferior. */
@@ -41,12 +42,16 @@ public:
/* Move constructor. */
gdb_environ (gdb_environ &&e)
- : m_environ_vector (std::move (e.m_environ_vector))
+ : m_environ_vector (std::move (e.m_environ_vector)),
+ m_user_set_env (std::move (e.m_user_set_env)),
+ m_user_unset_env (std::move (e.m_user_unset_env))
{
/* Make sure that the moved-from vector is left at a valid
state (only one NULL element). */
e.m_environ_vector.clear ();
e.m_environ_vector.push_back (NULL);
+ e.m_user_set_env.clear ();
+ e.m_user_unset_env.clear ();
}
/* Move assignment. */
@@ -73,9 +78,26 @@ public:
/* Return the environment vector represented as a 'char **'. */
char **envp () const;
+ /* Return the user-set environment vector. */
+ const std::set<std::string> &user_set_env () const;
+
+ /* Return the user-unset environment vector. */
+ const std::set<std::string> &user_unset_env () const;
+
private:
+ /* Unset VAR in environment. If UPDATE_UNSET_LIST is true, then
+ also update M_USER_UNSET_ENV to reflect the unsetting of the
+ environment variable. */
+ void unset (const char *var, bool update_unset_list);
+
/* A vector containing the environment variables. */
std::vector<char *> m_environ_vector;
+
+ /* The environment variables explicitly set by the user. */
+ std::set<std::string> m_user_set_env;
+
+ /* The environment variables explicitly unset by the user. */
+ std::set<std::string> m_user_unset_env;
};
#endif /* defined (ENVIRON_H) */
diff --git a/gdb/common/rsp-low.c b/gdb/common/rsp-low.c
index eb85ab5701..4befeb1dd8 100644
--- a/gdb/common/rsp-low.c
+++ b/gdb/common/rsp-low.c
@@ -132,6 +132,30 @@ hex2bin (const char *hex, gdb_byte *bin, int count)
/* See rsp-low.h. */
+std::string
+hex2str (const char *hex)
+{
+ std::string ret;
+ size_t len = strlen (hex);
+
+ ret.reserve (len / 2);
+ for (size_t i = 0; i < len; ++i)
+ {
+ if (hex[0] == '\0' || hex[1] == '\0')
+ {
+ /* Hex string is short, or of uneven length. Return what we
+ have so far. */
+ return ret;
+ }
+ ret += fromhex (hex[0]) * 16 + fromhex (hex[1]);
+ hex += 2;
+ }
+
+ return ret;
+}
+
+/* See rsp-low.h. */
+
int
bin2hex (const gdb_byte *bin, char *hex, int count)
{
@@ -146,6 +170,23 @@ bin2hex (const gdb_byte *bin, char *hex, int count)
return i;
}
+/* See rsp-low.h. */
+
+std::string
+bin2hex (const gdb_byte *bin, int count)
+{
+ std::string ret;
+
+ ret.reserve (count * 2);
+ for (int i = 0; i < count; ++i)
+ {
+ ret += tohex ((*bin >> 4) & 0xf);
+ ret += tohex (*bin++ & 0xf);
+ }
+
+ return ret;
+}
+
/* Return whether byte B needs escaping when sent as part of binary data. */
static int
diff --git a/gdb/common/rsp-low.h b/gdb/common/rsp-low.h
index b57f58bc75..2b3685f0f0 100644
--- a/gdb/common/rsp-low.h
+++ b/gdb/common/rsp-low.h
@@ -52,6 +52,10 @@ extern char *unpack_varlen_hex (char *buff, ULONGEST *result);
extern int hex2bin (const char *hex, gdb_byte *bin, int count);
+/* Like hex2bin, but return a std::string. */
+
+extern std::string hex2str (const char *hex);
+
/* Convert some bytes to a hexadecimal representation. BIN holds the
bytes to convert. COUNT says how many bytes to convert. The
resulting characters are stored in HEX, followed by a NUL
@@ -59,6 +63,10 @@ extern int hex2bin (const char *hex, gdb_byte *bin, int count);
extern int bin2hex (const gdb_byte *bin, char *hex, int count);
+/* Overloaded version of bin2hex that returns a std::string. */
+
+extern std::string bin2hex (const gdb_byte *bin, int count);
+
/* Convert BUFFER, binary data at least LEN_UNITS addressable memory units
long, into escaped binary data in OUT_BUF. Only copy memory units that fit
completely in OUT_BUF. Set *OUT_LEN_UNITS to the number of units from
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index d977b234d0..874cdebc11 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -2363,6 +2363,7 @@ print the names and values of all environment variables to be given to
your program. You can abbreviate @code{environment} as @code{env}.
@kindex set environment
+@anchor{set environment}
@item set environment @var{varname} @r{[}=@var{value}@r{]}
Set environment variable @var{varname} to @var{value}. The value
changes for your program (and the shell @value{GDBN} uses to launch
@@ -2391,12 +2392,21 @@ If necessary, you can avoid that by using the @samp{env} program as a
wrapper instead of using @code{set environment}. @xref{set
exec-wrapper}, for an example doing just that.
+Environment variables that are set by the user are also transmitted to
+@command{gdbserver} to be used when starting the remote inferior.
+@pxref{QEnvironmentHexEncoded}.
+
@kindex unset environment
+@anchor{unset environment}
@item unset environment @var{varname}
Remove variable @var{varname} from the environment to be passed to your
program. This is different from @samp{set env @var{varname} =};
@code{unset environment} removes the variable from the environment,
rather than assigning it an empty value.
+
+Environment variables that are unset by the user are also unset on
+@command{gdbserver} when starting the remote inferior.
+@pxref{QEnvironmentUnset}.
@end table
@emph{Warning:} On Unix systems, @value{GDBN} runs your program using
@@ -20849,6 +20859,18 @@ are:
@tab @code{QStartupWithShell}
@tab @code{set startup-with-shell}
+@item @code{environment-hex-encoded}
+@tab @code{QEnvironmentHexEncoded}
+@tab @code{set environment}
+
+@item @code{environment-unset}
+@tab @code{QEnvironmentUnset}
+@tab @code{unset environment}
+
+@item @code{environment-reset}
+@tab @code{QEnvironmentReset}
+@tab @code{Reset the inferior environment (i.e., unset user-set variables)}
+
@item @code{conditional-breakpoints-packet}
@tab @code{Z0 and Z1}
@tab @code{Support for target-side breakpoint condition evaluation}
@@ -36604,6 +36626,100 @@ actually support starting the inferior using a shell.
Use of this packet is controlled by the @code{set startup-with-shell}
command; @pxref{set startup-with-shell}.
+@item QEnvironmentHexEncoded:@var{hex-value}
+@anchor{QEnvironmentHexEncoded}
+@cindex set environment variable, remote request
+@cindex @samp{QEnvironmentHexEncoded} packet
+On UNIX-like targets, it is possible to set environment variables that
+will be passed to the inferior during the startup process. This
+packet is used to inform @command{gdbserver} of an environment
+variable that has been defined by the user on @value{GDBN} (@pxref{set
+environment}).
+
+The packet is composed by @var{hex-value}, an hex encoded
+representation of the @var{name=value} format representing an
+environment variable. The name of the environment variable is
+represented by @var{name}, and the value to be assigned to the
+environment variable is represented by @var{value}. If the variable
+has no value (i.e., the value is @code{null}), then @var{value} will
+not be present.
+
+This packet is only available in extended mode (@pxref{extended
+mode}).
+
+Reply:
+@table @samp
+@item OK
+The request succeeded.
+@end table
+
+This packet is not probed by default; the remote stub must request it,
+by supplying an appropriate @samp{qSupported} response
+(@pxref{qSupported}). This should only be done on targets that
+actually support passing environment variables to the starting
+inferior.
+
+This packet is related to the @code{set environment} command;
+@pxref{set environment}.
+
+@item QEnvironmentUnset:@var{hex-value}
+@anchor{QEnvironmentUnset}
+@cindex unset environment variable, remote request
+@cindex @samp{QEnvironmentUnset} packet
+On UNIX-like targets, it is possible to unset environment variables
+before starting the inferior in the remote target. This packet is
+used to inform @command{gdbserver} of an environment variable that has
+been unset by the user on @value{GDBN} (@pxref{unset environment}).
+
+The packet is composed by @var{hex-value}, an hex encoded
+representation of the name of the environment variable to be unset.
+
+This packet is only available in extended mode (@pxref{extended
+mode}).
+
+Reply:
+@table @samp
+@item OK
+The request succeeded.
+@end table
+
+This packet is not probed by default; the remote stub must request it,
+by supplying an appropriate @samp{qSupported} response
+(@pxref{qSupported}). This should only be done on targets that
+actually support passing environment variables to the starting
+inferior.
+
+This packet is related to the @code{unset environment} command;
+@pxref{unset environment}.
+
+@item QEnvironmentReset
+@anchor{QEnvironmentReset}
+@cindex reset environment, remote request
+@cindex @samp{QEnvironmentReset} packet
+On UNIX-like targets, this packet is used to reset the state of
+environment variables in the remote target before starting the
+inferior. In this context, reset means unsetting all environment
+variables that were previously set by the user (i.e., were not
+initially present in the environment). It is sent to
+@command{gdbserver} before the @samp{QEnvironmentHexEncoded}
+(@pxref{QEnvironmentHexEncoded}) and the @samp{QEnvironmentUnset}
+(@pxref{QEnvironmentUnset}) packets.
+
+This packet is only available in extended mode (@pxref{extended
+mode}).
+
+Reply:
+@table @samp
+@item OK
+The request succeeded.
+@end table
+
+This packet is not probed by default; the remote stub must request it,
+by supplying an appropriate @samp{qSupported} response
+(@pxref{qSupported}). This should only be done on targets that
+actually support passing environment variables to the starting
+inferior.
+
@item qfThreadInfo
@itemx qsThreadInfo
@cindex list active threads, remote request
diff --git a/gdb/gdbserver/server.c b/gdb/gdbserver/server.c
index 8200aa17c8..88a5ddc393 100644
--- a/gdb/gdbserver/server.c
+++ b/gdb/gdbserver/server.c
@@ -633,6 +633,67 @@ handle_general_set (char *own_buf)
return;
}
+ if (strcmp (own_buf, "QEnvironmentReset") == 0)
+ {
+ our_environ = gdb_environ::from_host_environ ();
+
+ write_ok (own_buf);
+ return;
+ }
+
+ if (startswith (own_buf, "QEnvironmentHexEncoded:"))
+ {
+ const char *p = own_buf + sizeof ("QEnvironmentHexEncoded:") - 1;
+ /* The final form of the environment variable. FINAL_VAR will
+ hold the 'VAR=VALUE' format. */
+ std::string final_var = hex2str (p);
+ std::string var_name, var_value;
+
+ if (remote_debug)
+ {
+ debug_printf (_("[QEnvironmentHexEncoded received '%s']\n"), p);
+ debug_printf (_("[Environment variable to be set: '%s']\n"),
+ final_var.c_str ());
+ debug_flush ();
+ }
+
+ size_t pos = final_var.find ('=');
+ if (pos == std::string::npos)
+ {
+ warning (_("Unexpected format for environment variable: '%s'"),
+ final_var.c_str ());
+ write_enn (own_buf);
+ return;
+ }
+
+ var_name = final_var.substr (0, pos);
+ var_value = final_var.substr (pos + 1, std::string::npos);
+
+ our_environ.set (var_name.c_str (), var_value.c_str ());
+
+ write_ok (own_buf);
+ return;
+ }
+
+ if (startswith (own_buf, "QEnvironmentUnset:"))
+ {
+ const char *p = own_buf + sizeof ("QEnvironmentUnset:") - 1;
+ std::string varname = hex2str (p);
+
+ if (remote_debug)
+ {
+ debug_printf (_("[QEnvironmentUnset received '%s']\n"), p);
+ debug_printf (_("[Environment variable to be unset: '%s']\n"),
+ varname.c_str ());
+ debug_flush ();
+ }
+
+ our_environ.unset (varname.c_str ());
+
+ write_ok (own_buf);
+ return;
+ }
+
if (strcmp (own_buf, "QStartNoAckMode") == 0)
{
if (remote_debug)
@@ -2230,7 +2291,9 @@ handle_query (char *own_buf, int packet_len, int *new_packet_len_p)
}
sprintf (own_buf,
- "PacketSize=%x;QPassSignals+;QProgramSignals+;QStartupWithShell+",
+ "PacketSize=%x;QPassSignals+;QProgramSignals+;"
+ "QStartupWithShell+;QEnvironmentHexEncoded+;"
+ "QEnvironmentReset+;QEnvironmentUnset+",
PBUFSIZ - 1);
if (target_supports_catch_syscall ())
diff --git a/gdb/remote.c b/gdb/remote.c
index 2249533258..3347dd8022 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -73,6 +73,7 @@
#include "record-btrace.h"
#include <algorithm>
#include "common/scoped_restore.h"
+#include "environ.h"
/* Temp hacks for tracepoint encoding migration. */
static char *target_buf;
@@ -1430,6 +1431,9 @@ enum {
PACKET_QCatchSyscalls,
PACKET_QProgramSignals,
PACKET_QStartupWithShell,
+ PACKET_QEnvironmentHexEncoded,
+ PACKET_QEnvironmentReset,
+ PACKET_QEnvironmentUnset,
PACKET_qCRC,
PACKET_qSearch_memory,
PACKET_vAttach,
@@ -4637,6 +4641,12 @@ static const struct protocol_feature remote_protocol_features[] = {
PACKET_QProgramSignals },
{ "QStartupWithShell", PACKET_DISABLE, remote_supported_packet,
PACKET_QStartupWithShell },
+ { "QEnvironmentHexEncoded", PACKET_DISABLE, remote_supported_packet,
+ PACKET_QEnvironmentHexEncoded },
+ { "QEnvironmentReset", PACKET_DISABLE, remote_supported_packet,
+ PACKET_QEnvironmentReset },
+ { "QEnvironmentUnset", PACKET_DISABLE, remote_supported_packet,
+ PACKET_QEnvironmentUnset },
{ "QStartNoAckMode", PACKET_DISABLE, remote_supported_packet,
PACKET_QStartNoAckMode },
{ "multiprocess", PACKET_DISABLE, remote_supported_packet,
@@ -9556,6 +9566,57 @@ extended_remote_run (const std::string &args)
}
}
+/* Helper function to send set/unset environment packets. ACTION is
+ either "set" or "unset". PACKET is either "QEnvironmentHexEncoded"
+ or "QEnvironmentUnsetVariable". VALUE is the variable to be
+ sent. */
+
+static void
+send_environment_packet (struct remote_state *rs,
+ const char *action,
+ const char *packet,
+ const char *value)
+{
+ /* Convert the environment variable to an hex string, which
+ is the best format to be transmitted over the wire. */
+ std::string encoded_value = bin2hex ((const gdb_byte *) value,
+ strlen (value));
+
+ xsnprintf (rs->buf, get_remote_packet_size (),
+ "%s:%s", packet, encoded_value.c_str ());
+
+ putpkt (rs->buf);
+ getpkt (&rs->buf, &rs->buf_size, 0);
+ if (strcmp (rs->buf, "OK") != 0)
+ warning (_("Unable to %s environment variable '%s' on remote."),
+ action, value);
+}
+
+/* Helper function to handle the QEnvironment* packets. */
+
+static void
+extended_remote_environment_support (struct remote_state *rs)
+{
+ if (packet_support (PACKET_QEnvironmentReset) != PACKET_DISABLE)
+ {
+ putpkt ("QEnvironmentReset");
+ getpkt (&rs->buf, &rs->buf_size, 0);
+ if (strcmp (rs->buf, "OK") != 0)
+ warning (_("Unable to reset environment on remote."));
+ }
+
+ gdb_environ *e = ¤t_inferior ()->environment;
+
+ if (packet_support (PACKET_QEnvironmentHexEncoded) != PACKET_DISABLE)
+ for (const std::string &el : e->user_set_env ())
+ send_environment_packet (rs, "set", "QEnvironmentHexEncoded",
+ el.c_str ());
+
+ if (packet_support (PACKET_QEnvironmentUnset) != PACKET_DISABLE)
+ for (const std::string &el : e->user_unset_env ())
+ send_environment_packet (rs, "unset", "QEnvironmentUnset", el.c_str ());
+}
+
/* In the extended protocol we want to be able to do things like
"run" and have them basically work as expected. So we need
a special create_inferior function. We support changing the
@@ -9596,6 +9657,8 @@ Remote replied unexpectedly while setting startup-with-shell: %s"),
rs->buf);
}
+ extended_remote_environment_support (rs);
+
/* Now restart the remote server. */
run_worked = extended_remote_run (args) != -1;
if (!run_worked)
@@ -14067,6 +14130,19 @@ Show the maximum size of the address (in bits) in a memory packet."), NULL,
add_packet_config_cmd (&remote_protocol_packets[PACKET_QStartupWithShell],
"QStartupWithShell", "startup-with-shell", 0);
+ add_packet_config_cmd (&remote_protocol_packets
+ [PACKET_QEnvironmentHexEncoded],
+ "QEnvironmentHexEncoded", "environment-hex-encoded",
+ 0);
+
+ add_packet_config_cmd (&remote_protocol_packets[PACKET_QEnvironmentReset],
+ "QEnvironmentReset", "environment-reset",
+ 0);
+
+ add_packet_config_cmd (&remote_protocol_packets[PACKET_QEnvironmentUnset],
+ "QEnvironmentUnset", "environment-unset",
+ 0);
+
add_packet_config_cmd (&remote_protocol_packets[PACKET_qSymbol],
"qSymbol", "symbol-lookup", 0);
diff --git a/gdb/testsuite/gdb.base/share-env-with-gdbserver.c b/gdb/testsuite/gdb.base/share-env-with-gdbserver.c
new file mode 100644
index 0000000000..740bd0fb52
--- /dev/null
+++ b/gdb/testsuite/gdb.base/share-env-with-gdbserver.c
@@ -0,0 +1,40 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+ Copyright 2017 Free Software Foundation, Inc.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+/* Wrapper around getenv for GDB. */
+
+static const char *
+my_getenv (const char *name)
+{
+ return getenv (name);
+}
+
+int
+main (int argc, char *argv[])
+{
+ const char *myvar = getenv ("GDB_TEST_VAR");
+
+ if (myvar != NULL)
+ printf ("It worked! myvar = '%s'\n", myvar);
+ else
+ printf ("It failed.");
+
+ return 0; /* break-here */
+}
diff --git a/gdb/testsuite/gdb.base/share-env-with-gdbserver.exp b/gdb/testsuite/gdb.base/share-env-with-gdbserver.exp
new file mode 100644
index 0000000000..7521be1e99
--- /dev/null
+++ b/gdb/testsuite/gdb.base/share-env-with-gdbserver.exp
@@ -0,0 +1,255 @@
+# This testcase is part of GDB, the GNU debugger.
+
+# Copyright 2017 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+# This test doesn't make sense on native-gdbserver.
+if { [use_gdb_stub] } {
+ untested "not supported"
+ return
+}
+
+standard_testfile
+
+if { [prepare_for_testing "failed to prepare" $testfile $srcfile debug] } {
+ return -1
+}
+
+set test_var_name "GDB_TEST_VAR"
+
+# Helper function that performs a check on the output of "getenv".
+#
+# - VAR_NAME is the name of the variable to be checked.
+#
+# - VAR_VALUE is the value expected.
+#
+# - TEST_MSG, if not empty, is the test message to be used by the
+# "gdb_test".
+#
+# - EMPTY_VAR_P, if non-zero, means that the variable is not expected
+# to exist. In this case, VAR_VALUE is not considered.
+
+proc check_getenv { var_name var_value { test_msg "" } { empty_var_p 0 } } {
+ global hex decimal
+
+ if { $test_msg == "" } {
+ set test_msg "print result of getenv for $var_name"
+ }
+
+ if { $empty_var_p } {
+ set var_value_match "0x0"
+ } else {
+ set var_value_match "$hex \"$var_value\""
+ }
+
+ gdb_test "print my_getenv (\"$var_name\")" "\\\$$decimal = $var_value_match" \
+ $test_msg
+}
+
+# Helper function to re-run to main and breaking at the "break-here"
+# label.
+
+proc do_prepare_inferior { } {
+ global decimal hex
+
+ if { ![runto_main] } {
+ return -1
+ }
+
+ gdb_breakpoint [gdb_get_line_number "break-here"]
+
+ gdb_test "continue" "Breakpoint $decimal, main \\\(argc=1, argv=$hex\\\) at.*" \
+ "continue until breakpoint"
+}
+
+# Helper function that does the actual testing.
+#
+# - VAR_VALUE is the value of the environment variable.
+#
+# - VAR_NAME is the name of the environment variable. If empty,
+# defaults to $test_var_name.
+#
+# - VAR_NAME_MATCH is the name (regex) that will be used to query the
+# environment about the variable (via getenv). This is useful when
+# we're testing variables with strange names (e.g., with an equal
+# sign in the name) and we know that the variable will actually be
+# set using another name. If empty, defatults, to $var_name.
+#
+# - VAR_VALUE_MATCH is the value (regex) that will be used to match
+# the result of getenv. The rationale is the same as explained for
+# VAR_NAME_MATCH. If empty, defaults, to $var_value.
+
+proc do_test { var_value { var_name "" } { var_name_match "" } { var_value_match "" } } {
+ global binfile test_var_name
+
+ clean_restart $binfile
+
+ if { $var_name == "" } {
+ set var_name $test_var_name
+ }
+
+ if { $var_name_match == "" } {
+ set var_name_match $var_name
+ }
+
+ if { $var_value_match == "" } {
+ set var_value_match $var_value
+ }
+
+ if { $var_value != "" } {
+ gdb_test_no_output "set environment $var_name = $var_value" \
+ "set $var_name = $var_value"
+ } else {
+ gdb_test "set environment $var_name =" \
+ "Setting environment variable \"$var_name\" to null value." \
+ "set $var_name to null value"
+ }
+
+ do_prepare_inferior
+
+ check_getenv "$var_name_match" "$var_value_match" \
+ "print result of getenv for $var_name"
+}
+
+with_test_prefix "long var value" {
+ do_test "this is my test variable; testing long vars; {}"
+}
+
+with_test_prefix "empty var" {
+ do_test ""
+}
+
+with_test_prefix "strange named var" {
+ # In this test we're doing the following:
+ #
+ # (gdb) set environment 'asd =' = 123 43; asd b ### [];;;
+ #
+ # However, due to how GDB parses this line, the environment
+ # variable will end up named <'asd> (without the <>), and its
+ # value will be <' = 123 43; asd b ### [];;;> (without the <>).
+ do_test "123 43; asd b ### \[\];;;" "'asd ='" "'asd" \
+ [string_to_regexp "' = 123 43; asd b ### \[\];;;"]
+}
+
+# Test setting and unsetting environment variables in various
+# fashions.
+
+proc test_set_unset_vars { } {
+ global binfile
+
+ clean_restart $binfile
+
+ with_test_prefix "set 3 environment variables" {
+ # Set some environment variables
+ gdb_test_no_output "set environment A = 1" \
+ "set A to 1"
+ gdb_test_no_output "set environment B = 2" \
+ "set B to 2"
+ gdb_test_no_output "set environment C = 3" \
+ "set C to 3"
+
+ do_prepare_inferior
+
+ # Check that the variables are known by the inferior
+ check_getenv "A" "1"
+ check_getenv "B" "2"
+ check_getenv "C" "3"
+ }
+
+ with_test_prefix "unset one variable, reset one" {
+ # Now, unset/reset some values
+ gdb_test_no_output "unset environment A" \
+ "unset A"
+ gdb_test_no_output "set environment B = 4" \
+ "set B to 4"
+
+ do_prepare_inferior
+
+ check_getenv "A" "" "" 1
+ check_getenv "B" "4"
+ check_getenv "C" "3"
+ }
+
+ with_test_prefix "unset two variables, reset one" {
+ # Unset more values
+ gdb_test_no_output "unset environment B" \
+ "unset B"
+ gdb_test_no_output "set environment A = 1" \
+ "set A to 1 again"
+ gdb_test_no_output "unset environment C" \
+ "unset C"
+
+ do_prepare_inferior
+
+ check_getenv "A" "1"
+ check_getenv "B" "" "" 1
+ check_getenv "C" "" "" 1
+ }
+}
+
+with_test_prefix "test set/unset of vars" {
+ test_set_unset_vars
+}
+
+# Test that unsetting works.
+
+proc test_unset { } {
+ global hex decimal binfile gdb_prompt
+
+ clean_restart $binfile
+
+ do_prepare_inferior
+
+ set test_msg "check if unset works"
+ set found_home 0
+ gdb_test_multiple "print my_getenv (\"HOME\")" $test_msg {
+ -re "\\\$$decimal = $hex \".*\"\r\n$gdb_prompt $" {
+ pass $test_msg
+ set found_home 1
+ }
+ -re "\\\$$decimal = 0x0\r\n$gdb_prompt $" {
+ untested $test_msg
+ }
+ }
+
+ if { $found_home == 1 } {
+ with_test_prefix "simple unset" {
+ # We can do the test, because $HOME exists (and therefore can
+ # be unset).
+ gdb_test_no_output "unset environment HOME" "unset HOME"
+
+ do_prepare_inferior
+
+ # $HOME now must be empty
+ check_getenv "HOME" "" "" 1
+ }
+
+ with_test_prefix "set-then-unset" {
+ clean_restart $binfile
+
+ # Test if setting and then unsetting $HOME works.
+ gdb_test_no_output "set environment HOME = test" "set HOME as test"
+ gdb_test_no_output "unset environment HOME" "unset HOME again"
+
+ do_prepare_inferior
+
+ check_getenv "HOME" "" "" 1
+ }
+ }
+}
+
+with_test_prefix "test unset of vars" {
+ test_unset
+}
diff --git a/gdb/unittests/environ-selftests.c b/gdb/unittests/environ-selftests.c
index 1e938e6746..e2d8c70985 100644
--- a/gdb/unittests/environ-selftests.c
+++ b/gdb/unittests/environ-selftests.c
@@ -22,132 +22,257 @@
#include "common/environ.h"
#include "common/diagnostics.h"
+static bool
+set_contains (const std::set<std::string> &set, std::string key)
+{
+ return set.find (key) != set.end ();
+}
+
namespace selftests {
namespace gdb_environ_tests {
+/* Test if the vector is initialized in a correct way. */
+
static void
-run_tests ()
+test_vector_initialization (gdb_environ *env)
{
- /* Set a test environment variable. This will be unset at the end
- of this function. */
- if (setenv ("GDB_SELFTEST_ENVIRON", "1", 1) != 0)
- error (_("Could not set environment variable for testing."));
-
- gdb_environ env;
-
/* When the vector is initialized, there should always be one NULL
element in it. */
- SELF_CHECK (env.envp ()[0] == NULL);
+ SELF_CHECK (env->envp ()[0] == NULL);
+ SELF_CHECK (env->user_set_env ().size () == 0);
+ SELF_CHECK (env->user_unset_env ().size () == 0);
/* Make sure that there is no other element. */
- SELF_CHECK (env.get ("PWD") == NULL);
+ SELF_CHECK (env->get ("PWD") == NULL);
+}
- /* Check if unset followed by a set in an empty vector works. */
- env.set ("PWD", "test");
- SELF_CHECK (strcmp (env.get ("PWD"), "test") == 0);
- /* The second element must be NULL. */
- SELF_CHECK (env.envp ()[1] == NULL);
- env.unset ("PWD");
- SELF_CHECK (env.envp ()[0] == NULL);
+/* Test initialization using the host's environ. */
+static void
+test_init_from_host_environ (gdb_environ *env)
+{
/* Initialize the environment vector using the host's environ. */
- env = gdb_environ::from_host_environ ();
+ *env = gdb_environ::from_host_environ ();
+
+ /* The user-set and user-unset lists must be empty. */
+ SELF_CHECK (env->user_set_env ().size () == 0);
+ SELF_CHECK (env->user_unset_env ().size () == 0);
/* Our test environment variable should be present at the
vector. */
- SELF_CHECK (strcmp (env.get ("GDB_SELFTEST_ENVIRON"), "1") == 0);
-
- /* Set our test variable to another value. */
- env.set ("GDB_SELFTEST_ENVIRON", "test");
- SELF_CHECK (strcmp (env.get ("GDB_SELFTEST_ENVIRON"), "test") == 0);
-
- /* And unset our test variable. The variable still exists in the
- host's environment, but doesn't exist in our vector. */
- env.unset ("GDB_SELFTEST_ENVIRON");
- SELF_CHECK (env.get ("GDB_SELFTEST_ENVIRON") == NULL);
-
- /* Re-set the test variable. */
- env.set ("GDB_SELFTEST_ENVIRON", "1");
- SELF_CHECK (strcmp (env.get ("GDB_SELFTEST_ENVIRON"), "1") == 0);
+ SELF_CHECK (strcmp (env->get ("GDB_SELFTEST_ENVIRON"), "1") == 0);
+}
- /* When we clear our environ vector, there should be only one
- element on it (NULL), and we shouldn't be able to get our test
- variable. */
- env.clear ();
- SELF_CHECK (env.envp ()[0] == NULL);
- SELF_CHECK (env.get ("GDB_SELFTEST_ENVIRON") == NULL);
+/* Test reinitialization using the host's environ. */
+static void
+test_reinit_from_host_environ (gdb_environ *env)
+{
/* Reinitialize our environ vector using the host environ. We
should be able to see one (and only one) instance of the test
variable. */
- env = gdb_environ::from_host_environ ();
- char **envp = env.envp ();
+ *env = gdb_environ::from_host_environ ();
+ char **envp = env->envp ();
int num_found = 0;
for (size_t i = 0; envp[i] != NULL; ++i)
if (strcmp (envp[i], "GDB_SELFTEST_ENVIRON=1") == 0)
++num_found;
SELF_CHECK (num_found == 1);
+}
- /* Get rid of our test variable. */
- unsetenv ("GDB_SELFTEST_ENVIRON");
+/* Test the case when we set a variable A, then set a variable B,
+ then unset A, and make sure that we cannot find A in the environ
+ vector, but can still find B. */
+
+static void
+test_set_A_unset_B_unset_A_cannot_find_A_can_find_B (gdb_environ *env)
+{
+ env->set ("GDB_SELFTEST_ENVIRON_1", "aaa");
+ SELF_CHECK (strcmp (env->get ("GDB_SELFTEST_ENVIRON_1"), "aaa") == 0);
+ /* User-set environ var list must contain one element. */
+ SELF_CHECK (env->user_set_env ().size () == 1);
+ SELF_CHECK (set_contains (env->user_set_env (),
+ std::string ("GDB_SELFTEST_ENVIRON_1=aaa")));
- /* Test the case when we set a variable A, then set a variable B,
- then unset A, and make sure that we cannot find A in the environ
- vector, but can still find B. */
- env.set ("GDB_SELFTEST_ENVIRON_1", "aaa");
- SELF_CHECK (strcmp (env.get ("GDB_SELFTEST_ENVIRON_1"), "aaa") == 0);
+ env->set ("GDB_SELFTEST_ENVIRON_2", "bbb");
+ SELF_CHECK (strcmp (env->get ("GDB_SELFTEST_ENVIRON_2"), "bbb") == 0);
- env.set ("GDB_SELFTEST_ENVIRON_2", "bbb");
- SELF_CHECK (strcmp (env.get ("GDB_SELFTEST_ENVIRON_2"), "bbb") == 0);
+ env->unset ("GDB_SELFTEST_ENVIRON_1");
+ SELF_CHECK (env->get ("GDB_SELFTEST_ENVIRON_1") == NULL);
+ SELF_CHECK (strcmp (env->get ("GDB_SELFTEST_ENVIRON_2"), "bbb") == 0);
- env.unset ("GDB_SELFTEST_ENVIRON_1");
- SELF_CHECK (env.get ("GDB_SELFTEST_ENVIRON_1") == NULL);
- SELF_CHECK (strcmp (env.get ("GDB_SELFTEST_ENVIRON_2"), "bbb") == 0);
+ /* The user-set environ var list must contain only one element
+ now. */
+ SELF_CHECK (set_contains (env->user_set_env (),
+ std::string ("GDB_SELFTEST_ENVIRON_2=bbb")));
+ SELF_CHECK (env->user_set_env ().size () == 1);
+}
- env.clear ();
+/* Check if unset followed by a set in an empty vector works. */
- /* Test that after a std::move the moved-from object is left at a
- valid state (i.e., its only element is NULL). */
- env.set ("A", "1");
- SELF_CHECK (strcmp (env.get ("A"), "1") == 0);
+static void
+test_unset_set_empty_vector (gdb_environ *env)
+{
+ env->set ("PWD", "test");
+ SELF_CHECK (strcmp (env->get ("PWD"), "test") == 0);
+ SELF_CHECK (set_contains (env->user_set_env (), std::string ("PWD=test")));
+ SELF_CHECK (env->user_unset_env ().size () == 0);
+ /* The second element must be NULL. */
+ SELF_CHECK (env->envp ()[1] == NULL);
+ SELF_CHECK (env->user_set_env ().size () == 1);
+ env->unset ("PWD");
+ SELF_CHECK (env->envp ()[0] == NULL);
+ SELF_CHECK (env->user_set_env ().size () == 0);
+ SELF_CHECK (env->user_unset_env ().size () == 1);
+ SELF_CHECK (set_contains (env->user_unset_env (), std::string ("PWD")));
+}
+
+/* When we clear our environ vector, there should be only one
+ element on it (NULL), and we shouldn't be able to get our test
+ variable. */
+
+static void
+test_vector_clear (gdb_environ *env)
+{
+ env->clear ();
+ SELF_CHECK (env->envp ()[0] == NULL);
+ SELF_CHECK (env->user_set_env ().size () == 0);
+ SELF_CHECK (env->user_unset_env ().size () == 0);
+ SELF_CHECK (env->get ("GDB_SELFTEST_ENVIRON") == NULL);
+}
+
+/* Test that after a std::move the moved-from object is left at a
+ valid state (i.e., its only element is NULL). */
+
+static void
+test_std_move (gdb_environ *env)
+{
+ env->set ("A", "1");
+ SELF_CHECK (strcmp (env->get ("A"), "1") == 0);
+ SELF_CHECK (set_contains (env->user_set_env (), std::string ("A=1")));
+ SELF_CHECK (env->user_set_env ().size () == 1);
gdb_environ env2;
- env2 = std::move (env);
- SELF_CHECK (env.envp ()[0] == NULL);
+ env2 = std::move (*env);
+ SELF_CHECK (env->envp ()[0] == NULL);
SELF_CHECK (strcmp (env2.get ("A"), "1") == 0);
SELF_CHECK (env2.envp ()[1] == NULL);
- env.set ("B", "2");
- SELF_CHECK (strcmp (env.get ("B"), "2") == 0);
- SELF_CHECK (env.envp ()[1] == NULL);
+ SELF_CHECK (env->user_set_env ().size () == 0);
+ SELF_CHECK (set_contains (env2.user_set_env (), std::string ("A=1")));
+ SELF_CHECK (env2.user_set_env ().size () == 1);
+ env->set ("B", "2");
+ SELF_CHECK (strcmp (env->get ("B"), "2") == 0);
+ SELF_CHECK (env->envp ()[1] == NULL);
+}
- /* Test that the move constructor leaves everything at a valid
- state. */
- env.clear ();
- env.set ("A", "1");
- SELF_CHECK (strcmp (env.get ("A"), "1") == 0);
- gdb_environ env3 = std::move (env);
- SELF_CHECK (env.envp ()[0] == NULL);
+/* Test that the move constructor leaves everything at a valid
+ state. */
+
+static void
+test_move_constructor (gdb_environ *env)
+{
+ env->clear ();
+ env->set ("A", "1");
+ SELF_CHECK (strcmp (env->get ("A"), "1") == 0);
+ SELF_CHECK (set_contains (env->user_set_env (), std::string ("A=1")));
+ gdb_environ env3 = std::move (*env);
+ SELF_CHECK (env->envp ()[0] == NULL);
+ SELF_CHECK (env->user_set_env ().size () == 0);
SELF_CHECK (strcmp (env3.get ("A"), "1") == 0);
SELF_CHECK (env3.envp ()[1] == NULL);
- env.set ("B", "2");
- SELF_CHECK (strcmp (env.get ("B"), "2") == 0);
- SELF_CHECK (env.envp ()[1] == NULL);
+ SELF_CHECK (set_contains (env3.user_set_env (), std::string ("A=1")));
+ SELF_CHECK (env3.user_set_env ().size () == 1);
+ env->set ("B", "2");
+ SELF_CHECK (strcmp (env->get ("B"), "2") == 0);
+ SELF_CHECK (env->envp ()[1] == NULL);
+ SELF_CHECK (set_contains (env->user_set_env (), std::string ("B=2")));
+ SELF_CHECK (env->user_set_env ().size () == 1);
+}
+
+/* Test that self-moving works. */
+static void
+test_self_move (gdb_environ *env)
+{
/* Test self-move. */
- env.clear ();
- env.set ("A", "1");
- SELF_CHECK (strcmp (env.get ("A"), "1") == 0);
+ env->clear ();
+ env->set ("A", "1");
+ SELF_CHECK (strcmp (env->get ("A"), "1") == 0);
+ SELF_CHECK (set_contains (env->user_set_env (), std::string ("A=1")));
+ SELF_CHECK (env->user_set_env ().size () == 1);
/* Some compilers warn about moving to self, but that's precisely what we want
to test here, so turn this warning off. */
DIAGNOSTIC_PUSH
DIAGNOSTIC_IGNORE_SELF_MOVE
- env = std::move (env);
+ *env = std::move (*env);
DIAGNOSTIC_POP
- SELF_CHECK (strcmp (env.get ("A"), "1") == 0);
- SELF_CHECK (strcmp (env.envp ()[0], "A=1") == 0);
- SELF_CHECK (env.envp ()[1] == NULL);
+ SELF_CHECK (strcmp (env->get ("A"), "1") == 0);
+ SELF_CHECK (strcmp (env->envp ()[0], "A=1") == 0);
+ SELF_CHECK (env->envp ()[1] == NULL);
+ SELF_CHECK (set_contains (env->user_set_env (), std::string ("A=1")));
+ SELF_CHECK (env->user_set_env ().size () == 1);
+}
+
+/* Test if set/unset/reset works. */
+
+static void
+test_set_unset_reset (gdb_environ *env)
+{
+ /* Set our test variable to another value. */
+ env->set ("GDB_SELFTEST_ENVIRON", "test");
+ SELF_CHECK (strcmp (env->get ("GDB_SELFTEST_ENVIRON"), "test") == 0);
+ SELF_CHECK (env->user_set_env ().size () == 1);
+ SELF_CHECK (env->user_unset_env ().size () == 0);
+
+ /* And unset our test variable. The variable still exists in the
+ host's environment, but doesn't exist in our vector. */
+ env->unset ("GDB_SELFTEST_ENVIRON");
+ SELF_CHECK (env->get ("GDB_SELFTEST_ENVIRON") == NULL);
+ SELF_CHECK (env->user_unset_env ().size () == 1);
+ SELF_CHECK (set_contains (env->user_unset_env (),
+ std::string ("GDB_SELFTEST_ENVIRON")));
+
+ /* Re-set the test variable. */
+ env->set ("GDB_SELFTEST_ENVIRON", "1");
+ SELF_CHECK (strcmp (env->get ("GDB_SELFTEST_ENVIRON"), "1") == 0);
+}
+
+static void
+run_tests ()
+{
+ /* Set a test environment variable. This will be unset at the end
+ of this function. */
+ if (setenv ("GDB_SELFTEST_ENVIRON", "1", 1) != 0)
+ error (_("Could not set environment variable for testing."));
+
+ gdb_environ env;
+
+ test_vector_initialization (&env);
+
+ test_unset_set_empty_vector (&env);
+
+ test_init_from_host_environ (&env);
+
+ test_set_unset_reset (&env);
+
+ test_vector_clear (&env);
+
+ test_reinit_from_host_environ (&env);
+
+ /* Get rid of our test variable. We won't need it anymore. */
+ unsetenv ("GDB_SELFTEST_ENVIRON");
+
+ test_set_A_unset_B_unset_A_cannot_find_A_can_find_B (&env);
+
+ env.clear ();
+
+ test_std_move (&env);
+
+ test_move_constructor (&env);
+
+ test_self_move (&env);
}
} /* namespace gdb_environ */
} /* namespace selftests */
--
2.13.3
^ permalink raw reply [flat|nested] 35+ messages in thread
* Re: [PATCH v4] Implement the ability to set/unset environment variables to GDBserver when starting the inferior
2017-08-28 23:13 ` [PATCH v4] " Sergio Durigan Junior
@ 2017-08-31 19:37 ` Simon Marchi
2017-08-31 20:34 ` Sergio Durigan Junior
0 siblings, 1 reply; 35+ messages in thread
From: Simon Marchi @ 2017-08-31 19:37 UTC (permalink / raw)
To: Sergio Durigan Junior, GDB Patches; +Cc: Pedro Alves, Simon Marchi
> diff --git a/gdb/unittests/environ-selftests.c b/gdb/unittests/environ-selftests.c
> index 1e938e6746..e2d8c70985 100644
> --- a/gdb/unittests/environ-selftests.c
> +++ b/gdb/unittests/environ-selftests.c
> @@ -22,132 +22,257 @@
> #include "common/environ.h"
> #include "common/diagnostics.h"
>
> +static bool
> +set_contains (const std::set<std::string> &set, std::string key)
> +{
> + return set.find (key) != set.end ();
> +}
> +
> namespace selftests {
> namespace gdb_environ_tests {
>
> +/* Test if the vector is initialized in a correct way. */
> +
> static void
> -run_tests ()
> +test_vector_initialization (gdb_environ *env)
> {
> - /* Set a test environment variable. This will be unset at the end
> - of this function. */
> - if (setenv ("GDB_SELFTEST_ENVIRON", "1", 1) != 0)
> - error (_("Could not set environment variable for testing."));
> -
> - gdb_environ env;
> -
> /* When the vector is initialized, there should always be one NULL
> element in it. */
> - SELF_CHECK (env.envp ()[0] == NULL);
> + SELF_CHECK (env->envp ()[0] == NULL);
> + SELF_CHECK (env->user_set_env ().size () == 0);
> + SELF_CHECK (env->user_unset_env ().size () == 0);
>
> /* Make sure that there is no other element. */
> - SELF_CHECK (env.get ("PWD") == NULL);
> + SELF_CHECK (env->get ("PWD") == NULL);
> +}
>
> - /* Check if unset followed by a set in an empty vector works. */
> - env.set ("PWD", "test");
> - SELF_CHECK (strcmp (env.get ("PWD"), "test") == 0);
> - /* The second element must be NULL. */
> - SELF_CHECK (env.envp ()[1] == NULL);
> - env.unset ("PWD");
> - SELF_CHECK (env.envp ()[0] == NULL);
> +/* Test initialization using the host's environ. */
>
> +static void
> +test_init_from_host_environ (gdb_environ *env)
> +{
> /* Initialize the environment vector using the host's environ. */
> - env = gdb_environ::from_host_environ ();
> + *env = gdb_environ::from_host_environ ();
> +
> + /* The user-set and user-unset lists must be empty. */
> + SELF_CHECK (env->user_set_env ().size () == 0);
> + SELF_CHECK (env->user_unset_env ().size () == 0);
>
> /* Our test environment variable should be present at the
> vector. */
> - SELF_CHECK (strcmp (env.get ("GDB_SELFTEST_ENVIRON"), "1") == 0);
> -
> - /* Set our test variable to another value. */
> - env.set ("GDB_SELFTEST_ENVIRON", "test");
> - SELF_CHECK (strcmp (env.get ("GDB_SELFTEST_ENVIRON"), "test") == 0);
> -
> - /* And unset our test variable. The variable still exists in the
> - host's environment, but doesn't exist in our vector. */
> - env.unset ("GDB_SELFTEST_ENVIRON");
> - SELF_CHECK (env.get ("GDB_SELFTEST_ENVIRON") == NULL);
> -
> - /* Re-set the test variable. */
> - env.set ("GDB_SELFTEST_ENVIRON", "1");
> - SELF_CHECK (strcmp (env.get ("GDB_SELFTEST_ENVIRON"), "1") == 0);
> + SELF_CHECK (strcmp (env->get ("GDB_SELFTEST_ENVIRON"), "1") == 0);
> +}
>
> - /* When we clear our environ vector, there should be only one
> - element on it (NULL), and we shouldn't be able to get our test
> - variable. */
> - env.clear ();
> - SELF_CHECK (env.envp ()[0] == NULL);
> - SELF_CHECK (env.get ("GDB_SELFTEST_ENVIRON") == NULL);
> +/* Test reinitialization using the host's environ. */
>
> +static void
> +test_reinit_from_host_environ (gdb_environ *env)
> +{
> /* Reinitialize our environ vector using the host environ. We
> should be able to see one (and only one) instance of the test
> variable. */
> - env = gdb_environ::from_host_environ ();
> - char **envp = env.envp ();
> + *env = gdb_environ::from_host_environ ();
> + char **envp = env->envp ();
> int num_found = 0;
>
> for (size_t i = 0; envp[i] != NULL; ++i)
> if (strcmp (envp[i], "GDB_SELFTEST_ENVIRON=1") == 0)
> ++num_found;
> SELF_CHECK (num_found == 1);
> +}
>
> - /* Get rid of our test variable. */
> - unsetenv ("GDB_SELFTEST_ENVIRON");
> +/* Test the case when we set a variable A, then set a variable B,
> + then unset A, and make sure that we cannot find A in the environ
> + vector, but can still find B. */
> +
> +static void
> +test_set_A_unset_B_unset_A_cannot_find_A_can_find_B (gdb_environ *env)
> +{
> + env->set ("GDB_SELFTEST_ENVIRON_1", "aaa");
> + SELF_CHECK (strcmp (env->get ("GDB_SELFTEST_ENVIRON_1"), "aaa") == 0);
> + /* User-set environ var list must contain one element. */
> + SELF_CHECK (env->user_set_env ().size () == 1);
> + SELF_CHECK (set_contains (env->user_set_env (),
> + std::string ("GDB_SELFTEST_ENVIRON_1=aaa")));
>
> - /* Test the case when we set a variable A, then set a variable B,
> - then unset A, and make sure that we cannot find A in the environ
> - vector, but can still find B. */
> - env.set ("GDB_SELFTEST_ENVIRON_1", "aaa");
> - SELF_CHECK (strcmp (env.get ("GDB_SELFTEST_ENVIRON_1"), "aaa") == 0);
> + env->set ("GDB_SELFTEST_ENVIRON_2", "bbb");
> + SELF_CHECK (strcmp (env->get ("GDB_SELFTEST_ENVIRON_2"), "bbb") == 0);
>
> - env.set ("GDB_SELFTEST_ENVIRON_2", "bbb");
> - SELF_CHECK (strcmp (env.get ("GDB_SELFTEST_ENVIRON_2"), "bbb") == 0);
> + env->unset ("GDB_SELFTEST_ENVIRON_1");
> + SELF_CHECK (env->get ("GDB_SELFTEST_ENVIRON_1") == NULL);
> + SELF_CHECK (strcmp (env->get ("GDB_SELFTEST_ENVIRON_2"), "bbb") == 0);
>
> - env.unset ("GDB_SELFTEST_ENVIRON_1");
> - SELF_CHECK (env.get ("GDB_SELFTEST_ENVIRON_1") == NULL);
> - SELF_CHECK (strcmp (env.get ("GDB_SELFTEST_ENVIRON_2"), "bbb") == 0);
> + /* The user-set environ var list must contain only one element
> + now. */
> + SELF_CHECK (set_contains (env->user_set_env (),
> + std::string ("GDB_SELFTEST_ENVIRON_2=bbb")));
> + SELF_CHECK (env->user_set_env ().size () == 1);
> +}
>
> - env.clear ();
> +/* Check if unset followed by a set in an empty vector works. */
>
> - /* Test that after a std::move the moved-from object is left at a
> - valid state (i.e., its only element is NULL). */
> - env.set ("A", "1");
> - SELF_CHECK (strcmp (env.get ("A"), "1") == 0);
> +static void
> +test_unset_set_empty_vector (gdb_environ *env)
> +{
> + env->set ("PWD", "test");
> + SELF_CHECK (strcmp (env->get ("PWD"), "test") == 0);
> + SELF_CHECK (set_contains (env->user_set_env (), std::string ("PWD=test")));
> + SELF_CHECK (env->user_unset_env ().size () == 0);
> + /* The second element must be NULL. */
> + SELF_CHECK (env->envp ()[1] == NULL);
> + SELF_CHECK (env->user_set_env ().size () == 1);
> + env->unset ("PWD");
> + SELF_CHECK (env->envp ()[0] == NULL);
> + SELF_CHECK (env->user_set_env ().size () == 0);
> + SELF_CHECK (env->user_unset_env ().size () == 1);
> + SELF_CHECK (set_contains (env->user_unset_env (), std::string ("PWD")));
> +}
> +
> +/* When we clear our environ vector, there should be only one
> + element on it (NULL), and we shouldn't be able to get our test
> + variable. */
> +
> +static void
> +test_vector_clear (gdb_environ *env)
> +{
> + env->clear ();
> + SELF_CHECK (env->envp ()[0] == NULL);
> + SELF_CHECK (env->user_set_env ().size () == 0);
> + SELF_CHECK (env->user_unset_env ().size () == 0);
> + SELF_CHECK (env->get ("GDB_SELFTEST_ENVIRON") == NULL);
> +}
> +
> +/* Test that after a std::move the moved-from object is left at a
> + valid state (i.e., its only element is NULL). */
> +
> +static void
> +test_std_move (gdb_environ *env)
> +{
> + env->set ("A", "1");
> + SELF_CHECK (strcmp (env->get ("A"), "1") == 0);
> + SELF_CHECK (set_contains (env->user_set_env (), std::string ("A=1")));
> + SELF_CHECK (env->user_set_env ().size () == 1);
> gdb_environ env2;
> - env2 = std::move (env);
> - SELF_CHECK (env.envp ()[0] == NULL);
> + env2 = std::move (*env);
> + SELF_CHECK (env->envp ()[0] == NULL);
> SELF_CHECK (strcmp (env2.get ("A"), "1") == 0);
> SELF_CHECK (env2.envp ()[1] == NULL);
> - env.set ("B", "2");
> - SELF_CHECK (strcmp (env.get ("B"), "2") == 0);
> - SELF_CHECK (env.envp ()[1] == NULL);
> + SELF_CHECK (env->user_set_env ().size () == 0);
> + SELF_CHECK (set_contains (env2.user_set_env (), std::string ("A=1")));
> + SELF_CHECK (env2.user_set_env ().size () == 1);
> + env->set ("B", "2");
> + SELF_CHECK (strcmp (env->get ("B"), "2") == 0);
> + SELF_CHECK (env->envp ()[1] == NULL);
> +}
>
> - /* Test that the move constructor leaves everything at a valid
> - state. */
> - env.clear ();
> - env.set ("A", "1");
> - SELF_CHECK (strcmp (env.get ("A"), "1") == 0);
> - gdb_environ env3 = std::move (env);
> - SELF_CHECK (env.envp ()[0] == NULL);
> +/* Test that the move constructor leaves everything at a valid
> + state. */
> +
> +static void
> +test_move_constructor (gdb_environ *env)
> +{
> + env->clear ();
> + env->set ("A", "1");
> + SELF_CHECK (strcmp (env->get ("A"), "1") == 0);
> + SELF_CHECK (set_contains (env->user_set_env (), std::string ("A=1")));
> + gdb_environ env3 = std::move (*env);
> + SELF_CHECK (env->envp ()[0] == NULL);
> + SELF_CHECK (env->user_set_env ().size () == 0);
> SELF_CHECK (strcmp (env3.get ("A"), "1") == 0);
> SELF_CHECK (env3.envp ()[1] == NULL);
> - env.set ("B", "2");
> - SELF_CHECK (strcmp (env.get ("B"), "2") == 0);
> - SELF_CHECK (env.envp ()[1] == NULL);
> + SELF_CHECK (set_contains (env3.user_set_env (), std::string ("A=1")));
> + SELF_CHECK (env3.user_set_env ().size () == 1);
> + env->set ("B", "2");
> + SELF_CHECK (strcmp (env->get ("B"), "2") == 0);
> + SELF_CHECK (env->envp ()[1] == NULL);
> + SELF_CHECK (set_contains (env->user_set_env (), std::string ("B=2")));
> + SELF_CHECK (env->user_set_env ().size () == 1);
> +}
> +
> +/* Test that self-moving works. */
>
> +static void
> +test_self_move (gdb_environ *env)
> +{
> /* Test self-move. */
> - env.clear ();
> - env.set ("A", "1");
> - SELF_CHECK (strcmp (env.get ("A"), "1") == 0);
> + env->clear ();
> + env->set ("A", "1");
> + SELF_CHECK (strcmp (env->get ("A"), "1") == 0);
> + SELF_CHECK (set_contains (env->user_set_env (), std::string ("A=1")));
> + SELF_CHECK (env->user_set_env ().size () == 1);
>
> /* Some compilers warn about moving to self, but that's precisely what we want
> to test here, so turn this warning off. */
> DIAGNOSTIC_PUSH
> DIAGNOSTIC_IGNORE_SELF_MOVE
> - env = std::move (env);
> + *env = std::move (*env);
> DIAGNOSTIC_POP
>
> - SELF_CHECK (strcmp (env.get ("A"), "1") == 0);
> - SELF_CHECK (strcmp (env.envp ()[0], "A=1") == 0);
> - SELF_CHECK (env.envp ()[1] == NULL);
> + SELF_CHECK (strcmp (env->get ("A"), "1") == 0);
> + SELF_CHECK (strcmp (env->envp ()[0], "A=1") == 0);
> + SELF_CHECK (env->envp ()[1] == NULL);
> + SELF_CHECK (set_contains (env->user_set_env (), std::string ("A=1")));
> + SELF_CHECK (env->user_set_env ().size () == 1);
> +}
> +
> +/* Test if set/unset/reset works. */
> +
> +static void
> +test_set_unset_reset (gdb_environ *env)
> +{
> + /* Set our test variable to another value. */
> + env->set ("GDB_SELFTEST_ENVIRON", "test");
> + SELF_CHECK (strcmp (env->get ("GDB_SELFTEST_ENVIRON"), "test") == 0);
> + SELF_CHECK (env->user_set_env ().size () == 1);
> + SELF_CHECK (env->user_unset_env ().size () == 0);
> +
> + /* And unset our test variable. The variable still exists in the
> + host's environment, but doesn't exist in our vector. */
> + env->unset ("GDB_SELFTEST_ENVIRON");
> + SELF_CHECK (env->get ("GDB_SELFTEST_ENVIRON") == NULL);
> + SELF_CHECK (env->user_unset_env ().size () == 1);
> + SELF_CHECK (set_contains (env->user_unset_env (),
> + std::string ("GDB_SELFTEST_ENVIRON")));
> +
> + /* Re-set the test variable. */
> + env->set ("GDB_SELFTEST_ENVIRON", "1");
> + SELF_CHECK (strcmp (env->get ("GDB_SELFTEST_ENVIRON"), "1") == 0);
> +}
> +
> +static void
> +run_tests ()
> +{
> + /* Set a test environment variable. This will be unset at the end
> + of this function. */
> + if (setenv ("GDB_SELFTEST_ENVIRON", "1", 1) != 0)
> + error (_("Could not set environment variable for testing."));
> +
> + gdb_environ env;
> +
> + test_vector_initialization (&env);
> +
> + test_unset_set_empty_vector (&env);
> +
> + test_init_from_host_environ (&env);
> +
> + test_set_unset_reset (&env);
> +
> + test_vector_clear (&env);
> +
> + test_reinit_from_host_environ (&env);
> +
> + /* Get rid of our test variable. We won't need it anymore. */
> + unsetenv ("GDB_SELFTEST_ENVIRON");
> +
> + test_set_A_unset_B_unset_A_cannot_find_A_can_find_B (&env);
> +
> + env.clear ();
> +
> + test_std_move (&env);
> +
> + test_move_constructor (&env);
> +
> + test_self_move (&env);
> }
> } /* namespace gdb_environ */
> } /* namespace selftests */
>
Hi Sergio,
The main point of splitting that long test into separate functions is to have each
of them independent from each other. So they should ideally not use the same environment
object. I suggest doing something like this (applied on top of your patch). I did this
super quickly, so please check that the tests still make sense :)
From 0a7d0381844d640792cc3f3051f643db8df42937 Mon Sep 17 00:00:00 2001
From: Simon Marchi <simon.marchi@ericsson.com>
Date: Tue, 29 Aug 2017 17:06:51 +0200
Subject: [PATCH] stuff
---
gdb/unittests/environ-selftests.c | 238 +++++++++++++++++++++-----------------
1 file changed, 130 insertions(+), 108 deletions(-)
diff --git a/gdb/unittests/environ-selftests.c b/gdb/unittests/environ-selftests.c
index e2d8c70..72a46af 100644
--- a/gdb/unittests/environ-selftests.c
+++ b/gdb/unittests/environ-selftests.c
@@ -22,6 +22,8 @@
#include "common/environ.h"
#include "common/diagnostics.h"
+static const char *gdb_selftest_env_var = "GDB_SELFTEST_ENVIRON";
+
static bool
set_contains (const std::set<std::string> &set, std::string key)
{
@@ -34,45 +36,48 @@ namespace gdb_environ_tests {
/* Test if the vector is initialized in a correct way. */
static void
-test_vector_initialization (gdb_environ *env)
+test_vector_initialization ()
{
+ gdb_environ env;
+
/* When the vector is initialized, there should always be one NULL
element in it. */
- SELF_CHECK (env->envp ()[0] == NULL);
- SELF_CHECK (env->user_set_env ().size () == 0);
- SELF_CHECK (env->user_unset_env ().size () == 0);
+ SELF_CHECK (env.envp ()[0] == NULL);
+ SELF_CHECK (env.user_set_env ().size () == 0);
+ SELF_CHECK (env.user_unset_env ().size () == 0);
/* Make sure that there is no other element. */
- SELF_CHECK (env->get ("PWD") == NULL);
+ SELF_CHECK (env.get ("PWD") == NULL);
}
/* Test initialization using the host's environ. */
static void
-test_init_from_host_environ (gdb_environ *env)
+test_init_from_host_environ ()
{
/* Initialize the environment vector using the host's environ. */
- *env = gdb_environ::from_host_environ ();
+ gdb_environ env = gdb_environ::from_host_environ ();
/* The user-set and user-unset lists must be empty. */
- SELF_CHECK (env->user_set_env ().size () == 0);
- SELF_CHECK (env->user_unset_env ().size () == 0);
+ SELF_CHECK (env.user_set_env ().size () == 0);
+ SELF_CHECK (env.user_unset_env ().size () == 0);
/* Our test environment variable should be present at the
vector. */
- SELF_CHECK (strcmp (env->get ("GDB_SELFTEST_ENVIRON"), "1") == 0);
+ SELF_CHECK (strcmp (env.get (gdb_selftest_env_var), "1") == 0);
}
/* Test reinitialization using the host's environ. */
static void
-test_reinit_from_host_environ (gdb_environ *env)
+test_reinit_from_host_environ ()
{
/* Reinitialize our environ vector using the host environ. We
should be able to see one (and only one) instance of the test
variable. */
- *env = gdb_environ::from_host_environ ();
- char **envp = env->envp ();
+ gdb_environ env = gdb_environ::from_host_environ ();
+ env = gdb_environ::from_host_environ ();
+ char **envp = env.envp ();
int num_found = 0;
for (size_t i = 0; envp[i] != NULL; ++i)
@@ -86,46 +91,50 @@ test_reinit_from_host_environ (gdb_environ *env)
vector, but can still find B. */
static void
-test_set_A_unset_B_unset_A_cannot_find_A_can_find_B (gdb_environ *env)
+test_set_A_unset_B_unset_A_cannot_find_A_can_find_B ()
{
- env->set ("GDB_SELFTEST_ENVIRON_1", "aaa");
- SELF_CHECK (strcmp (env->get ("GDB_SELFTEST_ENVIRON_1"), "aaa") == 0);
+ gdb_environ env;
+
+ env.set ("GDB_SELFTEST_ENVIRON_1", "aaa");
+ SELF_CHECK (strcmp (env.get ("GDB_SELFTEST_ENVIRON_1"), "aaa") == 0);
/* User-set environ var list must contain one element. */
- SELF_CHECK (env->user_set_env ().size () == 1);
- SELF_CHECK (set_contains (env->user_set_env (),
+ SELF_CHECK (env.user_set_env ().size () == 1);
+ SELF_CHECK (set_contains (env.user_set_env (),
std::string ("GDB_SELFTEST_ENVIRON_1=aaa")));
- env->set ("GDB_SELFTEST_ENVIRON_2", "bbb");
- SELF_CHECK (strcmp (env->get ("GDB_SELFTEST_ENVIRON_2"), "bbb") == 0);
+ env.set ("GDB_SELFTEST_ENVIRON_2", "bbb");
+ SELF_CHECK (strcmp (env.get ("GDB_SELFTEST_ENVIRON_2"), "bbb") == 0);
- env->unset ("GDB_SELFTEST_ENVIRON_1");
- SELF_CHECK (env->get ("GDB_SELFTEST_ENVIRON_1") == NULL);
- SELF_CHECK (strcmp (env->get ("GDB_SELFTEST_ENVIRON_2"), "bbb") == 0);
+ env.unset ("GDB_SELFTEST_ENVIRON_1");
+ SELF_CHECK (env.get ("GDB_SELFTEST_ENVIRON_1") == NULL);
+ SELF_CHECK (strcmp (env.get ("GDB_SELFTEST_ENVIRON_2"), "bbb") == 0);
/* The user-set environ var list must contain only one element
now. */
- SELF_CHECK (set_contains (env->user_set_env (),
+ SELF_CHECK (set_contains (env.user_set_env (),
std::string ("GDB_SELFTEST_ENVIRON_2=bbb")));
- SELF_CHECK (env->user_set_env ().size () == 1);
+ SELF_CHECK (env.user_set_env ().size () == 1);
}
/* Check if unset followed by a set in an empty vector works. */
static void
-test_unset_set_empty_vector (gdb_environ *env)
+test_unset_set_empty_vector ()
{
- env->set ("PWD", "test");
- SELF_CHECK (strcmp (env->get ("PWD"), "test") == 0);
- SELF_CHECK (set_contains (env->user_set_env (), std::string ("PWD=test")));
- SELF_CHECK (env->user_unset_env ().size () == 0);
+ gdb_environ env;
+
+ env.set ("PWD", "test");
+ SELF_CHECK (strcmp (env.get ("PWD"), "test") == 0);
+ SELF_CHECK (set_contains (env.user_set_env (), std::string ("PWD=test")));
+ SELF_CHECK (env.user_unset_env ().size () == 0);
/* The second element must be NULL. */
- SELF_CHECK (env->envp ()[1] == NULL);
- SELF_CHECK (env->user_set_env ().size () == 1);
- env->unset ("PWD");
- SELF_CHECK (env->envp ()[0] == NULL);
- SELF_CHECK (env->user_set_env ().size () == 0);
- SELF_CHECK (env->user_unset_env ().size () == 1);
- SELF_CHECK (set_contains (env->user_unset_env (), std::string ("PWD")));
+ SELF_CHECK (env.envp ()[1] == NULL);
+ SELF_CHECK (env.user_set_env ().size () == 1);
+ env.unset ("PWD");
+ SELF_CHECK (env.envp ()[0] == NULL);
+ SELF_CHECK (env.user_set_env ().size () == 0);
+ SELF_CHECK (env.user_unset_env ().size () == 1);
+ SELF_CHECK (set_contains (env.user_unset_env (), std::string ("PWD")));
}
/* When we clear our environ vector, there should be only one
@@ -133,110 +142,127 @@ test_unset_set_empty_vector (gdb_environ *env)
variable. */
static void
-test_vector_clear (gdb_environ *env)
+test_vector_clear ()
{
- env->clear ();
- SELF_CHECK (env->envp ()[0] == NULL);
- SELF_CHECK (env->user_set_env ().size () == 0);
- SELF_CHECK (env->user_unset_env ().size () == 0);
- SELF_CHECK (env->get ("GDB_SELFTEST_ENVIRON") == NULL);
+ gdb_environ env;
+
+ env.set (gdb_selftest_env_var, "1");
+
+ env.clear ();
+ SELF_CHECK (env.envp ()[0] == NULL);
+ SELF_CHECK (env.user_set_env ().size () == 0);
+ SELF_CHECK (env.user_unset_env ().size () == 0);
+ SELF_CHECK (env.get (gdb_selftest_env_var) == NULL);
}
/* Test that after a std::move the moved-from object is left at a
valid state (i.e., its only element is NULL). */
static void
-test_std_move (gdb_environ *env)
+test_std_move ()
{
- env->set ("A", "1");
- SELF_CHECK (strcmp (env->get ("A"), "1") == 0);
- SELF_CHECK (set_contains (env->user_set_env (), std::string ("A=1")));
- SELF_CHECK (env->user_set_env ().size () == 1);
+ gdb_environ env;
gdb_environ env2;
- env2 = std::move (*env);
- SELF_CHECK (env->envp ()[0] == NULL);
+
+ env.set ("A", "1");
+ SELF_CHECK (strcmp (env.get ("A"), "1") == 0);
+ SELF_CHECK (set_contains (env.user_set_env (), std::string ("A=1")));
+ SELF_CHECK (env.user_set_env ().size () == 1);
+
+ env2 = std::move (env);
+ SELF_CHECK (env.envp ()[0] == NULL);
SELF_CHECK (strcmp (env2.get ("A"), "1") == 0);
SELF_CHECK (env2.envp ()[1] == NULL);
- SELF_CHECK (env->user_set_env ().size () == 0);
+ SELF_CHECK (env.user_set_env ().size () == 0);
SELF_CHECK (set_contains (env2.user_set_env (), std::string ("A=1")));
SELF_CHECK (env2.user_set_env ().size () == 1);
- env->set ("B", "2");
- SELF_CHECK (strcmp (env->get ("B"), "2") == 0);
- SELF_CHECK (env->envp ()[1] == NULL);
+ env.set ("B", "2");
+ SELF_CHECK (strcmp (env.get ("B"), "2") == 0);
+ SELF_CHECK (env.envp ()[1] == NULL);
}
/* Test that the move constructor leaves everything at a valid
state. */
static void
-test_move_constructor (gdb_environ *env)
+test_move_constructor ()
{
- env->clear ();
- env->set ("A", "1");
- SELF_CHECK (strcmp (env->get ("A"), "1") == 0);
- SELF_CHECK (set_contains (env->user_set_env (), std::string ("A=1")));
- gdb_environ env3 = std::move (*env);
- SELF_CHECK (env->envp ()[0] == NULL);
- SELF_CHECK (env->user_set_env ().size () == 0);
- SELF_CHECK (strcmp (env3.get ("A"), "1") == 0);
- SELF_CHECK (env3.envp ()[1] == NULL);
- SELF_CHECK (set_contains (env3.user_set_env (), std::string ("A=1")));
- SELF_CHECK (env3.user_set_env ().size () == 1);
- env->set ("B", "2");
- SELF_CHECK (strcmp (env->get ("B"), "2") == 0);
- SELF_CHECK (env->envp ()[1] == NULL);
- SELF_CHECK (set_contains (env->user_set_env (), std::string ("B=2")));
- SELF_CHECK (env->user_set_env ().size () == 1);
+ gdb_environ env;
+
+ env.set ("A", "1");
+ SELF_CHECK (strcmp (env.get ("A"), "1") == 0);
+ SELF_CHECK (set_contains (env.user_set_env (), std::string ("A=1")));
+
+ gdb_environ env2 = std::move (env);
+ SELF_CHECK (env.envp ()[0] == NULL);
+ SELF_CHECK (env.user_set_env ().size () == 0);
+ SELF_CHECK (strcmp (env2.get ("A"), "1") == 0);
+ SELF_CHECK (env2.envp ()[1] == NULL);
+ SELF_CHECK (set_contains (env2.user_set_env (), std::string ("A=1")));
+ SELF_CHECK (env2.user_set_env ().size () == 1);
+
+ env.set ("B", "2");
+ SELF_CHECK (strcmp (env.get ("B"), "2") == 0);
+ SELF_CHECK (env.envp ()[1] == NULL);
+ SELF_CHECK (set_contains (env.user_set_env (), std::string ("B=2")));
+ SELF_CHECK (env.user_set_env ().size () == 1);
}
/* Test that self-moving works. */
static void
-test_self_move (gdb_environ *env)
+test_self_move ()
{
+ gdb_environ env;
+
/* Test self-move. */
- env->clear ();
- env->set ("A", "1");
- SELF_CHECK (strcmp (env->get ("A"), "1") == 0);
- SELF_CHECK (set_contains (env->user_set_env (), std::string ("A=1")));
- SELF_CHECK (env->user_set_env ().size () == 1);
+ env.set ("A", "1");
+ SELF_CHECK (strcmp (env.get ("A"), "1") == 0);
+ SELF_CHECK (set_contains (env.user_set_env (), std::string ("A=1")));
+ SELF_CHECK (env.user_set_env ().size () == 1);
/* Some compilers warn about moving to self, but that's precisely what we want
to test here, so turn this warning off. */
DIAGNOSTIC_PUSH
DIAGNOSTIC_IGNORE_SELF_MOVE
- *env = std::move (*env);
+ env = std::move (env);
DIAGNOSTIC_POP
- SELF_CHECK (strcmp (env->get ("A"), "1") == 0);
- SELF_CHECK (strcmp (env->envp ()[0], "A=1") == 0);
- SELF_CHECK (env->envp ()[1] == NULL);
- SELF_CHECK (set_contains (env->user_set_env (), std::string ("A=1")));
- SELF_CHECK (env->user_set_env ().size () == 1);
+ SELF_CHECK (strcmp (env.get ("A"), "1") == 0);
+ SELF_CHECK (strcmp (env.envp ()[0], "A=1") == 0);
+ SELF_CHECK (env.envp ()[1] == NULL);
+ SELF_CHECK (set_contains (env.user_set_env (), std::string ("A=1")));
+ SELF_CHECK (env.user_set_env ().size () == 1);
}
/* Test if set/unset/reset works. */
static void
-test_set_unset_reset (gdb_environ *env)
+test_set_unset_reset ()
{
+ gdb_environ env = gdb_environ::from_host_environ ();
+
+ /* Our test variable should already be present in the host's environment. */
+ SELF_CHECK (env.get ("GDB_SELFTEST_ENVIRON") != NULL);
+
/* Set our test variable to another value. */
- env->set ("GDB_SELFTEST_ENVIRON", "test");
- SELF_CHECK (strcmp (env->get ("GDB_SELFTEST_ENVIRON"), "test") == 0);
- SELF_CHECK (env->user_set_env ().size () == 1);
- SELF_CHECK (env->user_unset_env ().size () == 0);
+ env.set ("GDB_SELFTEST_ENVIRON", "test");
+ SELF_CHECK (strcmp (env.get ("GDB_SELFTEST_ENVIRON"), "test") == 0);
+ SELF_CHECK (env.user_set_env ().size () == 1);
+ SELF_CHECK (env.user_unset_env ().size () == 0);
/* And unset our test variable. The variable still exists in the
host's environment, but doesn't exist in our vector. */
- env->unset ("GDB_SELFTEST_ENVIRON");
- SELF_CHECK (env->get ("GDB_SELFTEST_ENVIRON") == NULL);
- SELF_CHECK (env->user_unset_env ().size () == 1);
- SELF_CHECK (set_contains (env->user_unset_env (),
+ env.unset ("GDB_SELFTEST_ENVIRON");
+ SELF_CHECK (env.get ("GDB_SELFTEST_ENVIRON") == NULL);
+ SELF_CHECK (env.user_set_env ().size () == 0);
+ SELF_CHECK (env.user_unset_env ().size () == 1);
+ SELF_CHECK (set_contains (env.user_unset_env (),
std::string ("GDB_SELFTEST_ENVIRON")));
/* Re-set the test variable. */
- env->set ("GDB_SELFTEST_ENVIRON", "1");
- SELF_CHECK (strcmp (env->get ("GDB_SELFTEST_ENVIRON"), "1") == 0);
+ env.set ("GDB_SELFTEST_ENVIRON", "1");
+ SELF_CHECK (strcmp (env.get ("GDB_SELFTEST_ENVIRON"), "1") == 0);
}
static void
@@ -247,32 +273,28 @@ run_tests ()
if (setenv ("GDB_SELFTEST_ENVIRON", "1", 1) != 0)
error (_("Could not set environment variable for testing."));
- gdb_environ env;
+ test_vector_initialization ();
- test_vector_initialization (&env);
+ test_unset_set_empty_vector ();
- test_unset_set_empty_vector (&env);
+ test_init_from_host_environ ();
- test_init_from_host_environ (&env);
+ test_set_unset_reset ();
- test_set_unset_reset (&env);
+ test_vector_clear ();
- test_vector_clear (&env);
-
- test_reinit_from_host_environ (&env);
+ test_reinit_from_host_environ ();
/* Get rid of our test variable. We won't need it anymore. */
unsetenv ("GDB_SELFTEST_ENVIRON");
- test_set_A_unset_B_unset_A_cannot_find_A_can_find_B (&env);
-
- env.clear ();
+ test_set_A_unset_B_unset_A_cannot_find_A_can_find_B ();
- test_std_move (&env);
+ test_std_move ();
- test_move_constructor (&env);
+ test_move_constructor ();
- test_self_move (&env);
+ test_self_move ();
}
} /* namespace gdb_environ */
} /* namespace selftests */
--
2.7.4
^ permalink raw reply [flat|nested] 35+ messages in thread
* Re: [PATCH v4] Implement the ability to set/unset environment variables to GDBserver when starting the inferior
2017-08-31 19:37 ` Simon Marchi
@ 2017-08-31 20:34 ` Sergio Durigan Junior
2017-08-31 20:39 ` Simon Marchi
0 siblings, 1 reply; 35+ messages in thread
From: Sergio Durigan Junior @ 2017-08-31 20:34 UTC (permalink / raw)
To: Simon Marchi; +Cc: GDB Patches, Pedro Alves, Simon Marchi
On Thursday, August 31 2017, Simon Marchi wrote:
>> diff --git a/gdb/unittests/environ-selftests.c b/gdb/unittests/environ-selftests.c
>> index 1e938e6746..e2d8c70985 100644
>> --- a/gdb/unittests/environ-selftests.c
>> +++ b/gdb/unittests/environ-selftests.c
>> @@ -22,132 +22,257 @@
>> #include "common/environ.h"
>> #include "common/diagnostics.h"
>>
>> +static bool
>> +set_contains (const std::set<std::string> &set, std::string key)
>> +{
>> + return set.find (key) != set.end ();
>> +}
>> +
>> namespace selftests {
>> namespace gdb_environ_tests {
>>
>> +/* Test if the vector is initialized in a correct way. */
>> +
>> static void
>> -run_tests ()
>> +test_vector_initialization (gdb_environ *env)
>> {
>> - /* Set a test environment variable. This will be unset at the end
>> - of this function. */
>> - if (setenv ("GDB_SELFTEST_ENVIRON", "1", 1) != 0)
>> - error (_("Could not set environment variable for testing."));
>> -
>> - gdb_environ env;
>> -
>> /* When the vector is initialized, there should always be one NULL
>> element in it. */
>> - SELF_CHECK (env.envp ()[0] == NULL);
>> + SELF_CHECK (env->envp ()[0] == NULL);
>> + SELF_CHECK (env->user_set_env ().size () == 0);
>> + SELF_CHECK (env->user_unset_env ().size () == 0);
>>
>> /* Make sure that there is no other element. */
>> - SELF_CHECK (env.get ("PWD") == NULL);
>> + SELF_CHECK (env->get ("PWD") == NULL);
>> +}
>>
>> - /* Check if unset followed by a set in an empty vector works. */
>> - env.set ("PWD", "test");
>> - SELF_CHECK (strcmp (env.get ("PWD"), "test") == 0);
>> - /* The second element must be NULL. */
>> - SELF_CHECK (env.envp ()[1] == NULL);
>> - env.unset ("PWD");
>> - SELF_CHECK (env.envp ()[0] == NULL);
>> +/* Test initialization using the host's environ. */
>>
>> +static void
>> +test_init_from_host_environ (gdb_environ *env)
>> +{
>> /* Initialize the environment vector using the host's environ. */
>> - env = gdb_environ::from_host_environ ();
>> + *env = gdb_environ::from_host_environ ();
>> +
>> + /* The user-set and user-unset lists must be empty. */
>> + SELF_CHECK (env->user_set_env ().size () == 0);
>> + SELF_CHECK (env->user_unset_env ().size () == 0);
>>
>> /* Our test environment variable should be present at the
>> vector. */
>> - SELF_CHECK (strcmp (env.get ("GDB_SELFTEST_ENVIRON"), "1") == 0);
>> -
>> - /* Set our test variable to another value. */
>> - env.set ("GDB_SELFTEST_ENVIRON", "test");
>> - SELF_CHECK (strcmp (env.get ("GDB_SELFTEST_ENVIRON"), "test") == 0);
>> -
>> - /* And unset our test variable. The variable still exists in the
>> - host's environment, but doesn't exist in our vector. */
>> - env.unset ("GDB_SELFTEST_ENVIRON");
>> - SELF_CHECK (env.get ("GDB_SELFTEST_ENVIRON") == NULL);
>> -
>> - /* Re-set the test variable. */
>> - env.set ("GDB_SELFTEST_ENVIRON", "1");
>> - SELF_CHECK (strcmp (env.get ("GDB_SELFTEST_ENVIRON"), "1") == 0);
>> + SELF_CHECK (strcmp (env->get ("GDB_SELFTEST_ENVIRON"), "1") == 0);
>> +}
>>
>> - /* When we clear our environ vector, there should be only one
>> - element on it (NULL), and we shouldn't be able to get our test
>> - variable. */
>> - env.clear ();
>> - SELF_CHECK (env.envp ()[0] == NULL);
>> - SELF_CHECK (env.get ("GDB_SELFTEST_ENVIRON") == NULL);
>> +/* Test reinitialization using the host's environ. */
>>
>> +static void
>> +test_reinit_from_host_environ (gdb_environ *env)
>> +{
>> /* Reinitialize our environ vector using the host environ. We
>> should be able to see one (and only one) instance of the test
>> variable. */
>> - env = gdb_environ::from_host_environ ();
>> - char **envp = env.envp ();
>> + *env = gdb_environ::from_host_environ ();
>> + char **envp = env->envp ();
>> int num_found = 0;
>>
>> for (size_t i = 0; envp[i] != NULL; ++i)
>> if (strcmp (envp[i], "GDB_SELFTEST_ENVIRON=1") == 0)
>> ++num_found;
>> SELF_CHECK (num_found == 1);
>> +}
>>
>> - /* Get rid of our test variable. */
>> - unsetenv ("GDB_SELFTEST_ENVIRON");
>> +/* Test the case when we set a variable A, then set a variable B,
>> + then unset A, and make sure that we cannot find A in the environ
>> + vector, but can still find B. */
>> +
>> +static void
>> +test_set_A_unset_B_unset_A_cannot_find_A_can_find_B (gdb_environ *env)
>> +{
>> + env->set ("GDB_SELFTEST_ENVIRON_1", "aaa");
>> + SELF_CHECK (strcmp (env->get ("GDB_SELFTEST_ENVIRON_1"), "aaa") == 0);
>> + /* User-set environ var list must contain one element. */
>> + SELF_CHECK (env->user_set_env ().size () == 1);
>> + SELF_CHECK (set_contains (env->user_set_env (),
>> + std::string ("GDB_SELFTEST_ENVIRON_1=aaa")));
>>
>> - /* Test the case when we set a variable A, then set a variable B,
>> - then unset A, and make sure that we cannot find A in the environ
>> - vector, but can still find B. */
>> - env.set ("GDB_SELFTEST_ENVIRON_1", "aaa");
>> - SELF_CHECK (strcmp (env.get ("GDB_SELFTEST_ENVIRON_1"), "aaa") == 0);
>> + env->set ("GDB_SELFTEST_ENVIRON_2", "bbb");
>> + SELF_CHECK (strcmp (env->get ("GDB_SELFTEST_ENVIRON_2"), "bbb") == 0);
>>
>> - env.set ("GDB_SELFTEST_ENVIRON_2", "bbb");
>> - SELF_CHECK (strcmp (env.get ("GDB_SELFTEST_ENVIRON_2"), "bbb") == 0);
>> + env->unset ("GDB_SELFTEST_ENVIRON_1");
>> + SELF_CHECK (env->get ("GDB_SELFTEST_ENVIRON_1") == NULL);
>> + SELF_CHECK (strcmp (env->get ("GDB_SELFTEST_ENVIRON_2"), "bbb") == 0);
>>
>> - env.unset ("GDB_SELFTEST_ENVIRON_1");
>> - SELF_CHECK (env.get ("GDB_SELFTEST_ENVIRON_1") == NULL);
>> - SELF_CHECK (strcmp (env.get ("GDB_SELFTEST_ENVIRON_2"), "bbb") == 0);
>> + /* The user-set environ var list must contain only one element
>> + now. */
>> + SELF_CHECK (set_contains (env->user_set_env (),
>> + std::string ("GDB_SELFTEST_ENVIRON_2=bbb")));
>> + SELF_CHECK (env->user_set_env ().size () == 1);
>> +}
>>
>> - env.clear ();
>> +/* Check if unset followed by a set in an empty vector works. */
>>
>> - /* Test that after a std::move the moved-from object is left at a
>> - valid state (i.e., its only element is NULL). */
>> - env.set ("A", "1");
>> - SELF_CHECK (strcmp (env.get ("A"), "1") == 0);
>> +static void
>> +test_unset_set_empty_vector (gdb_environ *env)
>> +{
>> + env->set ("PWD", "test");
>> + SELF_CHECK (strcmp (env->get ("PWD"), "test") == 0);
>> + SELF_CHECK (set_contains (env->user_set_env (), std::string ("PWD=test")));
>> + SELF_CHECK (env->user_unset_env ().size () == 0);
>> + /* The second element must be NULL. */
>> + SELF_CHECK (env->envp ()[1] == NULL);
>> + SELF_CHECK (env->user_set_env ().size () == 1);
>> + env->unset ("PWD");
>> + SELF_CHECK (env->envp ()[0] == NULL);
>> + SELF_CHECK (env->user_set_env ().size () == 0);
>> + SELF_CHECK (env->user_unset_env ().size () == 1);
>> + SELF_CHECK (set_contains (env->user_unset_env (), std::string ("PWD")));
>> +}
>> +
>> +/* When we clear our environ vector, there should be only one
>> + element on it (NULL), and we shouldn't be able to get our test
>> + variable. */
>> +
>> +static void
>> +test_vector_clear (gdb_environ *env)
>> +{
>> + env->clear ();
>> + SELF_CHECK (env->envp ()[0] == NULL);
>> + SELF_CHECK (env->user_set_env ().size () == 0);
>> + SELF_CHECK (env->user_unset_env ().size () == 0);
>> + SELF_CHECK (env->get ("GDB_SELFTEST_ENVIRON") == NULL);
>> +}
>> +
>> +/* Test that after a std::move the moved-from object is left at a
>> + valid state (i.e., its only element is NULL). */
>> +
>> +static void
>> +test_std_move (gdb_environ *env)
>> +{
>> + env->set ("A", "1");
>> + SELF_CHECK (strcmp (env->get ("A"), "1") == 0);
>> + SELF_CHECK (set_contains (env->user_set_env (), std::string ("A=1")));
>> + SELF_CHECK (env->user_set_env ().size () == 1);
>> gdb_environ env2;
>> - env2 = std::move (env);
>> - SELF_CHECK (env.envp ()[0] == NULL);
>> + env2 = std::move (*env);
>> + SELF_CHECK (env->envp ()[0] == NULL);
>> SELF_CHECK (strcmp (env2.get ("A"), "1") == 0);
>> SELF_CHECK (env2.envp ()[1] == NULL);
>> - env.set ("B", "2");
>> - SELF_CHECK (strcmp (env.get ("B"), "2") == 0);
>> - SELF_CHECK (env.envp ()[1] == NULL);
>> + SELF_CHECK (env->user_set_env ().size () == 0);
>> + SELF_CHECK (set_contains (env2.user_set_env (), std::string ("A=1")));
>> + SELF_CHECK (env2.user_set_env ().size () == 1);
>> + env->set ("B", "2");
>> + SELF_CHECK (strcmp (env->get ("B"), "2") == 0);
>> + SELF_CHECK (env->envp ()[1] == NULL);
>> +}
>>
>> - /* Test that the move constructor leaves everything at a valid
>> - state. */
>> - env.clear ();
>> - env.set ("A", "1");
>> - SELF_CHECK (strcmp (env.get ("A"), "1") == 0);
>> - gdb_environ env3 = std::move (env);
>> - SELF_CHECK (env.envp ()[0] == NULL);
>> +/* Test that the move constructor leaves everything at a valid
>> + state. */
>> +
>> +static void
>> +test_move_constructor (gdb_environ *env)
>> +{
>> + env->clear ();
>> + env->set ("A", "1");
>> + SELF_CHECK (strcmp (env->get ("A"), "1") == 0);
>> + SELF_CHECK (set_contains (env->user_set_env (), std::string ("A=1")));
>> + gdb_environ env3 = std::move (*env);
>> + SELF_CHECK (env->envp ()[0] == NULL);
>> + SELF_CHECK (env->user_set_env ().size () == 0);
>> SELF_CHECK (strcmp (env3.get ("A"), "1") == 0);
>> SELF_CHECK (env3.envp ()[1] == NULL);
>> - env.set ("B", "2");
>> - SELF_CHECK (strcmp (env.get ("B"), "2") == 0);
>> - SELF_CHECK (env.envp ()[1] == NULL);
>> + SELF_CHECK (set_contains (env3.user_set_env (), std::string ("A=1")));
>> + SELF_CHECK (env3.user_set_env ().size () == 1);
>> + env->set ("B", "2");
>> + SELF_CHECK (strcmp (env->get ("B"), "2") == 0);
>> + SELF_CHECK (env->envp ()[1] == NULL);
>> + SELF_CHECK (set_contains (env->user_set_env (), std::string ("B=2")));
>> + SELF_CHECK (env->user_set_env ().size () == 1);
>> +}
>> +
>> +/* Test that self-moving works. */
>>
>> +static void
>> +test_self_move (gdb_environ *env)
>> +{
>> /* Test self-move. */
>> - env.clear ();
>> - env.set ("A", "1");
>> - SELF_CHECK (strcmp (env.get ("A"), "1") == 0);
>> + env->clear ();
>> + env->set ("A", "1");
>> + SELF_CHECK (strcmp (env->get ("A"), "1") == 0);
>> + SELF_CHECK (set_contains (env->user_set_env (), std::string ("A=1")));
>> + SELF_CHECK (env->user_set_env ().size () == 1);
>>
>> /* Some compilers warn about moving to self, but that's precisely what we want
>> to test here, so turn this warning off. */
>> DIAGNOSTIC_PUSH
>> DIAGNOSTIC_IGNORE_SELF_MOVE
>> - env = std::move (env);
>> + *env = std::move (*env);
>> DIAGNOSTIC_POP
>>
>> - SELF_CHECK (strcmp (env.get ("A"), "1") == 0);
>> - SELF_CHECK (strcmp (env.envp ()[0], "A=1") == 0);
>> - SELF_CHECK (env.envp ()[1] == NULL);
>> + SELF_CHECK (strcmp (env->get ("A"), "1") == 0);
>> + SELF_CHECK (strcmp (env->envp ()[0], "A=1") == 0);
>> + SELF_CHECK (env->envp ()[1] == NULL);
>> + SELF_CHECK (set_contains (env->user_set_env (), std::string ("A=1")));
>> + SELF_CHECK (env->user_set_env ().size () == 1);
>> +}
>> +
>> +/* Test if set/unset/reset works. */
>> +
>> +static void
>> +test_set_unset_reset (gdb_environ *env)
>> +{
>> + /* Set our test variable to another value. */
>> + env->set ("GDB_SELFTEST_ENVIRON", "test");
>> + SELF_CHECK (strcmp (env->get ("GDB_SELFTEST_ENVIRON"), "test") == 0);
>> + SELF_CHECK (env->user_set_env ().size () == 1);
>> + SELF_CHECK (env->user_unset_env ().size () == 0);
>> +
>> + /* And unset our test variable. The variable still exists in the
>> + host's environment, but doesn't exist in our vector. */
>> + env->unset ("GDB_SELFTEST_ENVIRON");
>> + SELF_CHECK (env->get ("GDB_SELFTEST_ENVIRON") == NULL);
>> + SELF_CHECK (env->user_unset_env ().size () == 1);
>> + SELF_CHECK (set_contains (env->user_unset_env (),
>> + std::string ("GDB_SELFTEST_ENVIRON")));
>> +
>> + /* Re-set the test variable. */
>> + env->set ("GDB_SELFTEST_ENVIRON", "1");
>> + SELF_CHECK (strcmp (env->get ("GDB_SELFTEST_ENVIRON"), "1") == 0);
>> +}
>> +
>> +static void
>> +run_tests ()
>> +{
>> + /* Set a test environment variable. This will be unset at the end
>> + of this function. */
>> + if (setenv ("GDB_SELFTEST_ENVIRON", "1", 1) != 0)
>> + error (_("Could not set environment variable for testing."));
>> +
>> + gdb_environ env;
>> +
>> + test_vector_initialization (&env);
>> +
>> + test_unset_set_empty_vector (&env);
>> +
>> + test_init_from_host_environ (&env);
>> +
>> + test_set_unset_reset (&env);
>> +
>> + test_vector_clear (&env);
>> +
>> + test_reinit_from_host_environ (&env);
>> +
>> + /* Get rid of our test variable. We won't need it anymore. */
>> + unsetenv ("GDB_SELFTEST_ENVIRON");
>> +
>> + test_set_A_unset_B_unset_A_cannot_find_A_can_find_B (&env);
>> +
>> + env.clear ();
>> +
>> + test_std_move (&env);
>> +
>> + test_move_constructor (&env);
>> +
>> + test_self_move (&env);
>> }
>> } /* namespace gdb_environ */
>> } /* namespace selftests */
>>
>
> Hi Sergio,
>
> The main point of splitting that long test into separate functions is to have each
> of them independent from each other. So they should ideally not use the same environment
> object. I suggest doing something like this (applied on top of your patch). I did this
> super quickly, so please check that the tests still make sense :)
Ah, OK. I guess I didn't understand that, sorry.
Anyway, thanks for the patch. Yes, from what I have checked it covers
the existing tests. I'd probably even go further and do a
setenv/unsetenv inside each function that needs them, so as to really
isolate things.
Do you prefer me to apply your patch on top of mine and submit a v5?
Thanks,
--
Sergio
GPG key ID: 237A 54B1 0287 28BF 00EF 31F4 D0EB 7628 65FC 5E36
Please send encrypted e-mail if possible
http://sergiodj.net/
^ permalink raw reply [flat|nested] 35+ messages in thread
* Re: [PATCH v4] Implement the ability to set/unset environment variables to GDBserver when starting the inferior
2017-08-31 20:34 ` Sergio Durigan Junior
@ 2017-08-31 20:39 ` Simon Marchi
0 siblings, 0 replies; 35+ messages in thread
From: Simon Marchi @ 2017-08-31 20:39 UTC (permalink / raw)
To: Sergio Durigan Junior; +Cc: GDB Patches, Pedro Alves, Simon Marchi
On 2017-08-31 10:34 PM, Sergio Durigan Junior wrote:
> Anyway, thanks for the patch. Yes, from what I have checked it covers
> the existing tests. I'd probably even go further and do a
> setenv/unsetenv inside each function that needs them, so as to really
> isolate things.
>
> Do you prefer me to apply your patch on top of mine and submit a v5?
Yes, that would be best.
Simon
^ permalink raw reply [flat|nested] 35+ messages in thread
* [PATCH v5] Implement the ability to set/unset environment variables to GDBserver when starting the inferior
2017-06-29 19:41 [PATCH] Implement the ability to transmit environment variables to GDBserver when starting the inferior Sergio Durigan Junior
` (4 preceding siblings ...)
2017-08-28 23:13 ` [PATCH v4] " Sergio Durigan Junior
@ 2017-08-31 20:49 ` Sergio Durigan Junior
2017-08-31 21:03 ` Simon Marchi
2017-09-01 6:19 ` Eli Zaretskii
5 siblings, 2 replies; 35+ messages in thread
From: Sergio Durigan Junior @ 2017-08-31 20:49 UTC (permalink / raw)
To: GDB Patches; +Cc: Pedro Alves, Simon Marchi, Sergio Durigan Junior
Changes from v4:
- Applied Simon's patch to refactor the unittest. Thanks, Simon!
This patch implements the ability to set/unset environment variables
on the remote target, mimicking what GDB already offers to the user.
There are two features present here: user-set and user-unset
environment variables.
User-set environment variables are only the variables that are
explicitly set by the user, using the 'set environment' command. This
means that variables that were already present in the environment when
starting GDB/GDBserver are not transmitted/considered by this feature.
User-unset environment variables are variables that are explicitly
unset by the user, using the 'unset environment' command.
The idea behind this patch is to store user-set and user-unset
environment variables in two separate sets, both part of gdb_environ.
Then, when extended_remote_create_inferior is preparing to start the
inferior, it will iterate over the two sets and set/unset variables
accordingly. Three new packets are introduced:
- QEnvironmentHexEncoded, which is used to set environment variables,
and contains an hex-encoded string in the format "VAR=VALUE" (VALUE
can be empty if the user set a variable with a null value, by doing
'set environment VAR=').
- QEnvironmentUnset, which is used to unset environment variables, and
contains an hex-encoded string in the format "VAR".
- QEnvironmentReset, which is always the first packet to be
transmitted, and is used to reset the environment, i.e., discard any
changes made by the user on previous runs.
The QEnvironmentHexEncoded packet is inspired on LLDB's extensions to
the RSP. Details about it can be seen here:
<https://raw.githubusercontent.com/llvm-mirror/lldb/master/docs/lldb-gdb-remote.txt>
I decided not to implement the QEnvironment packet because it is
considered deprecated by LLDB. This packet, on LLDB, serves the same
purpose of QEnvironmentHexEncoded, but sends the information using a
plain text, non-hex-encoded string.
The other two packets are new.
This patch also includes updates to the documentation, testsuite, and
unit tests, without introducing regressions.
gdb/ChangeLog:
yyyy-mm-dd Sergio Durigan Junior <sergiodj@redhat.com>
* NEWS (Changes since GDB 8.0): Add entry mentioning new support
for setting/unsetting environment variables on the remote target.
(New remote packets): Add entries for QEnvironmentHexEncoded,
QEnvironmentUnset and QEnvironmentReset.
* common/environ.c (gdb_environ::operator=): Extend method to
handle m_user_set_env_list and m_user_unset_env_list.
(gdb_environ::clear): Likewise.
(match_var_in_string): Change type of first parameter from 'char
*' to 'const char *'.
(gdb_environ::set): Extend method to handle
m_user_set_env_list and m_user_unset_env_list.
(gdb_environ::unset): Likewise.
(gdb_environ::clear_user_set_env): New method.
(gdb_environ::user_set_envp): Likewise.
(gdb_environ::user_unset_envp): Likewise.
* common/environ.h (gdb_environ): Handle m_user_set_env_list and
m_user_unset_env_list on move constructor/assignment.
(unset): Add new default parameter 'update_unset_list = true'.
(clear_user_set_env): New method.
(user_set_envp): Likewise.
(user_unset_envp): Likewise.
(m_user_set_env_list): New std::set.
(m_user_unset_env_list): Likewise.
* common/rsp-low.c (hex2str): New function.
(bin2hex): New overload for bin2hex function.
* common/rsp-low.c (hex2str): New prototype.
(str2hex): New overload prototype.
* remote.c: Include "environ.h". Add QEnvironmentHexEncoded,
QEnvironmentUnset and QEnvironmentReset.
(remote_protocol_features): Add QEnvironmentHexEncoded,
QEnvironmentUnset and QEnvironmentReset packets.
(send_environment_packet): New function.
(extended_remote_environment_support): Likewise.
(extended_remote_create_inferior): Call
extended_remote_environment_support.
(_initialize_remote): Add QEnvironmentHexEncoded,
QEnvironmentUnset and QEnvironmentReset packet configs.
* unittests/environ-selftests.c (gdb_selftest_env_var):
New variable.
(test_vector_initialization): New function.
(test_init_from_host_environ): Likewise.
(test_reinit_from_host_environ): Likewise.
(test_set_A_unset_B_unset_A_cannot_find_A_can_find_B):
Likewise.
(test_unset_set_empty_vector): Likewise.
(test_vector_clear): Likewise.
(test_std_move): Likewise.
(test_move_constructor):
(test_self_move): Likewise.
(test_set_unset_reset): Likewise.
(run_tests): Rewrite in terms of the functions above.
gdb/gdbserver/ChangeLog:
yyyy-mm-dd Sergio Durigan Junior <sergiodj@redhat.com>
* server.c (handle_general_set): Handle QEnvironmentHexEncoded,
QEnvironmentUnset and QEnvironmentReset packets.
(handle_query): Inform remote that QEnvironmentHexEncoded,
QEnvironmentUnset and QEnvironmentReset are supported.
gdb/doc/ChangeLog:
yyyy-mm-dd Sergio Durigan Junior <sergiodj@redhat.com>
* gdb.texinfo (set environment): Add @anchor. Explain that
environment variables set by the user are sent to GDBserver.
(unset environment): Likewise, but for unsetting variables.
(Connecting) <Remote Packet>: Add "environment-hex-encoded",
"QEnvironmentHexEncoded", "environment-unset", "QEnvironmentUnset",
"environment-reset" and "QEnvironmentReset" to the table.
(Remote Protocol) <QEnvironmentHexEncoded, QEnvironmentUnset,
QEnvironmentReset>: New item, explaining the packet.
gdb/testsuite/ChangeLog:
yyyy-mm-dd Sergio Durigan Junior <sergiodj@redhat.com>
* gdb.base/share-env-with-gdbserver.c: New file.
* gdb.base/share-env-with-gdbserver.exp: Likewise.
---
gdb/NEWS | 28 +++
gdb/common/environ.c | 75 ++++--
gdb/common/environ.h | 24 +-
gdb/common/rsp-low.c | 41 ++++
gdb/common/rsp-low.h | 8 +
gdb/doc/gdb.texinfo | 116 ++++++++++
gdb/gdbserver/server.c | 65 +++++-
gdb/remote.c | 76 ++++++
gdb/testsuite/gdb.base/share-env-with-gdbserver.c | 40 ++++
.../gdb.base/share-env-with-gdbserver.exp | 255 +++++++++++++++++++++
gdb/unittests/environ-selftests.c | 244 ++++++++++++++++----
11 files changed, 907 insertions(+), 65 deletions(-)
create mode 100644 gdb/testsuite/gdb.base/share-env-with-gdbserver.c
create mode 100644 gdb/testsuite/gdb.base/share-env-with-gdbserver.exp
diff --git a/gdb/NEWS b/gdb/NEWS
index 735415495e..fda3064605 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -3,6 +3,18 @@
*** Changes since GDB 8.0
+* On Unix systems, GDB now supports transmitting environment variables
+ that are to be set or unset to GDBserver. These variables will
+ affect the environment to be passed to the remote inferior.
+
+ To inform GDB of environment variables that are to be transmitted to
+ GDBserver, use the "set environment" command. Only user set
+ environment variables are sent to GDBserver.
+
+ To inform GDB of environment variables that are to be unset before
+ the remote inferior is started by the GDBserver, use the "unset
+ environment" command.
+
* New features in the GDB remote stub, GDBserver
** New "--selftest" command line option runs some GDBserver self
@@ -17,8 +29,24 @@
"target remote", you can disable the startup with shell by using the
new "--no-startup-with-shell" GDBserver command line option.
+ ** On Unix systems, GDBserver now supports receiving environment
+ variables that are to be set or unset from GDB. These variables
+ will affect the environment to be passed to the inferior.
+
* New remote packets
+QEnvironmentHexEncoded
+ Inform GDBserver of an environment variable that is to be passed to
+ the inferior when starting it.
+
+QEnvironmentUnset
+ Inform GDBserver of an environment variable that is to be unset
+ before starting the remote inferior.
+
+QEnvironmentReset
+ Inform GDBserver that the environment should be reset (i.e.,
+ user-set environment variables should be unset).
+
QStartupWithShell
Indicates whether the inferior must be started with a shell or not.
diff --git a/gdb/common/environ.c b/gdb/common/environ.c
index 698bda3ac1..7753f6ebca 100644
--- a/gdb/common/environ.c
+++ b/gdb/common/environ.c
@@ -30,8 +30,12 @@ gdb_environ::operator= (gdb_environ &&e)
return *this;
m_environ_vector = std::move (e.m_environ_vector);
+ m_user_set_env = std::move (e.m_user_set_env);
+ m_user_unset_env = std::move (e.m_user_unset_env);
e.m_environ_vector.clear ();
e.m_environ_vector.push_back (NULL);
+ e.m_user_set_env.clear ();
+ e.m_user_unset_env.clear ();
return *this;
}
@@ -65,6 +69,8 @@ gdb_environ::clear ()
m_environ_vector.clear ();
/* Always add the NULL element. */
m_environ_vector.push_back (NULL);
+ m_user_set_env.clear ();
+ m_user_unset_env.clear ();
}
/* Helper function to check if STRING contains an environment variable
@@ -72,7 +78,7 @@ gdb_environ::clear ()
if it contains, false otherwise. */
static bool
-match_var_in_string (char *string, const char *var, size_t var_len)
+match_var_in_string (const char *string, const char *var, size_t var_len)
{
if (strncmp (string, var, var_len) == 0 && string[var_len] == '=')
return true;
@@ -99,32 +105,59 @@ gdb_environ::get (const char *var) const
void
gdb_environ::set (const char *var, const char *value)
{
+ char *fullvar = concat (var, "=", value, NULL);
+
/* We have to unset the variable in the vector if it exists. */
- unset (var);
+ unset (var, false);
/* Insert the element before the last one, which is always NULL. */
- m_environ_vector.insert (m_environ_vector.end () - 1,
- concat (var, "=", value, NULL));
+ m_environ_vector.insert (m_environ_vector.end () - 1, fullvar);
+
+ /* Mark this environment variable as having been set by the user.
+ This will be useful when we deal with setting environment
+ variables on the remote target. */
+ m_user_set_env.insert (std::string (fullvar));
+
+ /* If this environment variable is marked as unset by the user, then
+ remove it from the list, because now the user wants to set
+ it. */
+ m_user_unset_env.erase (std::string (var));
}
/* See common/environ.h. */
void
-gdb_environ::unset (const char *var)
+gdb_environ::unset (const char *var, bool update_unset_list)
{
size_t len = strlen (var);
+ std::vector<char *>::iterator it_env;
/* We iterate until '.end () - 1' because the last element is
always NULL. */
- for (std::vector<char *>::iterator el = m_environ_vector.begin ();
- el != m_environ_vector.end () - 1;
- ++el)
- if (match_var_in_string (*el, var, len))
- {
- xfree (*el);
- m_environ_vector.erase (el);
- break;
- }
+ for (it_env = m_environ_vector.begin ();
+ it_env != m_environ_vector.end () - 1;
+ ++it_env)
+ if (match_var_in_string (*it_env, var, len))
+ break;
+
+ if (it_env != m_environ_vector.end () - 1)
+ {
+ m_user_set_env.erase (std::string (*it_env));
+ xfree (*it_env);
+
+ m_environ_vector.erase (it_env);
+ }
+
+ if (update_unset_list)
+ m_user_unset_env.insert (std::string (var));
+}
+
+/* See common/environ.h. */
+
+void
+gdb_environ::unset (const char *var)
+{
+ unset (var, true);
}
/* See common/environ.h. */
@@ -134,3 +167,17 @@ gdb_environ::envp () const
{
return const_cast<char **> (&m_environ_vector[0]);
}
+
+/* See common/environ.h. */
+
+const std::set<std::string> &
+gdb_environ::user_set_env () const
+{
+ return m_user_set_env;
+}
+
+const std::set<std::string> &
+gdb_environ::user_unset_env () const
+{
+ return m_user_unset_env;
+}
diff --git a/gdb/common/environ.h b/gdb/common/environ.h
index 0bbb191361..4003f4eee8 100644
--- a/gdb/common/environ.h
+++ b/gdb/common/environ.h
@@ -18,6 +18,7 @@
#define ENVIRON_H 1
#include <vector>
+#include <set>
/* Class that represents the environment variables as seen by the
inferior. */
@@ -41,12 +42,16 @@ public:
/* Move constructor. */
gdb_environ (gdb_environ &&e)
- : m_environ_vector (std::move (e.m_environ_vector))
+ : m_environ_vector (std::move (e.m_environ_vector)),
+ m_user_set_env (std::move (e.m_user_set_env)),
+ m_user_unset_env (std::move (e.m_user_unset_env))
{
/* Make sure that the moved-from vector is left at a valid
state (only one NULL element). */
e.m_environ_vector.clear ();
e.m_environ_vector.push_back (NULL);
+ e.m_user_set_env.clear ();
+ e.m_user_unset_env.clear ();
}
/* Move assignment. */
@@ -73,9 +78,26 @@ public:
/* Return the environment vector represented as a 'char **'. */
char **envp () const;
+ /* Return the user-set environment vector. */
+ const std::set<std::string> &user_set_env () const;
+
+ /* Return the user-unset environment vector. */
+ const std::set<std::string> &user_unset_env () const;
+
private:
+ /* Unset VAR in environment. If UPDATE_UNSET_LIST is true, then
+ also update M_USER_UNSET_ENV to reflect the unsetting of the
+ environment variable. */
+ void unset (const char *var, bool update_unset_list);
+
/* A vector containing the environment variables. */
std::vector<char *> m_environ_vector;
+
+ /* The environment variables explicitly set by the user. */
+ std::set<std::string> m_user_set_env;
+
+ /* The environment variables explicitly unset by the user. */
+ std::set<std::string> m_user_unset_env;
};
#endif /* defined (ENVIRON_H) */
diff --git a/gdb/common/rsp-low.c b/gdb/common/rsp-low.c
index eb85ab5701..4befeb1dd8 100644
--- a/gdb/common/rsp-low.c
+++ b/gdb/common/rsp-low.c
@@ -132,6 +132,30 @@ hex2bin (const char *hex, gdb_byte *bin, int count)
/* See rsp-low.h. */
+std::string
+hex2str (const char *hex)
+{
+ std::string ret;
+ size_t len = strlen (hex);
+
+ ret.reserve (len / 2);
+ for (size_t i = 0; i < len; ++i)
+ {
+ if (hex[0] == '\0' || hex[1] == '\0')
+ {
+ /* Hex string is short, or of uneven length. Return what we
+ have so far. */
+ return ret;
+ }
+ ret += fromhex (hex[0]) * 16 + fromhex (hex[1]);
+ hex += 2;
+ }
+
+ return ret;
+}
+
+/* See rsp-low.h. */
+
int
bin2hex (const gdb_byte *bin, char *hex, int count)
{
@@ -146,6 +170,23 @@ bin2hex (const gdb_byte *bin, char *hex, int count)
return i;
}
+/* See rsp-low.h. */
+
+std::string
+bin2hex (const gdb_byte *bin, int count)
+{
+ std::string ret;
+
+ ret.reserve (count * 2);
+ for (int i = 0; i < count; ++i)
+ {
+ ret += tohex ((*bin >> 4) & 0xf);
+ ret += tohex (*bin++ & 0xf);
+ }
+
+ return ret;
+}
+
/* Return whether byte B needs escaping when sent as part of binary data. */
static int
diff --git a/gdb/common/rsp-low.h b/gdb/common/rsp-low.h
index b57f58bc75..2b3685f0f0 100644
--- a/gdb/common/rsp-low.h
+++ b/gdb/common/rsp-low.h
@@ -52,6 +52,10 @@ extern char *unpack_varlen_hex (char *buff, ULONGEST *result);
extern int hex2bin (const char *hex, gdb_byte *bin, int count);
+/* Like hex2bin, but return a std::string. */
+
+extern std::string hex2str (const char *hex);
+
/* Convert some bytes to a hexadecimal representation. BIN holds the
bytes to convert. COUNT says how many bytes to convert. The
resulting characters are stored in HEX, followed by a NUL
@@ -59,6 +63,10 @@ extern int hex2bin (const char *hex, gdb_byte *bin, int count);
extern int bin2hex (const gdb_byte *bin, char *hex, int count);
+/* Overloaded version of bin2hex that returns a std::string. */
+
+extern std::string bin2hex (const gdb_byte *bin, int count);
+
/* Convert BUFFER, binary data at least LEN_UNITS addressable memory units
long, into escaped binary data in OUT_BUF. Only copy memory units that fit
completely in OUT_BUF. Set *OUT_LEN_UNITS to the number of units from
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index d977b234d0..874cdebc11 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -2363,6 +2363,7 @@ print the names and values of all environment variables to be given to
your program. You can abbreviate @code{environment} as @code{env}.
@kindex set environment
+@anchor{set environment}
@item set environment @var{varname} @r{[}=@var{value}@r{]}
Set environment variable @var{varname} to @var{value}. The value
changes for your program (and the shell @value{GDBN} uses to launch
@@ -2391,12 +2392,21 @@ If necessary, you can avoid that by using the @samp{env} program as a
wrapper instead of using @code{set environment}. @xref{set
exec-wrapper}, for an example doing just that.
+Environment variables that are set by the user are also transmitted to
+@command{gdbserver} to be used when starting the remote inferior.
+@pxref{QEnvironmentHexEncoded}.
+
@kindex unset environment
+@anchor{unset environment}
@item unset environment @var{varname}
Remove variable @var{varname} from the environment to be passed to your
program. This is different from @samp{set env @var{varname} =};
@code{unset environment} removes the variable from the environment,
rather than assigning it an empty value.
+
+Environment variables that are unset by the user are also unset on
+@command{gdbserver} when starting the remote inferior.
+@pxref{QEnvironmentUnset}.
@end table
@emph{Warning:} On Unix systems, @value{GDBN} runs your program using
@@ -20849,6 +20859,18 @@ are:
@tab @code{QStartupWithShell}
@tab @code{set startup-with-shell}
+@item @code{environment-hex-encoded}
+@tab @code{QEnvironmentHexEncoded}
+@tab @code{set environment}
+
+@item @code{environment-unset}
+@tab @code{QEnvironmentUnset}
+@tab @code{unset environment}
+
+@item @code{environment-reset}
+@tab @code{QEnvironmentReset}
+@tab @code{Reset the inferior environment (i.e., unset user-set variables)}
+
@item @code{conditional-breakpoints-packet}
@tab @code{Z0 and Z1}
@tab @code{Support for target-side breakpoint condition evaluation}
@@ -36604,6 +36626,100 @@ actually support starting the inferior using a shell.
Use of this packet is controlled by the @code{set startup-with-shell}
command; @pxref{set startup-with-shell}.
+@item QEnvironmentHexEncoded:@var{hex-value}
+@anchor{QEnvironmentHexEncoded}
+@cindex set environment variable, remote request
+@cindex @samp{QEnvironmentHexEncoded} packet
+On UNIX-like targets, it is possible to set environment variables that
+will be passed to the inferior during the startup process. This
+packet is used to inform @command{gdbserver} of an environment
+variable that has been defined by the user on @value{GDBN} (@pxref{set
+environment}).
+
+The packet is composed by @var{hex-value}, an hex encoded
+representation of the @var{name=value} format representing an
+environment variable. The name of the environment variable is
+represented by @var{name}, and the value to be assigned to the
+environment variable is represented by @var{value}. If the variable
+has no value (i.e., the value is @code{null}), then @var{value} will
+not be present.
+
+This packet is only available in extended mode (@pxref{extended
+mode}).
+
+Reply:
+@table @samp
+@item OK
+The request succeeded.
+@end table
+
+This packet is not probed by default; the remote stub must request it,
+by supplying an appropriate @samp{qSupported} response
+(@pxref{qSupported}). This should only be done on targets that
+actually support passing environment variables to the starting
+inferior.
+
+This packet is related to the @code{set environment} command;
+@pxref{set environment}.
+
+@item QEnvironmentUnset:@var{hex-value}
+@anchor{QEnvironmentUnset}
+@cindex unset environment variable, remote request
+@cindex @samp{QEnvironmentUnset} packet
+On UNIX-like targets, it is possible to unset environment variables
+before starting the inferior in the remote target. This packet is
+used to inform @command{gdbserver} of an environment variable that has
+been unset by the user on @value{GDBN} (@pxref{unset environment}).
+
+The packet is composed by @var{hex-value}, an hex encoded
+representation of the name of the environment variable to be unset.
+
+This packet is only available in extended mode (@pxref{extended
+mode}).
+
+Reply:
+@table @samp
+@item OK
+The request succeeded.
+@end table
+
+This packet is not probed by default; the remote stub must request it,
+by supplying an appropriate @samp{qSupported} response
+(@pxref{qSupported}). This should only be done on targets that
+actually support passing environment variables to the starting
+inferior.
+
+This packet is related to the @code{unset environment} command;
+@pxref{unset environment}.
+
+@item QEnvironmentReset
+@anchor{QEnvironmentReset}
+@cindex reset environment, remote request
+@cindex @samp{QEnvironmentReset} packet
+On UNIX-like targets, this packet is used to reset the state of
+environment variables in the remote target before starting the
+inferior. In this context, reset means unsetting all environment
+variables that were previously set by the user (i.e., were not
+initially present in the environment). It is sent to
+@command{gdbserver} before the @samp{QEnvironmentHexEncoded}
+(@pxref{QEnvironmentHexEncoded}) and the @samp{QEnvironmentUnset}
+(@pxref{QEnvironmentUnset}) packets.
+
+This packet is only available in extended mode (@pxref{extended
+mode}).
+
+Reply:
+@table @samp
+@item OK
+The request succeeded.
+@end table
+
+This packet is not probed by default; the remote stub must request it,
+by supplying an appropriate @samp{qSupported} response
+(@pxref{qSupported}). This should only be done on targets that
+actually support passing environment variables to the starting
+inferior.
+
@item qfThreadInfo
@itemx qsThreadInfo
@cindex list active threads, remote request
diff --git a/gdb/gdbserver/server.c b/gdb/gdbserver/server.c
index 8200aa17c8..88a5ddc393 100644
--- a/gdb/gdbserver/server.c
+++ b/gdb/gdbserver/server.c
@@ -633,6 +633,67 @@ handle_general_set (char *own_buf)
return;
}
+ if (strcmp (own_buf, "QEnvironmentReset") == 0)
+ {
+ our_environ = gdb_environ::from_host_environ ();
+
+ write_ok (own_buf);
+ return;
+ }
+
+ if (startswith (own_buf, "QEnvironmentHexEncoded:"))
+ {
+ const char *p = own_buf + sizeof ("QEnvironmentHexEncoded:") - 1;
+ /* The final form of the environment variable. FINAL_VAR will
+ hold the 'VAR=VALUE' format. */
+ std::string final_var = hex2str (p);
+ std::string var_name, var_value;
+
+ if (remote_debug)
+ {
+ debug_printf (_("[QEnvironmentHexEncoded received '%s']\n"), p);
+ debug_printf (_("[Environment variable to be set: '%s']\n"),
+ final_var.c_str ());
+ debug_flush ();
+ }
+
+ size_t pos = final_var.find ('=');
+ if (pos == std::string::npos)
+ {
+ warning (_("Unexpected format for environment variable: '%s'"),
+ final_var.c_str ());
+ write_enn (own_buf);
+ return;
+ }
+
+ var_name = final_var.substr (0, pos);
+ var_value = final_var.substr (pos + 1, std::string::npos);
+
+ our_environ.set (var_name.c_str (), var_value.c_str ());
+
+ write_ok (own_buf);
+ return;
+ }
+
+ if (startswith (own_buf, "QEnvironmentUnset:"))
+ {
+ const char *p = own_buf + sizeof ("QEnvironmentUnset:") - 1;
+ std::string varname = hex2str (p);
+
+ if (remote_debug)
+ {
+ debug_printf (_("[QEnvironmentUnset received '%s']\n"), p);
+ debug_printf (_("[Environment variable to be unset: '%s']\n"),
+ varname.c_str ());
+ debug_flush ();
+ }
+
+ our_environ.unset (varname.c_str ());
+
+ write_ok (own_buf);
+ return;
+ }
+
if (strcmp (own_buf, "QStartNoAckMode") == 0)
{
if (remote_debug)
@@ -2230,7 +2291,9 @@ handle_query (char *own_buf, int packet_len, int *new_packet_len_p)
}
sprintf (own_buf,
- "PacketSize=%x;QPassSignals+;QProgramSignals+;QStartupWithShell+",
+ "PacketSize=%x;QPassSignals+;QProgramSignals+;"
+ "QStartupWithShell+;QEnvironmentHexEncoded+;"
+ "QEnvironmentReset+;QEnvironmentUnset+",
PBUFSIZ - 1);
if (target_supports_catch_syscall ())
diff --git a/gdb/remote.c b/gdb/remote.c
index 2249533258..3347dd8022 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -73,6 +73,7 @@
#include "record-btrace.h"
#include <algorithm>
#include "common/scoped_restore.h"
+#include "environ.h"
/* Temp hacks for tracepoint encoding migration. */
static char *target_buf;
@@ -1430,6 +1431,9 @@ enum {
PACKET_QCatchSyscalls,
PACKET_QProgramSignals,
PACKET_QStartupWithShell,
+ PACKET_QEnvironmentHexEncoded,
+ PACKET_QEnvironmentReset,
+ PACKET_QEnvironmentUnset,
PACKET_qCRC,
PACKET_qSearch_memory,
PACKET_vAttach,
@@ -4637,6 +4641,12 @@ static const struct protocol_feature remote_protocol_features[] = {
PACKET_QProgramSignals },
{ "QStartupWithShell", PACKET_DISABLE, remote_supported_packet,
PACKET_QStartupWithShell },
+ { "QEnvironmentHexEncoded", PACKET_DISABLE, remote_supported_packet,
+ PACKET_QEnvironmentHexEncoded },
+ { "QEnvironmentReset", PACKET_DISABLE, remote_supported_packet,
+ PACKET_QEnvironmentReset },
+ { "QEnvironmentUnset", PACKET_DISABLE, remote_supported_packet,
+ PACKET_QEnvironmentUnset },
{ "QStartNoAckMode", PACKET_DISABLE, remote_supported_packet,
PACKET_QStartNoAckMode },
{ "multiprocess", PACKET_DISABLE, remote_supported_packet,
@@ -9556,6 +9566,57 @@ extended_remote_run (const std::string &args)
}
}
+/* Helper function to send set/unset environment packets. ACTION is
+ either "set" or "unset". PACKET is either "QEnvironmentHexEncoded"
+ or "QEnvironmentUnsetVariable". VALUE is the variable to be
+ sent. */
+
+static void
+send_environment_packet (struct remote_state *rs,
+ const char *action,
+ const char *packet,
+ const char *value)
+{
+ /* Convert the environment variable to an hex string, which
+ is the best format to be transmitted over the wire. */
+ std::string encoded_value = bin2hex ((const gdb_byte *) value,
+ strlen (value));
+
+ xsnprintf (rs->buf, get_remote_packet_size (),
+ "%s:%s", packet, encoded_value.c_str ());
+
+ putpkt (rs->buf);
+ getpkt (&rs->buf, &rs->buf_size, 0);
+ if (strcmp (rs->buf, "OK") != 0)
+ warning (_("Unable to %s environment variable '%s' on remote."),
+ action, value);
+}
+
+/* Helper function to handle the QEnvironment* packets. */
+
+static void
+extended_remote_environment_support (struct remote_state *rs)
+{
+ if (packet_support (PACKET_QEnvironmentReset) != PACKET_DISABLE)
+ {
+ putpkt ("QEnvironmentReset");
+ getpkt (&rs->buf, &rs->buf_size, 0);
+ if (strcmp (rs->buf, "OK") != 0)
+ warning (_("Unable to reset environment on remote."));
+ }
+
+ gdb_environ *e = ¤t_inferior ()->environment;
+
+ if (packet_support (PACKET_QEnvironmentHexEncoded) != PACKET_DISABLE)
+ for (const std::string &el : e->user_set_env ())
+ send_environment_packet (rs, "set", "QEnvironmentHexEncoded",
+ el.c_str ());
+
+ if (packet_support (PACKET_QEnvironmentUnset) != PACKET_DISABLE)
+ for (const std::string &el : e->user_unset_env ())
+ send_environment_packet (rs, "unset", "QEnvironmentUnset", el.c_str ());
+}
+
/* In the extended protocol we want to be able to do things like
"run" and have them basically work as expected. So we need
a special create_inferior function. We support changing the
@@ -9596,6 +9657,8 @@ Remote replied unexpectedly while setting startup-with-shell: %s"),
rs->buf);
}
+ extended_remote_environment_support (rs);
+
/* Now restart the remote server. */
run_worked = extended_remote_run (args) != -1;
if (!run_worked)
@@ -14067,6 +14130,19 @@ Show the maximum size of the address (in bits) in a memory packet."), NULL,
add_packet_config_cmd (&remote_protocol_packets[PACKET_QStartupWithShell],
"QStartupWithShell", "startup-with-shell", 0);
+ add_packet_config_cmd (&remote_protocol_packets
+ [PACKET_QEnvironmentHexEncoded],
+ "QEnvironmentHexEncoded", "environment-hex-encoded",
+ 0);
+
+ add_packet_config_cmd (&remote_protocol_packets[PACKET_QEnvironmentReset],
+ "QEnvironmentReset", "environment-reset",
+ 0);
+
+ add_packet_config_cmd (&remote_protocol_packets[PACKET_QEnvironmentUnset],
+ "QEnvironmentUnset", "environment-unset",
+ 0);
+
add_packet_config_cmd (&remote_protocol_packets[PACKET_qSymbol],
"qSymbol", "symbol-lookup", 0);
diff --git a/gdb/testsuite/gdb.base/share-env-with-gdbserver.c b/gdb/testsuite/gdb.base/share-env-with-gdbserver.c
new file mode 100644
index 0000000000..740bd0fb52
--- /dev/null
+++ b/gdb/testsuite/gdb.base/share-env-with-gdbserver.c
@@ -0,0 +1,40 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+ Copyright 2017 Free Software Foundation, Inc.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+/* Wrapper around getenv for GDB. */
+
+static const char *
+my_getenv (const char *name)
+{
+ return getenv (name);
+}
+
+int
+main (int argc, char *argv[])
+{
+ const char *myvar = getenv ("GDB_TEST_VAR");
+
+ if (myvar != NULL)
+ printf ("It worked! myvar = '%s'\n", myvar);
+ else
+ printf ("It failed.");
+
+ return 0; /* break-here */
+}
diff --git a/gdb/testsuite/gdb.base/share-env-with-gdbserver.exp b/gdb/testsuite/gdb.base/share-env-with-gdbserver.exp
new file mode 100644
index 0000000000..7521be1e99
--- /dev/null
+++ b/gdb/testsuite/gdb.base/share-env-with-gdbserver.exp
@@ -0,0 +1,255 @@
+# This testcase is part of GDB, the GNU debugger.
+
+# Copyright 2017 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+# This test doesn't make sense on native-gdbserver.
+if { [use_gdb_stub] } {
+ untested "not supported"
+ return
+}
+
+standard_testfile
+
+if { [prepare_for_testing "failed to prepare" $testfile $srcfile debug] } {
+ return -1
+}
+
+set test_var_name "GDB_TEST_VAR"
+
+# Helper function that performs a check on the output of "getenv".
+#
+# - VAR_NAME is the name of the variable to be checked.
+#
+# - VAR_VALUE is the value expected.
+#
+# - TEST_MSG, if not empty, is the test message to be used by the
+# "gdb_test".
+#
+# - EMPTY_VAR_P, if non-zero, means that the variable is not expected
+# to exist. In this case, VAR_VALUE is not considered.
+
+proc check_getenv { var_name var_value { test_msg "" } { empty_var_p 0 } } {
+ global hex decimal
+
+ if { $test_msg == "" } {
+ set test_msg "print result of getenv for $var_name"
+ }
+
+ if { $empty_var_p } {
+ set var_value_match "0x0"
+ } else {
+ set var_value_match "$hex \"$var_value\""
+ }
+
+ gdb_test "print my_getenv (\"$var_name\")" "\\\$$decimal = $var_value_match" \
+ $test_msg
+}
+
+# Helper function to re-run to main and breaking at the "break-here"
+# label.
+
+proc do_prepare_inferior { } {
+ global decimal hex
+
+ if { ![runto_main] } {
+ return -1
+ }
+
+ gdb_breakpoint [gdb_get_line_number "break-here"]
+
+ gdb_test "continue" "Breakpoint $decimal, main \\\(argc=1, argv=$hex\\\) at.*" \
+ "continue until breakpoint"
+}
+
+# Helper function that does the actual testing.
+#
+# - VAR_VALUE is the value of the environment variable.
+#
+# - VAR_NAME is the name of the environment variable. If empty,
+# defaults to $test_var_name.
+#
+# - VAR_NAME_MATCH is the name (regex) that will be used to query the
+# environment about the variable (via getenv). This is useful when
+# we're testing variables with strange names (e.g., with an equal
+# sign in the name) and we know that the variable will actually be
+# set using another name. If empty, defatults, to $var_name.
+#
+# - VAR_VALUE_MATCH is the value (regex) that will be used to match
+# the result of getenv. The rationale is the same as explained for
+# VAR_NAME_MATCH. If empty, defaults, to $var_value.
+
+proc do_test { var_value { var_name "" } { var_name_match "" } { var_value_match "" } } {
+ global binfile test_var_name
+
+ clean_restart $binfile
+
+ if { $var_name == "" } {
+ set var_name $test_var_name
+ }
+
+ if { $var_name_match == "" } {
+ set var_name_match $var_name
+ }
+
+ if { $var_value_match == "" } {
+ set var_value_match $var_value
+ }
+
+ if { $var_value != "" } {
+ gdb_test_no_output "set environment $var_name = $var_value" \
+ "set $var_name = $var_value"
+ } else {
+ gdb_test "set environment $var_name =" \
+ "Setting environment variable \"$var_name\" to null value." \
+ "set $var_name to null value"
+ }
+
+ do_prepare_inferior
+
+ check_getenv "$var_name_match" "$var_value_match" \
+ "print result of getenv for $var_name"
+}
+
+with_test_prefix "long var value" {
+ do_test "this is my test variable; testing long vars; {}"
+}
+
+with_test_prefix "empty var" {
+ do_test ""
+}
+
+with_test_prefix "strange named var" {
+ # In this test we're doing the following:
+ #
+ # (gdb) set environment 'asd =' = 123 43; asd b ### [];;;
+ #
+ # However, due to how GDB parses this line, the environment
+ # variable will end up named <'asd> (without the <>), and its
+ # value will be <' = 123 43; asd b ### [];;;> (without the <>).
+ do_test "123 43; asd b ### \[\];;;" "'asd ='" "'asd" \
+ [string_to_regexp "' = 123 43; asd b ### \[\];;;"]
+}
+
+# Test setting and unsetting environment variables in various
+# fashions.
+
+proc test_set_unset_vars { } {
+ global binfile
+
+ clean_restart $binfile
+
+ with_test_prefix "set 3 environment variables" {
+ # Set some environment variables
+ gdb_test_no_output "set environment A = 1" \
+ "set A to 1"
+ gdb_test_no_output "set environment B = 2" \
+ "set B to 2"
+ gdb_test_no_output "set environment C = 3" \
+ "set C to 3"
+
+ do_prepare_inferior
+
+ # Check that the variables are known by the inferior
+ check_getenv "A" "1"
+ check_getenv "B" "2"
+ check_getenv "C" "3"
+ }
+
+ with_test_prefix "unset one variable, reset one" {
+ # Now, unset/reset some values
+ gdb_test_no_output "unset environment A" \
+ "unset A"
+ gdb_test_no_output "set environment B = 4" \
+ "set B to 4"
+
+ do_prepare_inferior
+
+ check_getenv "A" "" "" 1
+ check_getenv "B" "4"
+ check_getenv "C" "3"
+ }
+
+ with_test_prefix "unset two variables, reset one" {
+ # Unset more values
+ gdb_test_no_output "unset environment B" \
+ "unset B"
+ gdb_test_no_output "set environment A = 1" \
+ "set A to 1 again"
+ gdb_test_no_output "unset environment C" \
+ "unset C"
+
+ do_prepare_inferior
+
+ check_getenv "A" "1"
+ check_getenv "B" "" "" 1
+ check_getenv "C" "" "" 1
+ }
+}
+
+with_test_prefix "test set/unset of vars" {
+ test_set_unset_vars
+}
+
+# Test that unsetting works.
+
+proc test_unset { } {
+ global hex decimal binfile gdb_prompt
+
+ clean_restart $binfile
+
+ do_prepare_inferior
+
+ set test_msg "check if unset works"
+ set found_home 0
+ gdb_test_multiple "print my_getenv (\"HOME\")" $test_msg {
+ -re "\\\$$decimal = $hex \".*\"\r\n$gdb_prompt $" {
+ pass $test_msg
+ set found_home 1
+ }
+ -re "\\\$$decimal = 0x0\r\n$gdb_prompt $" {
+ untested $test_msg
+ }
+ }
+
+ if { $found_home == 1 } {
+ with_test_prefix "simple unset" {
+ # We can do the test, because $HOME exists (and therefore can
+ # be unset).
+ gdb_test_no_output "unset environment HOME" "unset HOME"
+
+ do_prepare_inferior
+
+ # $HOME now must be empty
+ check_getenv "HOME" "" "" 1
+ }
+
+ with_test_prefix "set-then-unset" {
+ clean_restart $binfile
+
+ # Test if setting and then unsetting $HOME works.
+ gdb_test_no_output "set environment HOME = test" "set HOME as test"
+ gdb_test_no_output "unset environment HOME" "unset HOME again"
+
+ do_prepare_inferior
+
+ check_getenv "HOME" "" "" 1
+ }
+ }
+}
+
+with_test_prefix "test unset of vars" {
+ test_unset
+}
diff --git a/gdb/unittests/environ-selftests.c b/gdb/unittests/environ-selftests.c
index 1e938e6746..81a71ee01e 100644
--- a/gdb/unittests/environ-selftests.c
+++ b/gdb/unittests/environ-selftests.c
@@ -22,64 +22,60 @@
#include "common/environ.h"
#include "common/diagnostics.h"
+static const char gdb_selftest_env_var[] = "GDB_SELFTEST_ENVIRON";
+
+static bool
+set_contains (const std::set<std::string> &set, std::string key)
+{
+ return set.find (key) != set.end ();
+}
+
namespace selftests {
namespace gdb_environ_tests {
+/* Test if the vector is initialized in a correct way. */
+
static void
-run_tests ()
+test_vector_initialization ()
{
- /* Set a test environment variable. This will be unset at the end
- of this function. */
- if (setenv ("GDB_SELFTEST_ENVIRON", "1", 1) != 0)
- error (_("Could not set environment variable for testing."));
-
gdb_environ env;
/* When the vector is initialized, there should always be one NULL
element in it. */
SELF_CHECK (env.envp ()[0] == NULL);
+ SELF_CHECK (env.user_set_env ().size () == 0);
+ SELF_CHECK (env.user_unset_env ().size () == 0);
/* Make sure that there is no other element. */
SELF_CHECK (env.get ("PWD") == NULL);
+}
- /* Check if unset followed by a set in an empty vector works. */
- env.set ("PWD", "test");
- SELF_CHECK (strcmp (env.get ("PWD"), "test") == 0);
- /* The second element must be NULL. */
- SELF_CHECK (env.envp ()[1] == NULL);
- env.unset ("PWD");
- SELF_CHECK (env.envp ()[0] == NULL);
+/* Test initialization using the host's environ. */
+static void
+test_init_from_host_environ ()
+{
/* Initialize the environment vector using the host's environ. */
- env = gdb_environ::from_host_environ ();
+ gdb_environ env = gdb_environ::from_host_environ ();
+
+ /* The user-set and user-unset lists must be empty. */
+ SELF_CHECK (env.user_set_env ().size () == 0);
+ SELF_CHECK (env.user_unset_env ().size () == 0);
/* Our test environment variable should be present at the
vector. */
- SELF_CHECK (strcmp (env.get ("GDB_SELFTEST_ENVIRON"), "1") == 0);
-
- /* Set our test variable to another value. */
- env.set ("GDB_SELFTEST_ENVIRON", "test");
- SELF_CHECK (strcmp (env.get ("GDB_SELFTEST_ENVIRON"), "test") == 0);
-
- /* And unset our test variable. The variable still exists in the
- host's environment, but doesn't exist in our vector. */
- env.unset ("GDB_SELFTEST_ENVIRON");
- SELF_CHECK (env.get ("GDB_SELFTEST_ENVIRON") == NULL);
-
- /* Re-set the test variable. */
- env.set ("GDB_SELFTEST_ENVIRON", "1");
- SELF_CHECK (strcmp (env.get ("GDB_SELFTEST_ENVIRON"), "1") == 0);
+ SELF_CHECK (strcmp (env.get (gdb_selftest_env_var), "1") == 0);
+}
- /* When we clear our environ vector, there should be only one
- element on it (NULL), and we shouldn't be able to get our test
- variable. */
- env.clear ();
- SELF_CHECK (env.envp ()[0] == NULL);
- SELF_CHECK (env.get ("GDB_SELFTEST_ENVIRON") == NULL);
+/* Test reinitialization using the host's environ. */
+static void
+test_reinit_from_host_environ ()
+{
/* Reinitialize our environ vector using the host environ. We
should be able to see one (and only one) instance of the test
variable. */
+ gdb_environ env = gdb_environ::from_host_environ ();
env = gdb_environ::from_host_environ ();
char **envp = env.envp ();
int num_found = 0;
@@ -88,15 +84,23 @@ run_tests ()
if (strcmp (envp[i], "GDB_SELFTEST_ENVIRON=1") == 0)
++num_found;
SELF_CHECK (num_found == 1);
+}
- /* Get rid of our test variable. */
- unsetenv ("GDB_SELFTEST_ENVIRON");
+/* Test the case when we set a variable A, then set a variable B,
+ then unset A, and make sure that we cannot find A in the environ
+ vector, but can still find B. */
+
+static void
+test_set_A_unset_B_unset_A_cannot_find_A_can_find_B ()
+{
+ gdb_environ env;
- /* Test the case when we set a variable A, then set a variable B,
- then unset A, and make sure that we cannot find A in the environ
- vector, but can still find B. */
env.set ("GDB_SELFTEST_ENVIRON_1", "aaa");
SELF_CHECK (strcmp (env.get ("GDB_SELFTEST_ENVIRON_1"), "aaa") == 0);
+ /* User-set environ var list must contain one element. */
+ SELF_CHECK (env.user_set_env ().size () == 1);
+ SELF_CHECK (set_contains (env.user_set_env (),
+ std::string ("GDB_SELFTEST_ENVIRON_1=aaa")));
env.set ("GDB_SELFTEST_ENVIRON_2", "bbb");
SELF_CHECK (strcmp (env.get ("GDB_SELFTEST_ENVIRON_2"), "bbb") == 0);
@@ -105,38 +109,117 @@ run_tests ()
SELF_CHECK (env.get ("GDB_SELFTEST_ENVIRON_1") == NULL);
SELF_CHECK (strcmp (env.get ("GDB_SELFTEST_ENVIRON_2"), "bbb") == 0);
+ /* The user-set environ var list must contain only one element
+ now. */
+ SELF_CHECK (set_contains (env.user_set_env (),
+ std::string ("GDB_SELFTEST_ENVIRON_2=bbb")));
+ SELF_CHECK (env.user_set_env ().size () == 1);
+}
+
+/* Check if unset followed by a set in an empty vector works. */
+
+static void
+test_unset_set_empty_vector ()
+{
+ gdb_environ env;
+
+ env.set ("PWD", "test");
+ SELF_CHECK (strcmp (env.get ("PWD"), "test") == 0);
+ SELF_CHECK (set_contains (env.user_set_env (), std::string ("PWD=test")));
+ SELF_CHECK (env.user_unset_env ().size () == 0);
+ /* The second element must be NULL. */
+ SELF_CHECK (env.envp ()[1] == NULL);
+ SELF_CHECK (env.user_set_env ().size () == 1);
+ env.unset ("PWD");
+ SELF_CHECK (env.envp ()[0] == NULL);
+ SELF_CHECK (env.user_set_env ().size () == 0);
+ SELF_CHECK (env.user_unset_env ().size () == 1);
+ SELF_CHECK (set_contains (env.user_unset_env (), std::string ("PWD")));
+}
+
+/* When we clear our environ vector, there should be only one
+ element on it (NULL), and we shouldn't be able to get our test
+ variable. */
+
+static void
+test_vector_clear ()
+{
+ gdb_environ env;
+
+ env.set (gdb_selftest_env_var, "1");
+
env.clear ();
+ SELF_CHECK (env.envp ()[0] == NULL);
+ SELF_CHECK (env.user_set_env ().size () == 0);
+ SELF_CHECK (env.user_unset_env ().size () == 0);
+ SELF_CHECK (env.get (gdb_selftest_env_var) == NULL);
+}
+
+/* Test that after a std::move the moved-from object is left at a
+ valid state (i.e., its only element is NULL). */
+
+static void
+test_std_move ()
+{
+ gdb_environ env;
+ gdb_environ env2;
- /* Test that after a std::move the moved-from object is left at a
- valid state (i.e., its only element is NULL). */
env.set ("A", "1");
SELF_CHECK (strcmp (env.get ("A"), "1") == 0);
- gdb_environ env2;
+ SELF_CHECK (set_contains (env.user_set_env (), std::string ("A=1")));
+ SELF_CHECK (env.user_set_env ().size () == 1);
+
env2 = std::move (env);
SELF_CHECK (env.envp ()[0] == NULL);
SELF_CHECK (strcmp (env2.get ("A"), "1") == 0);
SELF_CHECK (env2.envp ()[1] == NULL);
+ SELF_CHECK (env.user_set_env ().size () == 0);
+ SELF_CHECK (set_contains (env2.user_set_env (), std::string ("A=1")));
+ SELF_CHECK (env2.user_set_env ().size () == 1);
env.set ("B", "2");
SELF_CHECK (strcmp (env.get ("B"), "2") == 0);
SELF_CHECK (env.envp ()[1] == NULL);
+}
+
+/* Test that the move constructor leaves everything at a valid
+ state. */
+
+static void
+test_move_constructor ()
+{
+ gdb_environ env;
- /* Test that the move constructor leaves everything at a valid
- state. */
- env.clear ();
env.set ("A", "1");
SELF_CHECK (strcmp (env.get ("A"), "1") == 0);
- gdb_environ env3 = std::move (env);
+ SELF_CHECK (set_contains (env.user_set_env (), std::string ("A=1")));
+
+ gdb_environ env2 = std::move (env);
SELF_CHECK (env.envp ()[0] == NULL);
- SELF_CHECK (strcmp (env3.get ("A"), "1") == 0);
- SELF_CHECK (env3.envp ()[1] == NULL);
+ SELF_CHECK (env.user_set_env ().size () == 0);
+ SELF_CHECK (strcmp (env2.get ("A"), "1") == 0);
+ SELF_CHECK (env2.envp ()[1] == NULL);
+ SELF_CHECK (set_contains (env2.user_set_env (), std::string ("A=1")));
+ SELF_CHECK (env2.user_set_env ().size () == 1);
+
env.set ("B", "2");
SELF_CHECK (strcmp (env.get ("B"), "2") == 0);
SELF_CHECK (env.envp ()[1] == NULL);
+ SELF_CHECK (set_contains (env.user_set_env (), std::string ("B=2")));
+ SELF_CHECK (env.user_set_env ().size () == 1);
+}
+
+/* Test that self-moving works. */
+
+static void
+test_self_move ()
+{
+ gdb_environ env;
/* Test self-move. */
- env.clear ();
env.set ("A", "1");
SELF_CHECK (strcmp (env.get ("A"), "1") == 0);
+ SELF_CHECK (set_contains (env.user_set_env (), std::string ("A=1")));
+ SELF_CHECK (env.user_set_env ().size () == 1);
/* Some compilers warn about moving to self, but that's precisely what we want
to test here, so turn this warning off. */
@@ -148,6 +231,69 @@ run_tests ()
SELF_CHECK (strcmp (env.get ("A"), "1") == 0);
SELF_CHECK (strcmp (env.envp ()[0], "A=1") == 0);
SELF_CHECK (env.envp ()[1] == NULL);
+ SELF_CHECK (set_contains (env.user_set_env (), std::string ("A=1")));
+ SELF_CHECK (env.user_set_env ().size () == 1);
+}
+
+/* Test if set/unset/reset works. */
+
+static void
+test_set_unset_reset ()
+{
+ gdb_environ env = gdb_environ::from_host_environ ();
+
+ /* Our test variable should already be present in the host's environment. */
+ SELF_CHECK (env.get ("GDB_SELFTEST_ENVIRON") != NULL);
+
+ /* Set our test variable to another value. */
+ env.set ("GDB_SELFTEST_ENVIRON", "test");
+ SELF_CHECK (strcmp (env.get ("GDB_SELFTEST_ENVIRON"), "test") == 0);
+ SELF_CHECK (env.user_set_env ().size () == 1);
+ SELF_CHECK (env.user_unset_env ().size () == 0);
+
+ /* And unset our test variable. The variable still exists in the
+ host's environment, but doesn't exist in our vector. */
+ env.unset ("GDB_SELFTEST_ENVIRON");
+ SELF_CHECK (env.get ("GDB_SELFTEST_ENVIRON") == NULL);
+ SELF_CHECK (env.user_set_env ().size () == 0);
+ SELF_CHECK (env.user_unset_env ().size () == 1);
+ SELF_CHECK (set_contains (env.user_unset_env (),
+ std::string ("GDB_SELFTEST_ENVIRON")));
+
+ /* Re-set the test variable. */
+ env.set ("GDB_SELFTEST_ENVIRON", "1");
+ SELF_CHECK (strcmp (env.get ("GDB_SELFTEST_ENVIRON"), "1") == 0);
+}
+
+static void
+run_tests ()
+{
+ /* Set a test environment variable. */
+ if (setenv ("GDB_SELFTEST_ENVIRON", "1", 1) != 0)
+ error (_("Could not set environment variable for testing."));
+
+ test_vector_initialization ();
+
+ test_unset_set_empty_vector ();
+
+ test_init_from_host_environ ();
+
+ test_set_unset_reset ();
+
+ test_vector_clear ();
+
+ test_reinit_from_host_environ ();
+
+ /* Get rid of our test variable. We won't need it anymore. */
+ unsetenv ("GDB_SELFTEST_ENVIRON");
+
+ test_set_A_unset_B_unset_A_cannot_find_A_can_find_B ();
+
+ test_std_move ();
+
+ test_move_constructor ();
+
+ test_self_move ();
}
} /* namespace gdb_environ */
} /* namespace selftests */
--
2.13.3
^ permalink raw reply [flat|nested] 35+ messages in thread
* Re: [PATCH v5] Implement the ability to set/unset environment variables to GDBserver when starting the inferior
2017-08-31 20:49 ` [PATCH v5] " Sergio Durigan Junior
@ 2017-08-31 21:03 ` Simon Marchi
2017-08-31 21:26 ` Sergio Durigan Junior
2017-09-01 6:19 ` Eli Zaretskii
1 sibling, 1 reply; 35+ messages in thread
From: Simon Marchi @ 2017-08-31 21:03 UTC (permalink / raw)
To: Sergio Durigan Junior; +Cc: GDB Patches, Pedro Alves
On 2017-08-31 22:49, Sergio Durigan Junior wrote:
> Changes from v4:
>
> - Applied Simon's patch to refactor the unittest. Thanks, Simon!
>
>
> This patch implements the ability to set/unset environment variables
> on the remote target, mimicking what GDB already offers to the user.
> There are two features present here: user-set and user-unset
> environment variables.
>
> User-set environment variables are only the variables that are
> explicitly set by the user, using the 'set environment' command. This
> means that variables that were already present in the environment when
> starting GDB/GDBserver are not transmitted/considered by this feature.
>
> User-unset environment variables are variables that are explicitly
> unset by the user, using the 'unset environment' command.
>
> The idea behind this patch is to store user-set and user-unset
> environment variables in two separate sets, both part of gdb_environ.
> Then, when extended_remote_create_inferior is preparing to start the
> inferior, it will iterate over the two sets and set/unset variables
> accordingly. Three new packets are introduced:
>
> - QEnvironmentHexEncoded, which is used to set environment variables,
> and contains an hex-encoded string in the format "VAR=VALUE" (VALUE
> can be empty if the user set a variable with a null value, by doing
> 'set environment VAR=').
>
> - QEnvironmentUnset, which is used to unset environment variables, and
> contains an hex-encoded string in the format "VAR".
>
> - QEnvironmentReset, which is always the first packet to be
> transmitted, and is used to reset the environment, i.e., discard any
> changes made by the user on previous runs.
>
> The QEnvironmentHexEncoded packet is inspired on LLDB's extensions to
> the RSP. Details about it can be seen here:
>
>
> <https://raw.githubusercontent.com/llvm-mirror/lldb/master/docs/lldb-gdb-remote.txt>
>
> I decided not to implement the QEnvironment packet because it is
> considered deprecated by LLDB. This packet, on LLDB, serves the same
> purpose of QEnvironmentHexEncoded, but sends the information using a
> plain text, non-hex-encoded string.
>
> The other two packets are new.
>
> This patch also includes updates to the documentation, testsuite, and
> unit tests, without introducing regressions.
That version LGTM.
Thanks,
Simon
^ permalink raw reply [flat|nested] 35+ messages in thread
* Re: [PATCH v5] Implement the ability to set/unset environment variables to GDBserver when starting the inferior
2017-08-31 21:03 ` Simon Marchi
@ 2017-08-31 21:26 ` Sergio Durigan Junior
2017-09-05 15:33 ` Thomas Preudhomme
0 siblings, 1 reply; 35+ messages in thread
From: Sergio Durigan Junior @ 2017-08-31 21:26 UTC (permalink / raw)
To: Simon Marchi; +Cc: GDB Patches, Pedro Alves
On Thursday, August 31 2017, Simon Marchi wrote:
> On 2017-08-31 22:49, Sergio Durigan Junior wrote:
>> Changes from v4:
>>
>> - Applied Simon's patch to refactor the unittest. Thanks, Simon!
>>
>>
>> This patch implements the ability to set/unset environment variables
>> on the remote target, mimicking what GDB already offers to the user.
>> There are two features present here: user-set and user-unset
>> environment variables.
>>
>> User-set environment variables are only the variables that are
>> explicitly set by the user, using the 'set environment' command. This
>> means that variables that were already present in the environment when
>> starting GDB/GDBserver are not transmitted/considered by this feature.
>>
>> User-unset environment variables are variables that are explicitly
>> unset by the user, using the 'unset environment' command.
>>
>> The idea behind this patch is to store user-set and user-unset
>> environment variables in two separate sets, both part of gdb_environ.
>> Then, when extended_remote_create_inferior is preparing to start the
>> inferior, it will iterate over the two sets and set/unset variables
>> accordingly. Three new packets are introduced:
>>
>> - QEnvironmentHexEncoded, which is used to set environment variables,
>> and contains an hex-encoded string in the format "VAR=VALUE" (VALUE
>> can be empty if the user set a variable with a null value, by doing
>> 'set environment VAR=').
>>
>> - QEnvironmentUnset, which is used to unset environment variables, and
>> contains an hex-encoded string in the format "VAR".
>>
>> - QEnvironmentReset, which is always the first packet to be
>> transmitted, and is used to reset the environment, i.e., discard any
>> changes made by the user on previous runs.
>>
>> The QEnvironmentHexEncoded packet is inspired on LLDB's extensions to
>> the RSP. Details about it can be seen here:
>>
>>
>> <https://raw.githubusercontent.com/llvm-mirror/lldb/master/docs/lldb-gdb-remote.txt>
>>
>> I decided not to implement the QEnvironment packet because it is
>> considered deprecated by LLDB. This packet, on LLDB, serves the same
>> purpose of QEnvironmentHexEncoded, but sends the information using a
>> plain text, non-hex-encoded string.
>>
>> The other two packets are new.
>>
>> This patch also includes updates to the documentation, testsuite, and
>> unit tests, without introducing regressions.
>
> That version LGTM.
Thanks for all the reviews.
Pushed:
0a2dde4a321d2f7bd2ded9a558b9ae92892de0e2
--
Sergio
GPG key ID: 237A 54B1 0287 28BF 00EF 31F4 D0EB 7628 65FC 5E36
Please send encrypted e-mail if possible
http://sergiodj.net/
^ permalink raw reply [flat|nested] 35+ messages in thread
* Re: [PATCH v5] Implement the ability to set/unset environment variables to GDBserver when starting the inferior
2017-08-31 20:49 ` [PATCH v5] " Sergio Durigan Junior
2017-08-31 21:03 ` Simon Marchi
@ 2017-09-01 6:19 ` Eli Zaretskii
1 sibling, 0 replies; 35+ messages in thread
From: Eli Zaretskii @ 2017-09-01 6:19 UTC (permalink / raw)
To: Sergio Durigan Junior; +Cc: gdb-patches, palves, simon.marchi
> From: Sergio Durigan Junior <sergiodj@redhat.com>
> Cc: Pedro Alves <palves@redhat.com>, Simon Marchi <simon.marchi@polymtl.ca>, Sergio Durigan Junior <sergiodj@redhat.com>
> Date: Thu, 31 Aug 2017 16:49:32 -0400
>
> Changes from v4:
>
> - Applied Simon's patch to refactor the unittest. Thanks, Simon!
OK for the documentation parts.
Thanks.
^ permalink raw reply [flat|nested] 35+ messages in thread
* Re: [PATCH v5] Implement the ability to set/unset environment variables to GDBserver when starting the inferior
2017-08-31 21:26 ` Sergio Durigan Junior
@ 2017-09-05 15:33 ` Thomas Preudhomme
2017-09-05 16:26 ` Simon Marchi
0 siblings, 1 reply; 35+ messages in thread
From: Thomas Preudhomme @ 2017-09-05 15:33 UTC (permalink / raw)
To: Sergio Durigan Junior; +Cc: GDB Patches
Hi Sergio,
I noticed that the share-env-with-gdbserver.exp test you added is skipped if
using a gdb stub. However the comment says the test is meaningless for
native-gdbserver. Shouldn't the check also look whether this is a cross debugger?
Best regards,
Thomas
On 31/08/17 22:26, Sergio Durigan Junior wrote:
> On Thursday, August 31 2017, Simon Marchi wrote:
>
>> On 2017-08-31 22:49, Sergio Durigan Junior wrote:
>>> Changes from v4:
>>>
>>> - Applied Simon's patch to refactor the unittest. Thanks, Simon!
>>>
>>>
>>> This patch implements the ability to set/unset environment variables
>>> on the remote target, mimicking what GDB already offers to the user.
>>> There are two features present here: user-set and user-unset
>>> environment variables.
>>>
>>> User-set environment variables are only the variables that are
>>> explicitly set by the user, using the 'set environment' command. This
>>> means that variables that were already present in the environment when
>>> starting GDB/GDBserver are not transmitted/considered by this feature.
>>>
>>> User-unset environment variables are variables that are explicitly
>>> unset by the user, using the 'unset environment' command.
>>>
>>> The idea behind this patch is to store user-set and user-unset
>>> environment variables in two separate sets, both part of gdb_environ.
>>> Then, when extended_remote_create_inferior is preparing to start the
>>> inferior, it will iterate over the two sets and set/unset variables
>>> accordingly. Three new packets are introduced:
>>>
>>> - QEnvironmentHexEncoded, which is used to set environment variables,
>>> and contains an hex-encoded string in the format "VAR=VALUE" (VALUE
>>> can be empty if the user set a variable with a null value, by doing
>>> 'set environment VAR=').
>>>
>>> - QEnvironmentUnset, which is used to unset environment variables, and
>>> contains an hex-encoded string in the format "VAR".
>>>
>>> - QEnvironmentReset, which is always the first packet to be
>>> transmitted, and is used to reset the environment, i.e., discard any
>>> changes made by the user on previous runs.
>>>
>>> The QEnvironmentHexEncoded packet is inspired on LLDB's extensions to
>>> the RSP. Details about it can be seen here:
>>>
>>>
>>> <https://raw.githubusercontent.com/llvm-mirror/lldb/master/docs/lldb-gdb-remote.txt>
>>>
>>> I decided not to implement the QEnvironment packet because it is
>>> considered deprecated by LLDB. This packet, on LLDB, serves the same
>>> purpose of QEnvironmentHexEncoded, but sends the information using a
>>> plain text, non-hex-encoded string.
>>>
>>> The other two packets are new.
>>>
>>> This patch also includes updates to the documentation, testsuite, and
>>> unit tests, without introducing regressions.
>>
>> That version LGTM.
>
> Thanks for all the reviews.
>
> Pushed:
> 0a2dde4a321d2f7bd2ded9a558b9ae92892de0e2
>
^ permalink raw reply [flat|nested] 35+ messages in thread
* Re: [PATCH v5] Implement the ability to set/unset environment variables to GDBserver when starting the inferior
2017-09-05 15:33 ` Thomas Preudhomme
@ 2017-09-05 16:26 ` Simon Marchi
2017-09-05 17:09 ` Thomas Preudhomme
0 siblings, 1 reply; 35+ messages in thread
From: Simon Marchi @ 2017-09-05 16:26 UTC (permalink / raw)
To: Thomas Preudhomme, Sergio Durigan Junior; +Cc: GDB Patches
On 2017-09-05 05:32 PM, Thomas Preudhomme wrote:
> Hi Sergio,
>
> I noticed that the share-env-with-gdbserver.exp test you added is skipped if using a gdb stub. However the comment says the test is meaningless for native-gdbserver. Shouldn't the check also look whether this is a cross debugger?
>
> Best regards,
>
> Thomas
Why would a cross debugger make this test irrelevant? If you are debugging
your ARM board from your x86 PC, you can still set environment variables
for new processes that are spawned by gdbserver. I have the feeling that
you are confused by the meanings of the various terms (native/remote/
extended-remote/cross/etc), which is understandable because these terms
have different meanings depending on the context. Can you expand on why
you think this is wrong, and maybe we can find where the possible confusion
comes from?
Simon
^ permalink raw reply [flat|nested] 35+ messages in thread
* Re: [PATCH v5] Implement the ability to set/unset environment variables to GDBserver when starting the inferior
2017-09-05 16:26 ` Simon Marchi
@ 2017-09-05 17:09 ` Thomas Preudhomme
2017-09-05 18:41 ` Simon Marchi
0 siblings, 1 reply; 35+ messages in thread
From: Thomas Preudhomme @ 2017-09-05 17:09 UTC (permalink / raw)
To: Simon Marchi, Sergio Durigan Junior; +Cc: GDB Patches
Hi Simon,
Sorry if I wasn't clear. I actually meant that this test *should* be relevant
for cross-debugger using native gdbserver but it is currently skipped in that
case (because the test only checks if that gdb uses a stub).
Did I misunderstand the meaning of gdb stub?
Best regards,
Thomas
On 05/09/17 17:25, Simon Marchi wrote:
> On 2017-09-05 05:32 PM, Thomas Preudhomme wrote:
>> Hi Sergio,
>>
>> I noticed that the share-env-with-gdbserver.exp test you added is skipped if using a gdb stub. However the comment says the test is meaningless for native-gdbserver. Shouldn't the check also look whether this is a cross debugger?
>>
>> Best regards,
>>
>> Thomas
>
> Why would a cross debugger make this test irrelevant? If you are debugging
> your ARM board from your x86 PC, you can still set environment variables
> for new processes that are spawned by gdbserver. I have the feeling that
> you are confused by the meanings of the various terms (native/remote/
> extended-remote/cross/etc), which is understandable because these terms
> have different meanings depending on the context. Can you expand on why
> you think this is wrong, and maybe we can find where the possible confusion
> comes from?
>
> Simon
>
^ permalink raw reply [flat|nested] 35+ messages in thread
* Re: [PATCH v5] Implement the ability to set/unset environment variables to GDBserver when starting the inferior
2017-09-05 17:09 ` Thomas Preudhomme
@ 2017-09-05 18:41 ` Simon Marchi
2017-09-06 8:36 ` Thomas Preudhomme
0 siblings, 1 reply; 35+ messages in thread
From: Simon Marchi @ 2017-09-05 18:41 UTC (permalink / raw)
To: Thomas Preudhomme, Sergio Durigan Junior; +Cc: GDB Patches
On 2017-09-05 07:09 PM, Thomas Preudhomme wrote:
> Hi Simon,
>
> Sorry if I wasn't clear. I actually meant that this test *should* be relevant
> for cross-debugger using native gdbserver but it is currently skipped in that
> case (because the test only checks if that gdb uses a stub).
The native in native-gdbserver (as in --target_board=native-gdbserver) means
that we are testing against a gdbserver compiled for the same architecture as
the build machine. Moreover, that board happens to spawn gdbserver locally, on
the same machine that runs the testsuite. It's easy to build & test this
configuration, since you can do everything on the same machine. If you build a
cross-debugger, it means that the architecture where gdb runs is different from
the architecture gdbserver runs and debug. It kind of doesn't make sense to
use the native-gdbserver board when testing a cross debugger, since it would
imply that gdb and gdbserver run the same architecture, but then it's not a
cross-debugger...
You can (and maybe you already do) write your own board file in order to test a
cross-debugger. [1]
The reason why native-gdbserver is not relevant in that case is that it uses
the "remote" protocol, as opposed to the "extended-remote" protocol as used by
the native-extended-gdbserver board. A difference between the two is that only
in extended-remote can you spawn/run new processes. And setting the
environment variables in GDB with "set environment" only affects processes
created by gdb (and now gdbserver). This is why it doesn't make sense to run
the test with native-gdbserver (or anything that uses the non-extended "remote"
protocol).
> Did I misunderstand the meaning of gdb stub?
The way I understand it is that a "gdb stub" is a debugging interface that
talks the non-extended remote protocol and is embedded in whatever you try to
debug [2]. Therefore, there's no notion of starting a new process. When you
connect to it, the thing already exists. And if it exits, the remote
connection dies with it, since you don't have anybody to talk to anymore. So
with a GDB stub, you would always talk the remote protocol, not
extended-remote. In the testsuite, "use_gdb_stub" is pretty much equivalent to
the "remote" protocol. When using the native-extended-remote board file, you
are not using the extended-remote protocol, and can create new processes, and
therefore it's !use_gdb_stub.
Hopefully that help clear things up.
[1] https://sourceware.org/gdb/wiki/TestingGDB#Testing_gdbserver_in_a_remote_cross-target_configuration
[2] Of course, gdbserver is not embedded in what you try to debug, but it's
the same result, it has the same lifetime.
^ permalink raw reply [flat|nested] 35+ messages in thread
* Re: [PATCH v5] Implement the ability to set/unset environment variables to GDBserver when starting the inferior
2017-09-05 18:41 ` Simon Marchi
@ 2017-09-06 8:36 ` Thomas Preudhomme
0 siblings, 0 replies; 35+ messages in thread
From: Thomas Preudhomme @ 2017-09-06 8:36 UTC (permalink / raw)
To: Simon Marchi, Sergio Durigan Junior; +Cc: GDB Patches
Hi Simon,
Thanks for clarifying things up. Sorry for the noise then.
Best regards,
Thomas
On 05/09/17 19:41, Simon Marchi wrote:
> On 2017-09-05 07:09 PM, Thomas Preudhomme wrote:
>> Hi Simon,
>>
>> Sorry if I wasn't clear. I actually meant that this test *should* be relevant
>> for cross-debugger using native gdbserver but it is currently skipped in that
>> case (because the test only checks if that gdb uses a stub).
>
> The native in native-gdbserver (as in --target_board=native-gdbserver) means
> that we are testing against a gdbserver compiled for the same architecture as
> the build machine. Moreover, that board happens to spawn gdbserver locally, on
> the same machine that runs the testsuite. It's easy to build & test this
> configuration, since you can do everything on the same machine. If you build a
> cross-debugger, it means that the architecture where gdb runs is different from
> the architecture gdbserver runs and debug. It kind of doesn't make sense to
> use the native-gdbserver board when testing a cross debugger, since it would
> imply that gdb and gdbserver run the same architecture, but then it's not a
> cross-debugger...
>
> You can (and maybe you already do) write your own board file in order to test a
> cross-debugger. [1]
>
> The reason why native-gdbserver is not relevant in that case is that it uses
> the "remote" protocol, as opposed to the "extended-remote" protocol as used by
> the native-extended-gdbserver board. A difference between the two is that only
> in extended-remote can you spawn/run new processes. And setting the
> environment variables in GDB with "set environment" only affects processes
> created by gdb (and now gdbserver). This is why it doesn't make sense to run
> the test with native-gdbserver (or anything that uses the non-extended "remote"
> protocol).
>
>> Did I misunderstand the meaning of gdb stub?
>
> The way I understand it is that a "gdb stub" is a debugging interface that
> talks the non-extended remote protocol and is embedded in whatever you try to
> debug [2]. Therefore, there's no notion of starting a new process. When you
> connect to it, the thing already exists. And if it exits, the remote
> connection dies with it, since you don't have anybody to talk to anymore. So
> with a GDB stub, you would always talk the remote protocol, not
> extended-remote. In the testsuite, "use_gdb_stub" is pretty much equivalent to
> the "remote" protocol. When using the native-extended-remote board file, you
> are not using the extended-remote protocol, and can create new processes, and
> therefore it's !use_gdb_stub.
>
> Hopefully that help clear things up.
>
> [1] https://sourceware.org/gdb/wiki/TestingGDB#Testing_gdbserver_in_a_remote_cross-target_configuration
> [2] Of course, gdbserver is not embedded in what you try to debug, but it's
> the same result, it has the same lifetime.
>
^ permalink raw reply [flat|nested] 35+ messages in thread
end of thread, other threads:[~2017-09-06 8:36 UTC | newest]
Thread overview: 35+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-06-29 19:41 [PATCH] Implement the ability to transmit environment variables to GDBserver when starting the inferior Sergio Durigan Junior
2017-07-10 21:32 ` Sergio Durigan Junior
2017-07-13 21:47 ` Simon Marchi
2017-07-14 17:34 ` Sergio Durigan Junior
2017-07-27 3:36 ` [PATCH v2] Implement the ability to set/unset " Sergio Durigan Junior
2017-07-27 17:10 ` Eli Zaretskii
2017-07-27 22:05 ` Simon Marchi
2017-08-01 2:33 ` Sergio Durigan Junior
2017-08-01 9:35 ` Simon Marchi
2017-08-04 22:56 ` Sergio Durigan Junior
2017-07-29 22:36 ` Simon Marchi
2017-08-01 2:43 ` Sergio Durigan Junior
2017-08-01 9:54 ` Simon Marchi
2017-08-04 23:03 ` Sergio Durigan Junior
2017-08-05 18:14 ` Simon Marchi
2017-08-12 4:34 ` Sergio Durigan Junior
2017-08-12 8:11 ` Simon Marchi
[not found] ` <256083325d9f9c4cc4f5518fe6e5292d@polymtl.ca>
2017-08-12 4:19 ` Sergio Durigan Junior
2017-08-13 6:19 ` [PATCH v3] " Sergio Durigan Junior
2017-08-21 19:11 ` Sergio Durigan Junior
2017-08-24 19:25 ` Simon Marchi
2017-08-28 21:25 ` Sergio Durigan Junior
2017-08-28 23:13 ` [PATCH v4] " Sergio Durigan Junior
2017-08-31 19:37 ` Simon Marchi
2017-08-31 20:34 ` Sergio Durigan Junior
2017-08-31 20:39 ` Simon Marchi
2017-08-31 20:49 ` [PATCH v5] " Sergio Durigan Junior
2017-08-31 21:03 ` Simon Marchi
2017-08-31 21:26 ` Sergio Durigan Junior
2017-09-05 15:33 ` Thomas Preudhomme
2017-09-05 16:26 ` Simon Marchi
2017-09-05 17:09 ` Thomas Preudhomme
2017-09-05 18:41 ` Simon Marchi
2017-09-06 8:36 ` Thomas Preudhomme
2017-09-01 6:19 ` Eli Zaretskii
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox