[RFA] PR symtab/16264 - support DW_AT_main_subprogram

Message ID 1479152446-329-1-git-send-email-tom@tromey.com
State New, archived
Headers

Commit Message

Tom Tromey Nov. 14, 2016, 7:40 p.m. UTC
  This patch adds support for DW_AT_main_subprogram.
This is PR symtab/16264.

DW_AT_main_subprogram is used to mark a program's entry point.  GCC
can emit this, and I hope to change the Rust compiler to emit it as
well.

GDB already supports an older, pre-DWARF 4 convention adopted by
FORTRAN compilers, namely to emit DW_AT_calling_convention for the
"main" function.  However, I think this support in GDB had a small
bug, in that it seems to rely on the DW_AT_name being read before
DW_AT_calling_convention.  This patch fixes this as well.

Built and regtested on x86-64 Fedora 24 and the buildbot.  New test
case included.

2016-11-14  Tom Tromey  <tom@tromey.com>

	PR symtab/16264:
	* dwarf2read.c (struct partial_die_info) <main_subprogram>: New
	member.
	(add_partial_symbol): Call set_objfile_main_name.
	(read_partial_die): Handle DW_AT_main_subprogram.
	<DW_AT_calling_convention>: don't call set_objfile_main_name, but
	set main_subprogram flag.

2016-11-14  Tom Tromey  <tom@tromey.com>

	* gdb.dwarf2/main-subprogram.c: New file.
	* gdb.dwarf2/main-subprogram.exp: New file.
---
 gdb/ChangeLog                                | 10 ++++
 gdb/dwarf2read.c                             | 33 ++++++++-----
 gdb/testsuite/ChangeLog                      |  5 ++
 gdb/testsuite/gdb.dwarf2/main-subprogram.c   | 29 +++++++++++
 gdb/testsuite/gdb.dwarf2/main-subprogram.exp | 73 ++++++++++++++++++++++++++++
 5 files changed, 138 insertions(+), 12 deletions(-)
 create mode 100644 gdb/testsuite/gdb.dwarf2/main-subprogram.c
 create mode 100644 gdb/testsuite/gdb.dwarf2/main-subprogram.exp
  

Comments

