Re: [PATCH] Fix use of a dangling pointer for Python breakpoint objects

Pedro Alves
2016-06-23T16:15:01Z

On 06/21/2016 11:40 AM, Pierre-Marie de Rodat wrote:
> Hello,
>
> When a Python script tries to create a breakpoint but fails to do so,
> gdb.Breakpoint.__init__ raises an exception and the breakpoint does not
> exist anymore in the Python interpreter. However, GDB still keeps a
> reference to the Python object to be used for a later hook, which is
> wrong.

Urgh, this code is ugly.

So the problem is that the next time gdbpy_breakpoint_created
is called, for some other breakpoint, we'll dereference the dangling
pointer then, correct?

> +
> +# Skip all tests if Python scripting is not enabled.
> +if { [skip_python_tests] } { continue }
> +
> +gdb_test "source py-breakpoint2.py"
> +
> +# The following used to trigger an internal error because of a dangling
> +# reference to a Python breakpoint object.
> +gdb_test "start"

"start" doesn't work with "target remote" testing. Try:

$ make check \
RUNTESTFLAGS="--target_board=native-gdbserver" \
TESTS="gdb.python/py-breakpoint2.exp"

Can we instead run to main first, and then source the python
script?

> +
> +bp1 = gdb.Breakpoint('main', gdb.BP_BREAKPOINT)

I don't understand the idea behind creating this breakpoint
before the failing watchpoint one.

> +
> +# The following will create a breakpoint whose construction will abort (there
> +# is no such symbol), so GDB should not keep a reference to the corresponding
> +# Python object.
> +try:
> + bp2 = gdb.Breakpoint('does_not_exist', gdb.BP_WATCHPOINT)
> +except RuntimeError:
> + pass
> +else:
> + assert False

Wouldn't it better to create a breakpoint after the one
that failed, explicitly? Either in python, or perhaps
simpler, a regular command line breakpoint directly in
the .exp file.

Ah, I think I see -- I guess the test is relying on "start" creating
a magic breakpoint at "main", and that one being the one
that dereferences the dangling pointer. But, see above about
remote testing.

Thanks,
Pedro Alves