From patchwork Sat Dec 12 18:59:21 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Koenig X-Patchwork-Id: 1415461 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gcc.gnu.org Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=LgfP4xTa; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4CtcQY4KgDz9sPB for ; Sun, 13 Dec 2020 05:59:32 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9EAC1388E821; Sat, 12 Dec 2020 18:59:27 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9EAC1388E821 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1607799567; bh=cPACtqoaitC16GpXAq8iBtuE0zPpRT5TT7tGJzD7VXc=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=LgfP4xTa7OohExJKE4G7KcxvHOgB+DFGFxqsvNCDJ34C2zqVYzOq2uxi4TK53wlQr Dypo8tMIPdpAjBbeCFWgDvaiBf3obABATuhjxFiQJScIU7thc4CbfLOYv30FmyDXuo 4rSuOuqYNKDLEZipKX7kTIHW4econ6YbPfOQ76SI= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from cc-smtpout1.netcologne.de (cc-smtpout1.netcologne.de [89.1.8.211]) by sourceware.org (Postfix) with ESMTPS id 029163851C03; Sat, 12 Dec 2020 18:59:25 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 029163851C03 Received: from cc-smtpin1.netcologne.de (cc-smtpin1.netcologne.de [89.1.8.201]) by cc-smtpout1.netcologne.de (Postfix) with ESMTP id 870C21348D; Sat, 12 Dec 2020 19:59:23 +0100 (CET) Received: from localhost (localhost [127.0.0.1]) by cc-smtpin1.netcologne.de (Postfix) with ESMTP id 7981111E53; Sat, 12 Dec 2020 19:59:23 +0100 (CET) Received: from [2001:4dd6:1e68:0:3cb9:3e62:cd30:e31f] (helo=cc-smtpin1.netcologne.de) by localhost with ESMTP (eXpurgate 4.11.6) (envelope-from ) id 5fd5130b-02f5-7f0000012729-7f000001c088-1 for ; Sat, 12 Dec 2020 19:59:23 +0100 Received: from linux-p51k.fritz.box (2001-4dd6-1e68-0-3cb9-3e62-cd30-e31f.ipv6dyn.netcologne.de [IPv6:2001:4dd6:1e68:0:3cb9:3e62:cd30:e31f]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by cc-smtpin1.netcologne.de (Postfix) with ESMTPSA; Sat, 12 Dec 2020 19:59:21 +0100 (CET) To: "fortran@gcc.gnu.org" Subject: [patch, fortran] Optionally improve debugging of auxiliary variables Message-ID: Date: Sat, 12 Dec 2020 19:59:21 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.5.0 MIME-Version: 1.0 Content-Language: de-DE X-Spam-Status: No, score=-10.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Thomas Koenig via Gcc-patches From: Thomas Koenig Reply-To: Thomas Koenig Cc: gcc-patches Errors-To: gcc-patches-bounces@gcc.gnu.org Sender: "Gcc-patches" Hello world, I have struggled with debugging the GENERIC generated by the Fortran front end because it is only possible to look at the code via -fdump-tree-original, but it is not possible to inspect the values; additionally, the D.3456 form makes it hard to read which variable is which. This patch adds a flag which gives all variables generated by Fortran a name and makes them visible to debuggers. As an example, compiler-generated variables now look like this (for a "Hello, world" program): { struct __st_parameter_dt dt_parm:0; dt_parm:0.common.filename = &"hello.f90"[1]{lb: 1 sz: 1}; dt_parm:0.common.line = 2; dt_parm:0.common.flags = 128; dt_parm:0.common.unit = 6; _gfortran_st_write (&dt_parm:0); _gfortran_transfer_character_write (&dt_parm:0, &"Hello, world"[1]{lb: 1 sz: 1}, 12); _gfortran_st_write_done (&dt_parm:0); } Note the colon in the variable name, which I chose because it is not in the user's namespace, and gdb does not choke on it. In order to inspect the variables, you usually have to step a bit through the generated assembly code, but you can then print the values, manipulate them etc (and sometimes also hit an internal error in gdb). Example of a debugging session: (gdb) b _gfortran_st_write Breakpoint 1 at 0x4005f0 (gdb) r Starting program: /tmp/a.out [Thread debugging using libthread_db enabled] Using host libthread_db library "/lib64/libthread_db.so.1". Breakpoint 1, _gfortran_st_write (dtp=0x7fffffffd9f0) at ../../../coarray_native/libgfortran/io/transfer.c:4398 4398 { (gdb) fin Run till exit from #0 _gfortran_st_write (dtp=0x7fffffffd9f0) at ../../../coarray_native/libgfortran/io/transfer.c:4398 0x0000000000400719 in MAIN__ () at hello.f90:2 2 print *,"Hello, world" (gdb) info local dt_parm:0 = ( common = ( flags = 128, unit = 6, filename = 0x400810, line = 2, iomsg_len = 0, iomsg = 0x0, iostat = 0x0 ), rec = 0, size = 0x0, iolength = 0x0, internal_unit_desc = 0x0, format = 0x0, format_len = 0, advance_len = 0, advance = 0x0, internal_unit = 0x0, internal_unit_len = 0, namelist_name_len = 0, namelist_name = 0x0, id = 0x2, pos = -9223372036854775802, asynchronous = 0x0, asynchronous_len = 4569374499253603072, blank_len = 0, blank = 0x3f69ade5c1bd4b00, decimal = 0x7ffff79490f0 <_gfortrani_backtrace_handler>, decimal_len = 140737335317407, delim_len = 140737347096816, delim = 0x1000000, pad = 0x0, pad_len = 0, round_len = 0, round = 0x0, sign = 0x0, sign_len = 0, u = '`&\270\367\377\177\000\000\060H`\000\000\000\000\000\000\000\000\000\001', '\000' , '\002', '\000' , '\377\377\377\377\377\377\377\377', '\000' ... ) (gdb) p dt_parm:0%common $1 = ( flags = 128, unit = 6, filename = 0x400810, line = 2, iomsg_len = 0, iomsg = 0x0, iostat = 0x0 ) (gdb) p dt_parm:0%common%filename $2 = (PTR TO -> ( character(kind=1) )) 0x400810 There is no user impact (only developers will use this), so it is not surprising that there is no regression. So, OK for trunk? Best regards Thomas diff --git a/gcc/fortran/invoke.texi b/gcc/fortran/invoke.texi index 8bdc8a6b038..58e4e50b315 100644 --- a/gcc/fortran/invoke.texi +++ b/gcc/fortran/invoke.texi @@ -1227,6 +1227,17 @@ compiler itself. The output generated by this option might change between releases. This option may also generate internal compiler errors for features which have only recently been added. +@item -fdebug-aux-vars +@opindex @code{debug-aux-vars} +Make internal variables generated by the gfortran front end visible to a +debugger. This option also renames these internal variables shown by +@code{-fdump-tree-original} to a form @code{string:number} and enables +compiler warnings on them. + +This option is only really useful when debugging the gfortran compiler +itself together with @code{-g} or @code{-ggdb3} and +@code{-fdump-tree-original}. + @item -ffpe-trap=@var{list} @opindex @code{ffpe-trap=}@var{list} Specify a list of floating point exception traps to enable. On most diff --git a/gcc/fortran/lang.opt b/gcc/fortran/lang.opt index 96ed208cb85..57b0264458e 100644 --- a/gcc/fortran/lang.opt +++ b/gcc/fortran/lang.opt @@ -452,6 +452,10 @@ fd-lines-as-comments Fortran RejectNegative Treat lines with 'D' in column one as comments. +fdebug-aux-vars +Fortran Var(flag_debug_aux_vars) +Issue debug information for compiler-generated auxiliary variables. + fdec Fortran Var(flag_dec) Enable all DEC language extensions. diff --git a/gcc/fortran/trans.c b/gcc/fortran/trans.c index 025abe38985..1d5f6c28c7a 100644 --- a/gcc/fortran/trans.c +++ b/gcc/fortran/trans.c @@ -73,6 +73,40 @@ gfc_advance_chain (tree t, int n) return t; } +static int num_var; + +#define MAX_PREFIX_LEN 20 + +static tree +create_var_debug_raw (tree type, const char *prefix) +{ + /* Space for prefix + @ + 9-digit-number + \0. */ + char name_buf[MAX_PREFIX_LEN + 1 + 9 + 1]; + tree t; + + if (prefix == NULL) + prefix = "gfc"; + else + gcc_assert (strlen(prefix) <= MAX_PREFIX_LEN); + + snprintf (name_buf, sizeof(name_buf), "%s:%d", prefix, num_var++); + t = build_decl (input_location, VAR_DECL, get_identifier (name_buf), type); + + /* We want debug info for it. */ + DECL_IGNORED_P (t) = 0; + /* It should not be nameless. */ + DECL_NAMELESS (t) = 0; + + /* Make the variable writable. */ + TREE_READONLY (t) = 0; + + DECL_EXTERNAL (t) = 0; + TREE_STATIC (t) = 0; + TREE_USED (t) = 1; + + return t; +} + /* Creates a variable declaration with a given TYPE. */ tree @@ -80,6 +114,9 @@ gfc_create_var_np (tree type, const char *prefix) { tree t; + if (flag_debug_aux_vars) + return create_var_debug_raw (type, prefix); + t = create_tmp_var_raw (type, prefix); /* No warnings for anonymous variables. */