This is the mail archive of the gdb-patches@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PING][RFC][PATCH v2] Python API: add gdb.stack_may_be_invalid


On Fri, Nov 7, 2014 at 2:36 PM, Martin Galvan
<martin.galvan@tallertechnologies.com> wrote:
> On Fri, Nov 7, 2014 at 2:27 PM, Ulrich Weigand <uweigand@de.ibm.com> wrote:
>> Just one comment here: python_gdbarch isn't really correct here.
>> If you have a platform that supports multiple architectures, then
>> you really should use the appropriate gdbarch for PC.
>>
>> Ideally, the Python interface should carry enough information to
>> determine the appropriate gdbarch, e.g. by operating on a Frame
>> instead of a plain PC value.
>
> If I understand correctly, using a Frame would require the program to
> be already running by the time we call the API function, which isn't
> really what we want.
>
>> If that isn't possible, one fall-back might be to look up the
>> symbol table from the PC, and use the associated objfile arch.

Here's the new version of the patch. It uses the objfile's gdbarch
and, if not available, python_gdbarch.

diff --git a/gdb/python/python.c b/gdb/python/python.c
index d23325a..2dc2d41 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -703,6 +703,87 @@ gdbpy_solib_name (PyObject *self, PyObject *args)
   return str_obj;
 }

+/* Returns 1 if the given PC may be inside a prologue, 0 if it
definitely isn't,
+   and -1 if we have no debug info to use. */
+
+static int
+pc_may_be_in_prologue (gdb_py_ulongest pc)
+{
+  int result = -1;
+  struct symbol *function_symbol;
+  struct symtab_and_line function_body_start_sal;
+
+  function_symbol = find_pc_function(pc);
+
+  if (function_symbol)
+    {
+      function_body_start_sal = find_function_start_sal (function_symbol, 1);
+
+      result = pc < function_body_start_sal.pc;
+    }
+
+    return result;
+}
+
+static int
+stack_is_destroyed (gdb_py_ulongest pc)
+{
+  int result;
+  struct symtab *symtab = NULL;
+  struct gdbarch *gdbarch = NULL;
+
+  symtab = find_pc_symtab (pc);
+
+  if ((symtab != NULL) && (symtab->objfile != NULL))
+    {
+      gdbarch = get_objfile_arch (symtab->objfile);
+    }
+
+  if (gdbarch != NULL)
+    {
+      result = gdbarch_in_function_epilogue_p (gdbarch, pc);
+    }
+  else
+    {
+      result = gdbarch_in_function_epilogue_p (python_gdbarch, pc);
+    }
+
+  return result;
+}
+
+/* Returns True if a given PC may point to an address in which the stack frame
+   may not be valid (either because it may not be set up yet or because it was
+   destroyed, usually in a function's epilogue), False otherwise. */
+
+static PyObject *
+gdbpy_stack_may_be_invalid (PyObject *self, PyObject *args)
+{
+  gdb_py_ulongest pc;
+  PyObject *result = NULL;
+  int pc_maybe_in_prologue;
+
+  if (PyArg_ParseTuple (args, GDB_PY_LLU_ARG, &pc))
+    {
+      pc_maybe_in_prologue = pc_may_be_in_prologue (pc);
+
+      if (pc_maybe_in_prologue != -1)
+        {
+          result = stack_is_destroyed (pc) || pc_maybe_in_prologue ?
+                   Py_True : Py_False;
+
+          Py_INCREF (result);
+        }
+      else  /* No debug info at that point. */
+        {
+          PyErr_Format (PyExc_RuntimeError,
+                        _("There's no debug info for a function that\n"
+                          "could be enclosing the given PC."));
+        }
+    }
+
+  return result;
+}
+
 /* A Python function which is a wrapper for decode_line_1.  */

 static PyObject *
@@ -2000,6 +2081,15 @@ Return the selected inferior object." },
   { "inferiors", gdbpy_inferiors, METH_NOARGS,
     "inferiors () -> (gdb.Inferior, ...).\n\
 Return a tuple containing all inferiors." },
+
+
+  { "stack_may_be_invalid", gdbpy_stack_may_be_invalid, METH_VARARGS,
+    "stack_may_be_invalid (Long) -> Boolean.\n\
+Returns True if a given PC may point to an address in which the stack frame\n\
+may not be valid (either because it may not be set up yet or because it was\n\
+destroyed, usually in a function's epilogue), False otherwise."},
+
+
   {NULL, NULL, 0, NULL}
 };


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]