Cherrypick from master 2011-12-23 17:06:16 UTC Kevin Pouget <kpouget@sourceware.org> ' Introduce gdb.FinishBreakpoint in Python':
gdb/python/py-finishbreakpoint.c
gdb/testsuite/gdb.python/py-finish-breakpoint.c
gdb/testsuite/gdb.python/py-finish-breakpoint.exp
gdb/testsuite/gdb.python/py-finish-breakpoint.py
gdb/testsuite/gdb.python/py-finish-breakpoint2.cc
gdb/testsuite/gdb.python/py-finish-breakpoint2.exp
gdb/testsuite/gdb.python/py-finish-breakpoint2.py
--- /dev/null
+/* 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;
+};
+
+/* 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_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);
+}
+
+/* Triggered when gdbpy_should_stop is about to execute the `stop' callback
+ of the gdb.FinishBreakpoint object BP_OBJ. Will compute and cache the
+ `return_value', if possible. */
+
+void
+bpfinishpy_pre_stop_hook (struct breakpoint_object *bp_obj)
+{
+ struct finish_breakpoint_object *self_finishbp =
+ (struct finish_breakpoint_object *) bp_obj;
+ volatile struct gdb_exception except;
+
+ /* Can compute return_value only once. */
+ gdb_assert (!self_finishbp->return_value);
+
+ if (!self_finishbp->return_type)
+ return;
+
+ TRY_CATCH (except, RETURN_MASK_ALL)
+ {
+ struct value *ret =
+ get_return_value (type_object_to_type (self_finishbp->function_type),
+ type_object_to_type (self_finishbp->return_type));
+
+ if (ret)
+ {
+ self_finishbp->return_value = value_to_value_object (ret);
+ if (!self_finishbp->return_value)
+ gdbpy_print_stack ();
+ }
+ else
+ {
+ Py_INCREF (Py_None);
+ self_finishbp->return_value = Py_None;
+ }
+ }
+ if (except.reason < 0)
+ {
+ gdbpy_convert_exception (except);
+ gdbpy_print_stack ();
+ }
+}
+
+/* Triggered when gdbpy_should_stop has triggered the `stop' callback
+ of the gdb.FinishBreakpoint object BP_OBJ. */
+
+void
+bpfinishpy_post_stop_hook (struct breakpoint_object *bp_obj)
+{
+ volatile struct gdb_exception except;
+
+ TRY_CATCH (except, RETURN_MASK_ALL)
+ {
+ /* Can't delete it here, but it will be removed at the next stop. */
+ disable_breakpoint (bp_obj->bp);
+ gdb_assert (bp_obj->bp->disposition == disp_del);
+ }
+ if (except.reason < 0)
+ {
+ gdbpy_convert_exception (except);
+ gdbpy_print_stack ();
+ }
+}
+
+/* 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;
+ int thread;
+ 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, "|OO", keywords,
+ &frame_obj, &internal))
+ return -1;
+
+ /* Default frame to gdb.newest_frame if necessary. */
+ if (!frame_obj)
+ frame_obj = gdbpy_newest_frame (NULL, NULL);
+ else
+ Py_INCREF (frame_obj);
+
+ frame = frame_object_to_frame_info (frame_obj);
+ Py_DECREF (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;
+
+ thread = pid_to_thread_id (inferior_ptid);
+ if (thread == 0)
+ {
+ PyErr_SetString (PyExc_ValueError,
+ _("No thread currently selected."));
+ 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)
+ {
+ /* Ignore Python errors at this stage. */
+ self_bpfinish->return_type = type_to_type_object (ret_type);
+ PyErr_Clear ();
+ self_bpfinish->function_type =
+ type_to_type_object (SYMBOL_TYPE (function));
+ PyErr_Clear ();
+ }
+ }
+ }
+ }
+ if (except.reason < 0
+ || !self_bpfinish->return_type || !self_bpfinish->function_type)
+ {
+ /* Won't be able to compute return value. */
+ Py_XDECREF (self_bpfinish->return_type);
+ Py_XDECREF (self_bpfinish->function_type);
+
+ 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, thread,
+ 0,
+ 1 /*temp_flag*/,
+ bp_breakpoint,
+ 0,
+ AUTO_BOOLEAN_TRUE,
+ &bkpt_breakpoint_ops,
+ 0, 1, internal_bp);
+ }
+ GDB_PY_SET_HANDLE_EXCEPTION (except);
+
+ self_bpfinish->py_bp.bp->frame_id = frame_id;
+ self_bpfinish->py_bp.is_finish_bp = 1;
+
+ /* Bind the breakpoint with the current program space. */
+ self_bpfinish->py_bp.bp->pspace = current_program_space;
+
+ 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)
+{
+ volatile struct gdb_exception except;
+ breakpoint_object *bp_obj = (breakpoint_object *) bpfinish_obj;
+ PyObject *py_obj = (PyObject *) bp_obj;
+
+ if (bpfinish_obj->py_bp.bp->enable_state == bp_enabled
+ && 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. */
+
+static int
+bpfinishpy_detect_out_scope_cb (struct breakpoint *b, void *args)
+{
+ volatile struct gdb_exception except;
+ 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 ();
+
+ /* 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;
+
+ /* Check scope if not currently stopped at the FinishBreakpoint. */
+ if (b != bp_stopped)
+ {
+ TRY_CATCH (except, RETURN_MASK_ALL)
+ {
+ if (b->pspace == current_inferior ()->pspace
+ && (!target_has_registers
+ || frame_find_by_id (b->frame_id) == NULL))
+ bpfinishpy_out_of_scope (finish_bp);
+ }
+ if (except.reason < 0)
+ {
+ gdbpy_convert_exception (except);
+ gdbpy_print_stack ();
+ }
+ }
+ }
+
+ 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 */
+};
--- /dev/null
+/* 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>
+
+/* Defined in py-events-shlib.h. */
+extern void do_nothing (void);
+
+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;
+
+ do_nothing ();
+
+ 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. */
+}
--- /dev/null
+# 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
+}
+
+if {[skip_shlib_tests]} {
+ untested py-finish-breakpoint.exp
+ return 0
+}
+
+load_lib gdb-python.exp
+
+set libfile "py-events-shlib"
+set libsrc $srcdir/$subdir/$libfile.c
+set lib_sl $objdir/$subdir/$libfile-nodebug.so
+set lib_opts ""
+
+set testfile "py-finish-breakpoint"
+set srcfile ${testfile}.c
+set binfile ${objdir}/${subdir}/${testfile}
+set exec_opts [list debug shlib=$lib_sl]
+
+if [get_compiler_info ${binfile}] {
+ return -1
+}
+
+if { [gdb_compile_shlib $libsrc $lib_sl $lib_opts] != ""
+ || [gdb_compile $srcdir/$subdir/$srcfile $binfile executable $exec_opts] != ""} {
+ untested "Could not compile either $libsrc or $srcdir/$subdir/$srcfile."
+ return -1
+}
+
+# Start with a fresh gdb.
+clean_restart ${testfile}
+
+set python_file ${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}
+gdb_load_shlibs ${lib_sl}
+
+if ![runto_main] then {
+ fail "Cannot run to main."
+ return 0
+}
+
+gdb_test_no_output "set confirm off" "disable confirmation"
+gdb_test "source $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_default = gdb.FinishBreakpoint ()" \
+ "Temporary breakpoint.*" "set FinishBreakpoint with default frame value"
+gdb_test "python finishbp = MyFinishBreakpoint (gdb.parse_and_eval ('a'), gdb.newest_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.*return_value is: -5.*#0.*increase.*" \
+ "check MyFinishBreakpoint hit"
+gdb_test "python print finishbp.return_value" "-5.*" "check return_value"
+
+gdb_test "python print finishbp_default.hit_count" "1.*" "check finishBP on default frame has been hit"
+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_load_shlibs ${lib_sl}
+
+gdb_test "source $python_file" "Python script imported.*" \
+ "import python scripts"
+set cond_line [gdb_get_line_number "Condition Break."]
+
+if ![runto_main] then {
+ fail "Cannot run to main."
+ return 0
+}
+
+gdb_test "print do_nothing" "no debug info.*" "ensure that shared lib has no debug info"
+gdb_breakpoint "do_nothing" {temporary}
+gdb_test "continue" "Temporary breakpoint .*in do_nothing.*" "continue to do_nothing"
+
+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_load_shlibs ${lib_sl}
+
+gdb_test "source $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_load_shlibs ${lib_sl}
+
+gdb_test "source $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_load_shlibs ${lib_sl}
+
+gdb_test "source $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_load_shlibs ${lib_sl}
+
+gdb_test "source $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
--- /dev/null
+# 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 ())
+ print "return_value is: %d" % int (self.return_value)
+ 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"
--- /dev/null
+/* 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;
+}
--- /dev/null
+# 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
+
+if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug c++}] != "" } {
+ untested "Couldn't compile ${srcfile}"
+ return -1
+}
+
+# Start with a fresh gdb.
+gdb_exit
+gdb_start
+gdb_reinitialize_dir $srcdir/$subdir
+gdb_load ${binfile}
+
+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"
--- /dev/null
+# 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"
+ return True
+
+ def out_of_scope(self):
+ print "exception did not finish ..."
+
+
+print "Python script imported"