From patchwork Fri Sep 24 10:23:22 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 13922 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 1FFD0BDC71 for ; Fri, 24 Sep 2021 10:23:36 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id DD59769192; Fri, 24 Sep 2021 12:23:34 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="G/M8QDbw"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 04DF569188 for ; Fri, 24 Sep 2021 12:23:31 +0200 (CEST) Received: from pendragon.lan (62-78-145-57.bb.dnainternet.fi [62.78.145.57]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 9D93158B for ; Fri, 24 Sep 2021 12:23:30 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1632479010; bh=evYR6+uZ6P7pXUXp9HzemyUQDU+nfKG1LrzBvG6LjtI=; h=From:To:Subject:Date:In-Reply-To:References:From; b=G/M8QDbwS9x7NtGDx3eR8fpcAulbvG23ef+FsPsB/I7FYz73KhQadrMGKrgGBBt7v P17128+SwwxhNSBahTRX5DcM6eFPnpNFpYcm7gdW+YjhBgNCMUb2zNV6RAYcodTbrW aEPRpOVe8QCMRtxf07ffLe9Hi+On79s6dzQUgrhk= From: Laurent Pinchart 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 Subject: [libcamera-devel] [PATCH v1 3/4] libcamera: base: backtrace: Use libunwind when available X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" libunwind is an alternative to glibc's backtrace() to extract a backtrace. Use it when available to extend backtrace support to more platforms. Signed-off-by: Laurent Pinchart --- include/libcamera/base/backtrace.h | 3 ++ src/libcamera/base/backtrace.cpp | 61 ++++++++++++++++++++++++++++-- src/libcamera/base/meson.build | 6 +++ 3 files changed, 67 insertions(+), 3 deletions(-) diff --git a/include/libcamera/base/backtrace.h b/include/libcamera/base/backtrace.h index aefc76defa83..58ccc14c8f81 100644 --- a/include/libcamera/base/backtrace.h +++ b/include/libcamera/base/backtrace.h @@ -26,6 +26,9 @@ public: private: LIBCAMERA_DISABLE_COPY(Backtrace) + bool backtraceTrace(); + bool unwindTrace(); + std::vector backtrace_; }; diff --git a/src/libcamera/base/backtrace.cpp b/src/libcamera/base/backtrace.cpp index 011f2e428d5d..40fa60d0f9bf 100644 --- a/src/libcamera/base/backtrace.cpp +++ b/src/libcamera/base/backtrace.cpp @@ -18,6 +18,15 @@ #include #endif +#if HAVE_UNWIND +/* + * Disable support for remote unwinding to enable a more optimized + * implementation. + */ +#define UNW_LOCAL_ONLY +#include +#endif + #include #include @@ -146,6 +155,15 @@ std::string DwflParser::stackEntry(const void *ip) * It can later be converted to a string with toString(). */ Backtrace::Backtrace() +{ + /* Try libunwind first and fall back to backtrace() if it fails. */ + if (unwindTrace()) + return; + + backtraceTrace(); +} + +bool Backtrace::backtraceTrace() { #if HAVE_BACKTRACE backtrace_.resize(32); @@ -153,10 +171,44 @@ Backtrace::Backtrace() int num_entries = backtrace(backtrace_.data(), backtrace_.size()); if (num_entries < 0) { backtrace_.clear(); - return; + return false; } backtrace_.resize(num_entries); + + return true; +#else + return false; +#endif +} + +bool Backtrace::unwindTrace() +{ +#if HAVE_UNWIND + unw_context_t uc; + int ret = unw_getcontext(&uc); + if (ret) + return false; + + unw_cursor_t cursor; + ret = unw_init_local(&cursor, &uc); + if (ret) + return false; + + do { + unw_word_t ip; + ret = unw_get_reg(&cursor, UNW_REG_IP, &ip); + if (ret) { + backtrace_.push_back(nullptr); + continue; + } + + backtrace_.push_back(reinterpret_cast(ip)); + } while (unw_step(&cursor) > 0); + + return true; +#else + return false; #endif } @@ -181,8 +233,11 @@ Backtrace::Backtrace() */ std::string Backtrace::toString(unsigned int skipLevels) const { - /* Skip the first entry, corresponding to the Backtrace construction. */ - skipLevels += 1; + /* + * Skip the first two entries, corresponding to the Backtrace + * construction. + */ + skipLevels += 2; if (backtrace_.size() <= skipLevels) return std::string(); diff --git a/src/libcamera/base/meson.build b/src/libcamera/base/meson.build index 1fa894cf1896..8d14a65648fc 100644 --- a/src/libcamera/base/meson.build +++ b/src/libcamera/base/meson.build @@ -20,6 +20,7 @@ libcamera_base_sources = files([ ]) libdw = cc.find_library('libdw', required : false) +libunwind = cc.find_library('libunwind', required : false) if cc.has_header_symbol('execinfo.h', 'backtrace') config_h.set('HAVE_BACKTRACE', 1) @@ -29,10 +30,15 @@ if libdw.found() config_h.set('HAVE_DW', 1) endif +if libunwind.found() + config_h.set('HAVE_UNWIND', 1) +endif + libcamera_base_deps = [ dependency('threads'), libatomic, libdw, + libunwind, ] # Internal components must use the libcamera_base_private dependency to enable