Luis Machado Nov. 16, 2016, 2:51 p.m. UTC | #1
On 11/14/2016 01:40 PM, Tom Tromey wrote:
> This patch adds support for DW_AT_main_subprogram.
> This is PR symtab/16264.
>
> DW_AT_main_subprogram is used to mark a program's entry point.  GCC
> can emit this, and I hope to change the Rust compiler to emit it as
> well.
>
> GDB already supports an older, pre-DWARF 4 convention adopted by
> FORTRAN compilers, namely to emit DW_AT_calling_convention for the
> "main" function.  However, I think this support in GDB had a small
> bug, in that it seems to rely on the DW_AT_name being read before
> DW_AT_calling_convention.  This patch fixes this as well.
>
> Built and regtested on x86-64 Fedora 24 and the buildbot.  New test
> case included.
>
> 2016-11-14  Tom Tromey  <tom@tromey.com>
>
> 	PR symtab/16264:
> 	* dwarf2read.c (struct partial_die_info) <main_subprogram>: New
> 	member.
> 	(add_partial_symbol): Call set_objfile_main_name.
> 	(read_partial_die): Handle DW_AT_main_subprogram.
> 	<DW_AT_calling_convention>: don't call set_objfile_main_name, but
> 	set main_subprogram flag.
>
> 2016-11-14  Tom Tromey  <tom@tromey.com>
>
> 	* gdb.dwarf2/main-subprogram.c: New file.
> 	* gdb.dwarf2/main-subprogram.exp: New file.
> ---
>  gdb/ChangeLog                                | 10 ++++
>  gdb/dwarf2read.c                             | 33 ++++++++-----
>  gdb/testsuite/ChangeLog                      |  5 ++
>  gdb/testsuite/gdb.dwarf2/main-subprogram.c   | 29 +++++++++++
>  gdb/testsuite/gdb.dwarf2/main-subprogram.exp | 73 ++++++++++++++++++++++++++++
>  5 files changed, 138 insertions(+), 12 deletions(-)
>  create mode 100644 gdb/testsuite/gdb.dwarf2/main-subprogram.c
>  create mode 100644 gdb/testsuite/gdb.dwarf2/main-subprogram.exp
>
> diff --git a/gdb/ChangeLog b/gdb/ChangeLog
> index b4398f6..fafeea9 100644
> --- a/gdb/ChangeLog
> +++ b/gdb/ChangeLog
> @@ -1,3 +1,13 @@
> +2016-11-14  Tom Tromey  <tom@tromey.com>
> +
> +	PR symtab/16264:
> +	* dwarf2read.c (struct partial_die_info) <main_subprogram>: New
> +	member.
> +	(add_partial_symbol): Call set_objfile_main_name.
> +	(read_partial_die): Handle DW_AT_main_subprogram.
> +	<DW_AT_calling_convention>: don't call set_objfile_main_name, but
> +	set main_subprogram flag.
> +
>  2016-11-12  Tom Tromey  <tom@tromey.com>
>
>  	* rust-exp.y (super_name): Use std::vector.
> diff --git a/gdb/dwarf2read.c b/gdb/dwarf2read.c
> index 1ad6b00..ced7f72 100644
> --- a/gdb/dwarf2read.c
> +++ b/gdb/dwarf2read.c
> @@ -1105,6 +1105,9 @@ struct partial_die_info
>      unsigned int has_pc_info : 1;
>      unsigned int may_be_inlined : 1;
>
> +    /* This DIE has been marked DW_AT_main_subprogram.  */
> +    unsigned int main_subprogram : 1;
> +
>      /* Flag set if the SCOPE field of this structure has been
>         computed.  */
>      unsigned int scope_set : 1;
> @@ -6930,6 +6933,9 @@ add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
>  			       &objfile->static_psymbols,
>  			       addr, cu->language, objfile);
>  	}
> +
> +      if (pdi->main_subprogram && actual_name != NULL)
> +	set_objfile_main_name (objfile, actual_name, cu->language);
>        break;
>      case DW_TAG_constant:
>        {
> @@ -15945,19 +15951,18 @@ read_partial_die (const struct die_reader_specs *reader,
>  	     to describe functions' calling conventions.
>
>  	     However, because it's a necessary piece of information in
> -	     Fortran, and because DW_CC_program is the only piece of debugging
> -	     information whose definition refers to a 'main program' at all,
> -	     several compilers have begun marking Fortran main programs with
> -	     DW_CC_program --- even when those functions use the standard
> -	     calling conventions.
> -
> -	     So until DWARF specifies a way to provide this information and
> -	     compilers pick up the new representation, we'll support this
> -	     practice.  */
> +	     Fortran, and before DWARF 4 DW_CC_program was the only
> +	     piece of debugging information whose definition refers to
> +	     a 'main program' at all, several compilers marked Fortran
> +	     main programs with DW_CC_program --- even when those
> +	     functions use the standard calling conventions.
> +
> +	     Although DWARF now specifies a way to provide this
> +	     information, we support this practice for backward
> +	     compatibility.  */
>  	  if (DW_UNSND (&attr) == DW_CC_program
> -	      && cu->language == language_fortran
> -	      && part_die->name != NULL)
> -	    set_objfile_main_name (objfile, part_die->name, language_fortran);
> +	      && cu->language == language_fortran)
> +	    part_die->main_subprogram = 1;
>  	  break;
>  	case DW_AT_inline:
>  	  if (DW_UNSND (&attr) == DW_INL_inlined
> @@ -15974,6 +15979,10 @@ read_partial_die (const struct die_reader_specs *reader,
>  	    }
>  	  break;
>
> +	case DW_AT_main_subprogram:
> +	  part_die->main_subprogram = DW_UNSND (&attr);
> +	  break;
> +
>  	default:
>  	  break;
>  	}
> diff --git a/gdb/testsuite/ChangeLog b/gdb/testsuite/ChangeLog
> index 054629f..d971347 100644
> --- a/gdb/testsuite/ChangeLog
> +++ b/gdb/testsuite/ChangeLog
> @@ -1,3 +1,8 @@
> +2016-11-14  Tom Tromey  <tom@tromey.com>
> +
> +	* gdb.dwarf2/main-subprogram.c: New file.
> +	* gdb.dwarf2/main-subprogram.exp: New file.
> +
>  2016-11-11  Catherine Moore  <clm@codesourcery.com>
>
>  	* gdb.base/dump.exp: Identify verilog format dump tests as such.
> diff --git a/gdb/testsuite/gdb.dwarf2/main-subprogram.c b/gdb/testsuite/gdb.dwarf2/main-subprogram.c
> new file mode 100644
> index 0000000..97ca22e
> --- /dev/null
> +++ b/gdb/testsuite/gdb.dwarf2/main-subprogram.c
> @@ -0,0 +1,29 @@
> +/* This testcase is part of GDB, the GNU debugger.
> +
> +   Copyright 2016 Free Software Foundation, Inc.
> +
> +   This program is free software; you can redistribute it and/or modify
> +   it under the terms of the GNU General Public License as published by
> +   the Free Software Foundation; either version 3 of the License, or
> +   (at your option) any later version.
> +
> +   This program is distributed in the hope that it will be useful,
> +   but WITHOUT ANY WARRANTY; without even the implied warranty of
> +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> +   GNU General Public License for more details.
> +
> +   You should have received a copy of the GNU General Public License
> +   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
> +
> +int
> +mymain ()
> +{
> +  asm ("mymain_label: .globl mymain_label");
> +  return 0;
> +}
> +
> +int
> +main()
> +{
> +  return mymain ();
> +}
> diff --git a/gdb/testsuite/gdb.dwarf2/main-subprogram.exp b/gdb/testsuite/gdb.dwarf2/main-subprogram.exp
> new file mode 100644
> index 0000000..8adb801
> --- /dev/null
> +++ b/gdb/testsuite/gdb.dwarf2/main-subprogram.exp
> @@ -0,0 +1,73 @@
> +# Copyright 2016 Free Software Foundation, Inc.
> +
> +# This program is free software; you can redistribute it and/or modify
> +# it under the terms of the GNU General Public License as published by
> +# the Free Software Foundation; either version 3 of the License, or
> +# (at your option) any later version.
> +#
> +# This program is distributed in the hope that it will be useful,
> +# but WITHOUT ANY WARRANTY; without even the implied warranty of
> +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> +# GNU General Public License for more details.
> +#
> +# You should have received a copy of the GNU General Public License
> +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
> +load_lib dwarf.exp
> +
> +# This test can only be run on targets which support DWARF-2 and use gas.

Since DW_AT_main_subprogram was added to DWARF 4, should this say 
DWARF-4 instead?

> +if {![dwarf2_support]} {
> +    return 0
> +}
> +
> +standard_testfile main-subprogram.c enum-type-dw.S
> +
> +# Make some DWARF for the test.
> +set asm_file [standard_output_file $srcfile2]
> +Dwarf::assemble $asm_file {
> +    global srcdir subdir srcfile
> +
> +    set range [function_range mymain ${srcdir}/${subdir}/${srcfile}]
> +    set mymain_start [lindex $range 0]
> +    set mymain_length [lindex $range 1]
> +
> +    cu {} {
> +	DW_TAG_compile_unit {
> +                {DW_AT_language @DW_LANG_C}
> +                {DW_AT_name     main-subprogram.c}
> +                {DW_AT_comp_dir /tmp}
> +        } {
> +	    declare_labels type
> +
> +            type: DW_TAG_base_type {
> +                {DW_AT_byte_size 4 DW_FORM_sdata}
> +                {DW_AT_encoding  @DW_ATE_signed}
> +                {DW_AT_name      int}
> +            }
> +
> +	    DW_TAG_subprogram {
> +		{name mymain}
> +		{low_pc $mymain_start addr}
> +                {high_pc "$mymain_start + $mymain_length" addr}
> +		{type :$type}
> +		{DW_AT_main_subprogram 1 flag}
> +	    } {
> +	    }
> +	}
> +    }
> +}
> +
> +if {[prepare_for_testing ${testfile}.exp ${testfile} \
> +	 [list $srcfile $asm_file] {nodebug}]} {
> +    return -1
> +}
> +
> +# Test that the "start" command stops in the "mymain" function.
> +# This should happen because we used DW_AT_main_subprogram to tell gdb
> +# that this was the real "main".
> +
> +if {[gdb_start_cmd] < 0} {
> +    untested ${testfile}

untested "Failed to stop at mymain"?

> +    return -1
> +}
> +
> +gdb_test "" "Temporary breakpoint .* mymain.*" "start"

Similarly:

gdb_test "" "Temporary breakpoint .* mymain.*" "Stopped at mymain"

Otherwise looks good to me.

Thanks,
Luis
  
Tom Tromey Nov. 18, 2016, 2:48 p.m. UTC | #2
>>>>> "Luis" == Luis Machado <lgustavo@codesourcery.com> writes:

>> +# This test can only be run on targets which support DWARF-2 and use gas.

Luis> Since DW_AT_main_subprogram was added to DWARF 4, should this say
Luis> DWARF-4 instead?

I changed it.  That comment is just copied from various other tests in
that directory.

>> +if {[gdb_start_cmd] < 0} {
>> +    untested ${testfile}

Luis> untested "Failed to stop at mymain"?

I used "Could not start ${testfile}".

Luis> Similarly:
Luis> gdb_test "" "Temporary breakpoint .* mymain.*" "Stopped at mymain"
Luis> Otherwise looks good to me.

I changed this.

Tom
  
Tom Tromey Dec. 1, 2016, 8:56 p.m. UTC | #3
>>>>> "Tom" == Tom Tromey <tom@tromey.com> writes:

Ping:

Tom> 2016-11-14  Tom Tromey  <tom@tromey.com>
Tom> 	PR symtab/16264:
Tom> 	* dwarf2read.c (struct partial_die_info) <main_subprogram>: New
Tom> 	member.
Tom> 	(add_partial_symbol): Call set_objfile_main_name.
Tom> 	(read_partial_die): Handle DW_AT_main_subprogram.
Tom> 	<DW_AT_calling_convention>: don't call set_objfile_main_name, but
Tom> 	set main_subprogram flag.

Tom> 2016-11-14  Tom Tromey  <tom@tromey.com>
Tom> 	* gdb.dwarf2/main-subprogram.c: New file.
Tom> 	* gdb.dwarf2/main-subprogram.exp: New file.

thanks,
Tom
  
Pedro Alves Dec. 2, 2016, 11:38 a.m. UTC | #4
On 11/18/2016 02:48 PM, Tom Tromey wrote:

>>> +if {[gdb_start_cmd] < 0} {
>>> +    untested ${testfile}
> 
> Luis> untested "Failed to stop at mymain"?
> 
> I used "Could not start ${testfile}".
> 
> Luis> Similarly:
> Luis> gdb_test "" "Temporary breakpoint .* mymain.*" "Stopped at mymain"
> Luis> Otherwise looks good to me.
> 
> I changed this.

Please use lowercase "could" and "stopped", though.

OK with that change.

Thanks,
Pedro Alves
  

Patch

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index b4398f6..fafeea9 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,13 @@ 
+2016-11-14  Tom Tromey  <tom@tromey.com>
+
+	PR symtab/16264:
+	* dwarf2read.c (struct partial_die_info) <main_subprogram>: New
+	member.
+	(add_partial_symbol): Call set_objfile_main_name.
+	(read_partial_die): Handle DW_AT_main_subprogram.
+	<DW_AT_calling_convention>: don't call set_objfile_main_name, but
+	set main_subprogram flag.
+
 2016-11-12  Tom Tromey  <tom@tromey.com>
 
 	* rust-exp.y (super_name): Use std::vector.
diff --git a/gdb/dwarf2read.c b/gdb/dwarf2read.c
index 1ad6b00..ced7f72 100644
--- a/gdb/dwarf2read.c
+++ b/gdb/dwarf2read.c
@@ -1105,6 +1105,9 @@  struct partial_die_info
     unsigned int has_pc_info : 1;
     unsigned int may_be_inlined : 1;
 
+    /* This DIE has been marked DW_AT_main_subprogram.  */
+    unsigned int main_subprogram : 1;
+
     /* Flag set if the SCOPE field of this structure has been
        computed.  */
     unsigned int scope_set : 1;
@@ -6930,6 +6933,9 @@  add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
 			       &objfile->static_psymbols,
 			       addr, cu->language, objfile);
 	}
