{"id":13922,"url":"https://patchwork.libcamera.org/api/1.1/patches/13922/?format=json","web_url":"https://patchwork.libcamera.org/patch/13922/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20210924102323.26787-4-laurent.pinchart@ideasonboard.com>","date":"2021-09-24T10:23:22","name":"[libcamera-devel,v1,3/4] libcamera: base: backtrace: Use libunwind when available","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"4ee8b784944b4d8cc1b5d6ba6cb9f420e1a93a13","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/1.1/people/2/?format=json","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/13922/mbox/","series":[{"id":2562,"url":"https://patchwork.libcamera.org/api/1.1/series/2562/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=2562","date":"2021-09-24T10:23:19","name":"libcamera: Improve backtraces","version":1,"mbox":"https://patchwork.libcamera.org/series/2562/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/13922/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/13922/checks/","tags":{},"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 1FFD0BDC71\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri, 24 Sep 2021 10:23:36 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id DD59769192;\n\tFri, 24 Sep 2021 12:23:34 +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 04DF569188\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 24 Sep 2021 12:23:31 +0200 (CEST)","from pendragon.lan (62-78-145-57.bb.dnainternet.fi [62.78.145.57])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 9D93158B\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 24 Sep 2021 12:23:30 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"G/M8QDbw\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1632479010;\n\tbh=evYR6+uZ6P7pXUXp9HzemyUQDU+nfKG1LrzBvG6LjtI=;\n\th=From:To:Subject:Date:In-Reply-To:References:From;\n\tb=G/M8QDbwS9x7NtGDx3eR8fpcAulbvG23ef+FsPsB/I7FYz73KhQadrMGKrgGBBt7v\n\tP17128+SwwxhNSBahTRX5DcM6eFPnpNFpYcm7gdW+YjhBgNCMUb2zNV6RAYcodTbrW\n\taEPRpOVe8QCMRtxf07ffLe9Hi+On79s6dzQUgrhk=","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Fri, 24 Sep 2021 13:23:22 +0300","Message-Id":"<20210924102323.26787-4-laurent.pinchart@ideasonboard.com>","X-Mailer":"git-send-email 2.32.0","In-Reply-To":"<20210924102323.26787-1-laurent.pinchart@ideasonboard.com>","References":"<20210924102323.26787-1-laurent.pinchart@ideasonboard.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH v1 3/4] libcamera: base: backtrace: Use\n\tlibunwind 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>"},"content":"libunwind is an alternative to glibc's backtrace() to extract a\nbacktrace. Use it when available to extend backtrace support to more\nplatforms.\n\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\n include/libcamera/base/backtrace.h |  3 ++\n src/libcamera/base/backtrace.cpp   | 61 ++++++++++++++++++++++++++++--\n src/libcamera/base/meson.build     |  6 +++\n 3 files changed, 67 insertions(+), 3 deletions(-)","diff":"diff --git a/include/libcamera/base/backtrace.h b/include/libcamera/base/backtrace.h\nindex 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 \tLIBCAMERA_DISABLE_COPY(Backtrace)\n \n+\tbool backtraceTrace();\n+\tbool unwindTrace();\n+\n \tstd::vector<void *> backtrace_;\n };\n \ndiff --git a/src/libcamera/base/backtrace.cpp b/src/libcamera/base/backtrace.cpp\nindex 011f2e428d5d..40fa60d0f9bf 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+ * 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,15 @@ std::string DwflParser::stackEntry(const void *ip)\n  * It can later be converted to a string with toString().\n  */\n Backtrace::Backtrace()\n+{\n+\t/* Try libunwind first and fall back to backtrace() if it fails. */\n+\tif (unwindTrace())\n+\t\treturn;\n+\n+\tbacktraceTrace();\n+}\n+\n+bool Backtrace::backtraceTrace()\n {\n #if HAVE_BACKTRACE\n \tbacktrace_.resize(32);\n@@ -153,10 +171,44 @@ Backtrace::Backtrace()\n \tint num_entries = backtrace(backtrace_.data(), backtrace_.size());\n \tif (num_entries < 0) {\n \t\tbacktrace_.clear();\n-\t\treturn;\n+\t\treturn false;\n \t}\n \n \tbacktrace_.resize(num_entries);\n+\n+\treturn true;\n+#else\n+\treturn false;\n+#endif\n+}\n+\n+bool Backtrace::unwindTrace()\n+{\n+#if HAVE_UNWIND\n+\tunw_context_t uc;\n+\tint ret = unw_getcontext(&uc);\n+\tif (ret)\n+\t\treturn false;\n+\n+\tunw_cursor_t cursor;\n+\tret = unw_init_local(&cursor, &uc);\n+\tif (ret)\n+\t\treturn false;\n+\n+\tdo {\n+\t\tunw_word_t ip;\n+\t\tret = unw_get_reg(&cursor, UNW_REG_IP, &ip);\n+\t\tif (ret) {\n+\t\t\tbacktrace_.push_back(nullptr);\n+\t\t\tcontinue;\n+\t\t}\n+\n+\t\tbacktrace_.push_back(reinterpret_cast<void *>(ip));\n+\t} while (unw_step(&cursor) > 0);\n+\n+\treturn true;\n+#else\n+\treturn false;\n #endif\n }\n \n@@ -181,8 +233,11 @@ Backtrace::Backtrace()\n  */\n std::string Backtrace::toString(unsigned int skipLevels) const\n {\n-\t/* Skip the first entry, corresponding to the Backtrace construction. */\n-\tskipLevels += 1;\n+\t/*\n+\t * Skip the first two entries, corresponding to the Backtrace\n+\t * construction.\n+\t */\n+\tskipLevels += 2;\n \n \tif (backtrace_.size() <= skipLevels)\n \t\treturn std::string();\ndiff --git a/src/libcamera/base/meson.build b/src/libcamera/base/meson.build\nindex 1fa894cf1896..8d14a65648fc 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","prefixes":["libcamera-devel","v1","3/4"]}