Re: [RFA v2 7/8] Allow breakpoint commands to be set from Python

Phil Muldoon
2018-04-30T13:07:49Z

>
> @node Finish Breakpoints in Python
> diff --git a/gdb/python/py-breakpoint.c b/gdb/python/py-breakpoint.c
> index d654b92a8c..a66e553cf8 100644
> --- a/gdb/python/py-breakpoint.c
> +++ b/gdb/python/py-breakpoint.c
> @@ -510,6 +510,49 @@ bppy_get_commands (PyObject *self, void *closure)
> return host_string_to_python_string (stb.c_str ());
> }
>
> +/* Set the commands attached to a breakpoint. Returns 0 on success.
> + Returns -1 on error, with a python exception set. */
> +static int
> +bppy_set_commands (PyObject *self, PyObject *newvalue, void *closure)
> +{
> + gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
> + struct breakpoint *bp = self_bp->bp;
> + struct gdb_exception except = exception_none;
> +
> + BPPY_SET_REQUIRE_VALID (self_bp);
> +
> + gdb::unique_xmalloc_ptr<char> commands
> + (python_string_to_host_string (newvalue));
> + if (commands == nullptr)
> + return -1;
> +
> + TRY
> + {
> + bool first = true;
> + char *save_ptr = nullptr;
> + auto reader
> + = [&] ()
> + {
> + const char *result = strtok_r (first ? commands.get () : nullptr,
> + "\n", &save_ptr);
> + first = false;
> + return result;
> + };
> +
> + counted_command_line lines = read_command_lines_1 (reader, 1, nullptr);
> + breakpoint_set_commands (self_bp->bp, std::move (lines));
> + }
> + CATCH (ex, RETURN_MASK_ALL)
> + {
> + except = ex;
> + }
> + END_CATCH
> +
> + GDB_PY_SET_HANDLE_EXCEPTION (except);
> +
> + return 0;
> +}

The code bits LGTM but on a somewhat side note I'm wondering if there
are any side effects with the Python breakpoint stop callback? This
shouldn't interfere with the viability of this patch, though, because
a breakpoint previously could have had a command list attached to it
after being created in Python and via the commands command. I'm mildly
curious what would get called first.

Cheers

Phil