+
+      if (pdi->main_subprogram && actual_name != NULL)
+	set_objfile_main_name (objfile, actual_name, cu->language);
       break;
     case DW_TAG_constant:
       {
@@ -15945,19 +15951,18 @@  read_partial_die (const struct die_reader_specs *reader,
 	     to describe functions' calling conventions.
 
 	     However, because it's a necessary piece of information in
-	     Fortran, and because DW_CC_program is the only piece of debugging
-	     information whose definition refers to a 'main program' at all,
-	     several compilers have begun marking Fortran main programs with
-	     DW_CC_program --- even when those functions use the standard
-	     calling conventions.
-
-	     So until DWARF specifies a way to provide this information and
-	     compilers pick up the new representation, we'll support this
-	     practice.  */
+	     Fortran, and before DWARF 4 DW_CC_program was the only
+	     piece of debugging information whose definition refers to
+	     a 'main program' at all, several compilers marked Fortran
+	     main programs with DW_CC_program --- even when those
+	     functions use the standard calling conventions.
+
+	     Although DWARF now specifies a way to provide this
+	     information, we support this practice for backward
+	     compatibility.  */
 	  if (DW_UNSND (&attr) == DW_CC_program
-	      && cu->language == language_fortran
-	      && part_die->name != NULL)
-	    set_objfile_main_name (objfile, part_die->name, language_fortran);
+	      && cu->language == language_fortran)
+	    part_die->main_subprogram = 1;
 	  break;
 	case DW_AT_inline:
 	  if (DW_UNSND (&attr) == DW_INL_inlined
@@ -15974,6 +15979,10 @@  read_partial_die (const struct die_reader_specs *reader,
 	    }
 	  break;
 
+	case DW_AT_main_subprogram:
+	  part_die->main_subprogram = DW_UNSND (&attr);
+	  break;
+
 	default:
 	  break;
 	}
diff --git a/gdb/testsuite/ChangeLog b/gdb/testsuite/ChangeLog
index 054629f..d971347 100644
--- a/gdb/testsuite/ChangeLog
+++ b/gdb/testsuite/ChangeLog
@@ -1,3 +1,8 @@ 
+2016-11-14  Tom Tromey  <tom@tromey.com>
+
+	* gdb.dwarf2/main-subprogram.c: New file.
+	* gdb.dwarf2/main-subprogram.exp: New file.
+
 2016-11-11  Catherine Moore  <clm@codesourcery.com>
 
 	* gdb.base/dump.exp: Identify verilog format dump tests as such.
diff --git a/gdb/testsuite/gdb.dwarf2/main-subprogram.c b/gdb/testsuite/gdb.dwarf2/main-subprogram.c
new file mode 100644
index 0000000..97ca22e
--- /dev/null
+++ b/gdb/testsuite/gdb.dwarf2/main-subprogram.c
@@ -0,0 +1,29 @@ 
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright 2016 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+int
+mymain ()
+{
+  asm ("mymain_label: .globl mymain_label");
+  return 0;
+}
+
+int
+main()
+{
+  return mymain ();
+}
diff --git a/gdb/testsuite/gdb.dwarf2/main-subprogram.exp b/gdb/testsuite/gdb.dwarf2/main-subprogram.exp
new file mode 100644
index 0000000..8adb801
--- /dev/null
+++ b/gdb/testsuite/gdb.dwarf2/main-subprogram.exp
@@ -0,0 +1,73 @@ 
+# Copyright 2016 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+load_lib dwarf.exp
+
+# This test can only be run on targets which support DWARF-2 and use gas.
+if {![dwarf2_support]} {
+    return 0
+}
+
+standard_testfile main-subprogram.c enum-type-dw.S
+
+# Make some DWARF for the test.
+set asm_file [standard_output_file $srcfile2]
+Dwarf::assemble $asm_file {
+    global srcdir subdir srcfile
+
+    set range [function_range mymain ${srcdir}/${subdir}/${srcfile}]
+    set mymain_start [lindex $range 0]
+    set mymain_length [lindex $range 1]
+
+    cu {} {
+	DW_TAG_compile_unit {
+                {DW_AT_language @DW_LANG_C}
+                {DW_AT_name     main-subprogram.c}
+                {DW_AT_comp_dir /tmp}
+        } {
+	    declare_labels type
+
+            type: DW_TAG_base_type {
+                {DW_AT_byte_size 4 DW_FORM_sdata}
+                {DW_AT_encoding  @DW_ATE_signed}
+                {DW_AT_name      int}
+            }
+
+	    DW_TAG_subprogram {
+		{name mymain}
+		{low_pc $mymain_start addr}
+                {high_pc "$mymain_start + $mymain_length" addr}
+		{type :$type}
+		{DW_AT_main_subprogram 1 flag}
+	    } {
+	    }
+	}
+    }
+}
+
+if {[prepare_for_testing ${testfile}.exp ${testfile} \
+	 [list $srcfile $asm_file] {nodebug}]} {
+    return -1
+}
+
+# Test that the "start" command stops in the "mymain" function.
+# This should happen because we used DW_AT_main_subprogram to tell gdb
+# that this was the real "main".
+
+if {[gdb_start_cmd] < 0} {
+    untested ${testfile}
+    return -1
+}
+
+gdb_test "" "Temporary breakpoint .* mymain.*" "start"