Re: [PATCH,RFC] Make libbacktrace more standalone

2013-05-21 Thread Ian Lance Taylor
On Tue, May 21, 2013 at 9:52 AM, Alexander Monakov  wrote:
>
> 1. The documentation does not say whether reusing "state" objects is allowed.
> For instance, I'm using backtrace_simple, and then in the callback I'm
> invoking dladdr and backtrace_pcinfo to gather file-line info if available.
> Looks like at the moment it's fine, as no frame-specific information is stored
> there.

Yes, you can reuse backtrace_state objects.

> 2. The reason I'm invoking dladdr instead of backtrace_syminfo is the need to
> obtain the name of the module containing the symbol.  Is that intended?
> (IOW, would be nice if libbacktrace syminfo callback provided name and base
> address of the loaded module as well)

I'm fine with changing libbacktrace_syminfo so that it passes the
module name and the base address to the callback.

> 3. In libbacktrace/elf.c, elf_syminfo() does not seem to walk the chain of
> elf_syminfo_data (i.e. does not use edata->next).  Is that a bug?

Hmmm, yes, that does look like a bug.  Whoops.

Ian


Re: [PATCH,RFC] Make libbacktrace more standalone

2013-05-21 Thread Alexander Monakov
On Mon, 20 May 2013, Ian Lance Taylor wrote:

> To be clear: are you withdrawing your earlier patch?

Yes; at this point I don't think it improves anything sufficiently to spend
more time on it.

Forgive me a digression, but having now actually used libbacktrace, I have
the following questions.

1. The documentation does not say whether reusing "state" objects is allowed.
For instance, I'm using backtrace_simple, and then in the callback I'm
invoking dladdr and backtrace_pcinfo to gather file-line info if available.
Looks like at the moment it's fine, as no frame-specific information is stored
there.

2. The reason I'm invoking dladdr instead of backtrace_syminfo is the need to
obtain the name of the module containing the symbol.  Is that intended?
(IOW, would be nice if libbacktrace syminfo callback provided name and base
address of the loaded module as well)

3. In libbacktrace/elf.c, elf_syminfo() does not seem to walk the chain of
elf_syminfo_data (i.e. does not use edata->next).  Is that a bug?

Thanks.

Alexander


Re: [PATCH,RFC] Make libbacktrace more standalone

2013-05-20 Thread Ian Lance Taylor
On Mon, May 20, 2013 at 10:07 AM, Alexander Monakov  wrote:
> I now realize that a less intrusive approach is available: just provide
> replacement filenames.h, dwarf2.h in a separate directory and pick up those
> headers for a standalone build.  In replacement dwarf2.h, include system
> dwarf.h, #define potentially missing enum values and provide dummy enum tags.
> The downside is that GCC complains about using variables of dummy enum types
> in comparisons and switch statements, necessitating -Wno-switch and
> -Wno-enum-compare.
>
> Build system dependencies are a bigger problem that is beyond "scratching my
> own itch" at the moment (I have to provide a CMake build script anyhow).
>
> The below is what I submitted to import libbacktrace in the APITrace project.
> I'm not sure whether it makes sense to commit it in GCC: it's not particularly
> useful without the standalone CMake build script.

To be clear: are you withdrawing your earlier patch?

I agree that your proposal seems like a reasonable approach, and I
agree that it doesn't need to be in GCC.  It could be in a standalone
copy of libbacktrace if anybody creates one.

Ian


Re: [PATCH,RFC] Make libbacktrace more standalone

2013-05-20 Thread Alexander Monakov
I now realize that a less intrusive approach is available: just provide
replacement filenames.h, dwarf2.h in a separate directory and pick up those
headers for a standalone build.  In replacement dwarf2.h, include system
dwarf.h, #define potentially missing enum values and provide dummy enum tags.
The downside is that GCC complains about using variables of dummy enum types
in comparisons and switch statements, necessitating -Wno-switch and
-Wno-enum-compare.

Build system dependencies are a bigger problem that is beyond "scratching my
own itch" at the moment (I have to provide a CMake build script anyhow).

The below is what I submitted to import libbacktrace in the APITrace project.
I'm not sure whether it makes sense to commit it in GCC: it's not particularly
useful without the standalone CMake build script.

