Bug 18073 - gdb.Type.fields() throws an exception when called on a function type
Summary: gdb.Type.fields() throws an exception when called on a function type
Status: RESOLVED FIXED
Alias: None
Product: gdb
Classification: Unclassified
Component: python (show other bugs)
Version: HEAD
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-03-02 19:00 UTC by pipcet
Modified: 2015-10-21 20:03 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments
patch (307 bytes, patch)
2015-03-02 19:00 UTC, pipcet
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description pipcet 2015-03-02 19:00:39 UTC
Created attachment 8160 [details]
patch

I'd like to submit the following obvious patch. It makes the python gdb.Type.fields function return fields for function types, rather than throwing an exception.

I do not know enough about gdb types to know whether there are any other type codes that .fields() might validly get called on. (I do know that both the python and the guile interface to types appear to be broken right now, which is an unfortunate coincidence.)

diff --git a/gdb/python/py-type.c b/gdb/python/py-type.c
index a3da678..aed6b03 100644
--- a/gdb/python/py-type.c
+++ b/gdb/python/py-type.c
@@ -486,10 +486,11 @@ typy_get_composite (struct type *type)
      exception.  */
   if (TYPE_CODE (type) != TYPE_CODE_STRUCT
       && TYPE_CODE (type) != TYPE_CODE_UNION
-      && TYPE_CODE (type) != TYPE_CODE_ENUM)
+      && TYPE_CODE (type) != TYPE_CODE_ENUM
+      && TYPE_CODE (type) != TYPE_CODE_FUNC)
     {
       PyErr_SetString (PyExc_TypeError,
-		       "Type is not a structure, union, or enum type.");
+		       "Type is not a structure, union, enum, or function type.");
       return NULL;
     }
Comment 1 Sourceware Commits 2015-10-21 19:59:35 UTC
The master branch has been updated by Simon Marchi <simark@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=bed91f4da252b5f30d3cb4fd9f9febd61ad9560d

commit bed91f4da252b5f30d3cb4fd9f9febd61ad9560d
Author: Simon Marchi <simon.marchi@ericsson.com>
Date:   Wed Oct 21 15:57:22 2015 -0400

    Fix accessing a function's fields (parameters) from Python (PR 18073)
    
    Since 7.4, gdb doesn't allow calling .fields() on a function type, even
    though the documentation states it should return a list corresponding to
    the function's parameters.  This patch restores the intended behaviour
    and adds a test for it.
    
    Reg-tested on Arch Linux x86-64.
    
    gdb/ChangeLog:
    
    	PR python/18073
    	* python/py-type.c (typy_get_composite): Allow returning a
    	function type.
    
    gdb/testsuite/ChangeLog:
    
    	PR python/18073
    	* gdb.python/py-type.c (C::a_method): New.
    	(C::a_const_method): New.
    	(C::a_static_method): New.
    	(a_function): New.
    	* gdb.python/py-type.exp (test_fields): Test getting fields
    	from function and method.
Comment 2 Simon Marchi 2015-10-21 20:03:18 UTC
This should be fixed in master.  Thanks for reporting and providing the fix!