From: Pedro Alves <palves@redhat.com>
To: Sergio Durigan Junior <sergiodj@redhat.com>
Cc: GDB Patches <gdb-patches@sourceware.org>,
Simon Marchi <simon.marchi@polymtl.ca>
Subject: Re: [PATCH v3] C++ify gdb/common/environ.c
Date: Thu, 04 May 2017 15:30:00 -0000 [thread overview]
Message-ID: <744e83e1-e099-3c75-38f7-53db41d578b0@redhat.com> (raw)
In-Reply-To: <87o9vdjoz4.fsf@redhat.com>
On 05/01/2017 03:22 AM, Sergio Durigan Junior wrote:
>>> + this->m_environ_vector.insert (this->m_environ_vector.end () - 1,
>>> + xstrdup (std::string (var
>>> + + '='
>>> + + value).c_str ()));
>>
>> I don't really understand the "m_environ_vector.end () - 1" here.
>
> This is needed because the last element of m_environ_vector needs to be
> always NULL. Therefore, this code is basically inserting the new
> variable in the second-to-last position of the vector. I made a comment
> on top of it to clarify this part.
Ah, OK.
>
> Also, there are other places where I need to iterate through the
> elements of the vector, and I'm also using the "- 1" in these places.
> I'll put comments where applicable.
OK, I'll take another look when you post it.
>>> -extern struct gdb_environ *make_environ (void);
>>> +class gdb_environ
>>> +{
>>> +public:
>>> + /* Regular constructor and destructor. */
>>> + gdb_environ ();
>>> + ~gdb_environ ();
>>>
>>> -extern void free_environ (struct gdb_environ *);
>>> + /* Reinitialize the environment stored. This is used when the user
>>> + wants to delete all environment variables added by him/her. */
>>> + void reinit ();
>>
>> This should mention that the initial state is copied from the host's
>> environ. I think the default ctor could use a similar comment.
>> I was going to suggest to call this clear() instead, to go
>> with the standard containers' terminology, until I realized what
>> "init" really does. Or maybe even find a more explicit name,
>> reset_from_host_environ or some such.
>
> Sorry, I guess I wasn't aware of the importance of specifying that the
> variables come from the host. Somehow I thought this was implied.
>
> I guess reset_from_host_environ is a good name for the method; my other
> option would be "reinit_using_host_environ", but that's longer.
>
>> Perhaps an even clearer approach would be to make the default ctor
>> not do a deep copy of the host's "environ", but instead add a
>> static factor method that returned such a new gdb_environ, like:
>>
>> static gdb_environ
>> gdb_environ::from_host_environ ()
>> {
>> // build/return a gdb_environ that wraps the host's environ global.
>> }
>>
>> Not sure. Only experimenting would tell.
>
> Sorry, I'm not sure I understand your suggestion. Please correct me if
> I'm wrong.
>
> You're basically saying that the default ctor shouldn't actually do
> anything; instead, the class should have this new from_host_environ
> method which would be the "official" ctor. The users would then call
> from_host_environ directly when they wanted an instance of the class,
> and the method would be responsible for initializing the object. Is
> that correct?
>
> If yes, I think I fail to see the advantage of this method over having a
> normal ctor (aside from explicitly naming the new ctor after the fact
> that we're using the host environ to build the object).
The advantage was all in that "aside" -- to make the code
document itself. It was totally non-obvious to me at first
that:
gdb_environ env;
makes a copy of the host's environment. I just assumed it created
an empty environment. Probably because in my mind I don't think
it'll make sense for a remote process to inherit the host's
environment variables.
>
> ... after some reading ...
>
> Oh, I think I see what you're suggesting. Instead of building one
> gdb_environ every time someone requests it, we build just one and pass
> it along. OK, now it makes sense.
That wasn't actually what I was saying. :-)
>
>> Speaking of copying the host environ:
>>
>>> _initialize_mi_cmd_env (void)
>>> {
>>> - struct gdb_environ *environment;
>>> + gdb_environ environment;
>>> const char *env;
>>>
>>> /* We want original execution path to reset to, if desired later.
>>> @@ -278,13 +278,10 @@ _initialize_mi_cmd_env (void)
>>> current_inferior ()->environment. Also, there's no obvious
>>> place where this code can be moved such that it surely run
>>> before any code possibly mangles original PATH. */
>>> - environment = make_environ ();
>>> - init_environ (environment);
>>> - env = get_in_environ (environment, path_var_name);
>>> + env = environment.get (path_var_name);
>>>
>>> /* Can be null if path is not set. */
>>> if (!env)
>>> env = "";
>>> orig_path = xstrdup (env);
>>> - free_environ (environment);
>>> }
>>
>> This usage of gdb_environ looks like pointless
>> wrapping / dupping / freeing to me -- I don't see why we
>> need to dup the whole host environment just to get at some
>> env variable. Using good old getenv(3) directly should do,
>> and end up trimming off a bit of work from gdb's startup.
>
> Absolutely. I didn't pay close attention to this bit; I was just
> mechanically converting the code.
>
>> I could see perhaps wanting to avoid / optimize the linear
>> walks that getenv must do, if we have many getenv calls in
>> gdb, but then that suggests keeping a gdb_environ global that
>> is initialized early on and is reused. But that would miss
>> any setenv call that changes the environment since that
>> gdb_environ is created, so I'd prefer not do that unless
>> we find a real need.
>
> I'll make the code use getenv instead.
Thanks. That bit could/should go in as its own
preparatory/obvious patch first, no need to carry it in
the same patch.
Thanks,
Pedro Alves
next prev parent reply other threads:[~2017-05-04 15:30 UTC|newest]
Thread overview: 47+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-04-13 4:05 [PATCH] " Sergio Durigan Junior
2017-04-15 18:51 ` [PATCH v2] " Sergio Durigan Junior
2017-04-15 21:22 ` Simon Marchi
2017-04-18 2:49 ` Sergio Durigan Junior
2017-04-16 5:09 ` Simon Marchi
2017-04-16 17:32 ` Sergio Durigan Junior
2017-04-18 3:03 ` [PATCH v3] " Sergio Durigan Junior
2017-04-19 4:56 ` Simon Marchi
2017-04-19 16:30 ` Pedro Alves
2017-04-19 18:14 ` Pedro Alves
2017-05-01 2:22 ` Sergio Durigan Junior
2017-05-04 15:30 ` Pedro Alves [this message]
2017-06-14 19:22 ` [PATCH v4] " Sergio Durigan Junior
2017-06-16 15:45 ` Pedro Alves
2017-06-16 18:01 ` Sergio Durigan Junior
2017-06-16 18:23 ` Pedro Alves
2017-06-16 21:59 ` Sergio Durigan Junior
2017-06-16 22:23 ` [PATCH v5] " Sergio Durigan Junior
2017-06-17 8:54 ` Simon Marchi
2017-06-19 4:19 ` Sergio Durigan Junior
2017-06-19 13:40 ` Pedro Alves
2017-06-19 16:19 ` Sergio Durigan Junior
2017-06-19 12:13 ` Pedro Alves
2017-06-20 14:02 ` Pedro Alves
2017-06-19 4:36 ` [PATCH v6] " Sergio Durigan Junior
2017-06-19 4:51 ` Sergio Durigan Junior
2017-06-19 7:18 ` Simon Marchi
2017-06-19 14:26 ` Pedro Alves
2017-06-19 15:30 ` Simon Marchi
2017-06-19 15:44 ` Pedro Alves
2017-06-19 15:47 ` Pedro Alves
2017-06-19 16:26 ` Simon Marchi
2017-06-19 16:55 ` Pedro Alves
2017-06-19 17:59 ` Sergio Durigan Junior
2017-06-19 18:09 ` Pedro Alves
2017-06-19 18:23 ` Sergio Durigan Junior
2017-06-19 18:36 ` Pedro Alves
2017-06-19 18:38 ` Pedro Alves
2017-06-19 14:26 ` Pedro Alves
2017-06-19 16:13 ` Sergio Durigan Junior
2017-06-19 16:38 ` Pedro Alves
2017-06-19 16:46 ` Sergio Durigan Junior
2017-06-19 18:27 ` [PATCH v7] " Sergio Durigan Junior
2017-06-20 3:27 ` [PATCH v8] " Sergio Durigan Junior
2017-06-20 12:13 ` Pedro Alves
2017-06-20 12:46 ` Simon Marchi
2017-06-20 13:00 ` Sergio Durigan Junior
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=744e83e1-e099-3c75-38f7-53db41d578b0@redhat.com \
--to=palves@redhat.com \
--cc=gdb-patches@sourceware.org \
--cc=sergiodj@redhat.com \
--cc=simon.marchi@polymtl.ca \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).