From patchwork Thu Feb 18 17:26:19 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom de Vries X-Patchwork-Id: 584835 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 5D4F0140C2E for ; Fri, 19 Feb 2016 04:26:46 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b=m+ykb01G; dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :subject:references:cc:to:from:message-id:date:mime-version :in-reply-to:content-type; q=dns; s=default; b=tdW1E6QK+m8O3NEUa GFV4hMiOEehPc3N6mCfbyXOhNnolI9MXUA0l0zhqtzdod+wqLmEUrw8Wp0fQ3ViP Jvrhs8g6W23ZqXKS5nR/5HlcoTP1NrTLizIyrHqVSOxOsGUh/uXMfXSaXO3bVvLa qTtG5FIiAWKLhcTNmKOj0rNzuA= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :subject:references:cc:to:from:message-id:date:mime-version :in-reply-to:content-type; s=default; bh=McksuqZ+w4ecfGPmXoFYu8y ro0A=; b=m+ykb01GndC4xxPG6VDli526Ig3qT2J9Vu0Ip6HCk05RL+vAoG7FDrW Bsq3kxJBuZQ8yntZqPuHlj8ECOqYmXuc4J0umyfe+os1LBuxrBkScId8g+iGWW3W fk7HRwpJjRkS+oyLhcQrk0QjOsWu5xxC+Byf4wBc0l/6QPgQQ/i0= Received: (qmail 42889 invoked by alias); 18 Feb 2016 17:26:39 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 42878 invoked by uid 89); 18 Feb 2016 17:26:39 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.0 required=5.0 tests=AWL, BAYES_00, RP_MATCHES_RCVD, SPF_PASS autolearn=ham version=3.3.2 spammy=Hx-languages-length:2831, HX-detected-operating-system:Windows X-HELO: fencepost.gnu.org Received: from fencepost.gnu.org (HELO fencepost.gnu.org) (208.118.235.10) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-SHA encrypted) ESMTPS; Thu, 18 Feb 2016 17:26:37 +0000 Received: from eggs.gnu.org ([2001:4830:134:3::10]:47002) by fencepost.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:256) (Exim 4.82) (envelope-from ) id 1aWSLb-0005FP-9t for gcc-patches@gnu.org; Thu, 18 Feb 2016 12:26:35 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aWSLX-00014X-Nk for gcc-patches@gnu.org; Thu, 18 Feb 2016 12:26:34 -0500 Received: from relay1.mentorg.com ([192.94.38.131]:51286) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aWSLX-000144-Hh for gcc-patches@gnu.org; Thu, 18 Feb 2016 12:26:31 -0500 Received: from nat-ies.mentorg.com ([192.94.31.2] helo=SVR-IES-FEM-01.mgc.mentorg.com) by relay1.mentorg.com with esmtp id 1aWSLU-0004yN-QT from Tom_deVries@mentor.com ; Thu, 18 Feb 2016 09:26:29 -0800 Received: from [127.0.0.1] (137.202.0.76) by SVR-IES-FEM-01.mgc.mentorg.com (137.202.0.104) with Microsoft SMTP Server id 14.3.224.2; Thu, 18 Feb 2016 17:26:27 +0000 Subject: Re: [PATCH] Add debug_function_to_file References: <56C46A70.4070605@mentor.com> <56C5D52E.9040302@mentor.com> <56C5E0A9.1040900@mentor.com> <56C5E69A.20000@mentor.com> CC: Richard Biener , "gcc-patches@gnu.org" To: David Malcolm From: Tom de Vries Message-ID: <56C5FEBB.5000805@mentor.com> Date: Thu, 18 Feb 2016 18:26:19 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.5.1 MIME-Version: 1.0 In-Reply-To: <56C5E69A.20000@mentor.com> X-detected-operating-system: by eggs.gnu.org: Windows NT kernel [generic] [fuzzy] X-Received-From: 192.94.38.131 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? OK for stage4/stage1? Thanks, - Tom 2016-02-18 Tom de Vries * gdbhooks.py (class debug_function_to_file): Add and instantiate. Add debug-function-to-file to gdbhooks.py --- gcc/gdbhooks.py | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/gcc/gdbhooks.py b/gcc/gdbhooks.py index 0d5cc97..cffac2a 100644 --- a/gcc/gdbhooks.py +++ b/gcc/gdbhooks.py @@ -589,4 +589,68 @@ class BreakOnPass(gdb.Command): BreakOnPass() +class debug_function_to_file(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. + + Examples of use: + (gdb) debug-function-to-file foo.1 + (gdb) debug-function-to-file foo.1 cfun->decl + (gdb) debug-function-to-file foo.1 cfun->decl 0 + (gdb) debug-function-to-file foo.1 cfun->decl dump_flags + """ + + def __init__(self): + gdb.Command.__init__(self, 'debug-function-to-file', gdb.COMMAND_USER) + + def invoke(self, arg, from_tty): + # Parse args, check number of args + args = gdb.string_to_argv(arg) + if len(args) > 3: + print ("Too many arguments") + return + if len(args) == 0: + print ("Too little arguments") + return + + # Set filename + filename = args[0] + + # Set func + if len(args) >= 2: + funcname = args[1] + 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 + + # Set flags + if len(args) >= 3: + flags = gdb.parse_and_eval(args[2]) + else: + flags = 0 + + # Open file + fp = gdb.parse_and_eval("fopen (\"%s\", \"w\")" % filename) + if fp == 0: + print ("Could not open file: %s" % filename) + return + + # Dump function to file + dumpargs = "(tree)%u, (FILE *)%u, %u" % (func, fp, flags) + _ = gdb.parse_and_eval("dump_function_to_file (%s)" % dumpargs) + + # Close file + ret = gdb.parse_and_eval("fclose ((FILE *)%u)" % fp) + if ret != 0: + print ("Could not close file: %s" % filename) + +debug_function_to_file() + print('Successfully loaded GDB hooks for GCC')