Re: [RFC] Python Finish Breakpoints

Kevin Pouget
2011-11-04T14:25:23Z

[-- Attachment #1: Type: text/plain, Size: 15517 bytes --]

On Wed, Nov 2, 2011 at 3:43 PM, Kevin Pouget <kevin.pouget@gmail.com> wrote:
> On Fri, Oct 28, 2011 at 10:37 PM, Tom Tromey <tromey@redhat.com> wrote:
>
> Hello,
> thanks for you're answer, I reply inline
>
>> You need a ChangeLog.
>
> sorry, I forgot to include it in the e-mail, you'll find it at the bottom
>
>> Kevin> -static PyTypeObject breakpoint_object_type;
>> Kevin> +PyTypeObject breakpoint_object_type;
>>
>> The old line here was a forward declaration.
>> I think you should just entirely remove it.
>>
>> Kevin> +  /* If it's a temporary breakpoint.  */
>> Kevin> +  if (bpfinishpy_is_finish_bp (py_bp))
>> Kevin> +    {
>> Kevin> +      /* Can't delete it here.  */
>> Kevin> +      gdb_assert (b->disposition == disp_del);
>> Kevin> +      disable_breakpoint (b);
>> Kevin> +    }
>>
>> I don't understand this code.
>> It seems to me that disp_del is a better setting here.
>
> I need to do it this way because as far as I've seen,
> "breakpoint_auto_delete" is only triggered in infrun.c::normal_stop,
> so if Breakpoint.stop returns False, the breakpoint is not deleted
> immediately, and may be hit twice.
> So I disable the breakpoint to avoid it.
>
>> Kevin> +      newbp->is_finish_bp = bpfinishpy_is_finish_bp ((PyObject *) newbp);
>>
>> I think this flag could be set more cleanly in bpfinishpy_init.
>
> you're right, fixed
>
>> Kevin> +  /* gdb.Type object of the function finished by this breakpoint.  */
>> Kevin> +  PyObject *function_type;
>> Kevin> +  /* gdb.Type object of the value return by the breakpointed function.  */
>> Kevin> +  PyObject *return_type;
>> Kevin> +  /* When stopped at this FinishBreakpoint, value returned by the function;
>> Kevin> +     Py_None if the value is not computable; NULL if GDB is not stopped at
>> Kevin> +     a FinishBreakpoint.  */
>> Kevin> +  PyObject *return_value;
>>
>> I think the new class needs a destructor which decrefs these.
>
> fixed
>
>> Kevin> +static void
>> Kevin> +bpfinish_stopped_at_finish_bp (struct finish_breakpoint_object *py_bp)
>> Kevin> +{
>> Kevin> +  if (py_bp->return_type)
>> Kevin> +    {
>> Kevin> +      struct value *ret =
>> Kevin> +          get_return_value (type_object_to_type (py_bp->function_type),
>> Kevin> +                            type_object_to_type (py_bp->return_type));
>>
>> Calls into gdb have to be wrapped in TRY_CATCH.
>> Then errors have to be propagated to Python somehow, or printed and
>> ignored via gdbpy_print_stack.  Given the context I would say propagate.
>
> you're right,
>
> bpfinish_stopped_at_finish_bp is called in 2 situations, so it just
> sets the error flag, then
>
> - direct access to returnvalue member --> propage exception to Python
> - bpfinishpy_handle_stop --> bpfinishpy_handle_stop()
>
>> Kevin> +  /* Check if we have a cached value.  */
>> Kevin> +  if (!self_finishbp->return_value)
>> Kevin> +    {
>> Kevin> +      bpstat bs;
>> Kevin> +
>> Kevin> +      BPPY_REQUIRE_VALID (&self_finishbp->py_bp);
>> Kevin> +
>> Kevin> +      for (bs = inferior_thread ()->control.stop_bpstat;
>> Kevin> +          bs; bs = bs->next)
>> Kevin> +        {
>> Kevin> +          struct breakpoint *bp = bs->breakpoint_at;
>> Kevin> +
>> Kevin> +          if (bp != NULL && (PyObject *) bp->py_bp_object == self)
>> Kevin> +              bpfinish_stopped_at_finish_bp (self_finishbp);
>> Kevin> +        }
>>
>> It seems like it should be an error to try to compute the return value
>> when not stopped at this breakpoint.
>
> I'm not totally convinced ...
> what would you think about throwing an AttributeError("return_value
> not available yet") when accessing the attribute before the breakpoint
> is hit, but keep the cached value afterwards?
>
>
>> Kevin> +struct breakpoint *
>> Kevin> +gdbpy_is_stopped_at_finish_bp (bpstat stop_bpstat)
>> Kevin> +{
>>
>> Since the name implies that this is a predicate and since the result is
>> only ever used as a boolean, I think this should return int.
>
> changed
>
>> Kevin> +  bpstat bs;
>> Kevin> +
>> Kevin> +  for (bs = stop_bpstat; bs; bs = bs->next)
>> Kevin> +    {
>> Kevin> +      if (bs->breakpoint_at
>> Kevin> +          && bpfinishpy_bp_is_finish_bp ((breakpoint_object *)
>> Kevin> +                                         bs->breakpoint_at->py_bp_object))
>>
>> I am not really sure about this.  It seems like it may be pedantically
>> incorrect, though it is hard to see when it could possibly fail.  That
>> is, is the GIL required or not?  It doesn't call a function and the
>> breakpoint owns a ref to the breakpoint object, so it seems like it
>> could not be deleted out from under us.
>>
>> I'm inclined to say it is ok.
>
> no, the GIL is not required.
> This function is triggered from infrun.c:normal_stop, to determine if
> we want to save
> the stop_registers, like it's done for `finish'
>
>> Kevin> +  breakpoint_object *self_bp = (breakpoint_object *) self;
>> Kevin> +  struct finish_breakpoint_object *self_bpfinish =
>> Kevin> +      (struct finish_breakpoint_object *) self;
>>
>> I think this is the only use of self in this function.
>> Just drop it and cast directly to the most specific subclass.
>
> I'm not sure what you meant, *self_bp* was redundant so I removed it
>
>> Kevin> +  prev_frame = get_prev_frame (frame);
>> Kevin> +  if (prev_frame == 0)
>> Kevin> +    {
>> Kevin> +      PyErr_SetString (PyExc_ValueError,
>> Kevin> +           _("\"FinishBreakpoint\" not meaningful in the outermost frame."));
>> Kevin> +      return -1;
>> Kevin> +    }
>> Kevin> +  else if (get_frame_type (prev_frame) == DUMMY_FRAME)
>> Kevin> +      {
>> Kevin> +        PyErr_SetString (PyExc_ValueError,
>> Kevin> +                  _("\"FinishBreakpoint\" cannot be set on a dummy frame."));
>> Kevin> +        return -1;
>> Kevin> +      }
>>
>> I think the calls to get_prev_frame and get_frame_type need to be
>> wrapped in a TRY_CATCH.
>>
>> Kevin> +  frame_id = get_frame_id (prev_frame);
>> Kevin> +  if (frame_id_eq (frame_id, null_frame_id))
>>
>> Likewise.
>> I'd try to put all the gdb-facing logic into a single big TRY_CATCH.
>
> I wrapped them all and propagate the error with gdbpy_convert_exception
>
>> Kevin> +  if (internal)
>> Kevin> +    {
>> Kevin> +      internal_bp = PyObject_IsTrue (internal);
>> Kevin> +      if (internal_bp == -1)
>> Kevin> +        {
>> Kevin> +          PyErr_SetString (PyExc_ValueError,
>> Kevin> +                           _("The value of `internal' must be a boolean."));
>> Kevin> +          return -1;
>>
>> Do you need to decref 'frame_obj' here?  I suspect so.
>> There are other early returns that probably need this.
>> A typical solution is a label where all the locals are xdecref'd then
>> return -1.
>
>> PyArg_ParseTupleAndKeywords (args, kwargs, "O|O", keywords, &frame_obj, &internal)
>
> No, according to the documentation,
>> Note that any Python object references which are provided to the caller are borrowed references; do not decrement their reference count!
>
>
>> Kevin> +  /* Find the function we will return from.  */
>> Kevin> +  self_bpfinish->return_type = NULL;
>> Kevin> +  self_bpfinish->function_type = NULL;
>>
>> These can be left NULL in the object.  What happens if you try to fetch
>> the return value in that case?
>
> I've updated the comments to make it clearer:
>
>  /* gdb.Type object of the value return by the breakpointed function.
>     May be NULL if no debug information was available or return type
>     was VOID.  */
>  PyObject *return_type;
>  /* gdb.Type object of the function finished by this breakpoint.  Will be
>     NULL if return_type is NULL.  */
>  PyObject *function_type;
>
> if return_type is NULL, "return_value" attribute will be None (the
> return value is not computed/computable)
>
> I've updated the doc as well:
>> The value will be @code{None} if the function return type is @code{VOID} or if the return value was not computable.
>
>> Kevin> +  if (get_frame_pc_if_available (frame, &pc))
>> Kevin> +    {
>> Kevin> +      function = find_pc_function (pc);
>> Kevin> +      if (function != NULL)
>> Kevin> +        {
>> Kevin> +          struct type *ret_type = TYPE_TARGET_TYPE (SYMBOL_TYPE (function));
>> Kevin> +
>>
>> More TRY_CATCH.
>>
>> Kevin> +          /* Remember only non-VOID return types.  */
>> Kevin> +          if (TYPE_CODE (ret_type) != TYPE_CODE_VOID)
>> Kevin> +            {
>> Kevin> +              self_bpfinish->return_type = type_to_type_object (ret_type);
>>
>> Error check.
>>
>> Kevin> +              self_bpfinish->function_type =
>> Kevin> +                  type_to_type_object (SYMBOL_TYPE (function));
>>
>> Likewise.
>
> I'm not sure about the best way to handle these exceptions, so for all
> the error thrown when trying to compute the return_type/function_type,
> I just silently swallow it, and set the return_type to None.
>
> Propagating the exception is not suitable, because I don't want not to
> prevent the F_BP creation just because of the return value;
> and I don't want to write a warning within Python code ...
>
>
>> Kevin> +  if (except.reason < 0)
>> Kevin> +    {
>> Kevin> +      PyErr_Format (except.reason == RETURN_QUIT
>> Kevin> +                    ? PyExc_KeyboardInterrupt : PyExc_RuntimeError,
>> Kevin> +                    "%s", except.message);
>> Kevin> +      return -1;
>>
>> Just use GDB_PY_SET_HANDLE_EXCEPTION.
>
> right, fixed
> (the same should apply to gdb.Breakpoint creation, I copied the code from there)
>
>> Kevin> +int
>> Kevin> +bpfinishpy_is_finish_bp (PyObject *obj)
>> Kevin> +{
>> Kevin> +  return obj != NULL
>> Kevin> +         && PyObject_TypeCheck (obj, &finish_breakpoint_object_type);
>> Kevin> +}
>>
>> Kevin> +int
>> Kevin> +bpfinishpy_bp_is_finish_bp (breakpoint_object *bp_obj)
>> Kevin> +{
>> Kevin> +  return bp_obj != NULL
>> Kevin> +         && bp_obj->is_finish_bp;
>> Kevin> +}
>>
>> Are both of these needed?
>
> no, not anymore,
> I even removed both of them,
>
>> newbp->is_finish_bp = bpfinishpy_is_finish_bp ((PyObject *) newbp);
>> I think this flag could be set more cleanly in bpfinishpy_init.
>
> but I've rewritten this bit
>
>> Kevin> +static void
>> Kevin> +bpfinishpy_handle_stop (struct bpstats *bs, int print_frame)
>> Kevin> +{
>> Kevin> +  iterate_over_breakpoints (bpfinishpy_detect_out_scope_cb,
>> Kevin> +                            bs == NULL ? NULL : bs->breakpoint_at);
>> Kevin> +}
>>
>> The way this is written, it will acquire and release the GIL for each
>> breakpoint.
>>
>> I think it would be better to acquire + release just once.
>>
>> Kevin> +static void
>> Kevin> +bpfinishpy_handle_exit (struct inferior *inf)
>> Kevin> +{
>> Kevin> +  iterate_over_breakpoints (bpfinishpy_detect_out_scope_cb, NULL);
>> Kevin> +}
>>
>> Likewise.
>
> fixed
>
> I'm not 100% confident with this "ensure_python_env" and the value of
> the "arch" parameter;
> I based my code on how py-inferior does, so:
>
> observer "normal stop" --> get_current_arch ()
> observer "exit" --> target_gdbarch
>
> and I kept
>> garch = b->gdbarch ? b->gdbarch : get_current_arch ();
> in the callback, to ensure that the arch is actually the one from the BP, if any
>
>
> let me know if this is not the right way to do
>
>> Kevin> +struct breakpoint *gdbpy_is_stopped_at_finish_bp (bpstat stop_bpstat);
>> Kevin>  #endif /* GDB_PYTHON_H */
>>
>> Newline between these two lines.
>
> fixed
>
> and finally, from the previous mail
>
>> You could subclass breakpoint and add a new field there.  This would
>> segregate the changes in the python subdirectory.  This may be best.
>
>> Alternatively you could make a bp_finish breakpoint.  I'm not sure
>> whether this is sufficient or whether you would also need an additional
>> flag.  If the latter you could make a bp_finish_python or something like
>> that -- I said earlier that this was probably invasive, but grepping for
>> bp_finish makes me think it wouldn't be too bad.
>
>
> I'm not sure about what you mean exactly, seems like half of it was
> already done ... anyway, I think that the original problem is almost
> gone:
>
> - FinishBreakpoint inherits from Breakpoint
> - there is a field "is_finish_bp" in gdb.Breakpoint C structure,
> - infrun.c can know without the GIL if a breakpoint object is a
> FinishBreakpoint using "gdbpy_is_stopped_at_finish_bp"
>
>
> Thanks for this thorough review,
>
> Kevin

Hello,

there was a little confusion in the previous patch,
ensure_python_env/cleanup commented out,

here is a cleaner version.

Thanks,

Kevin

2011-11-04 Kevin Pouget <kevin.pouget@st.com>

Introduce gdb.FinishBreakpoints in Python

* Makefile.in (SUBDIR_PYTHON_OBS): Add py-finishbreakpoint.o.
(SUBDIR_PYTHON_SRCS): Add python/py-finishbreakpoint.c.
Add build rule for this file.
* infcmd.c (get_return_value): New function.
(print_return_value): Split to create get_return_value.
* inferior.h (get_return_value): New prototype.
* infrun.c: Include python/python.h.
(stop_registers): Mention FinishBreakpoint in description.
(normal_stop): Set stop_registers if stopped at FinishBreakpoint.
* python/py-breakpoint.c (bppy_pending_object): Make non-static.
(gdbpy_should_stop): Disable temporary breakpoints.
(gdbpy_breakpoint_created): Set is_py_finish_bp is necessary.
(struct breakpoint_object): Move to python-internal.h
(BPPY_REQUIRE_VALID): Likewise.
(BPPY_SET_REQUIRE_VALID): Likewise.
(gdbpy_breakpoint_created): Initialize is_finish_bp.
* python/python-internal.h (breakpoint_object_type): Add as extern.
(bppy_pending_object): Likewise.
(typedef struct breakpoint_object) Removed.
(struct breakpoint_object): Moved from py-breakpoint.c.
Add field is_finish_bp.
(BPPY_REQUIRE_VALID): Likewise.
(BPPY_SET_REQUIRE_VALID): Likewise.
(frame_object_to_frame_info): New prototype.
(gdbpy_initialize_finishbreakpoints): New prototype.
(bpfinishpy_is_finish_bp): Likewise.
* python/py-finishbreakpoint.c: New file.
* python/py-frame.c(frame_object_to_frame_info): Make non-static and
accept PyObject instead of frame_object.
(frapy_is_valid): Don't cast to frame_object.
(frapy_name): Likewise.
(frapy_type): Likewise.
(frapy_unwind_stop_reason): Likewise.
(frapy_pc): Likewise.
(frapy_block): Likewise.
(frapy_function): Likewise.
(frapy_older): Likewise.
(frapy_newer): Likewise.
(frapy_find_sal): Likewise.
(frapy_read_var): Likewise.
(frapy_select): Likewise.
* python/python.c (gdbpy_is_stopped_at_finish_bp): New noop function.
(_initialize_python): Add gdbpy_initialize_finishbreakpoints.
* python/python.h: Include breakpoint.h
(gdbpy_is_stopped_at_finish_bp): New prototype.

doc/
* gdb.texinfo (Breakpoints In Python): New subsection: Finish
Breakpoints.
(Python API): Add menu entry for Finish Breakpoints

testsuite/
* gdb.python/py-breakpoint.exp (mult_line): Define and use variable
instead of line number.
* gdb.python/py-finish-breakpoint.c: New file.
* gdb.python/py-finish-breakpoint.exp: New file.
* gdb.python/py-finish-breakpoint.py: New file.
* gdb.python/py-finish-breakpoint2.cc: New file.
* gdb.python/py-finish-breakpoint2.exp: New file.
* gdb.python/py-finish-breakpoint2.py: New file.

[-- Attachment #2: 0001-Introduce-gdb.FinishBreakpoints.patch --]
[-- Type: text/x-patch, Size: 55389 bytes --]

From ad87340e9eaa52b1b72c7ab7837fe02ac243e3ba Mon Sep 17 00:00:00 2001
From: Kevin Pouget <kevin.pouget@st.com>
Date: Tue, 20 Sep 2011 13:59:23 +0200
Subject: [PATCH] Introduce gdb.FinishBreakpoint

---
gdb/Makefile.in | 6 +
gdb/NEWS | 4 +
gdb/doc/gdb.texinfo | 50 +++
gdb/infcmd.c | 21 +-
gdb/inferior.h | 3 +
gdb/infrun.c | 9 +-
gdb/python/py-breakpoint.c | 50 +--
gdb/python/py-finishbreakpoint.c | 443 ++++++++++++++++++++
gdb/python/py-frame.c | 32 +-
gdb/python/python-internal.h | 47 ++-
gdb/python/python.c | 6 +
gdb/python/python.h | 3 +
gdb/testsuite/gdb.python/py-breakpoint.exp | 7 +-
gdb/testsuite/gdb.python/py-finish-breakpoint.c | 97 +++++
gdb/testsuite/gdb.python/py-finish-breakpoint.exp | 230 ++++++++++
gdb/testsuite/gdb.python/py-finish-breakpoint.py | 88 ++++
gdb/testsuite/gdb.python/py-finish-breakpoint2.cc | 59 +++
gdb/testsuite/gdb.python/py-finish-breakpoint2.exp | 65 +++
gdb/testsuite/gdb.python/py-finish-breakpoint2.py | 34 ++
19 files changed, 1188 insertions(+), 66 deletions(-)
create mode 100644 gdb/python/py-finishbreakpoint.c
create mode 100644 gdb/testsuite/gdb.python/py-finish-breakpoint.c
create mode 100644 gdb/testsuite/gdb.python/py-finish-breakpoint.exp
create mode 100644 gdb/testsuite/gdb.python/py-finish-breakpoint.py
create mode 100644 gdb/testsuite/gdb.python/py-finish-breakpoint2.cc
create mode 100644 gdb/testsuite/gdb.python/py-finish-breakpoint2.exp
create mode 100644 gdb/testsuite/gdb.python/py-finish-breakpoint2.py

diff --git a/gdb/Makefile.in b/gdb/Makefile.in
index 7a7ff9f..31cda1b 100644
--- a/gdb/Makefile.in
+++ b/gdb/Makefile.in
@@ -287,6 +287,7 @@ SUBDIR_PYTHON_OBS = \
py-evtregistry.o \
py-evts.o \
py-exitedevent.o \
+ py-finishbreakpoint.o \
py-frame.o \
py-function.o \
py-inferior.o \
@@ -318,6 +319,7 @@ SUBDIR_PYTHON_SRCS = \
python/py-evtregistry.c \
python/py-evts.c \
python/py-exitedevent.c \
+ python/py-finishbreakpoint.c \
python/py-frame.c \
python/py-function.c \
python/py-inferior.c \
@@ -2103,6 +2105,10 @@ py-exitedevent.o: $(srcdir)/python/py-exitedevent.c
$(COMPILE) $(PYTHON_CFLAGS) $(srcdir)/python/py-exitedevent.c
$(POSTCOMPILE)

+py-finishbreakpoint.o: $(srcdir)/python/py-finishbreakpoint.c
+ $(COMPILE) $(PYTHON_CFLAGS) $(srcdir)/python/py-finishbreakpoint.c
+ $(POSTCOMPILE)
+
py-frame.o: $(srcdir)/python/py-frame.c
$(COMPILE) $(PYTHON_CFLAGS) $(srcdir)/python/py-frame.c
$(POSTCOMPILE)
diff --git a/gdb/NEWS b/gdb/NEWS
index 1713049..b170b0a 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -51,6 +51,10 @@
** The "gdb.breakpoint" function has been deprecated in favor of
"gdb.breakpoints".

+ ** A new class "gdb.FinishBreakpoint" is provided to catch the return
+ of a function. This class is based on the "finish" command
+ available in the CLI.
+
** Type objects for struct and union types now allow access to
the fields using standard Python dictionary (mapping) methods.
For example, "some_type['myfield']" now works, as does
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index 93450c6..3de11fc 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -21405,6 +21405,8 @@ situation, a Python @code{KeyboardInterrupt} exception is thrown.
* Symbol Tables In Python:: Python representation of symbol tables.
* Lazy Strings In Python:: Python representation of lazy strings.
* Breakpoints In Python:: Manipulating breakpoints using Python.
+* Finish Breakpoints in Python:: Setting Breakpoints on function return
+ using Python.
@end menu

@node Basic Python
@@ -24239,6 +24241,54 @@ commands, separated by newlines. If there are no commands, this
attribute is @code{None}. This attribute is not writable.
@end defvar

+@node Finish Breakpoints in Python
+@subsubsection Finish Breakpoints
+
+@cindex python finish breakpoints
+@tindex gdb.FinishBreakpoint
+
+A finish breakpoint is a temporary breakpoint set at the return address of
+a frame, based on the @code{finish} command. @code{gdb.FinishBreakpoint}
+extends @code{gdb.Breakpoint}. The underlying breakpoint will be disabled
+and deleted when the execution will run out of the breakpoint scope (i.e.@:
+@code{Breakpoint.stop} or @code{FinishBreakpoint.out_of_scope} triggered).
+
+@defun FinishBreakpoint.__init__ (@var{frame}, @r{[}@var{internal}@r{]})
+Create a finish breakpoint at the return address of the @code{gdb.Frame}
+object @var{frame}. The optional @var{internal} argument allows the
+breakpoint to become invisible to the user. @xref{Breakpoints In Python},
+for further details about this argument.
+@end defun
+
+@defun FinishBreakpoint.out_of_scope (self)
+In some circumstances (e.g.@: @code{longjmp}, C@t{++} exceptions, @value{GDBN}
+@code{return} command, @dots{}), a function may not properly terminate, and
+thus never hit the finish breakpoint. When @value{GDBN} notices such a
+situation, the @code{out_of_scope} callback will be triggered.
+
+You may want to sub-class @code{gdb.FinishBreakpoint} and override this
+method:
+
+@smallexample
+class MyFinishBreakpoint (gdb.FinishBreakpoint)
+ def stop (self):
+ print "normal finish"
+ return True
+
+ def out_of_scope ():
+ print "abnormal finish"
+@end smallexample
+@end defun
+
+@defvar FinishBreakpoint.return_value
+When @value{GDBN} is stopped at a finish breakpoint and the frame
+used to build the @code{gdb.FinishBreakpoint} object had debug symbols, this
+attribute will contain a @code{gdb.Value} object corresponding to the return
+value of the function. The value will be @code{None} if the function return
+type is @code{VOID} or if the return value was not computable. This attribute
+is not writable.
+@end defvar
+
@node Lazy Strings In Python
@subsubsection Python representation of lazy strings.

diff --git a/gdb/infcmd.c b/gdb/infcmd.c
index 7b935fe..ce21851 100644
--- a/gdb/infcmd.c
+++ b/gdb/infcmd.c
@@ -1413,14 +1413,12 @@ advance_command (char *arg, int from_tty)
until_break_command (arg, from_tty, 1);
}
\f
-/* Print the result of a function at the end of a 'finish' command. */
+/* Return the value of the result at the end of a 'finish' command/BP. */

-static void
-print_return_value (struct type *func_type, struct type *value_type)
+struct value *
+get_return_value (struct type *func_type, struct type *value_type)
{
struct gdbarch *gdbarch = get_regcache_arch (stop_registers);
- struct cleanup *old_chain;
- struct ui_stream *stb;
struct value *value;
struct ui_out *uiout = current_uiout;

@@ -1451,6 +1449,19 @@ print_return_value (struct type *func_type, struct type *value_type)
internal_error (__FILE__, __LINE__, _("bad switch"));
}

+ return value;
+}
+
+/* Print the result of a function at the end of a 'finish' command. */
+
+static void
+print_return_value (struct type *func_type, struct type *value_type)
+{
+ struct value *value = get_return_value (func_type, value_type);
+ struct cleanup *old_chain;
+ struct ui_stream *stb;
+ struct ui_out *uiout = current_uiout;
+
if (value)
{
struct value_print_options opts;
diff --git a/gdb/inferior.h b/gdb/inferior.h
index cfaea7f..f198696 100644
--- a/gdb/inferior.h
+++ b/gdb/inferior.h
@@ -269,6 +269,9 @@ extern void detach_command (char *, int);

extern void notice_new_inferior (ptid_t, int, int);

+extern struct value *get_return_value (struct type *func_type,
+ struct type *value_type);
+
/* Address at which inferior stopped. */

extern CORE_ADDR stop_pc;
diff --git a/gdb/infrun.c b/gdb/infrun.c
index 7ec0788..4d6db30 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -57,6 +57,7 @@
#include "continuations.h"
#include "interps.h"
#include "skip.h"
+#include "python/python.h"

/* Prototypes for local functions */

@@ -366,7 +367,8 @@ show_stop_on_solib_events (struct ui_file *file, int from_tty,
int stop_after_trap;

/* Save register contents here when executing a "finish" command or are
- about to pop a stack dummy frame, if-and-only-if proceed_to_finish is set.
+ about to pop a stack dummy frame, if-and-only-if proceed_to_finish is set
+ or a Python FinishBreakpoint has been hit.
Thus this contains the return value from the called function (assuming
values are returned in a register). */

@@ -6030,8 +6032,9 @@ normal_stop (void)

/* Save the function value return registers, if we care.
We might be about to restore their previous contents. */
- if (inferior_thread ()->control.proceed_to_finish
- && execution_direction != EXEC_REVERSE)
+ if (gdbpy_is_stopped_at_finish_bp (inferior_thread ()->control.stop_bpstat)
+ || (inferior_thread ()->control.proceed_to_finish
+ && execution_direction != EXEC_REVERSE))
{
/* This should not be necessary. */
if (stop_registers)
diff --git a/gdb/python/py-breakpoint.c b/gdb/python/py-breakpoint.c
index f235bbc..67de8de 100644
--- a/gdb/python/py-breakpoint.c
+++ b/gdb/python/py-breakpoint.c
@@ -31,52 +31,16 @@
#include "arch-utils.h"
#include "language.h"

-static PyTypeObject breakpoint_object_type;
-
/* Number of live breakpoints. */
static int bppy_live;

/* Variables used to pass information between the Breakpoint
constructor and the breakpoint-created hook function. */
-static breakpoint_object *bppy_pending_object;
+breakpoint_object *bppy_pending_object;

/* Function that is called when a Python condition is evaluated. */
static char * const stop_func = "stop";

-struct breakpoint_object
-{
- PyObject_HEAD
-
- /* The breakpoint number according to gdb. */
- int number;
-
- /* The gdb breakpoint object, or NULL if the breakpoint has been
- deleted. */
- struct breakpoint *bp;
-};
-
-/* Require that BREAKPOINT be a valid breakpoint ID; throw a Python
- exception if it is invalid. */
-#define BPPY_REQUIRE_VALID(Breakpoint) \
- do { \
- if ((Breakpoint)->bp == NULL) \
- return PyErr_Format (PyExc_RuntimeError, \
- _("Breakpoint %d is invalid."), \
- (Breakpoint)->number); \
- } while (0)
-
-/* Require that BREAKPOINT be a valid breakpoint ID; throw a Python
- exception if it is invalid. This macro is for use in setter functions. */
-#define BPPY_SET_REQUIRE_VALID(Breakpoint) \
- do { \
- if ((Breakpoint)->bp == NULL) \
- { \
- PyErr_Format (PyExc_RuntimeError, _("Breakpoint %d is invalid."), \
- (Breakpoint)->number); \
- return -1; \
- } \
- } while (0)
-
/* This is used to initialize various gdb.bp_* constants. */
struct pybp_code
{
@@ -777,6 +741,15 @@ gdbpy_should_stop (struct breakpoint_object *bp_obj)
else
gdbpy_print_stack ();
}
+
+ /* If it's a temporary breakpoint. */
+ if (bp_obj->is_finish_bp)
+ {
+ /* Can't delete it here. */
+ gdb_assert (b->disposition == disp_del);
+ disable_breakpoint (b);
+ }
+
do_cleanups (cleanup);

return stop;
@@ -839,6 +812,7 @@ gdbpy_breakpoint_created (struct breakpoint *bp)
newbp->number = bp->number;
newbp->bp = bp;
newbp->bp->py_bp_object = newbp;
+ newbp->is_finish_bp = 0;
Py_INCREF (newbp);
++bppy_live;
}
@@ -1000,7 +974,7 @@ static PyMethodDef breakpoint_object_methods[] =
{ NULL } /* Sentinel. */
};

-static PyTypeObject breakpoint_object_type =
+PyTypeObject breakpoint_object_type =
{
PyObject_HEAD_INIT (NULL)
0, /*ob_size*/
diff --git a/gdb/python/py-finishbreakpoint.c b/gdb/python/py-finishbreakpoint.c
new file mode 100644
index 0000000..26dc7ef
--- /dev/null
+++ b/gdb/python/py-finishbreakpoint.c
@@ -0,0 +1,443 @@
+/* Python interface to finish breakpoints
+
+ Copyright (C) 2011 Free Software Foundation, Inc.
+
+ This file is part of GDB.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+
+
+#include "defs.h"
+#include "exceptions.h"
+#include "python-internal.h"
+#include "breakpoint.h"
+#include "frame.h"
+#include "gdbthread.h"
+#include "arch-utils.h"
+#include "language.h"
+#include "observer.h"
+#include "inferior.h"
+
+static PyTypeObject finish_breakpoint_object_type;
+
+/* Function that is called when a Python finish bp is found out of scope. */
+static char * const outofscope_func = "out_of_scope";
+
+/* struct implementing the gdb.FinishBreakpoint object by extending
+ the gdb.Breakpoint class. */
+struct finish_breakpoint_object
+{
+ /* gdb.Breakpoint base class. */
+ breakpoint_object py_bp;
+ /* gdb.Type object of the value return by the breakpointed function.
+ May be NULL if no debug information was available or return type
+ was VOID. */
+ PyObject *return_type;
+ /* gdb.Type object of the function finished by this breakpoint. Will be
+ NULL if return_type is NULL. */
+ PyObject *function_type;
+ /* When stopped at this FinishBreakpoint, gdb.Value object returned by
+ the function; Py_None if the value is not computable; NULL if GDB is
+ not stopped at a FinishBreakpoint. */
+ PyObject *return_value;
+};
+
+/* Triggered when GDB stops at PY_BP. Computes and caches the `return_value',
+ if available. May set Python exception flag if the return value couldn't
+ be computed. */
+
+static void
+bpfinish_stopped_at_finish_bp (struct finish_breakpoint_object *py_bp)
+{
+ volatile struct gdb_exception except;
+
+ if (!py_bp->return_type)
+ return;
+
+ TRY_CATCH (except, RETURN_MASK_ALL)
+ {
+ struct value *ret =
+ get_return_value (type_object_to_type (py_bp->function_type),
+ type_object_to_type (py_bp->return_type));
+
+ if (ret)
+ py_bp->return_value = value_to_value_object (ret);
+ else
+ py_bp->return_value = Py_None;
+ }
+ if (except.reason < 0)
+ gdbpy_convert_exception(except);
+}
+
+/* Python function to get the 'return_value' attribute of
+ FinishBreakpoint. */
+
+static PyObject *
+bpfinishpy_get_returnvalue (PyObject *self, void *closure)
+{
+ struct finish_breakpoint_object *self_finishbp =
+ (struct finish_breakpoint_object *) self;
+
+ if (self_finishbp->return_type == NULL)
+ Py_RETURN_NONE;
+
+ /* Check if we have a cached value. */
+ if (!self_finishbp->return_value)
+ {
+ bpstat bs;
+
+ BPPY_REQUIRE_VALID (&self_finishbp->py_bp);
+
+ for (bs = inferior_thread ()->control.stop_bpstat;
+ bs; bs = bs->next)
+ {
+ struct breakpoint *bp = bs->breakpoint_at;
+
+ if (bp != NULL && (PyObject *) bp->py_bp_object == self)
+ {
+ bpfinish_stopped_at_finish_bp (self_finishbp);
+ if (PyErr_Occurred ())
+ return NULL;
+ }
+ }
+ }
+
+ if (!self_finishbp->return_value)
+ Py_RETURN_NONE;
+
+ Py_INCREF (self_finishbp->return_value);
+ return self_finishbp->return_value;
+}
+
+/* Deallocate FinishBreakpoint object. */
+
+static void
+bpfinishpy_dealloc (PyObject *self)
+{
+ struct finish_breakpoint_object *self_bpfinish =
+ (struct finish_breakpoint_object *) self;
+
+ Py_XDECREF (self_bpfinish->function_type);
+ Py_XDECREF (self_bpfinish->return_type);
+ Py_XDECREF (self_bpfinish->return_value);
+}
+
+/* If STOP_BPSTAT contains a Python breakpoint whose type is TYPE, returns
+ this breakpoint. Returns NULL otherwise. */
+
+int
+gdbpy_is_stopped_at_finish_bp (bpstat stop_bpstat)
+{
+ bpstat bs;
+
+ for (bs = stop_bpstat; bs; bs = bs->next)
+ {
+ if (bs->breakpoint_at
+ && bs->breakpoint_at->py_bp_object
+ && ((breakpoint_object *)
+ bs->breakpoint_at->py_bp_object)->is_finish_bp)
+ return 1;
+ }
+
+ return 0;
+}
+
+/* Python function to create a new breakpoint. */
+
+static int
+bpfinishpy_init (PyObject *self, PyObject *args, PyObject *kwargs)
+{
+ static char *keywords[] = { "frame", "internal", NULL };
+ struct finish_breakpoint_object *self_bpfinish =
+ (struct finish_breakpoint_object *) self;
+ int type = bp_breakpoint;
+ PyObject *frame_obj = NULL;
+ struct frame_info *frame, *prev_frame = NULL;
+ struct frame_id frame_id;
+ PyObject *internal = NULL;
+ int internal_bp = 0;
+ CORE_ADDR finish_pc, pc;
+ volatile struct gdb_exception except;
+ char *addr_str, small_buf[100];
+ struct symbol *function;
+
+ if (!PyArg_ParseTupleAndKeywords (args, kwargs, "O|O", keywords,
+ &frame_obj, &internal))
+ return -1;
+
+ if (!frame_obj)
+ goto invalid_frame;
+
+ frame = frame_object_to_frame_info (frame_obj);
+ if (frame == NULL)
+ goto invalid_frame;
+
+ TRY_CATCH (except, RETURN_MASK_ALL)
+ {
+ prev_frame = get_prev_frame (frame);
+ if (prev_frame == 0)
+ {
+ PyErr_SetString (PyExc_ValueError,
+ _("\"FinishBreakpoint\" not meaningful in the outermost frame."));
+ }
+ else if (get_frame_type (prev_frame) == DUMMY_FRAME)
+ {
+ PyErr_SetString (PyExc_ValueError,
+ _("\"FinishBreakpoint\" cannot be set on a dummy frame."));
+ }
+ else
+ {
+ frame_id = get_frame_id (prev_frame);
+ if (frame_id_eq (frame_id, null_frame_id))
+ PyErr_SetString (PyExc_ValueError,
+ _("Invalid ID for the `frame' object."));
+ }
+ }
+ if (except.reason < 0)
+ {
+ gdbpy_convert_exception(except);
+ return -1;
+ }
+ else if (PyErr_Occurred ())
+ return -1;
+
+ if (internal)
+ {
+ internal_bp = PyObject_IsTrue (internal);
+ if (internal_bp == -1)
+ {
+ PyErr_SetString (PyExc_ValueError,
+ _("The value of `internal' must be a boolean."));
+ return -1;
+ }
+ }
+
+ /* Find the function we will return from. */
+ self_bpfinish->return_type = NULL;
+ self_bpfinish->function_type = NULL;
+
+ TRY_CATCH (except, RETURN_MASK_ALL)
+ {
+ if (get_frame_pc_if_available (frame, &pc))
+ {
+ function = find_pc_function (pc);
+ if (function != NULL)
+ {
+ struct type *ret_type =
+ TYPE_TARGET_TYPE (SYMBOL_TYPE (function));
+
+ /* Remember only non-VOID return types. */
+ if (TYPE_CODE (ret_type) != TYPE_CODE_VOID)
+ {
+ self_bpfinish->return_type = type_to_type_object (ret_type);
+ self_bpfinish->function_type =
+ type_to_type_object (SYMBOL_TYPE (function));
+ }
+ }
+ }
+ }
+ if (except.reason < 0
+ || !self_bpfinish->return_type || !self_bpfinish->function_type)
+ {
+ /* Won't be able to compute return value. */
+ self_bpfinish->return_type = NULL;
+ self_bpfinish->function_type = NULL;
+ }
+
+ bppy_pending_object = &self_bpfinish->py_bp;
+ bppy_pending_object->number = -1;
+ bppy_pending_object->bp = NULL;
+
+ TRY_CATCH (except, RETURN_MASK_ALL)
+ {
+ /* Set a breakpoint on the return address. */
+ finish_pc = get_frame_pc (prev_frame);
+ sprintf (small_buf, "*%s", hex_string (finish_pc));
+ addr_str = small_buf;
+
+ create_breakpoint (python_gdbarch,
+ addr_str, NULL, -1,
+ 0,
+ 1 /*temp_flag*/,
+ bp_breakpoint,
+ 0,
+ AUTO_BOOLEAN_TRUE,
+ &bkpt_breakpoint_ops,
+ 0, 1, internal_bp);
+ }
+ GDB_PY_SET_HANDLE_EXCEPTION (except);
+
+ BPPY_SET_REQUIRE_VALID (&self_bpfinish->py_bp);
+
+ self_bpfinish->py_bp.bp->frame_id = frame_id;
+ self_bpfinish->py_bp.is_finish_bp = 1;
+
+ return 0;
+
+ invalid_frame:
+ PyErr_SetString (PyExc_ValueError,
+ _("Invalid ID for the `frame' object."));
+ return -1;
+}
+
+/* Called when GDB notices that the finish breakpoint BP_OBJ is out of
+ the current callstack. Triggers the method OUT_OF_SCOPE if implemented,
+ then delete the breakpoint. */
+
+static void
+bpfinishpy_out_of_scope (struct finish_breakpoint_object *bpfinish_obj)
+{
+ breakpoint_object *bp_obj = (breakpoint_object *) bpfinish_obj;
+ PyObject *py_obj = (PyObject *) bp_obj;
+
+ if (PyObject_HasAttrString (py_obj, outofscope_func))
+ {
+ if (!PyObject_CallMethod (py_obj, outofscope_func, NULL))
+ gdbpy_print_stack ();
+ }
+
+ delete_breakpoint (bpfinish_obj->py_bp.bp);
+}
+
+/* Callback for `bpfinishpy_detect_out_scope'. Triggers Python's
+ `B->out_of_scope' function if B is a FinishBreakpoint out of its
+ scope or turn of out_of_scope notification if B has been hit. */
+
+static int
+bpfinishpy_detect_out_scope_cb (struct breakpoint *b, void *args)
+{
+ struct breakpoint *bp_stopped = (struct breakpoint *) args;
+ PyObject *py_bp = (PyObject *) b->py_bp_object;
+ struct gdbarch *garch = b->gdbarch ? b->gdbarch : get_current_arch ();
+ struct cleanup *cleanup = ensure_python_env (garch, current_language);
+
+ /* Trigger out_of_scope if this is a FinishBreakpoint and its frame is
+ not anymore in the current callstack. */
+ if (py_bp != NULL && b->py_bp_object->is_finish_bp)
+ {
+ struct finish_breakpoint_object *finish_bp =
+ (struct finish_breakpoint_object *) py_bp;
+
+ if (b == bp_stopped)
+ {
+ bpfinish_stopped_at_finish_bp (finish_bp);
+ if (PyErr_Occurred ())
+ gdbpy_print_stack ();
+ }
+ else if (b->pspace == current_inferior ()->pspace
+ && (!target_has_registers
+ || frame_find_by_id (b->frame_id) == NULL))
+ {
+ bpfinishpy_out_of_scope (finish_bp);
+ }
+ }
+ do_cleanups (cleanup);
+ return 0;
+}
+
+/* Attached to `stop' notifications, check if the execution has run
+ out of the scope of any FinishBreakpoint before it has been hit. */
+
+static void
+bpfinishpy_handle_stop (struct bpstats *bs, int print_frame)
+{
+ struct cleanup *cleanup = ensure_python_env (get_current_arch (),
+ current_language);
+
+ iterate_over_breakpoints (bpfinishpy_detect_out_scope_cb,
+ bs == NULL ? NULL : bs->breakpoint_at);
+
+ do_cleanups (cleanup);
+}
+
+/* Attached to `exit' notifications, triggers all the necessary out of
+ scope notifications. */
+
+static void
+bpfinishpy_handle_exit (struct inferior *inf)
+{
+ struct cleanup *cleanup = ensure_python_env (target_gdbarch,
+ current_language);
+
+ iterate_over_breakpoints (bpfinishpy_detect_out_scope_cb, NULL);
+
+ do_cleanups (cleanup);
+}
+
+/* Initialize the Python finish breakpoint code. */
+
+void
+gdbpy_initialize_finishbreakpoints (void)
+{
+ if (PyType_Ready (&finish_breakpoint_object_type) < 0)
+ return;
+
+ Py_INCREF (&finish_breakpoint_object_type);
+ PyModule_AddObject (gdb_module, "FinishBreakpoint",
+ (PyObject *) &finish_breakpoint_object_type);
+
+ observer_attach_normal_stop (bpfinishpy_handle_stop);
+ observer_attach_inferior_exit (bpfinishpy_handle_exit);
+}
+
+static PyGetSetDef finish_breakpoint_object_getset[] = {
+ { "return_value", bpfinishpy_get_returnvalue, NULL,
+ "gdb.Value object representing the return value, if any. \
+None otherwise.", NULL },
+ { NULL } /* Sentinel. */
+};
+
+static PyTypeObject finish_breakpoint_object_type =
+{
+ PyObject_HEAD_INIT (NULL)
+ 0, /*ob_size*/
+ "gdb.FinishBreakpoint", /*tp_name*/
+ sizeof (struct finish_breakpoint_object), /*tp_basicsize*/
+ 0, /*tp_itemsize*/
+ bpfinishpy_dealloc, /*tp_dealloc*/
+ 0, /*tp_print*/
+ 0, /*tp_getattr*/
+ 0, /*tp_setattr*/
+ 0, /*tp_compare*/
+ 0, /*tp_repr*/
+ 0, /*tp_as_number*/
+ 0, /*tp_as_sequence*/
+ 0, /*tp_as_mapping*/
+ 0, /*tp_hash */
+ 0, /*tp_call*/
+ 0, /*tp_str*/
+ 0, /*tp_getattro*/
+ 0, /*tp_setattro */
+ 0, /*tp_as_buffer*/
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
+ "GDB finish breakpoint object", /* tp_doc */
+ 0, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ 0, /* tp_methods */
+ 0, /* tp_members */
+ finish_breakpoint_object_getset,/* tp_getset */
+ &breakpoint_object_type, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ bpfinishpy_init, /* tp_init */
+ 0, /* tp_alloc */
+ 0 /* tp_new */
+};
diff --git a/gdb/python/py-frame.c b/gdb/python/py-frame.c
index 20064ca..c334f63 100644
--- a/gdb/python/py-frame.c
+++ b/gdb/python/py-frame.c
@@ -60,9 +60,10 @@ static PyTypeObject frame_object_type;
object. If the frame doesn't exist anymore (the frame id doesn't
correspond to any frame in the inferior), returns NULL. */

-static struct frame_info *
-frame_object_to_frame_info (frame_object *frame_obj)
+struct frame_info *
+frame_object_to_frame_info (PyObject *obj)
{
+ frame_object *frame_obj = (frame_object *) obj;
struct frame_info *frame;

frame = frame_find_by_id (frame_obj->frame_id);
@@ -106,7 +107,7 @@ frapy_is_valid (PyObject *self, PyObject *args)

TRY_CATCH (except, RETURN_MASK_ALL)
{
- frame = frame_object_to_frame_info ((frame_object *) self);
+ frame = frame_object_to_frame_info (self);
}
GDB_PY_HANDLE_EXCEPTION (except);

@@ -130,7 +131,7 @@ frapy_name (PyObject *self, PyObject *args)

TRY_CATCH (except, RETURN_MASK_ALL)
{
- FRAPY_REQUIRE_VALID ((frame_object *) self, frame);
+ FRAPY_REQUIRE_VALID (self, frame);

find_frame_funname (frame, &name, &lang, NULL);
}
@@ -159,7 +160,7 @@ frapy_type (PyObject *self, PyObject *args)

TRY_CATCH (except, RETURN_MASK_ALL)
{
- FRAPY_REQUIRE_VALID ((frame_object *) self, frame);
+ FRAPY_REQUIRE_VALID (self, frame);

type = get_frame_type (frame);
}
@@ -180,7 +181,7 @@ frapy_unwind_stop_reason (PyObject *self, PyObject *args)

TRY_CATCH (except, RETURN_MASK_ALL)
{
- FRAPY_REQUIRE_VALID ((frame_object *) self, frame);
+ FRAPY_REQUIRE_VALID (self, frame);
}
GDB_PY_HANDLE_EXCEPTION (except);

@@ -201,7 +202,7 @@ frapy_pc (PyObject *self, PyObject *args)

TRY_CATCH (except, RETURN_MASK_ALL)
{
- FRAPY_REQUIRE_VALID ((frame_object *) self, frame);
+ FRAPY_REQUIRE_VALID (self, frame);

pc = get_frame_pc (frame);
}
@@ -222,7 +223,7 @@ frapy_block (PyObject *self, PyObject *args)

TRY_CATCH (except, RETURN_MASK_ALL)
{
- FRAPY_REQUIRE_VALID ((frame_object *) self, frame);
+ FRAPY_REQUIRE_VALID (self, frame);
block = get_frame_block (frame, NULL);
}
GDB_PY_HANDLE_EXCEPTION (except);
@@ -263,7 +264,7 @@ frapy_function (PyObject *self, PyObject *args)

TRY_CATCH (except, RETURN_MASK_ALL)
{
- FRAPY_REQUIRE_VALID ((frame_object *) self, frame);
+ FRAPY_REQUIRE_VALID (self, frame);

sym = find_pc_function (get_frame_address_in_block (frame));
}
@@ -330,7 +331,7 @@ frapy_older (PyObject *self, PyObject *args)

TRY_CATCH (except, RETURN_MASK_ALL)
{
- FRAPY_REQUIRE_VALID ((frame_object *) self, frame);
+ FRAPY_REQUIRE_VALID (self, frame);

prev = get_prev_frame (frame);
if (prev)
@@ -359,7 +360,7 @@ frapy_newer (PyObject *self, PyObject *args)

TRY_CATCH (except, RETURN_MASK_ALL)
{
- FRAPY_REQUIRE_VALID ((frame_object *) self, frame);
+ FRAPY_REQUIRE_VALID (self, frame);

next = get_next_frame (frame);
if (next)
@@ -388,7 +389,7 @@ frapy_find_sal (PyObject *self, PyObject *args)

TRY_CATCH (except, RETURN_MASK_ALL)
{
- FRAPY_REQUIRE_VALID ((frame_object *) self, frame);
+ FRAPY_REQUIRE_VALID (self, frame);

find_frame_sal (frame, &sal);
sal_obj = symtab_and_line_to_sal_object (sal);
@@ -444,7 +445,7 @@ frapy_read_var (PyObject *self, PyObject *args)

TRY_CATCH (except, RETURN_MASK_ALL)
{
- FRAPY_REQUIRE_VALID ((frame_object *) self, frame);
+ FRAPY_REQUIRE_VALID (self, frame);

if (!block)
block = get_frame_block (frame, NULL);
@@ -472,7 +473,7 @@ frapy_read_var (PyObject *self, PyObject *args)

TRY_CATCH (except, RETURN_MASK_ALL)
{
- FRAPY_REQUIRE_VALID ((frame_object *) self, frame);
+ FRAPY_REQUIRE_VALID (self, frame);

val = read_var_value (var, frame);
}
@@ -487,12 +488,11 @@ static PyObject *
frapy_select (PyObject *self, PyObject *args)
{
struct frame_info *fi;
- frame_object *frame = (frame_object *) self;
volatile struct gdb_exception except;

TRY_CATCH (except, RETURN_MASK_ALL)
{
- FRAPY_REQUIRE_VALID (frame, fi);
+ FRAPY_REQUIRE_VALID (self, fi);

select_frame (fi);
}
diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h
index ef39d5d..4d46466 100644
--- a/gdb/python/python-internal.h
+++ b/gdb/python/python-internal.h
@@ -120,9 +120,50 @@ extern PyTypeObject symbol_object_type;
extern PyTypeObject event_object_type;
extern PyTypeObject events_object_type;
extern PyTypeObject stop_event_object_type;
+extern PyTypeObject breakpoint_object_type;
+
+typedef struct breakpoint_object
+{
+ PyObject_HEAD
+
+ /* The breakpoint number according to gdb. */
+ int number;
+
+ /* The gdb breakpoint object, or NULL if the breakpoint has been
+ deleted. */
+ struct breakpoint *bp;
+
+ /* 1 is this is a FinishBreakpoint object, 0 otherwise. */
+ int is_finish_bp;
+} breakpoint_object;
+
+/* Require that BREAKPOINT be a valid breakpoint ID; throw a Python
+ exception if it is invalid. */
+#define BPPY_REQUIRE_VALID(Breakpoint) \
+ do { \
+ if ((Breakpoint)->bp == NULL) \
+ return PyErr_Format (PyExc_RuntimeError, \
+ _("Breakpoint %d is invalid."), \
+ (Breakpoint)->number); \
+ } while (0)
+
+/* Require that BREAKPOINT be a valid breakpoint ID; throw a Python
+ exception if it is invalid. This macro is for use in setter functions. */
+#define BPPY_SET_REQUIRE_VALID(Breakpoint) \
+ do { \
+ if ((Breakpoint)->bp == NULL) \
+ { \
+ PyErr_Format (PyExc_RuntimeError, _("Breakpoint %d is invalid."), \
+ (Breakpoint)->number); \
+ return -1; \
+ } \
+ } while (0)
+
+
+/* Variables used to pass information between the Breakpoint
+ constructor and the breakpoint-created hook function. */
+extern breakpoint_object *bppy_pending_object;

-/* Defined in py-breakpoint.c */
-typedef struct breakpoint_object breakpoint_object;

typedef struct
{
@@ -188,6 +229,7 @@ struct value *convert_value_from_python (PyObject *obj);
struct type *type_object_to_type (PyObject *obj);
struct symtab *symtab_object_to_symtab (PyObject *obj);
struct symtab_and_line *sal_object_to_symtab_and_line (PyObject *obj);
+struct frame_info *frame_object_to_frame_info (PyObject *frame_obj);

void gdbpy_initialize_auto_load (void);
void gdbpy_initialize_values (void);
@@ -202,6 +244,7 @@ void gdbpy_initialize_functions (void);
void gdbpy_initialize_pspace (void);
void gdbpy_initialize_objfile (void);
void gdbpy_initialize_breakpoints (void);
+void gdbpy_initialize_finishbreakpoints (void);
void gdbpy_initialize_lazy_string (void);
void gdbpy_initialize_parameters (void);
void gdbpy_initialize_thread (void);
diff --git a/gdb/python/python.c b/gdb/python/python.c
index 3a5a6b5..55c0c1e 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -1048,6 +1048,11 @@ gdbpy_breakpoint_has_py_cond (struct breakpoint_object *bp_obj)
"scripting is not supported."));
}

+int
+gdbpy_is_stopped_at_finish_bp (bpstat stop_bpstat)
+{
+ return 0;
+}
#endif /* HAVE_PYTHON */

\f
@@ -1237,6 +1242,7 @@ Enables or disables printing of Python stack traces."),
gdbpy_initialize_pspace ();
gdbpy_initialize_objfile ();
gdbpy_initialize_breakpoints ();
+ gdbpy_initialize_finishbreakpoints ();
gdbpy_initialize_lazy_string ();
gdbpy_initialize_thread ();
gdbpy_initialize_inferior ();
diff --git a/gdb/python/python.h b/gdb/python/python.h
index ae55cc2..cdf0263 100644
--- a/gdb/python/python.h
+++ b/gdb/python/python.h
@@ -21,6 +21,7 @@
#define GDB_PYTHON_H

#include "value.h"
+#include "breakpoint.h"

struct breakpoint_object;

@@ -47,4 +48,6 @@ int gdbpy_should_stop (struct breakpoint_object *bp_obj);

int gdbpy_breakpoint_has_py_cond (struct breakpoint_object *bp_obj);

+int gdbpy_is_stopped_at_finish_bp (bpstat stop_bpstat);
+
#endif /* GDB_PYTHON_H */
diff --git a/gdb/testsuite/gdb.python/py-breakpoint.exp b/gdb/testsuite/gdb.python/py-breakpoint.exp
index e0dd087..0e3adbd 100644
--- a/gdb/testsuite/gdb.python/py-breakpoint.exp
+++ b/gdb/testsuite/gdb.python/py-breakpoint.exp
@@ -44,7 +44,8 @@ gdb_py_test_silent_cmd "python blist = gdb.breakpoints()" "Get Breakpoint List"
gdb_test "python print blist\[0\]" "<gdb.Breakpoint object at $hex>" "Check obj exists"
gdb_test "python print blist\[0\].location" "main." "Check breakpoint location"

-gdb_breakpoint [gdb_get_line_number "Break at multiply."]
+set mult_line [gdb_get_line_number "Break at multiply."]
+gdb_breakpoint ${mult_line}
gdb_continue_to_breakpoint "Break at multiply."

# Check that the Python breakpoint code noted the addition of a
@@ -54,7 +55,9 @@ gdb_test "python print len(blist)" "2" "Check for two breakpoints"
gdb_test "python print blist\[0\]" "<gdb.Breakpoint object at $hex>" "Check obj exists"
gdb_test "python print blist\[0\].location" "main." "Check breakpoint location"
gdb_test "python print blist\[1\]" "<gdb.Breakpoint object at $hex>" "Check obj exists"
-gdb_test "python print blist\[1\].location" "py-breakpoint\.c:41*" "Check breakpoint location"
+
+gdb_test "python print blist\[1\].location" "py-breakpoint\.c:${mult_line}*" \
+ "Check breakpoint location"

# Check hit and ignore counts.
gdb_test "python print blist\[1\].hit_count" "1" "Check breakpoint hit count"
diff --git a/gdb/testsuite/gdb.python/py-finish-breakpoint.c b/gdb/testsuite/gdb.python/py-finish-breakpoint.c
new file mode 100644
index 0000000..5b708e3
--- /dev/null
+++ b/gdb/testsuite/gdb.python/py-finish-breakpoint.c
@@ -0,0 +1,97 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+ Copyright 2011 Free Software Foundation, Inc.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include <setjmp.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+int increase_1 (int *a)
+{
+ *a += 1;
+ return -5;
+}
+
+void increase (int *a)
+{
+ increase_1 (a);
+}
+
+int
+test_1 (int i, int j)
+{
+ return i == j;
+}
+
+int
+test(int i, int j)
+{
+ return test_1 (i, j);
+}
+
+int
+call_longjmp_1 (jmp_buf *buf)
+{
+ longjmp (*buf, 1);
+}
+
+int
+call_longjmp (jmp_buf *buf)
+{
+ call_longjmp_1 (buf);
+}
+
+void
+test_exec_exit(int do_exit)
+{
+ if (do_exit)
+ exit(0);
+ else
+ execl ("/bin/echo", "echo", "-1", (char *)0);
+}
+
+int main (int argc, char *argv[])
+{
+ jmp_buf env;
+ int foo = 5;
+ int bar = 42;
+ int i, j;
+
+ getpid();
+
+ i = 0;
+ /* Break at increase. */
+ increase (&i);
+ increase (&i);
+ increase (&i);
+
+ for (i = 0; i < 10; i++)
+ {
+ j += 1; /* Condition Break. */
+ }
+
+ if (setjmp (env) == 0) /* longjmp caught */
+ {
+ call_longjmp (&env);
+ }
+ else
+ j += 1; /* after longjmp. */
+
+ test_exec_exit(1);
+
+ return j; /* Break at end. */
+}
diff --git a/gdb/testsuite/gdb.python/py-finish-breakpoint.exp b/gdb/testsuite/gdb.python/py-finish-breakpoint.exp
new file mode 100644
index 0000000..c4d3bef
--- /dev/null
+++ b/gdb/testsuite/gdb.python/py-finish-breakpoint.exp
@@ -0,0 +1,230 @@
+# Copyright (C) 2011 Free Software Foundation, Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+# This file is part of the GDB testsuite. It tests the mechanism
+# exposing values to Python.
+
+if $tracelevel then {
+ strace $tracelevel
+}
+
+load_lib gdb-python.exp
+
+set testfile "py-finish-breakpoint"
+set srcfile ${testfile}.c
+
+if { [prepare_for_testing ${testfile}.exp ${testfile} ${srcfile}] } {
+ return -1
+}
+
+set remote_python_file [remote_download host ${srcdir}/${subdir}/${testfile}.py]
+
+
+# Skip all tests if Python scripting is not enabled.
+if { [skip_python_tests] } { continue }
+
+#
+# Test FinishBreakpoint in normal conditions
+#
+
+clean_restart ${testfile}
+
+if ![runto_main] then {
+ fail "Cannot run to main."
+ return 0
+}
+gdb_test_no_output "set confirm off" "disable confirmation"
+gdb_test "source $remote_python_file" "Python script imported.*" \
+ "import python scripts"
+gdb_breakpoint "increase_1"
+gdb_test "continue" "Breakpoint .*at.*" "continue to the function to finish"
+
+# set FinishBreakpoint
+
+gdb_test "python finishbp = MyFinishBreakpoint (gdb.parse_and_eval ('a'), gdb.selected_frame ())" \
+ "Temporary breakpoint.*" "set FinishBreakpoint"
+gdb_test "python print finishbp.return_value" "None.*" \
+ "check return_value at init"
+
+# check normal bp hit
+
+gdb_test "continue" "MyFinishBreakpoint stop with.*#0.*increase.*" \
+ "check MyFinishBreakpoint hit"
+gdb_test "python print finishbp.return_value" "-5.*" "check return_value"
+
+gdb_test "python print finishbp.is_valid()" "False.*"\
+ "ensure that finish bp is invalid afer normal hit"
+
+# check FinishBreakpoint in main no allowed
+
+gdb_test "finish" "main.*" "return to main()"
+gdb_test "python MyFinishBreakpoint (None, gdb.selected_frame ())" \
+ "ValueError: \"FinishBreakpoint\" not meaningful in the outermost frame..*" \
+ "check FinishBP not allowed in main"
+
+#
+# Test FinishBreakpoint with no debug symbol
+#
+
+clean_restart ${testfile}
+
+gdb_test "source $remote_python_file" "Python script imported.*" \
+ "import python scripts"
+set cond_line [gdb_get_line_number "Condition Break."]
+
+if ![runto "getpid"] then {
+ fail "Cannot run to getpid."
+ return 0
+}
+
+gdb_test "python finishBP = SimpleFinishBreakpoint(gdb.newest_frame())" \
+ "SimpleFinishBreakpoint init" \
+ "set finish breakpoint"
+gdb_test "continue" "SimpleFinishBreakpoint stop.*" "check FinishBreakpoint hit"
+gdb_test "python print finishBP.return_value" "None" "check return value without debug symbol"
+
+#
+# Test FinishBreakpoint in function returned by longjmp
+#
+
+clean_restart ${testfile}
+
+gdb_test "source $remote_python_file" "Python script imported.*" \
+ "import python scripts"
+
+if ![runto call_longjmp_1] then {
+ perror "couldn't run to breakpoint call_longjmp"
+ continue
+}
+
+gdb_test "python finishbp = SimpleFinishBreakpoint(gdb.newest_frame())" \
+ "SimpleFinishBreakpoint init" \
+ "set finish breakpoint"
+gdb_test "break [gdb_get_line_number "after longjmp."]" "Breakpoint.* at .*" \
+ "set BP after the jump"
+gdb_test "continue" "SimpleFinishBreakpoint out of scope.*" \
+ "check FinishBP out of scope notification"
+gdb_test "python print finishbp.is_valid()" "False.*"\
+ "ensure that finish bp is invalid afer out of scope notification"
+
+#
+# Test FinishBreakpoint in BP condition evaluation
+# (finish in dummy frame)
+#
+
+clean_restart ${testfile}
+
+gdb_test "source $remote_python_file" "Python script imported.*" \
+ "import python scripts"
+
+
+if ![runto_main] then {
+ fail "Cannot run to main."
+ return 0
+}
+
+gdb_test "break ${cond_line} if test_1(i,8)" "Breakpoint .* at .*" \
+ "set a conditional BP"
+gdb_test "python TestBreakpoint()" "TestBreakpoint init" \
+ "set FinishBP in a breakpoint condition"
+gdb_test "continue" \
+ "\"FinishBreakpoint\" cannot be set on a dummy frame.*" \
+ "don't allow FinishBreakpoint on dummy frames"
+gdb_test "print i" "8" "check stopped location"
+
+#
+# Test FinishBreakpoint in BP condition evaluation
+# (finish in normal frame)
+#
+
+clean_restart ${testfile}
+
+gdb_test "source $remote_python_file" "Python script imported.*" \
+ "import python scripts"
+
+if ![runto_main] then {
+ fail "Cannot run to main."
+ return 0
+}
+
+gdb_test "break ${cond_line} if test(i,8)" \
+ "Breakpoint .* at .*" "set conditional BP"
+gdb_test "python TestBreakpoint()" "TestBreakpoint init" "set BP in condition"
+
+gdb_test "continue" \
+ "test don't stop 1.*test don't stop 2.*test stop.*Error in testing breakpoint condition.*The program being debugged stopped while in a function called from GDB.*" \
+ "stop in condition function"
+
+gdb_test "continue" "Continuing.*" "finish condition evaluation"
+gdb_test "continue" "Breakpoint.*" "stop at conditional breakpoint"
+gdb_test "print i" "8" "check stopped location"
+
+#
+# Test FinishBreakpoint in explicit inferior function call
+#
+
+clean_restart ${testfile}
+
+gdb_test "source $remote_python_file" "Python script imported.*" \
+ "import python scripts"
+
+if ![runto_main] then {
+ fail "Cannot run to main."
+ return 0
+}
+
+# return address in dummy frame
+
+gdb_test "python TestExplicitBreakpoint('increase_1')" "Breakpoint.*at.*" \
+ "prepare TestExplicitBreakpoint"
+gdb_test "print increase_1(&i)" \
+ "\"FinishBreakpoint\" cannot be set on a dummy frame.*" \
+ "don't allow FinishBreakpoint on dummy frames"
+
+# return address in normal frame
+
+delete_breakpoints
+gdb_test "python TestExplicitBreakpoint(\"increase_1\")" "Breakpoint.*at.*" \
+ "prepare TestExplicitBreakpoint"
+gdb_test "print increase(&i)" \
+ "SimpleFinishBreakpoint init.*SimpleFinishBreakpoint stop.*The program being debugged stopped while in a function called from GDB.*" \
+ "FinishBP stop at during explicit function call"
+
+
+#
+# Test FinishBreakpoint when inferior exits
+#
+
+if ![runto "test_exec_exit"] then {
+ fail "Cannot run to test_exec_exit."
+ return 0
+}
+
+gdb_test "python SimpleFinishBreakpoint(gdb.newest_frame())" "SimpleFinishBreakpoint init" "set FinishBP after the exit()"
+gdb_test "continue" "SimpleFinishBreakpoint out of scope.*" "catch out of scope after exit"
+
+#
+# Test FinishBreakpoint when inferior execs
+#
+
+if ![runto "test_exec_exit"] then {
+ fail "Cannot run to test_exec_exit."
+ return 0
+}
+
+gdb_test_no_output "set var do_exit = 0" "switch to execve() test"
+gdb_test "python SimpleFinishBreakpoint(gdb.newest_frame())" "SimpleFinishBreakpoint init" "set FinishBP after the exec"
+gdb_test "catch exec" "Catchpoint.*\(exec\).*" "catch exec"
+gdb_test "continue" "SimpleFinishBreakpoint out of scope.*" "catch out of scope after exec"
\ No newline at end of file
diff --git a/gdb/testsuite/gdb.python/py-finish-breakpoint.py b/gdb/testsuite/gdb.python/py-finish-breakpoint.py
new file mode 100644
index 0000000..adf10b0
--- /dev/null
+++ b/gdb/testsuite/gdb.python/py-finish-breakpoint.py
@@ -0,0 +1,88 @@
+# Copyright (C) 2011 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+# This file is part of the GDB testsuite. It tests python Finish
+# Breakpoints.
+
+class MyFinishBreakpoint(gdb.FinishBreakpoint):
+ def __init__(self, val, frame):
+ gdb.FinishBreakpoint.__init__ (self, frame)
+ print "MyFinishBreakpoint init"
+ self.val = val
+
+ def stop(self):
+ print "MyFinishBreakpoint stop with %d" % int(self.val.dereference())
+ gdb.execute("where 1")
+ return True
+
+ def out_of_scope(self):
+ print "MyFinishBreakpoint out of scope..."
+
+class TestBreakpoint(gdb.Breakpoint):
+ def __init__(self):
+ gdb.Breakpoint.__init__(self, spec="test_1", internal=1)
+ self.silent = True
+ self.count = 0
+ print "TestBreakpoint init"
+
+ def stop(self):
+ self.count += 1
+ try:
+ TestFinishBreakpoint(gdb.newest_frame(), self.count)
+ except ValueError as e:
+ print e
+ return False
+
+class TestFinishBreakpoint(gdb.FinishBreakpoint):
+ def __init__(self, frame, count):
+ self.count = count
+ gdb.FinishBreakpoint.__init__(self, frame, internal=1)
+
+
+ def stop(self):
+ print "-->", self.number
+ if (self.count == 3):
+ print "test stop ... %d" % self.count
+ return True
+ else:
+ print "test don't stop %d" % self.count
+ return False
+
+
+ def out_of_scope(self):
+ print "test didn't finish ... %d" % self.count
+
+class TestExplicitBreakpoint(gdb.Breakpoint):
+ def stop(self):
+ try:
+ SimpleFinishBreakpoint(gdb.newest_frame())
+ except ValueError as e:
+ print e
+ return False
+
+class SimpleFinishBreakpoint(gdb.FinishBreakpoint):
+ def __init__(self, frame):
+ gdb.FinishBreakpoint.__init__ (self, frame)
+
+ print "SimpleFinishBreakpoint init"
+
+ def stop(self):
+ print "SimpleFinishBreakpoint stop"
+ return True
+
+ def out_of_scope(self):
+ print "SimpleFinishBreakpoint out of scope..."
+
+print "Python script importedd"
diff --git a/gdb/testsuite/gdb.python/py-finish-breakpoint2.cc b/gdb/testsuite/gdb.python/py-finish-breakpoint2.cc
new file mode 100644
index 0000000..a0eea06
--- /dev/null
+++ b/gdb/testsuite/gdb.python/py-finish-breakpoint2.cc
@@ -0,0 +1,59 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+ Copyright 2011 Free Software Foundation, Inc.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+
+#include <iostream>
+
+void
+throw_exception_1 (int e)
+{
+ throw new int (e);
+}
+
+void
+throw_exception (int e)
+{
+ throw_exception_1 (e);
+}
+
+int
+main (void)
+{
+ int i;
+ try
+ {
+ throw_exception_1 (10);
+ }
+ catch (const int *e)
+ {
+ std::cerr << "Exception #" << *e << std::endl;
+ }
+ i += 1; /* Break after exception 1. */
+
+ try
+ {
+ throw_exception (10);
+ }
+ catch (const int *e)
+ {
+ std::cerr << "Exception #" << *e << std::endl;
+ }
+ i += 1; /* Break after exception 2. */
+
+ return i;
+}
diff --git a/gdb/testsuite/gdb.python/py-finish-breakpoint2.exp b/gdb/testsuite/gdb.python/py-finish-breakpoint2.exp
new file mode 100644
index 0000000..fae798c
--- /dev/null
+++ b/gdb/testsuite/gdb.python/py-finish-breakpoint2.exp
@@ -0,0 +1,65 @@
+# Copyright (C) 2011 Free Software Foundation, Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+# This file is part of the GDB testsuite. It tests the mechanism
+# exposing values to Python.
+
+if $tracelevel then {
+ strace $tracelevel
+}
+
+load_lib gdb-python.exp
+
+set testfile "py-finish-breakpoint2"
+set srcfile ${testfile}.cc
+set binfile ${objdir}/${subdir}/${testfile}
+set pyfile ${srcdir}/${subdir}/${testfile}.py
+
+# Start with a fresh gdb.
+gdb_exit
+gdb_start
+gdb_reinitialize_dir $srcdir/$subdir
+gdb_load ${binfile}
+
+if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug c++}] != "" } {
+ untested "Couldn't compile ${srcfile}"
+ return -1
+}
+
+if ![runto_main] then {
+ fail "Cannot run to main."
+ return 0
+}
+
+#
+# Check FinishBreakpoints against C++ exceptions
+#
+
+gdb_breakpoint [gdb_get_line_number "Break after exception 2"]
+
+gdb_test "source $pyfile" ".*Python script imported.*" \
+ "import python scripts"
+
+gdb_breakpoint "throw_exception_1"
+gdb_test "continue" "Breakpoint .*throw_exception_1.*" "run to exception 1"
+
+gdb_test "python print len(gdb.breakpoints())" "3" "check BP count"
+gdb_test "python ExceptionFinishBreakpoint(gdb.newest_frame())" "init ExceptionFinishBreakpoint" "set FinishBP after the exception"
+gdb_test "continue" ".*stopped at ExceptionFinishBreakpoint.*" "check FinishBreakpoint in catch()"
+gdb_test "python print len(gdb.breakpoints())" "3" "check finish BP removal"
+
+gdb_test "continue" ".*Breakpoint.* throw_exception_1.*" "continue to second exception"
+gdb_test "python ExceptionFinishBreakpoint(gdb.newest_frame())" "init ExceptionFinishBreakpoint" "set FinishBP after the exception"
+gdb_test "continue" ".*exception did not finish.*" "FinishBreakpoint with exception thrown not caught"
diff --git a/gdb/testsuite/gdb.python/py-finish-breakpoint2.py b/gdb/testsuite/gdb.python/py-finish-breakpoint2.py
new file mode 100644
index 0000000..9ea4cf1
--- /dev/null
+++ b/gdb/testsuite/gdb.python/py-finish-breakpoint2.py
@@ -0,0 +1,34 @@
+# Copyright (C) 2011 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+# This file is part of the GDB testsuite. It tests python Finish
+# Breakpoints.
+
+class ExceptionFinishBreakpoint(gdb.FinishBreakpoint):
+ def __init__(self, frame):
+ gdb.FinishBreakpoint.__init__(self, frame, internal=1)
+ self.silent = True;
+ print "init ExceptionFinishBreakpoint"
+
+ def stop(self):
+ print "stopped at ExceptionFinishBreakpoint"
+ gdb.post_event(self.delete)
+ return True
+
+ def out_of_scope(self):
+ print "exception did not finish ..."
+
+
+print "Python script imported"
--
1.7.6.4