[{"id":20132,"web_url":"https://patchwork.libcamera.org/comment/20132/","msgid":"<163403593053.2837254.10322221265202881108@Monstersaurus>","date":"2021-10-12T10:52:10","subject":"Re: [libcamera-devel] [PATCH v2 3/4] libcamera: base: backtrace:\n\tUse libunwind when available","submitter":{"id":4,"url":"https://patchwork.libcamera.org/api/people/4/","name":"Kieran Bingham","email":"kieran.bingham@ideasonboard.com"},"content":"Quoting Laurent Pinchart (2021-10-03 23:36:05)\n> libunwind is an alternative to glibc's backtrace() to extract a\n> backtrace. Use it when available to extend backtrace support to more\n> platforms.\n> \n> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n> ---\n> Changes since v1:\n> \n> - Use __noinline__ attribute for backtraceTrace() and unwindTrace()\n\nIs this guaranteed ? Or do we need to do some checks at runtime?\n\nPerhaps we can deal with that if we find out we need it.\n\n> ---\n>  include/libcamera/base/backtrace.h |  3 ++\n>  src/libcamera/base/backtrace.cpp   | 67 ++++++++++++++++++++++++++++--\n>  src/libcamera/base/meson.build     |  6 +++\n>  3 files changed, 73 insertions(+), 3 deletions(-)\n> \n> diff --git a/include/libcamera/base/backtrace.h b/include/libcamera/base/backtrace.h\n> index aefc76defa83..58ccc14c8f81 100644\n> --- a/include/libcamera/base/backtrace.h\n> +++ b/include/libcamera/base/backtrace.h\n> @@ -26,6 +26,9 @@ public:\n>  private:\n>         LIBCAMERA_DISABLE_COPY(Backtrace)\n>  \n> +       bool backtraceTrace();\n> +       bool unwindTrace();\n> +\n>         std::vector<void *> backtrace_;\n>  };\n>  \n> diff --git a/src/libcamera/base/backtrace.cpp b/src/libcamera/base/backtrace.cpp\n> index 79e4a31f3d21..0aafc6a366c5 100644\n> --- a/src/libcamera/base/backtrace.cpp\n> +++ b/src/libcamera/base/backtrace.cpp\n> @@ -18,6 +18,15 @@\n>  #include <unistd.h>\n>  #endif\n>  \n> +#if HAVE_UNWIND\n> +/*\n> + * Disable support for remote unwinding to enable a more optimized\n\nWhat is 'remote' unwinding? Not that it matters too much here.\n\nReviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n\n> + * implementation.\n> + */\n> +#define UNW_LOCAL_ONLY\n> +#include <libunwind.h>\n> +#endif\n> +\n>  #include <sstream>\n>  \n>  #include <libcamera/base/span.h>\n> @@ -146,6 +155,20 @@ std::string DwflParser::stackEntry(const void *ip)\n>   * It can later be converted to a string with toString().\n>   */\n>  Backtrace::Backtrace()\n> +{\n> +       /* Try libunwind first and fall back to backtrace() if it fails. */\n> +       if (unwindTrace())\n> +               return;\n> +\n> +       backtraceTrace();\n> +}\n> +\n> +/*\n> + * Avoid inlining to make sure that the Backtrace constructor adds exactly two\n> + * calls to the stack, which are later skipped in toString().\n> + */\n> +__attribute__((__noinline__))\n> +bool Backtrace::backtraceTrace()\n>  {\n>  #if HAVE_BACKTRACE\n>         backtrace_.resize(32);\n> @@ -153,10 +176,45 @@ Backtrace::Backtrace()\n>         int num_entries = backtrace(backtrace_.data(), backtrace_.size());\n>         if (num_entries < 0) {\n>                 backtrace_.clear();\n> -               return;\n> +               return false;\n>         }\n>  \n>         backtrace_.resize(num_entries);\n> +\n> +       return true;\n> +#else\n> +       return false;\n> +#endif\n> +}\n> +\n> +__attribute__((__noinline__))\n> +bool Backtrace::unwindTrace()\n> +{\n> +#if HAVE_UNWIND\n> +       unw_context_t uc;\n> +       int ret = unw_getcontext(&uc);\n> +       if (ret)\n> +               return false;\n> +\n> +       unw_cursor_t cursor;\n> +       ret = unw_init_local(&cursor, &uc);\n> +       if (ret)\n> +               return false;\n> +\n> +       do {\n> +               unw_word_t ip;\n> +               ret = unw_get_reg(&cursor, UNW_REG_IP, &ip);\n> +               if (ret) {\n> +                       backtrace_.push_back(nullptr);\n> +                       continue;\n> +               }\n> +\n> +               backtrace_.push_back(reinterpret_cast<void *>(ip));\n> +       } while (unw_step(&cursor) > 0);\n> +\n> +       return true;\n> +#else\n> +       return false;\n>  #endif\n>  }\n>  \n> @@ -181,8 +239,11 @@ Backtrace::Backtrace()\n>   */\n>  std::string Backtrace::toString(unsigned int skipLevels) const\n>  {\n> -       /* Skip the first entry, corresponding to the Backtrace construction. */\n> -       skipLevels += 1;\n> +       /*\n> +        * Skip the first two entries, corresponding to the Backtrace\n> +        * construction.\n> +        */\n> +       skipLevels += 2;\n>  \n>         if (backtrace_.size() <= skipLevels)\n>                 return std::string();\n> diff --git a/src/libcamera/base/meson.build b/src/libcamera/base/meson.build\n> index 4c44b9f55011..05fed7acf561 100644\n> --- a/src/libcamera/base/meson.build\n> +++ b/src/libcamera/base/meson.build\n> @@ -20,6 +20,7 @@ libcamera_base_sources = files([\n>  ])\n>  \n>  libdw = cc.find_library('libdw', required : false)\n> +libunwind = cc.find_library('libunwind', required : false)\n>  \n>  if cc.has_header_symbol('execinfo.h', 'backtrace')\n>      config_h.set('HAVE_BACKTRACE', 1)\n> @@ -29,10 +30,15 @@ if libdw.found()\n>      config_h.set('HAVE_DW', 1)\n>  endif\n>  \n> +if libunwind.found()\n> +    config_h.set('HAVE_UNWIND', 1)\n> +endif\n> +\n>  libcamera_base_deps = [\n>      dependency('threads'),\n>      libatomic,\n>      libdw,\n> +    libunwind,\n>  ]\n>  \n>  # Internal components must use the libcamera_base_private dependency to enable\n> -- \n> Regards,\n> \n> Laurent Pinchart\n>","headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 2BF84C323E\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue, 12 Oct 2021 10:52:15 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id A3FB168F51;\n\tTue, 12 Oct 2021 12:52:14 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 7150D68F4C\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 12 Oct 2021 12:52:13 +0200 (CEST)","from pendragon.ideasonboard.com\n\t(cpc89244-aztw30-2-0-cust3082.18-1.cable.virginm.net [86.31.172.11])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 0433AF1\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 12 Oct 2021 12:52:12 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"kSx4/YNo\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1634035933;\n\tbh=QxtojCYfcqUIsbKVZpzTw42ZIoM7C3GAVRbcXPASjdY=;\n\th=In-Reply-To:References:Subject:From:To:Date:From;\n\tb=kSx4/YNoWPcXMqxKNq5sOBOIvPu0WpOiwcFDzHCXQKVsQXMyyh6JExRaWxxnD2HO7\n\tHDd074STi5PPWOYn5Ka+hDl6xv+0pU+VKuPY32PonfRpvm8VABzNFnZBhPaL9Z7oCD\n\tnslNI0cHip3xdPok3FqxSip6e2/W5C/UAYvg1Q3Q=","Content-Type":"text/plain; charset=\"utf-8\"","MIME-Version":"1.0","Content-Transfer-Encoding":"quoted-printable","In-Reply-To":"<20211003223606.20016-4-laurent.pinchart@ideasonboard.com>","References":"<20211003223606.20016-1-laurent.pinchart@ideasonboard.com>\n\t<20211003223606.20016-4-laurent.pinchart@ideasonboard.com>","From":"Kieran Bingham <kieran.bingham@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Tue, 12 Oct 2021 11:52:10 +0100","Message-ID":"<163403593053.2837254.10322221265202881108@Monstersaurus>","User-Agent":"alot/0.9.1","Subject":"Re: [libcamera-devel] [PATCH v2 3/4] libcamera: base: backtrace:\n\tUse libunwind when available","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}},{"id":20151,"web_url":"https://patchwork.libcamera.org/comment/20151/","msgid":"<YWXy+iCiAvKBNIce@pendragon.ideasonboard.com>","date":"2021-10-12T20:41:30","subject":"Re: [libcamera-devel] [PATCH v2 3/4] libcamera: base: backtrace:\n\tUse libunwind when available","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Kieran,\n\nOn Tue, Oct 12, 2021 at 11:52:10AM +0100, Kieran Bingham wrote:\n> Quoting Laurent Pinchart (2021-10-03 23:36:05)\n> > libunwind is an alternative to glibc's backtrace() to extract a\n> > backtrace. Use it when available to extend backtrace support to more\n> > platforms.\n> > \n> > Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n> > ---\n> > Changes since v1:\n> > \n> > - Use __noinline__ attribute for backtraceTrace() and unwindTrace()\n> \n> Is this guaranteed ? Or do we need to do some checks at runtime?\n\nAs far as I understand, it's not fully guaranteed:\n\nhttps://gcc.gnu.org/onlinedocs/gcc-11.2.0/gcc/Common-Function-Attributes.html#index-noinline-function-attribute\n\n> Perhaps we can deal with that if we find out we need it.\n\nThat was exactly my reasoning :-)\n\n> > ---\n> >  include/libcamera/base/backtrace.h |  3 ++\n> >  src/libcamera/base/backtrace.cpp   | 67 ++++++++++++++++++++++++++++--\n> >  src/libcamera/base/meson.build     |  6 +++\n> >  3 files changed, 73 insertions(+), 3 deletions(-)\n> > \n> > diff --git a/include/libcamera/base/backtrace.h b/include/libcamera/base/backtrace.h\n> > index aefc76defa83..58ccc14c8f81 100644\n> > --- a/include/libcamera/base/backtrace.h\n> > +++ b/include/libcamera/base/backtrace.h\n> > @@ -26,6 +26,9 @@ public:\n> >  private:\n> >         LIBCAMERA_DISABLE_COPY(Backtrace)\n> >  \n> > +       bool backtraceTrace();\n> > +       bool unwindTrace();\n> > +\n> >         std::vector<void *> backtrace_;\n> >  };\n> >  \n> > diff --git a/src/libcamera/base/backtrace.cpp b/src/libcamera/base/backtrace.cpp\n> > index 79e4a31f3d21..0aafc6a366c5 100644\n> > --- a/src/libcamera/base/backtrace.cpp\n> > +++ b/src/libcamera/base/backtrace.cpp\n> > @@ -18,6 +18,15 @@\n> >  #include <unistd.h>\n> >  #endif\n> >  \n> > +#if HAVE_UNWIND\n> > +/*\n> > + * Disable support for remote unwinding to enable a more optimized\n> \n> What is 'remote' unwinding? Not that it matters too much here.\n\nIt means unwinding a stack of another process (possibly running on a\ndifferent machine).\n\n> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n> \n> > + * implementation.\n> > + */\n> > +#define UNW_LOCAL_ONLY\n> > +#include <libunwind.h>\n> > +#endif\n> > +\n> >  #include <sstream>\n> >  \n> >  #include <libcamera/base/span.h>\n> > @@ -146,6 +155,20 @@ std::string DwflParser::stackEntry(const void *ip)\n> >   * It can later be converted to a string with toString().\n> >   */\n> >  Backtrace::Backtrace()\n> > +{\n> > +       /* Try libunwind first and fall back to backtrace() if it fails. */\n> > +       if (unwindTrace())\n> > +               return;\n> > +\n> > +       backtraceTrace();\n> > +}\n> > +\n> > +/*\n> > + * Avoid inlining to make sure that the Backtrace constructor adds exactly two\n> > + * calls to the stack, which are later skipped in toString().\n> > + */\n> > +__attribute__((__noinline__))\n> > +bool Backtrace::backtraceTrace()\n> >  {\n> >  #if HAVE_BACKTRACE\n> >         backtrace_.resize(32);\n> > @@ -153,10 +176,45 @@ Backtrace::Backtrace()\n> >         int num_entries = backtrace(backtrace_.data(), backtrace_.size());\n> >         if (num_entries < 0) {\n> >                 backtrace_.clear();\n> > -               return;\n> > +               return false;\n> >         }\n> >  \n> >         backtrace_.resize(num_entries);\n> > +\n> > +       return true;\n> > +#else\n> > +       return false;\n> > +#endif\n> > +}\n> > +\n> > +__attribute__((__noinline__))\n> > +bool Backtrace::unwindTrace()\n> > +{\n> > +#if HAVE_UNWIND\n> > +       unw_context_t uc;\n> > +       int ret = unw_getcontext(&uc);\n> > +       if (ret)\n> > +               return false;\n> > +\n> > +       unw_cursor_t cursor;\n> > +       ret = unw_init_local(&cursor, &uc);\n> > +       if (ret)\n> > +               return false;\n> > +\n> > +       do {\n> > +               unw_word_t ip;\n> > +               ret = unw_get_reg(&cursor, UNW_REG_IP, &ip);\n> > +               if (ret) {\n> > +                       backtrace_.push_back(nullptr);\n> > +                       continue;\n> > +               }\n> > +\n> > +               backtrace_.push_back(reinterpret_cast<void *>(ip));\n> > +       } while (unw_step(&cursor) > 0);\n> > +\n> > +       return true;\n> > +#else\n> > +       return false;\n> >  #endif\n> >  }\n> >  \n> > @@ -181,8 +239,11 @@ Backtrace::Backtrace()\n> >   */\n> >  std::string Backtrace::toString(unsigned int skipLevels) const\n> >  {\n> > -       /* Skip the first entry, corresponding to the Backtrace construction. */\n> > -       skipLevels += 1;\n> > +       /*\n> > +        * Skip the first two entries, corresponding to the Backtrace\n> > +        * construction.\n> > +        */\n> > +       skipLevels += 2;\n> >  \n> >         if (backtrace_.size() <= skipLevels)\n> >                 return std::string();\n> > diff --git a/src/libcamera/base/meson.build b/src/libcamera/base/meson.build\n> > index 4c44b9f55011..05fed7acf561 100644\n> > --- a/src/libcamera/base/meson.build\n> > +++ b/src/libcamera/base/meson.build\n> > @@ -20,6 +20,7 @@ libcamera_base_sources = files([\n> >  ])\n> >  \n> >  libdw = cc.find_library('libdw', required : false)\n> > +libunwind = cc.find_library('libunwind', required : false)\n> >  \n> >  if cc.has_header_symbol('execinfo.h', 'backtrace')\n> >      config_h.set('HAVE_BACKTRACE', 1)\n> > @@ -29,10 +30,15 @@ if libdw.found()\n> >      config_h.set('HAVE_DW', 1)\n> >  endif\n> >  \n> > +if libunwind.found()\n> > +    config_h.set('HAVE_UNWIND', 1)\n> > +endif\n> > +\n> >  libcamera_base_deps = [\n> >      dependency('threads'),\n> >      libatomic,\n> >      libdw,\n> > +    libunwind,\n> >  ]\n> >  \n> >  # Internal components must use the libcamera_base_private dependency to enable","headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id E666FC323E\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue, 12 Oct 2021 20:41:46 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 4035268F4F;\n\tTue, 12 Oct 2021 22:41:46 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id D1EB468F4C\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 12 Oct 2021 22:41:44 +0200 (CEST)","from pendragon.ideasonboard.com (62-78-145-57.bb.dnainternet.fi\n\t[62.78.145.57])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 51CDBF1;\n\tTue, 12 Oct 2021 22:41:44 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"EL/Y1qUG\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1634071304;\n\tbh=Rrz6yQhdQx5CnT8iGAq9sRDvzgtt0GjsUblyv8J/3mw=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=EL/Y1qUGq3iOvWVK8iRc+p/44B43sjUf+qjymNXRI3Caui+aJYRYOFIttwOWtDzRS\n\tlWQ7nqTMAcisTnWfG/318L3AluhSWugaWNl1+9pU7Zalb4gYyg66gIVY1z+peY5JWH\n\tlrLNvjMlNhEoXNPXcUICmKnNXUcJsdutJ1OimJqg=","Date":"Tue, 12 Oct 2021 23:41:30 +0300","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Kieran Bingham <kieran.bingham@ideasonboard.com>","Message-ID":"<YWXy+iCiAvKBNIce@pendragon.ideasonboard.com>","References":"<20211003223606.20016-1-laurent.pinchart@ideasonboard.com>\n\t<20211003223606.20016-4-laurent.pinchart@ideasonboard.com>\n\t<163403593053.2837254.10322221265202881108@Monstersaurus>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<163403593053.2837254.10322221265202881108@Monstersaurus>","Subject":"Re: [libcamera-devel] [PATCH v2 3/4] libcamera: base: backtrace:\n\tUse libunwind when available","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","Cc":"libcamera-devel@lists.libcamera.org","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]