Patch Detail
Show a patch.
GET /api/1.1/patches/14031/?format=api
{ "id": 14031, "url": "https://patchwork.libcamera.org/api/1.1/patches/14031/?format=api", "web_url": "https://patchwork.libcamera.org/patch/14031/", "project": { "id": 1, "url": "https://patchwork.libcamera.org/api/1.1/projects/1/?format=api", "name": "libcamera", "link_name": "libcamera", "list_id": "libcamera_core", "list_email": "libcamera-devel@lists.libcamera.org", "web_url": "", "scm_url": "", "webscm_url": "" }, "msgid": "<20211003223606.20016-4-laurent.pinchart@ideasonboard.com>", "date": "2021-10-03T22:36:05", "name": "[libcamera-devel,v2,3/4] libcamera: base: backtrace: Use libunwind when available", "commit_ref": null, "pull_url": null, "state": "accepted", "archived": false, "hash": "4a4e291afabee41b763dbe709ecc40f50c4fb3b5", "submitter": { "id": 2, "url": "https://patchwork.libcamera.org/api/1.1/people/2/?format=api", "name": "Laurent Pinchart", "email": "laurent.pinchart@ideasonboard.com" }, "delegate": null, "mbox": "https://patchwork.libcamera.org/patch/14031/mbox/", "series": [ { "id": 2593, "url": "https://patchwork.libcamera.org/api/1.1/series/2593/?format=api", "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=2593", "date": "2021-10-03T22:36:03", "name": "libcamera: Improve backtraces", "version": 2, "mbox": "https://patchwork.libcamera.org/series/2593/mbox/" } ], "comments": "https://patchwork.libcamera.org/api/patches/14031/comments/", "check": "pending", "checks": "https://patchwork.libcamera.org/api/patches/14031/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 D7C49C3244\n\tfor <parsemail@patchwork.libcamera.org>;\n\tSun, 3 Oct 2021 22:36:19 +0000 (UTC)", "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 46B2A691BA;\n\tMon, 4 Oct 2021 00:36:19 +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 ADFEB691B8\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 4 Oct 2021 00:36:15 +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 52D8DA2A\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 4 Oct 2021 00:36:15 +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=\"ERIOU/Mi\"; dkim-atps=neutral", "DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1633300575;\n\tbh=LqgZH5BD3bW6HGSkDUq9YXSu/1Vq0OvdVOR6UZM2BOI=;\n\th=From:To:Subject:Date:In-Reply-To:References:From;\n\tb=ERIOU/MiyLs8/QAVVmE+crCDpa5uVQDCCaKYm3oHjsLbAZk7USUyAFBYZVaizVnck\n\tBCOK/Kic8zY6La2iCntkPXho89Bb3n0y2u1sP01a8u8SJOdWfjzmw7K0apX98bbuKD\n\tlB42Vw+Fg6v4nBeAyAk11GdfbJmlqRDtn+u4AXCI=", "From": "Laurent Pinchart <laurent.pinchart@ideasonboard.com>", "To": "libcamera-devel@lists.libcamera.org", "Date": "Mon, 4 Oct 2021 01:36:05 +0300", "Message-Id": "<20211003223606.20016-4-laurent.pinchart@ideasonboard.com>", "X-Mailer": "git-send-email 2.32.0", "In-Reply-To": "<20211003223606.20016-1-laurent.pinchart@ideasonboard.com>", "References": "<20211003223606.20016-1-laurent.pinchart@ideasonboard.com>", "MIME-Version": "1.0", "Content-Transfer-Encoding": "8bit", "Subject": "[libcamera-devel] [PATCH v2 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---\nChanges since v1:\n\n- Use __noinline__ attribute for backtraceTrace() and unwindTrace()\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(-)", "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 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+ * 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+\t/* Try libunwind first and fall back to backtrace() if it fails. */\n+\tif (unwindTrace())\n+\t\treturn;\n+\n+\tbacktraceTrace();\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 \tbacktrace_.resize(32);\n@@ -153,10 +176,45 @@ 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+__attribute__((__noinline__))\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 +239,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 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", "prefixes": [ "libcamera-devel", "v2", "3/4" ] }