Alexander

diff --git a/libbacktrace/auxincl/dwarf2.h b/libbacktrace/auxincl/dwarf2.h
new file mode 100644
index 000..d135d8a
--- /dev/null
+++ b/libbacktrace/auxincl/dwarf2.h
@@ -0,0 +1,52 @@
+/* dwarf2.h -- minimal GCC dwarf2.h replacement for libbacktrace
+   Contributed by Alexander Monakov, ISP RAS
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are
+met:
+
+(1) Redistributions of source code must retain the above copyright
+notice, this list of conditions and the following disclaimer.
+
+(2) Redistributions in binary form must reproduce the above copyright
+notice, this list of conditions and the following disclaimer in
+the documentation and/or other materials provided with the
+distribution.
+
+(3) The name of the author may not be used to
+endorse or promote products derived from this software without
+specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
+INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
+IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.  */
+
+#ifndef BACKTRACE_AUX_DWARF2_H
+#define BACKTRACE_AUX_DWARF2_H
+
+/* Use the system header for the bulk of the definitions.  */
+#include 
+
+/* Provide stub enum tags.  */
+enum dwarf_attribute {_dummy_dwarf_attribute};
+enum dwarf_form {_dummy_dwarf_form};
+enum dwarf_tag {_dummy_dwarf_tag};
+
+/* Define potentially missing enum values.  */
+#define DW_FORM_GNU_addr_index 0x1f01
+#define DW_FORM_GNU_str_index  0x1f02
+
+#define DW_FORM_GNU_ref_alt0x1f20
+#define DW_FORM_GNU_strp_alt   0x1f21
+
+#define DW_LNS_extended_op 0
+
+#endif
diff --git a/libbacktrace/auxincl/filenames.h b/libbacktrace/auxincl/filenames.h
new file mode 100644
index 000..000dd30
--- /dev/null
+++ b/libbacktrace/auxincl/filenames.h
@@ -0,0 +1,40 @@
+/* filenames.h -- minimal GCC filenames.h replacement for libbacktrace
+   Contributed by Alexander Monakov, ISP RAS
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are
+met:
+
+(1) Redistributions of source code must retain the above copyright
+notice, this list of conditions and the following disclaimer.
+
+(2) Redistributions in binary form must reproduce the above copyright
+notice, this list of conditions and the following disclaimer in
+the documentation and/or other materials provided with the
+distribution.
+
+(3) The name of the author may not be used to
+endorse or promote products derived from this software without
+specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
+INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
+IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.  */
+
+#ifndef BACKTRACE_AUX_FILENAMES_H
+#define BACKTRACE_AUX_FILENAMES_H
+
+/* Assume POSIX paths.  */
+
+#define IS_DIR_SEPARATOR(c) ((c) == '/')
+#define IS_ABSOLUTE_PATH(f) ((f)[0] == '/')
+
+#endif


Re: [PATCH,RFC] Make libbacktrace more standalone

2013-05-14 Thread Alexander Monakov
> > In the case where IN_GCC is defined, where are the types
> > dwarf_attribute, dwarf_form, and dwarf_tag defined?
> 
> In GCC's own dwarf2.h as enum tags; dwarf.h uses anonymous enums.

Ah, I still should have typedef'ed those types to enum tags when IN_GCC.  I've
verified the following patch bootstraps and passes testing.

libbacktrace/Changelog:
2013-05-14  Alexander Monakov  

* btest.c: [!IN_GCC] (IS_DIR_SEPARATOR): Define.
* configure.ac: (standalone): New configuration flag.
(EXTRA_FLAGS): Add -DIN_GCC.
(HAVE_DWARF2_FISSION, HAVE_DWARF2_DWZ_MULTIFILE): New
tests.  Use ...
* dwarf.c: (read_attribute): ... here.
[IN_GCC] (dwarf_attribute, dwarf_form, dwarf_tag): Typedef to
corresponding enums from dwarf2.h.  Update all uses.
[!IN_GCC] Use system dwarf.h. 
[!IN_GCC] (dwarf_attribute, dwarf_form, dwarf_tag): Typedef to int.
[!IN_GCC] (IS_ABSOLUTE_PATH): Define.
(read_line_program): Avoid use of DW_LNS_extended_op.
* configure: Regenerate.
* config.h.in: Regenerate.
* Makefile.in: Regenerate.


