public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Simon Marchi <simon.marchi@polymtl.ca>
To: Andrew Burgess <aburgess@redhat.com>,
	Simon Marchi <simon.marchi@efficios.com>
Cc: gdb-patches@sourceware.org
Subject: Re: [PATCH] gdb/python: remove gdb._mi_commands dict
Date: Fri, 18 Mar 2022 20:29:48 -0400	[thread overview]
Message-ID: <a89e55c6-63b0-4c2e-45fa-9d18f9d03310@polymtl.ca> (raw)
In-Reply-To: <20220318224431.GS1212730@redhat.com>



On 2022-03-18 18:44, Andrew Burgess via Gdb-patches wrote:
> * Simon Marchi via Gdb-patches <gdb-patches@sourceware.org> [2022-03-18 15:55:26 -0400]:
> 
>> The motivation for this patch is the fact that py-micmd.c doesn't build
>> with Python 2, due to PyDict_GetItemWithError being a Python 3-only
>> function:
>>
>>       CXX    python/py-micmd.o
>>     /home/smarchi/src/binutils-gdb/gdb/python/py-micmd.c: In function ‘int micmdpy_uninstall_command(micmdpy_object*)’:
>>     /home/smarchi/src/binutils-gdb/gdb/python/py-micmd.c:430:20: error: ‘PyDict_GetItemWithError’ was not declared in this scope; did you mean ‘PyDict_GetItemString’?
>>       430 |   PyObject *curr = PyDict_GetItemWithError (mi_cmd_dict.get (),
>>           |                    ^~~~~~~~~~~~~~~~~~~~~~~
>>           |                    PyDict_GetItemString
>>
>> A first solution to fix this would be to try to replace
>> PyDict_GetItemWithError equivalent Python 2 code.  But I looked at why
>> we are doing this in the first place: it is to maintain the
>> `gdb._mi_commands` Python dictionary that we use as a `name ->
>> gdb.MICommand object` map.  Since the `gdb._mi_commands` dictionary is
>> never actually used in Python, it seems like a lot of trouble to use a
>> Python object for this.
>>
>> My first idea was to replace it with a C++ map
>> (std::unordered_map<std::string, gdbpy_ref<micmdpy_object>>).  While
>> implementing this, I realized we don't really need this map at all.  The
>> mi_command_py objects registered in the main MI command table can own
>> their backing micmdpy_object (that's a gdb.MICommand, but seen from the
>> C++ code).  To know whether an mi_command is an mi_command_py, we can
>> use a dynamic cast.  Since there's one less data structure to maintain,
>> there are less chances of messing things up.
>>
>>  - Change mi_command_py::m_pyobj to a gdbpy_ref, the mi_command_py is
>>    now what keeps the MICommand alive.
>>  - Set micmdpy_object::mi_command in the constructor of mi_command_py.
>>    If mi_command_py manages setting/clearing that field in
>>    swap_python_object, I think it makes sense that it also takes care of
>>    setting it initially.
>>  - Move a bunch of checks from micmdpy_install_command to
>>    swap_python_object, and make them gdb_asserts.
>>  - In micmdpy_install_command, start by doing an mi_cmd_lookup.  This is
>>    needed to know whether there's a Python MI command already registered
>>    with that name.  But we can already tell if there's a non-Python
>>    command registered with that name.  Return an error if that happens,
>>    rather than waiting for insert_mi_cmd_entry to fail.  Change the
>>    error message to "name is already in use" rather than "may already be
>>    in use", since it's more precise.
> 
> This all looks good to me.  I think the small patch below should also
> be included though.
> 
> Thanks,
> Andrew
> 
> ---
> 
> diff --git a/gdb/python/py-micmd.c b/gdb/python/py-micmd.c
> index 18a199de6b9..9b2b7863827 100644
> --- a/gdb/python/py-micmd.c
> +++ b/gdb/python/py-micmd.c
> @@ -579,11 +579,10 @@ micmdpy_dealloc (PyObject *obj)
>  			(cmd->mi_command_name == nullptr
>  			 ? "(null)" : cmd->mi_command_name));
>  
> -  /* Remove the command from the MI command table if needed.  This will
> -     cause the mi_command_py object to be deleted, which, in turn, will
> -     clear the cmd->mi_command member variable, hence the assert.  */
> -  if (cmd->mi_command != nullptr)
> -    remove_mi_cmd_entry (cmd->mi_command->name ());
> +  /* As the mi_command_py object holds a reference to the micmdpy_object,
> +     the only way the dealloc function can be called is if he mi_command_py
> +     object has been deleted, in which case the following assert will
> +     hold.  */
>    gdb_assert (cmd->mi_command == nullptr);
>  
>    /* Free the memory that holds the command name.  */
> 

Good catch.  Ammended and pushed, thanks!

Simon

      reply	other threads:[~2022-03-19  0:29 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-18 19:55 Simon Marchi
2022-03-18 22:44 ` Andrew Burgess
2022-03-19  0:29   ` Simon Marchi [this message]

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=a89e55c6-63b0-4c2e-45fa-9d18f9d03310@polymtl.ca \
    --to=simon.marchi@polymtl.ca \
    --cc=aburgess@redhat.com \
    --cc=gdb-patches@sourceware.org \
    --cc=simon.marchi@efficios.com \
    /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).