gdbhooks.py: dump-fn, dot-fn: cast ret values of fopen/fclose

Message ID 87r26z8fin.fsf@ispras.ru
State New
Headers show
Series
  • gdbhooks.py: dump-fn, dot-fn: cast ret values of fopen/fclose
Related show

Commit Message

Vladislav Ivanishin July 9, 2019, 2:10 p.m.
Hi,

Without the patch, I see these error messages with gdb 8.3:

    (gdb) Python Exception <class 'gdb.error'> 'fclose@@GLIBC_2.2.5' has
    unknown return type; cast the call to its declared return type:
    (gdb) Error occurred in Python: 'fclose@@GLIBC_2.2.5' has unknown
    return type; cast the call to its declared return type

One doesn't have to use python to reproduce that: start debugging cc1
and issue

    (gdb) call fopen ("", "")

This actually looks like a GDB bug: from looking at cc1's (built with
either -g, or -ggdb3) DWARF with either dwarfdump, or readelf I see that
there is info about the return type (for fopen it's FILE *, and `ptype
FILE` in gdb gives the full struct).

Tom, you contributed the {dot,dump}-fn functions.  Do they still work
for you without the patch?  (And if so, do you happen to have debuginfo
for libc installed on you machine?)

I think, the patch itself is obvious (as a workaround).  I've only
tested it with the version of GDB I have (8.3, which is the latest
release), but expect this to work for older versions as well.

(Comparisons of gdb.Value's returned from parse_and_eval, like fp == 0
and their conversion to python strings in "%s" % fp work automagically.)
-- 
Vlad

Comments

Tom de Vries July 14, 2019, 10:50 a.m. | #1
On 09-07-19 16:10, Vladislav Ivanishin wrote:
> Hi,

> 

> Without the patch, I see these error messages with gdb 8.3:

> 

>     (gdb) Python Exception <class 'gdb.error'> 'fclose@@GLIBC_2.2.5' has

>     unknown return type; cast the call to its declared return type:

>     (gdb) Error occurred in Python: 'fclose@@GLIBC_2.2.5' has unknown

>     return type; cast the call to its declared return type

> 

> One doesn't have to use python to reproduce that: start debugging cc1

> and issue

> 

>     (gdb) call fopen ("", "")

> 

> This actually looks like a GDB bug: from looking at cc1's (built with

> either -g, or -ggdb3) DWARF with either dwarfdump, or readelf I see that

> there is info about the return type (for fopen it's FILE *, and `ptype

> FILE` in gdb gives the full struct).

> 

> Tom, you contributed the {dot,dump}-fn functions.  Do they still work

> for you without the patch?  (And if so, do you happen to have debuginfo

> for libc installed on you machine?)

> 


I just observed the same problem, and managed to make it go away by
installing libc debuginfo.

> I think, the patch itself is obvious (as a workaround).  I've only

> tested it with the version of GDB I have (8.3, which is the latest

> release), but expect this to work for older versions as well.

> 

> (Comparisons of gdb.Value's returned from parse_and_eval, like fp == 0

> and their conversion to python strings in "%s" % fp work automagically.)

> 

> 


LGTM. I think you can check this in under the obvious rule.

Thanks,
- Tom

Patch

        * gdbhooks.py (DumpFn.invoke): Add explicit casts of return values of
        fopen and fclose to their respective types.
        (DotFn.invoke): Ditto.
---
 gcc/gdbhooks.py | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)

diff --git a/gcc/gdbhooks.py b/gcc/gdbhooks.py
index 15f9738aeee..c68bffb4d1a 100644
--- a/gcc/gdbhooks.py
+++ b/gcc/gdbhooks.py
@@ -773,18 +773,17 @@  class DumpFn(gdb.Command):
             f.close()
 
         # Open file
-        fp = gdb.parse_and_eval("fopen (\"%s\", \"w\")" % filename)
+        fp = gdb.parse_and_eval("(FILE *) fopen (\"%s\", \"w\")" % filename)
         if fp == 0:
             print ("Could not open file: %s" % filename)
             return
-        fp = "(FILE *)%u" % fp
 
         # Dump function to file
         _ = gdb.parse_and_eval("dump_function_to_file (%s, %s, %u)" %
                                (func, fp, flags))
 
         # Close file
-        ret = gdb.parse_and_eval("fclose (%s)" % fp)
+        ret = gdb.parse_and_eval("(int) fclose (%s)" % fp)
         if ret != 0:
             print ("Could not close file: %s" % filename)
             return
@@ -843,11 +842,10 @@  class DotFn(gdb.Command):
 
         # Close and reopen temp file to get C FILE*
         f.close()
-        fp = gdb.parse_and_eval("fopen (\"%s\", \"w\")" % filename)
+        fp = gdb.parse_and_eval("(FILE *) fopen (\"%s\", \"w\")" % filename)
         if fp == 0:
             print("Cannot open temp file")
             return
-        fp = "(FILE *)%u" % fp
 
         # Write graph to temp file
         _ = gdb.parse_and_eval("start_graph_dump (%s, \"<debug>\")" % fp)
@@ -856,7 +854,7 @@  class DotFn(gdb.Command):
         _ = gdb.parse_and_eval("end_graph_dump (%s)" % fp)
 
         # Close temp file
-        ret = gdb.parse_and_eval("fclose (%s)" % fp)
+        ret = gdb.parse_and_eval("(int) fclose (%s)" % fp)
         if ret != 0:
             print("Could not close temp file: %s" % filename)
             return
-- 
2.22.0