+/* 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 ());
+}
+
+/* Helper function to set the current working directory for the
+ inferior in the remote target. */
+
+static void
+extended_remote_set_inferior_cwd (struct remote_state *rs)
+{
+ if (packet_support (PACKET_QSetWorkingDir) != PACKET_DISABLE)
+ {
+ const char *inferior_cwd = get_inferior_cwd ();
+
+ if (inferior_cwd != NULL)
+ {
+ std::string hexpath = bin2hex ((const gdb_byte *) inferior_cwd,
+ strlen (inferior_cwd));
+
+ xsnprintf (rs->buf, get_remote_packet_size (),
+ "QSetWorkingDir:%s", hexpath.c_str ());
+ }
+ else
+ {
+ /* An empty inferior_cwd means that the user wants us to
+ reset the remote server's inferior's cwd. */
+ xsnprintf (rs->buf, get_remote_packet_size (),
+ "QSetWorkingDir:");
+ }
+
+ putpkt (rs->buf);
+ getpkt (&rs->buf, &rs->buf_size, 0);
+ if (packet_ok (rs->buf,
+ &remote_protocol_packets[PACKET_QSetWorkingDir])
+ != PACKET_OK)
+ error (_("\
+Remote replied unexpectedly while setting the inferior's working\n\
+directory: %s"),
+ rs->buf);
+
+ }
+}
+