Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Matt Rice <ratmice@gmail.com>
To: pmuldoon@redhat.com
Cc: gdb-patches@sourceware.org
Subject: Re: [patch] [python] find_line_pc_range
Date: Mon, 04 Jul 2011 06:36:00 -0000	[thread overview]
Message-ID: <CACTLOFo5rD6PCLeK+hmoBt-vAHGJZDNS_gPbZG6tP=CP=gFeog@mail.gmail.com> (raw)
In-Reply-To: <m3y60fmj76.fsf@redhat.com>

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

On Sun, Jul 3, 2011 at 9:18 AM, Phil Muldoon <pmuldoon@redhat.com> wrote:
> Matt Rice <ratmice@gmail.com> writes:
>
>> don't know a whole much about python but,
>> would it be better to return None on error, instead of a tuple containing Nones?
>
> Thanks. I would return either an exception or None depending on the
> situational context of the failure.  If a None type scenario is expected,
> and normal then it is okay to return that, given that the Python
> scripter would understand the reason for that.  So documentation is key
> here.  I think find_line_pc_range is just a utility function,
> which can very well not find the line range.  So I would replicate that
> functionality in your API, except I would return a single Py_None over a
> tuple.
>
>
>> +salpy_find_line_pc_range (PyObject *self, PyObject *args)
>> +{
>> +  struct symtab_and_line *sal = NULL;
>> +  CORE_ADDR start_pc, end_pc;
>> +  PyObject *start;
>> +  PyObject *end;
>> +  PyObject *ret_tuple;
>> +
>> +  SALPY_REQUIRE_VALID (self, sal);
>> +
>> +  ret_tuple = PyTuple_New (2);
>
> This call can fail and return NULL, so in this case you need to return
> NULL to propagate the failure.
>
>> +  if (find_line_pc_range (*sal, &start_pc, &end_pc))
>> +    {
>> +
>> +      start = gdb_py_long_from_longest (start_pc);
>> +      end = gdb_py_long_from_longest (end_pc);
>> +      PyTuple_SET_ITEM (ret_tuple, 0, start);
>> +      PyTuple_SET_ITEM (ret_tuple, 1, end);
>> +    }
>
> start and end could be local to this block I think.
>
> Also gdb_py_long_from_longest is macro that points to Py
> Long_FromUnsignedLongLong.  This can also return NULL which indicates a
> failure, so similar to above, you need to check the return and return
> NULL if one of them fails.  If one of them does return NULL, you also
> need to appropriately clean-up previous references that were created in
> Python.  We normally do this by creating a local error: goto.
>
> A minor nit, SET_ITEM does not do any error checking, while SetItem
> does.  As this is a new tuple, then it is ok to use it.  But normally
> (and personally, in new tuples) I always use the error checking
> equivalent.
>
>
> +  else
> +    {
> +      PyTuple_SET_ITEM (ret_tuple, 0, Py_None);
> +      PyTuple_SET_ITEM (ret_tuple, 1, Py_None);
> +    }
> +
>
> In this case I would just return Py_None (see first paragraph).  It
> seems a little redundant to return a tuple with Py_None for both
> elements, and this will always be the case on a lookup failure with
> find_line_pc_range. Make sure you incref Py_None before returning it,
> also.
>
> Also, this patch needs tests.

Thanks, attached is an updated patch that also includes tests.

2011-07-03  Matt Rice  <ratmice@gmail.com>

        * python/py-symtab.c: Populate sal_object_methods.
        (salpy_find_line_pc_range): New function.

2011-07-03  Matt Rice  <ratmice@gmail.com>

        * gdb.texinfo (Symbol Tables In Python): Add find_line_pc_range method.

2011-07-03  Matt Rice  <ratmice@gmail.com>

        * gdb.python/py-symtab.exp: New Tests for find_line_pc_range.

[-- Attachment #2: foo.diff --]
[-- Type: application/octet-stream, Size: 3354 bytes --]

diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index dbaf30e..c637ba2 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -23293,6 +23293,11 @@ exist in @value{GDBN} any longer.  All other
 @code{gdb.Symtab_and_line} methods will throw an exception if it is
 invalid at the time the method is called.
 @end defmethod
+
+@defmethod Symtab_and_line find_line_pc_range
+If found returns a @code{Tuple} containing the start and end program counter
+addresses for the line attribute.  Otherwise returns @code{None}.
+@end defmethod
 @end table
 
 A @code{gdb.Symtab} object has the following attributes:
diff --git a/gdb/python/py-symtab.c b/gdb/python/py-symtab.c
index 107cdec..21811d1 100644
--- a/gdb/python/py-symtab.c
+++ b/gdb/python/py-symtab.c
@@ -215,6 +215,53 @@ salpy_get_line (PyObject *self, void *closure)
 }
 
 static PyObject *
+salpy_find_line_pc_range (PyObject *self, PyObject *args)
+{
+  struct symtab_and_line *sal = NULL;
+  CORE_ADDR start_pc, end_pc;
+
+  SALPY_REQUIRE_VALID (self, sal);
+
+  if (find_line_pc_range (*sal, &start_pc, &end_pc))
+    {
+      PyObject *ret_tuple;
+      PyObject *start;
+      PyObject *end;
+
+      ret_tuple = PyTuple_New (2);
+      if (!ret_tuple)
+        return Py_None;
+
+      start = gdb_py_long_from_ulongest (start_pc);
+      if (!start)
+	goto fail_ret;
+      if (PyTuple_SetItem (ret_tuple, 0, start) != 0)
+	{
+	  Py_XDECREF (start);
+	  goto fail_ret;
+	}
+
+      end = gdb_py_long_from_ulongest (end_pc);
+      if (!end)
+	goto fail_ret;
+      if (PyTuple_SetItem (ret_tuple, 1, end) != 0)
+	{
+	  Py_XDECREF (end);
+	  goto fail_ret;
+	}
+
+      return ret_tuple;
+
+      fail_ret:
+	Py_XDECREF (ret_tuple);
+	return Py_None;
+    }
+
+  return Py_None;
+
+}
+
+static PyObject *
 salpy_get_symtab (PyObject *self, void *closure)
 {
   struct symtab_and_line *sal;
@@ -526,6 +573,8 @@ static PyMethodDef sal_object_methods[] = {
   { "is_valid", salpy_is_valid, METH_NOARGS,
     "is_valid () -> Boolean.\n\
 Return true if this symbol table and line is valid, false if not." },
+  { "find_line_pc_range", salpy_find_line_pc_range, METH_NOARGS,
+    "Return a tuple of start and end ranges for the symbol table." },
   {NULL}  /* Sentinel */
 };
 
diff --git a/gdb/testsuite/gdb.python/py-symtab.exp b/gdb/testsuite/gdb.python/py-symtab.exp
index c52f5ef..b6f3423 100644
--- a/gdb/testsuite/gdb.python/py-symtab.exp
+++ b/gdb/testsuite/gdb.python/py-symtab.exp
@@ -58,6 +58,14 @@ gdb_test "python print sal.symtab" "gdb/testsuite/gdb.python/py-symbol.c.*" "Tes
 gdb_test "python print sal.pc" "${decimal}" "Test sal.pc"
 gdb_test "python print sal.line" "42" "Test sal.line"
 gdb_test "python print sal.is_valid()" "True" "Test sal.is_valid"
+gdb_test "python print sal.pc == sal.find_line_pc_range()\[0\]" "True" "Test sal.find_line_pc_range() 1"
+gdb_test "python print sal.find_line_pc_range()\[1\]" "${decimal}" "Test sal.find_line_pc_range() 2"
+
+# Test sal find_line_pc_range errors.
+gdb_test "python bad_line_sal = gdb.decode_line(\"py-symbol.c:404\")\[1\]\[0\]" "" \
+"test decode_line python.c:404"
+gdb_test "python print bad_line_sal.find_line_pc_range() == None" "True" \
+"find_line_pc_range for non-existent line"
 
 # Test symbol table.
 gdb_test "python print symtab.filename" "testsuite/gdb.python/py-symbol.c.*" "Test symtab.filename"

  parent reply	other threads:[~2011-07-04  6:04 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-07-02  4:03 Matt Rice
2011-07-03 16:35 ` Phil Muldoon
2011-07-04  5:38   ` Phil Muldoon
2011-07-04  6:36   ` Matt Rice [this message]
2011-07-04 10:09     ` Phil Muldoon
2011-07-05  2:47       ` Matt Rice
2011-07-05 20:41         ` Phil Muldoon
2011-07-04 10:50     ` Eli Zaretskii
2011-07-04 10:56       ` Matt Rice
2011-07-04 13:14         ` Eli Zaretskii

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CACTLOFo5rD6PCLeK+hmoBt-vAHGJZDNS_gPbZG6tP=CP=gFeog@mail.gmail.com' \
    --to=ratmice@gmail.com \
    --cc=gdb-patches@sourceware.org \
    --cc=pmuldoon@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox