Send connman mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        https://lists.01.org/mailman/listinfo/connman
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of connman digest..."


Today's Topics:

   1. Re: [PATCH] configure: check for execinfo.h (Yann E. MORIN)
   2. [PATCHv2] log: don't require backtrace() (Yann E. MORIN)


----------------------------------------------------------------------

Message: 1
Date: Fri, 1 Jan 2016 11:39:42 +0100
From: "Yann E. MORIN" <[email protected]>
To: Patrik Flykt <[email protected]>
Cc: [email protected]
Subject: Re: [PATCH] configure: check for execinfo.h
Message-ID: <[email protected]>
Content-Type: text/plain; charset=utf-8

Patrik, All,

On 2015-12-29 13:29 +0200, Patrik Flykt spake thusly:
> On Thu, 2015-12-24 at 13:55 +0100, Yann E. MORIN wrote:
> > Not all toolchains have execinfo.h. For example, support for it is
> > optional in uClibc, while it is entirely missing from musl.
> > 
> > Add a check in configure to look for it.
> > 
> > Since execinfo.h is /only/ used to dump a backtrace in case of failure,
> > just do nothing when execinfo.h is missing.
> > 
> > Signed-off-by: "Yann E. MORIN" <[email protected]>
> > ---
> >  configure.ac | 2 ++
> >  src/log.c    | 4 ++++
> >  2 files changed, 6 insertions(+)
> > 
> > diff --git a/configure.ac b/configure.ac
> > index b51d6b3..28e657b 100644
> > --- a/configure.ac
> > +++ b/configure.ac
> > @@ -182,6 +182,8 @@ AC_CHECK_LIB(resolv, ns_initparse, dummy=yes, [
> >             AC_MSG_ERROR(resolver library support is required))
> >  ])
> >  
> > +AC_CHECK_HEADERS([execinfo.h])
> > +
> >  AC_CHECK_FUNC(signalfd, dummy=yes,
> >                     AC_MSG_ERROR(signalfd support is required))
> >  
> > diff --git a/src/log.c b/src/log.c
> > index a693bd0..76e10e7 100644
> > --- a/src/log.c
> > +++ b/src/log.c
> > @@ -30,7 +30,9 @@
> >  #include <stdlib.h>
> >  #include <string.h>
> >  #include <syslog.h>
> > +#if defined(HAVE_EXECINFO_H)
> >  #include <execinfo.h>
> > +#endif
> >  #include <dlfcn.h>
> >  
> >  #include "connman.h"
> > @@ -112,6 +114,7 @@ void connman_debug(const char *format, ...)
> >  
> >  static void print_backtrace(unsigned int offset)
> >  {
> > +#if defined(HAVE_EXECINFO_H)
> 
> Generally we avoid having ifdefs in the code. As only backtrace
> functionality is concerned, it should be made into a file of its own
> which can then be conditionally compiled in. See e.g. Bluez that has
> ended up with a src/backtrace.c file for this specific functionality.
> We'd want a similar solution in ConnMan.

OK, I'll try to see what I can do.

Thanks, and Happy New Year!

Regards,
Yann E. MORIN.

-- 
.-----------------.--------------------.------------------.--------------------.
|  Yann E. MORIN  | Real-Time Embedded | /"\ ASCII RIBBON | Erics' conspiracy: |
| +33 662 376 056 | Software  Designer | \ / CAMPAIGN     |  ___               |
| +33 223 225 172 `------------.-------:  X  AGAINST      |  \e/  There is no  |
| http://ymorin.is-a-geek.org/ | _/*\_ | / \ HTML MAIL    |   v   conspiracy.  |
'------------------------------^-------^------------------^--------------------'


------------------------------

Message: 2
Date: Fri,  1 Jan 2016 15:20:02 +0100
From: "Yann E. MORIN" <[email protected]>
To: [email protected]
Subject: [PATCHv2] log: don't require backtrace()
Message-ID: <[email protected]>

Not all toolchains have execinfo.h and backtrace(). For example,
support for it is optional in uClibc, while it is entirely missing
from musl.

In glibc, execinfo.h only declares backtrace() and no other function,
so we can rely on its presence/abscence to determine if we can use
backtrace().

We fix that by:
  - adding a ./configure check for execinfo.h;
  - moving backtrace to its own file;
  - compiling backtrace.c only when execinfo.h was found.

Signed-off-by: "Yann E. MORIN" <[email protected]>
Cc: Patrik Flykt <[email protected]>

---
Changes v1 -> v2:
  - move backtrace to its own file and only compile it if execinfo.h was
    found  (Patrik)

Notes: this makes connman build under uClibc without execinfo.h and
backtrace(), but more work is needed to make it build with musl. I may
be able to work on this in the coming days/week, but I can't make any
hard promise... :-/

---
 Makefile.am         |  21 +++++---
 configure.ac        |   3 ++
 include/backtrace.h |  32 ++++++++++++
 src/backtrace.c     | 138 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/connman.h       |   2 +
 src/log.c           | 105 +--------------------------------------
 6 files changed, 189 insertions(+), 112 deletions(-)
 create mode 100644 include/backtrace.h
 create mode 100644 src/backtrace.c

diff --git a/Makefile.am b/Makefile.am
index fb7c74e..d70725c 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -19,7 +19,8 @@ noinst_HEADERS = include/rtnl.h include/task.h \
                        include/dbus.h include/option.h \
                        include/provider.h include/vpn-dbus.h \
                        include/utsname.h include/timeserver.h include/proxy.h \
-                       include/technology.h include/setting.h
+                       include/technology.h include/setting.h \
+                       include/backtrace.h
 
 local_headers = $(foreach file,$(include_HEADERS) $(nodist_include_HEADERS) \
                        $(noinst_HEADERS), include/connman/$(notdir $(file)))
@@ -42,6 +43,10 @@ else
 gweb_sources += gweb/giognutls.h gweb/gionotls.c
 endif
 
+if BACKTRACE
+backtrace_sources = src/backtrace.c
+endif
+
 shared_sources = src/shared/util.h src/shared/util.c \
                src/shared/netlink.h src/shared/netlink.c
 
@@ -101,7 +106,7 @@ MANUAL_PAGES =
 
 sbin_PROGRAMS = src/connmand src/connmand-wait-online
 
-src_connmand_SOURCES = $(gdhcp_sources) $(gweb_sources) \
+src_connmand_SOURCES = $(gdhcp_sources) $(gweb_sources) $(backtrace_sources) \
                        $(builtin_sources) $(shared_sources) src/connman.ver \
                        src/main.c src/connman.h src/log.c \
                        src/error.c src/plugin.c src/task.c \
@@ -144,7 +149,7 @@ builtin_vpn_cflags =
 
 sbin_PROGRAMS += vpn/connman-vpnd
 
-vpn_connman_vpnd_SOURCES = $(builtin_vpn_sources) \
+vpn_connman_vpnd_SOURCES = $(builtin_vpn_sources) $(backtrace_sources) \
                        $(gweb_sources) vpn/vpn.ver vpn/main.c vpn/vpn.h \
                        src/log.c src/error.c src/plugin.c src/task.c \
                        vpn/vpn-manager.c vpn/vpn-provider.c \
@@ -273,8 +278,8 @@ endif
 
 noinst_PROGRAMS += unit/test-ippool
 
-unit_test_ippool_SOURCES = src/log.c src/dbus.c src/error.c \
-                                       src/ippool.c unit/test-ippool.c
+unit_test_ippool_SOURCES = $(backtrace_sources) src/log.c src/dbus.c \
+                                       src/error.c src/ippool.c 
unit/test-ippool.c
 unit_test_ippool_LDADD = gdbus/libgdbus-internal.la \
                                @GLIB_LIBS@ @DBUS_LIBS@ -ldl
 
@@ -325,12 +330,12 @@ tools_dbus_test_LDADD = gdbus/libgdbus-internal.la 
@GLIB_LIBS@ @DBUS_LIBS@
 
 tools_polkit_test_LDADD = @DBUS_LIBS@
 
-tools_iptables_test_SOURCES = src/log.c src/iptables.c tools/iptables-test.c
+tools_iptables_test_SOURCES = $(backtrace_sources) src/log.c src/iptables.c 
tools/iptables-test.c
 tools_iptables_test_LDADD = @GLIB_LIBS@ @XTABLES_LIBS@ -ldl
 
 tools_private_network_test_LDADD = @GLIB_LIBS@ @DBUS_LIBS@
 
-tools_session_test_SOURCES = src/log.c src/dbus.c src/error.c \
+tools_session_test_SOURCES = $(backtrace_sources) src/log.c src/dbus.c 
src/error.c \
                tools/session-test.c tools/session-utils.c tools/manager-api.c \
                tools/session-api.c tools/session-test.h
 tools_session_test_LDADD = gdbus/libgdbus-internal.la \
@@ -338,7 +343,7 @@ tools_session_test_LDADD = gdbus/libgdbus-internal.la \
 
 tools_iptables_unit_CFLAGS = @DBUS_CFLAGS@ @GLIB_CFLAGS@ @XTABLES_CFLAGS@ \
                -DIPTABLES_SAVE=\""${IPTABLES_SAVE}"\"
-tools_iptables_unit_SOURCES = src/log.c \
+tools_iptables_unit_SOURCES = $(backtrace_sources) src/log.c \
                 src/iptables.c src/firewall.c src/nat.c tools/iptables-unit.c
 tools_iptables_unit_LDADD = gdbus/libgdbus-internal.la \
                                @GLIB_LIBS@ @DBUS_LIBS@ @XTABLES_LIBS@ -ldl
diff --git a/configure.ac b/configure.ac
index 2cf2e14..9a5a70e 100644
--- a/configure.ac
+++ b/configure.ac
@@ -182,6 +182,9 @@ AC_CHECK_LIB(resolv, ns_initparse, dummy=yes, [
                AC_MSG_ERROR(resolver library support is required))
 ])
 
+AC_CHECK_HEADERS([execinfo.h])
+AM_CONDITIONAL([BACKTRACE], [test "${ac_cv_header_execinfo_h}" = "yes"])
+
 AC_CHECK_FUNC(signalfd, dummy=yes,
                        AC_MSG_ERROR(signalfd support is required))
 
diff --git a/include/backtrace.h b/include/backtrace.h
new file mode 100644
index 0000000..12a202d
--- /dev/null
+++ b/include/backtrace.h
@@ -0,0 +1,32 @@
+/*
+ *
+ *  Connection Manager
+ *
+ *  Copyright (C) 2016  Yann E. MORIN <[email protected]>. All rights 
reserved.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License version 2 as
+ *  published by the Free Software Foundation.
+ *
+ *  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, write to the Free Software
+ *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+
+#ifndef __CONNMAN_BACKTRACE_H
+#define __CONNMAN_BACKTRACE_H
+
+#ifdef HAVE_EXECINFO_H
+void print_backtrace(const char* program_path, const char* program_exec,
+               unsigned int offset);
+#else
+#define print_backtrace(P,E,O)
+#endif
+
+#endif /* __CONNMAN_BACKTRACE_H */
diff --git a/src/backtrace.c b/src/backtrace.c
new file mode 100644
index 0000000..6a66c0a
--- /dev/null
+++ b/src/backtrace.c
@@ -0,0 +1,138 @@
+/*
+ *
+ *  Connection Manager
+ *
+ *  Copyright (C) 2007-2013  Intel Corporation. All rights reserved.
+ *  Copyright (C) 2016  Yann E. MORIN <[email protected]>. All rights 
reserved.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License version 2 as
+ *  published by the Free Software Foundation.
+ *
+ *  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, write to the Free Software
+ *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <string.h>
+#include <execinfo.h>
+#include <dlfcn.h>
+
+#include "connman.h"
+
+void print_backtrace(const char* program_path, const char* program_exec,
+               unsigned int offset)
+{
+       void *frames[99];
+       size_t n_ptrs;
+       unsigned int i;
+       int outfd[2], infd[2];
+       int pathlen;
+       pid_t pid;
+
+       if (!program_exec)
+               return;
+
+       pathlen = strlen(program_path);
+
+       n_ptrs = backtrace(frames, G_N_ELEMENTS(frames));
+       if (n_ptrs < offset)
+               return;
+
+       if (pipe(outfd) < 0)
+               return;
+
+       if (pipe(infd) < 0) {
+               close(outfd[0]);
+               close(outfd[1]);
+               return;
+       }
+
+       pid = fork();
+       if (pid < 0) {
+               close(outfd[0]);
+               close(outfd[1]);
+               close(infd[0]);
+               close(infd[1]);
+               return;
+       }
+
+       if (pid == 0) {
+               close(outfd[1]);
+               close(infd[0]);
+
+               dup2(outfd[0], STDIN_FILENO);
+               dup2(infd[1], STDOUT_FILENO);
+
+               execlp("addr2line", "-C", "-f", "-e", program_exec, NULL);
+
+               exit(EXIT_FAILURE);
+       }
+
+       close(outfd[0]);
+       close(infd[1]);
+
+       connman_error("++++++++ backtrace ++++++++");
+
+       for (i = offset; i < n_ptrs - 1; i++) {
+               Dl_info info;
+               char addr[20], buf[PATH_MAX * 2];
+               int len, written;
+               char *ptr, *pos;
+
+               dladdr(frames[i], &info);
+
+               len = snprintf(addr, sizeof(addr), "%p\n", frames[i]);
+               if (len < 0)
+                       break;
+
+               written = write(outfd[1], addr, len);
+               if (written < 0)
+                       break;
+
+               len = read(infd[0], buf, sizeof(buf) - 1);
+               if (len < 0)
+                       break;
+
+               buf[len] = '\0';
+
+               pos = strchr(buf, '\n');
+               *pos++ = '\0';
+
+               if (strcmp(buf, "??") == 0) {
+                       connman_error("#%-2u %p in %s", i - offset,
+                                               frames[i], info.dli_fname);
+                       continue;
+               }
+
+               ptr = strchr(pos, '\n');
+               *ptr++ = '\0';
+
+               if (strncmp(pos, program_path, pathlen) == 0)
+                       pos += pathlen + 1;
+
+               connman_error("#%-2u %p in %s() at %s", i - offset,
+                                               frames[i], buf, pos);
+       }
+
+       connman_error("+++++++++++++++++++++++++++");
+
+       kill(pid, SIGTERM);
+
+       close(outfd[1]);
+       close(infd[0]);
+}
diff --git a/src/connman.h b/src/connman.h
index 35eb3f5..3049f08 100644
--- a/src/connman.h
+++ b/src/connman.h
@@ -135,6 +135,8 @@ void __connman_log_cleanup(gboolean backtrace);
 void __connman_log_enable(struct connman_debug_desc *start,
                                        struct connman_debug_desc *stop);
 
+#include <connman/backtrace.h>
+
 #include <connman/option.h>
 
 #include <connman/setting.h>
diff --git a/src/log.c b/src/log.c
index a693bd0..9bae4a3 100644
--- a/src/log.c
+++ b/src/log.c
@@ -30,7 +30,6 @@
 #include <stdlib.h>
 #include <string.h>
 #include <syslog.h>
-#include <execinfo.h>
 #include <dlfcn.h>
 
 #include "connman.h"
@@ -110,113 +109,11 @@ void connman_debug(const char *format, ...)
        va_end(ap);
 }
 
-static void print_backtrace(unsigned int offset)
-{
-       void *frames[99];
-       size_t n_ptrs;
-       unsigned int i;
-       int outfd[2], infd[2];
-       int pathlen;
-       pid_t pid;
-
-       if (!program_exec)
-               return;
-
-       pathlen = strlen(program_path);
-
-       n_ptrs = backtrace(frames, G_N_ELEMENTS(frames));
-       if (n_ptrs < offset)
-               return;
-
-       if (pipe(outfd) < 0)
-               return;
-
-       if (pipe(infd) < 0) {
-               close(outfd[0]);
-               close(outfd[1]);
-               return;
-       }
-
-       pid = fork();
-       if (pid < 0) {
-               close(outfd[0]);
-               close(outfd[1]);
-               close(infd[0]);
-               close(infd[1]);
-               return;
-       }
-
-       if (pid == 0) {
-               close(outfd[1]);
-               close(infd[0]);
-
-               dup2(outfd[0], STDIN_FILENO);
-               dup2(infd[1], STDOUT_FILENO);
-
-               execlp("addr2line", "-C", "-f", "-e", program_exec, NULL);
-
-               exit(EXIT_FAILURE);
-       }
-
-       close(outfd[0]);
-       close(infd[1]);
-
-       connman_error("++++++++ backtrace ++++++++");
-
-       for (i = offset; i < n_ptrs - 1; i++) {
-               Dl_info info;
-               char addr[20], buf[PATH_MAX * 2];
-               int len, written;
-               char *ptr, *pos;
-
-               dladdr(frames[i], &info);
-
-               len = snprintf(addr, sizeof(addr), "%p\n", frames[i]);
-               if (len < 0)
-                       break;
-
-               written = write(outfd[1], addr, len);
-               if (written < 0)
-                       break;
-
-               len = read(infd[0], buf, sizeof(buf) - 1);
-               if (len < 0)
-                       break;
-
-               buf[len] = '\0';
-
-               pos = strchr(buf, '\n');
-               *pos++ = '\0';
-
-               if (strcmp(buf, "??") == 0) {
-                       connman_error("#%-2u %p in %s", i - offset,
-                                               frames[i], info.dli_fname);
-                       continue;
-               }
-
-               ptr = strchr(pos, '\n');
-               *ptr++ = '\0';
-
-               if (strncmp(pos, program_path, pathlen) == 0)
-                       pos += pathlen + 1;
-
-               connman_error("#%-2u %p in %s() at %s", i - offset,
-                                               frames[i], buf, pos);
-       }
-
-       connman_error("+++++++++++++++++++++++++++");
-
-       kill(pid, SIGTERM);
-
-       close(outfd[1]);
-       close(infd[0]);
-}
-
 static void signal_handler(int signo)
 {
        connman_error("Aborting (signal %d) [%s]", signo, program_exec);
 
-       print_backtrace(2);
+       print_backtrace(program_path, program_exec, 2);
 
        exit(EXIT_FAILURE);
 }
-- 
1.9.1



------------------------------

Subject: Digest Footer

_______________________________________________
connman mailing list
[email protected]
https://lists.01.org/mailman/listinfo/connman


------------------------------

End of connman Digest, Vol 3, Issue 1
*************************************

Reply via email to