diff --git a/libbacktrace/btest.c b/libbacktrace/btest.c
index cc647b8..1516099 100644
--- a/libbacktrace/btest.c
+++ b/libbacktrace/btest.c
@@ -38,7 +38,11 @@ POSSIBILITY OF SUCH DAMAGE.  */
 #include 
 #include 
 
+#ifdef IN_GCC
 #include "filenames.h"
+#else
+#define IS_DIR_SEPARATOR(c) ((c) == '/')
+#endif
 
 #include "backtrace.h"
 #include "backtrace-supported.h"
diff --git a/libbacktrace/configure.ac b/libbacktrace/configure.ac
index 28b2a1c..ae23da4 100644
--- a/libbacktrace/configure.ac
+++ b/libbacktrace/configure.ac
@@ -58,6 +58,9 @@ AM_MAINTAINER_MODE
 AC_ARG_WITH(target-subdir,
 [  --with-target-subdir=SUBDIR  Configuring in a subdirectory for target])
 
+AC_ARG_ENABLE(standalone,
+[  --enable-standalone Do not use internal GCC headers])
+
 # We must force CC to /not/ be precious variables; otherwise
 # the wrong, non-multilib-adjusted value will be used in multilibs.
 # As a side effect, we have to subst CFLAGS ourselves.
@@ -72,7 +75,7 @@ AC_PROG_RANLIB
 
 AC_PROG_AWK
 case "$AWK" in
