This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC 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: [PATCH] Add debug_function_to_file


On 18/02/16 20:41, David Malcolm wrote:
On Thu, 2016-02-18 at 18:26 +0100, Tom de Vries wrote:
On 18/02/16 16:43, Tom de Vries wrote:
On 18/02/16 16:27, Richard Biener wrote:
I would be nice if we could avoid the ${1,2,3} printouts
and value
history
assignments, but I'm not sure how to do that.



Using gdb.parse_and_eval does the trick.


This updated version uses gdb.parse_and_eval, and adds error
handling.

And this updated version adds handling different number of arguments,
and a help text. I think this could be ready for committing.

Is a bootstrap/regtest useful/necessary?

I don't think so; I don't think we have any automated coverage for
gdb_hooks.py.  Presumably you've tested it by hand.


Indeed, I did.

What version of Python do you have embedded in gdb?  (IIRC some people
have Python 2, others Python 3)


AFAIU, Python 3, more specifically:
...
$ cat /etc/issue
Ubuntu 14.04.3 LTS \n \l
$ gdb --version
GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.2) 7.7.1
$ gdb
(gdb) python import sys; print(sys.version)
3.4.3 (default, Oct 14 2015, 20:31:36)
[GCC 4.8.4]
(gdb)
...

+            print ("Too little arguments")

Nit: can you change this to "Not enough arguments".


In the attached reworked version, this error message no longer occurs.

OK for stage4/stage1?

Looks reasonable to me.


I've done two minor cleanups:
- I've moved casting the C call arguments out of the call expressions,
  and into the argument variables themselves
- I've changed the naming of the class to be consistent with the rest
  of the file (capitalize words, connect without underscore)

Furthermore, I've tried to improve usability:
- I've renamed the command to a shorter dump-fn (similar to dot-fn)
- I've added a pop-up mode (also similar to dot-fn), where we:
  - write to a temp file,
  - invoke an editor using the $EDITOR command (I needed to set
    EDITOR=emacs to get this working, my default EDITOR="emacs -nw"
    didn't work, giving not-a-tty error), and
  - remove the temp file after the editor is closed.
- the new pop-up mode is the default.
- the original file-only mode is selected by using /f <file> as first
  two arguments.

Thanks,
- Tom
Add debug-function-to-file to gdbhooks.py

2016-02-18  Tom de Vries  <tom@codesourcery.com>

	* gdbhooks.py (class debug_function_to_file): Add and instantiate.

---
 gcc/gdbhooks.py | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 91 insertions(+)

diff --git a/gcc/gdbhooks.py b/gcc/gdbhooks.py
index 0d5cc97..ed72016 100644
--- a/gcc/gdbhooks.py
+++ b/gcc/gdbhooks.py
@@ -133,6 +133,7 @@ Instead (for now) you must access m_vecdata:
 import os.path
 import re
 import sys
+import tempfile
 
 import gdb
 import gdb.printing
@@ -589,4 +590,94 @@ class BreakOnPass(gdb.Command):
 
 BreakOnPass()
 
+class DumpFn(gdb.Command):
+    """
+    A custom command to dump a gimple/rtl function to file.  By default, it
+    dumps the current function using 0 as dump_flags, but the function and flags
+    can also be specified. If /f <file> are passed as the first two arguments,
+    the dump is written to that file.  Otherwise, a temporary file is created
+    and opened in the text editor specified in the EDITOR environment variable.
+
+    Examples of use:
+      (gdb) dump-fn
+      (gdb) dump-fn /f foo.1.txt
+      (gdb) dump-fn cfun->decl
+      (gdb) dump-fn /f foo.1.txt cfun->decl
+      (gdb) dump-fn cfun->decl 0
+      (gdb) dump-fn cfun->decl dump_flags
+    """
+
+    def __init__(self):
+        gdb.Command.__init__(self, 'dump-fn', gdb.COMMAND_USER)
+
+    def invoke(self, arg, from_tty):
+        # Parse args, check number of args
+        args = gdb.string_to_argv(arg)
+        if len(args) >= 1 and args[0] == "/f":
+            if len(args) == 1:
+                print ("Missing file argument")
+                return
+            filename = args[1]
+            editor_mode = False
+            base_arg = 2
+        else:
+            editor = os.getenv("EDITOR", "")
+            if editor == "":
+                print ("EDITOR environment variable not defined")
+                return
+            editor_mode = True
+            base_arg = 0
+        if len(args) - base_arg > 2:
+            print ("Too many arguments")
+            return
+
+        # Set func
+        if len(args) - base_arg >= 1:
+            funcname = args[base_arg]
+            printfuncname = "function %s" % funcname
+        else:
+            funcname = "cfun ? cfun->decl : current_function_decl"
+            printfuncname = "current function"
+        func = gdb.parse_and_eval(funcname)
+        if func == 0:
+            print ("Could not find %s" % printfuncname)
+            return
+        func = "(tree)%u" % func
+
+        # Set flags
+        if len(args) - base_arg >= 2:
+            flags = gdb.parse_and_eval(args[base_arg + 1])
+        else:
+            flags = 0
+
+        # Get tempory file, if necessary
+        if editor_mode:
+            f = tempfile.NamedTemporaryFile(delete=False, suffix=".txt")
+            filename = f.name
+            f.close()
+
+        # Open file
+        fp = gdb.parse_and_eval("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)
+        if ret != 0:
+            print ("Could not close file: %s" % filename)
+            return
+
+        # Open file in editor, if necessary
+        if editor_mode:
+            os.system("( %s \"%s\"; rm \"%s\" ) &" %
+                      (editor, filename, filename))
+
+DumpFn()
+
 print('Successfully loaded GDB hooks for GCC')

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