-"") AC_MSG_ERROR([can't build without awk]) ;;
+"") AC_MSG_ERROR([cannot build without awk]) ;;
 esac
 
 LT_INIT([disable-shared])
@@ -125,6 +128,10 @@ else
 EXTRA_FLAGS="$EXTRA_FLAGS -frandom-seed=\$@"
   fi
 fi
+
+if test "${enable_standalone}" != "yes"; then
+  EXTRA_FLAGS="$EXTRA_FLAGS -DIN_GCC"
+fi
 AC_SUBST(EXTRA_FLAGS)
 
 ACX_PROG_CC_WARNING_OPTS([-W -Wall -Wwrite-strings -Wstrict-prototypes \
@@ -314,6 +321,40 @@ if test "$have_getexecname" = "yes"; then
   AC_DEFINE(HAVE_GETEXECNAME, 1, [Define if getexecname is available.])
 fi
 
+# Check for DWARF2 extensions
+if test "${enable_standalone}" != "yes"; then
+  have_dwarf2_fission=yes
+  have_dwarf2_dwz_multifile=yes
+else
+  AC_CHECK_HEADER([dwarf.h],
+[
+  AC_MSG_CHECKING([for DW_FORM_GNU_addr_index])
+  AC_COMPILE_IFELSE(
+   [AC_LANG_PROGRAM(
+ [#include ],
+ [int i = DW_FORM_GNU_addr_index;])],
+   [have_dwarf2_fission=yes],
+   [have_dwarf2_fission=no])
+  AC_MSG_RESULT([$have_dwarf2_fission])
+  AC_MSG_CHECKING([for DW_FORM_GNU_ref_alt])
+  AC_COMPILE_IFELSE(
+   [AC_LANG_PROGRAM(
+ [#include ],
+ [int i = DW_FORM_GNU_ref_alt;])],
+   [have_dwarf2_dwz_multifile=yes],
+   [have_dwarf2_dwz_multifile=no])
+  AC_MSG_RESULT([$have_dwarf2_dwz_multifile])],
+[AC_MSG_ERROR([dwarf.h required when building standalone])])
+fi
+if test "$have_dwarf2_fission" = "yes"; then
+  AC_DEFINE(HAVE_DWARF2_FISSION, 1,
+   [Define if DWARF2 Fission enumeration values are defined.])
+fi
+if test "$have_dwarf2_dwz_multifile" = "yes"; then
+  AC_DEFINE(HAVE_DWARF2_DWZ_MULTFILE, 1,
+   [Define if DWARF2 DWZ multifile enumeration values are defined.])
+fi
+
 AC_CACHE_CHECK([whether tests can run],
   [libbacktrace_cv_sys_native],
   [AC_RUN_IFELSE([AC_LANG_PROGRAM([], [return 0;])],
diff --git a/libbacktrace/dwarf.c b/libbacktrace/dwarf.c
index 501afe5..6170ca0 100644
--- a/libbacktrace/dwarf.c
+++ b/libbacktrace/dwarf.c
@@ -37,9 +37,22 @@ POSSIBILITY OF SUCH DAMAGE.  */
 #include 
 #include 
 
+#ifdef IN_GCC
 #include "dwarf2.h"
 #include "filenames.h"
 
+typedef enum dwarf_attribute dwarf_attribute;
+typedef enum dwarf_form dwarf_form;
+typedef enum dwarf_tag dwarf_tag;
+#else
+#include 
+#define IS_ABSOLUTE_PATH(f) ((f)[0] == '/')
+
+typedef int dwarf_attribute;
+typedef int dwarf_form;
+typedef int dwarf_tag;
+#endif
+
 #include "backtrace.h"
 #include "internal.h"
 
@@ -89,9 +102,9 @@ struct dwarf_buf
 struct attr
 {
   /* The attribute name.  */
-  enum dwarf_attribute name;
+  dwarf_attribute name;
   /* The attribute form.  */
-  enum dwarf_form form;
+  dwarf_form form;
 };
 
 /* A single DWARF abbreviation.  */
@@ -101,7 +114,7 @@ struct abbrev
   /* The abbrev code--the number used to refer to the abbrev.  */
   uint64_t code;
   /* The 

Re: [PATCH,RFC] Make libbacktrace more standalone

2013-05-13 Thread Alexander Monakov
On Tue, May 14, 2013 at 12:45 AM, Tom Tromey  wrote:
>> "Alexander" == Alexander Monakov  writes:
>
> Alexander> In GCC's own dwarf2.h as enum tags; dwarf.h uses anonymous enums.
>
> I'm curious to know which dwarf2.h you are testing against and/or intend
> to support.  I think there is more than one.

Either dwarf.h from libdwarf (the "official" one if I understand
correctly), or dwarf.h provided by elfutils or libdw. Right now I'm
testing against /usr/include/dwarf.h provided by elfutils package on
Gentoo.

As far as I know, those are very similar except for presence of some
enum values, hence the configure checks.

Alexander


Re: [PATCH,RFC] Make libbacktrace more standalone

2013-05-13 Thread Tom Tromey
> "Alexander" == Alexander Monakov  writes:

Alexander> In GCC's own dwarf2.h as enum tags; dwarf.h uses anonymous enums.

I'm curious to know which dwarf2.h you are testing against and/or intend
to support.  I think there is more than one.

Tom


Re: [PATCH,RFC] Make libbacktrace more standalone

2013-05-13 Thread Alexander Monakov
On Wed, 8 May 2013, Ian Lance Taylor wrote:
> > +#ifdef IN_GCC
> 
> Where is IN_GCC defined?

I've amended configure.ac to provide a new configuration flag
('--enable-standalone') and add -DIN_GCC to EXTRA_FLAGS unless that flag is
given.  Previously I've misread grep output and thought that IN_GCC is defined
globally.

> This isn't right.  Using test -n "${with_target_subdir}" tests whether
> libbacktrace is being built as a target library, using the newly built
> compiler.  It does not test whether it is being used in a standalone
> build.  with_target_subdir will be empty when building libbacktrace as
> part of the host compiler.  In that case we still want to use
> include/dwarf2.def, and we do not want to give an error if the system
> does not have dwarf.h.

Sorry.  I'm amended that sequence to test $enable_standalone instead.

> > +#include "backtrace.h"
> > +#include "internal.h"
> 
> Please keep these after the #include of the other header files.

Done.

> > +#ifdef IN_GCC
> >  #include "dwarf2.h"
> >  #include "filenames.h"
> > +#else
> > +#include 
> > +typedef int dwarf_attribute;
> > +typedef int dwarf_form;
> > +typedef int dwarf_tag;
> >
> > -#include "backtrace.h"
> > -#include "internal.h"
> > +#define IS_ABSOLUTE_PATH(f) ((f)[0] == '/')
> > +#endif
> 
> In the case where IN_GCC is defined, where are the types
> dwarf_attribute, dwarf_form, and dwarf_tag defined?

In GCC's own dwarf2.h as enum tags; dwarf.h uses anonymous enums.

> When IN_GCC is defined, something needs to ensure that
> HAVE_DWARF2_FISSION and HAVE_DWARF2_DWZ_MULTIFILE are defined.

It is ensured by defining have_dwarf2_* unconditionally to "yes" in that
case.

Updated patch below.  Thanks!

libbacktrace/Changelog:
2013-05-13  Alexander Monakov  

* btest.c: [!IN_GCC] (IS_DIR_SEPARATOR): Define.
* configure.ac: (standalone): New configuration flag.
(EXTRA_FLAGS): Add -DIN_GCC unless building standalone.
(HAVE_DWARF2_FISSION, HAVE_DWARF2_DWZ_MULTIFILE): New
tests.  Use ...
* dwarf.c: (read_attribute): ... here.
[!IN_GCC] Use system dwarf.h. 
[!IN_GCC] (dwarf_attribute, dwarf_form, dwarf_tag): Typedef to int.
Update all uses.
[!IN_GCC] (IS_ABSOLUTE_PATH): Define.
(read_line_program): Avoid use of DW_LNS_extended_op.
* configure: Regenerate.
* config.h.in: Regenerate.
* Makefile.in: Regenerate.


diff --git a/libbacktrace/btest.c b/libbacktrace/btest.c
index cc647b8..1516099 100644
--- a/libbacktrace/btest.c
+++ b/libbacktrace/btest.c
@@ -38,7 +38,11 @@ POSSIBILITY OF SUCH DAMAGE.  */
 #include 
 #include 
 
+#ifdef IN_GCC
 #include "filenames.h"
+#else
+#define IS_DIR_SEPARATOR(c) ((c) == '/')
+#endif
 
 #include "backtrace.h"
 #include "backtrace-supported.h"
diff --git a/libbacktrace/configure.ac b/libbacktrace/configure.ac
index 28b2a1c..ae23da4 100644
--- a/libbacktrace/configure.ac
+++ b/libbacktrace/configure.ac
@@ -58,6 +58,9 @@ AM_MAINTAINER_MODE
 AC_ARG_WITH(target-subdir,
 [  --with-target-subdir=SUBDIR  Configuring in a subdirectory for target])
 
+AC_ARG_ENABLE(standalone,
+[  --enable-standalone Do not use internal GCC headers])
+
 # We must force CC to /not/ be precious variables; otherwise
 # the wrong, non-multilib-adjusted value will be used in multilibs.
 # As a side effect, we have to subst CFLAGS ourselves.
@@ -72,7 +75,7 @@ AC_PROG_RANLIB
 
 AC_PROG_AWK
 case "$AWK" in
-"") AC_MSG_ERROR([can't build without awk]) ;;
+"") AC_MSG_ERROR([cannot build without awk]) ;;
 esac
 
 LT_INIT([disable-shared])
@@ -125,6 +128,10 @@ else
 EXTRA_FLAGS="$EXTRA_FLAGS -frandom-seed=\$@"
   fi
 fi
+
+if test "${enable_standalone}" != "yes"; then
+  EXTRA_FLAGS="$EXTRA_FLAGS -DIN_GCC"
+fi
 AC_SUBST(EXTRA_FLAGS)
 
 ACX_PROG_CC_WARNING_OPTS([-W -Wall -Wwrite-strings -Wstrict-prototypes \
@@ -314,6 +321,40 @@ if test "$have_getexecname" = "yes"; then
   AC_DEFINE(HAVE_GETEXECNAME, 1, [Define if getexecname is available.])
 fi
 
+# Check for DWARF2 extensions
+if test "${enable_standalone}" != "yes"; then
+  have_dwarf2_fission=yes
+  have_dwarf2_dwz_multifile=yes
+else
+  AC_CHECK_HEADER([dwarf.h],
+[
+  AC_MSG_CHECKING([for DW_FORM_GNU_addr_index])
+  AC_COMPILE_IFELSE(
+   [AC_LANG_PROGRAM(
+ [#include ],
+ [int i = DW_FORM_GNU_addr_index;])],
+   [have_dwarf2_fission=yes],
+   [have_dwarf2_fission=no])
+  AC_MSG_RESULT([$have_dwarf2_fission])
+  AC_MSG_CHECKING([for DW_FORM_GNU_ref_alt])
+  AC_COMPILE_IFELSE(
+   [AC_LANG_PROGRAM(
+ [#include ],
+ [int i = DW_FORM_GNU_ref_alt;])],
+   [have_dwarf2_dwz_multifile=yes],
+   [have_dwarf2_dwz_multifile=no])
+  AC_MSG_RESULT([$have_dwarf2_dwz_multifile])],
+[AC_MSG_ERROR([dwarf.h required when building standalone])])
+fi
+if test "$have_dwarf2_fission" = "yes"; then
+  AC_DEFINE(HAVE_DWARF2_FISSION, 1,
+   [Define if DWARF2 Fission enumeration values are defined.]

Re: [PATCH,RFC] Make libbacktrace more standalone

2013-05-08 Thread Ian Lance Taylor
On Wed, May 8, 2013 at 2:35 PM, Ondřej Bílka  wrote:
>
> Is it possible to link staticaly without external dependencies? This
> simplifies generating backtrace when inside linker.

I'm not sure I understand the question.  Is it possible to link
libbacktrace statically?  Sure.  That is what happens by default.

Ian


Re: [PATCH,RFC] Make libbacktrace more standalone

2013-05-08 Thread Ondřej Bílka
On Wed, May 08, 2013 at 07:56:18PM +0400, Alexander Monakov wrote:
> Hello,
> 
> I'd like to make libbacktrace easier to import for use in other software.
> Right now it's mostly standalone, but depends on rest of GCC in the following:
> 
> 1. Build system.
> 2. Trivially depends on "filenames.h".
> 3. Depends on "dwarf2.{def,h}" to obtain DW_* enumeration values.
> 
> My autotools-foo is not strong enough to elegantly resolve build system
> dependencies (and for my intended use in APITrace I will need to write a
> CMake-based build recipe anyhow).  To remove dependency on GCC dwarf2.h, I'm
> proposing to enable building with system dwarf.h header.  However, in that
> header the enums are anonymous, and some enum values may be missing.  My
> preference is that references to named enums are retained, so I'm typedef'ing
> those to 'int' if unavailable.
> 
> To deal with potentially missing enum values, I'm adding configure checks and
> wrapping uses in HAVE_foo.  I'm also removing one use of DW_LNS_extended_op:
> the standard does not seem to define that name, but instead says that value 0
> is treated specially.
> 
> The following patch is the best compromise I have found between adding ugly
> fixups and making libbacktrace buildable standalone (with the caveat that
> either a different build system is used, or autotools files are hacked to
> remove GCC dependencies).
> 
> Comments?

Is it possible to link staticaly without external dependencies? This
simplifies generating backtrace when inside linker.


Re: [PATCH,RFC] Make libbacktrace more standalone

2013-05-08 Thread Ian Lance Taylor
On Wed, May 8, 2013 at 8:56 AM, Alexander Monakov  wrote:
>
> I'd like to make libbacktrace easier to import for use in other software.

> Comments?

> +#ifdef IN_GCC

Where is IN_GCC defined?


> +# Test if DWARF2 extensions enumeration values are declared
> +if test -n "${with_target_subdir}"; then
> +  have_dwarf2_fission=yes
> +  have_dwarf2_dwz_multifile=yes
> +else
> +  AC_CHECK_HEADER([dwarf.h],
> +[
> +  AC_MSG_CHECKING([for DW_FORM_GNU_addr_index])
> +  AC_COMPILE_IFELSE(
> +   [AC_LANG_PROGRAM(
> + [#include ],
> + [int i = DW_FORM_GNU_addr_index;])],
> +   [have_dwarf2_fission=yes],
> +   [have_dwarf2_fission=no])
> +  AC_MSG_RESULT([$have_dwarf2_fission])
> +  AC_MSG_CHECKING([for DW_FORM_GNU_ref_alt])
> +  AC_COMPILE_IFELSE(
> +   [AC_LANG_PROGRAM(
> + [#include ],
> + [int i = DW_FORM_GNU_ref_alt;])],
> +   [have_dwarf2_dwz_multifile=yes],
> +   [have_dwarf2_dwz_multifile=no])
> +  AC_MSG_RESULT([$have_dwarf2_dwz_multifile])],
> +[AC_MSG_ERROR([dwarf.h required for standalone build])])
> +fi

This isn't right.  Using test -n "${with_target_subdir}" tests whether
libbacktrace is being built as a target library, using the newly built
compiler.  It does not test whether it is being used in a standalone
build.  with_target_subdir will be empty when building libbacktrace as
part of the host compiler.  In that case we still want to use
include/dwarf2.def, and we do not want to give an error if the system
does not have dwarf.h.


> +#include "backtrace.h"
> +#include "internal.h"

Please keep these after the #include of the other header files.


> +#ifdef IN_GCC
>  #include "dwarf2.h"
>  #include "filenames.h"
> +#else
> +#include 
> +typedef int dwarf_attribute;
> +typedef int dwarf_form;
> +typedef int dwarf_tag;
>
> -#include "backtrace.h"
> -#include "internal.h"
> +#define IS_ABSOLUTE_PATH(f) ((f)[0] == '/')
> +#endif

In the case where IN_GCC is defined, where are the types
dwarf_attribute, dwarf_form, and dwarf_tag defined?

When IN_GCC is defined, something needs to ensure that
HAVE_DWARF2_FISSION and HAVE_DWARF2_DWZ_MULTIFILE are defined.

Ian


[PATCH,RFC] Make libbacktrace more standalone

2013-05-08 Thread Alexander Monakov
Hello,

I'd like to make libbacktrace easier to import for use in other software.
Right now it's mostly standalone, but depends on rest of GCC in the following:

1. Build system.
2. Trivially depends on "filenames.h".
3. Depends on "dwarf2.{def,h}" to obtain DW_* enumeration values.

My autotools-foo is not strong enough to elegantly resolve build system
dependencies (and for my intended use in APITrace I will need to write a
CMake-based build recipe anyhow).  To remove dependency on GCC dwarf2.h, I'm
proposing to enable building with system dwarf.h header.  However, in that
header the enums are anonymous, and some enum values may be missing.  My
preference is that references to named enums are retained, so I'm typedef'ing
those to 'int' if unavailable.

To deal with potentially missing enum values, I'm adding configure checks and
wrapping uses in HAVE_foo.  I'm also removing one use of DW_LNS_extended_op:
the standard does not seem to define that name, but instead says that value 0
is treated specially.

The following patch is the best compromise I have found between adding ugly
fixups and making libbacktrace buildable standalone (with the caveat that
either a different build system is used, or autotools files are hacked to
remove GCC dependencies).

Comments?

libbacktrace/Changelog:
2013-05-08  Alexander Monakov  

* btest.c: [!IN_GCC] (IS_DIR_SEPARATOR): Define.
* configure.ac: (HAVE_DWARF2_FISSION, HAVE_DWARF2_DWZ_MULTIFILE): New
tests.  Use ...
* dwarf.c: (read_attribute): ... here.
[!IN_GCC] Use system dwarf.h. 
[!IN_GCC] (dwarf_attribute, dwarf_form, dwarf_tag): Typedef to int.
Update all uses.
[!IN_GCC] (IS_ABSOLUTE_PATH): Define.
(read_line_program): Avoid use of DW_LNS_extended_op.
* configure: Regenerate.
* config.h.in: Regenerate.
* Makefile.in: Regenerate.


diff --git a/libbacktrace/btest.c b/libbacktrace/btest.c
index cc647b8..1516099 100644
--- a/libbacktrace/btest.c
+++ b/libbacktrace/btest.c
@@ -38,7 +38,11 @@ POSSIBILITY OF SUCH DAMAGE.  */
 #include 
 #include 
 
+#ifdef IN_GCC
 #include "filenames.h"
+#else
+#define IS_DIR_SEPARATOR(c) ((c) == '/')
+#endif
 
 #include "backtrace.h"
 #include "backtrace-supported.h"
diff --git a/libbacktrace/configure.ac b/libbacktrace/configure.ac
index 28b2a1c..2728d4d 100644
--- a/libbacktrace/configure.ac
+++ b/libbacktrace/configure.ac
@@ -72,7 +72,7 @@ AC_PROG_RANLIB
 
 AC_PROG_AWK
 case "$AWK" in
-"") AC_MSG_ERROR([can't build without awk]) ;;
+"") AC_MSG_ERROR([cannot build without awk]) ;;
 esac
 
 LT_INIT([disable-shared])
@@ -314,6 +314,40 @@ if test "$have_getexecname" = "yes"; then
   AC_DEFINE(HAVE_GETEXECNAME, 1, [Define if getexecname is available.])
 fi
 
+# Test if DWARF2 extensions enumeration values are declared
+if test -n "${with_target_subdir}"; then
+  have_dwarf2_fission=yes
+  have_dwarf2_dwz_multifile=yes
+else
+  AC_CHECK_HEADER([dwarf.h],
+[
+  AC_MSG_CHECKING([for DW_FORM_GNU_addr_index])
+  AC_COMPILE_IFELSE(
+   [AC_LANG_PROGRAM(
+ [#include ],
+ [int i = DW_FORM_GNU_addr_index;])],
+   [have_dwarf2_fission=yes],
+   [have_dwarf2_fission=no])
+  AC_MSG_RESULT([$have_dwarf2_fission])
+  AC_MSG_CHECKING([for DW_FORM_GNU_ref_alt])
+  AC_COMPILE_IFELSE(
+   [AC_LANG_PROGRAM(
+ [#include ],
+ [int i = DW_FORM_GNU_ref_alt;])],
+   [have_dwarf2_dwz_multifile=yes],
+   [have_dwarf2_dwz_multifile=no])
+  AC_MSG_RESULT([$have_dwarf2_dwz_multifile])],
+[AC_MSG_ERROR([dwarf.h required for standalone build])])
+fi
+if test "$have_dwarf2_fission" = "yes"; then
+  AC_DEFINE(HAVE_DWARF2_FISSION, 1,
+   [Define if DWARF2 Fission enumeration values are defined.])
+fi
+if test "$have_dwarf2_dwz_multifile" = "yes"; then
+  AC_DEFINE(HAVE_DWARF2_DWZ_MULTIFILE, 1,
+   [Define if DWARF2 DWZ multifile enumeration values are defined.])
+fi
+
 AC_CACHE_CHECK([whether tests can run],
   [libbacktrace_cv_sys_native],
   [AC_RUN_IFELSE([AC_LANG_PROGRAM([], [return 0;])],
diff --git a/libbacktrace/dwarf.c b/libbacktrace/dwarf.c
index 501afe5..49a57a3 100644
--- a/libbacktrace/dwarf.c
+++ b/libbacktrace/dwarf.c
@@ -37,11 +37,20 @@ POSSIBILITY OF SUCH DAMAGE.  */
 #include 
 #include 
 
+#include "backtrace.h"
+#include "internal.h"
+
+#ifdef IN_GCC
 #include "dwarf2.h"
 #include "filenames.h"
+#else
+#include 
+typedef int dwarf_attribute;
+typedef int dwarf_form;
+typedef int dwarf_tag;
 
-#include "backtrace.h"
-#include "internal.h"
+#define IS_ABSOLUTE_PATH(f) ((f)[0] == '/')
+#endif
 
 #if !defined(HAVE_DECL_STRNLEN) || !HAVE_DECL_STRNLEN
 
@@ -89,9 +98,9 @@ struct dwarf_buf
 struct attr
 {
   /* The attribute name.  */
-  enum dwarf_attribute name;
+  dwarf_attribute name;
   /* The attribute form.  */
-  enum dwarf_form form;
+  dwarf_form form;
 };
 
 /* A single DWARF abbreviation.  */
@@ -101,7 +110,7 @@ struct abbrev
   /* The abbrev code--the