From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from [87.239.111.99] (localhost [127.0.0.1]) by dev.tarantool.org (Postfix) with ESMTP id 4FD876EFDA; Wed, 18 May 2022 15:31:45 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 4FD876EFDA DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1652877105; bh=gaEJcUznajWbO56wi7hdAIXpAAwA2EQvcVreORc2Huc=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=UxO/sXeJHw3W9VMN0EH++dc7DsNvzYfTofU/JTnW7Lp0W/bD4lAm2ynbqR81+e53p NZTiO81iMQ6qLQ0jktsFAYu+QHz3iQZYkEHBtfFAS7kFhILy84hwGHSDo17tRrJJ5I yU1jybJbda1x/h0Iuu5S9pd5ft7VL1BlMYYw6XUw= Received: from mail-lf1-f45.google.com (mail-lf1-f45.google.com [209.85.167.45]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id BB6B66E093 for ; Wed, 18 May 2022 15:31:16 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org BB6B66E093 Received: by mail-lf1-f45.google.com with SMTP id bu29so3425613lfb.0 for ; Wed, 18 May 2022 05:31:16 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=tbgevcr8eqjo45uMfkcdOlFqYx2ZwntwO5AuSZ6fzrs=; b=mOnCamUJtD8uJ5d67WnVJXiXEWG6AC6wa9+1KQK/Ykc6oLji8NxjU3aDwDj12qYTlO HB1KdzKIVH6iNCASmeqflOF/mpK4/C8BKAvT9Hq1DgJE8bFUDGkHs9LfVDg1iXj8Ri1f uRQEOp0dtEkEINkA1wTXomYuij/TUOZL7U6dl80CMdT8OhTOV1BqteOEC7uf5gXwIPz8 Ty7R/+wYBX+pmbtdDcPN/1GJLC5E74Az/zX1aHqFLbHxZK7UL99fn023XJQi8BagiO/j 4Y+RyVpj9v66zfx3ROYS/EanG5mv2IKaGnfF/r4iJl8+ieLcZmgRL3+l7M35/9wDRM92 JrxQ== X-Gm-Message-State: AOAM532P02rrjOJNm0hyXLhsOKBjylnTVaJFEW/34+9R3c1DuyLWcywX g2qLn7fntQ8H+brbulUvAgffR7TTWjWEBA== X-Google-Smtp-Source: ABdhPJzVi/gHsN13FfWT4cG3QcknGn5kN7UaabrrW/r2nrvFtxA+0IsqhxUvQ5GWLVZZNQz/kMMu3g== X-Received: by 2002:ac2:43d6:0:b0:472:2c21:3e0b with SMTP id u22-20020ac243d6000000b004722c213e0bmr20273054lfl.312.1652877075702; Wed, 18 May 2022 05:31:15 -0700 (PDT) Received: from localhost.localdomain ([93.175.11.199]) by smtp.gmail.com with ESMTPSA id j5-20020ac25505000000b0047255d210f5sm201730lfk.36.2022.05.18.05.31.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 May 2022 05:31:15 -0700 (PDT) X-Google-Original-From: Maxim Kokryashkin To: tarantool-patches@dev.tarantool.org, imun@tarantool.org, skaplun@tarantool.org Date: Wed, 18 May 2022 15:31:08 +0300 Message-Id: <20220518123108.45915-2-m.kokryashkin@tarantool.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220518123108.45915-1-m.kokryashkin@tarantool.org> References: <20220518123108.45915-1-m.kokryashkin@tarantool.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Subject: [Tarantool-patches] [PATCH luajit] sysprof: replace `backtrace` with libunwind X-BeenThere: tarantool-patches@dev.tarantool.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Maxim Kokryashkin via Tarantool-patches Reply-To: Maxim Kokryashkin Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" `backtrace` fails to unwind the host stack in LuaJIT, since there is no frame pointers during the vm calls. Sometimes, those failures cause crashes. This commit replaces it with the libunwind-based unwinder, which makes use of additional runtime info to provide robust unwinding without any frame pointers. Also, this commit enables C API tests, which used to crash with `backtrace`. Part of tarantool/tarantool#781 --- Branch: https://github.com/tarantool/luajit/tree/fckxorg/sysprof-rc-full-ci CMakeLists.txt | 20 +++-- cmake/FindLibUnwind.cmake | 86 +++++++++++++++++++ cmake/GetLibUnwindVersion.cmake | 12 +++ src/CMakeLists.txt | 17 ++++ src/lj_sysprof.c | 51 ++++++++--- .../misclib-sysprof-capi.test.lua | 7 +- 6 files changed, 167 insertions(+), 26 deletions(-) create mode 100644 cmake/FindLibUnwind.cmake create mode 100644 cmake/GetLibUnwindVersion.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 659ee879..63db42c2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -89,12 +89,20 @@ if(CMAKE_LIBRARY_ARCHITECTURE) AppendFlags(TARGET_C_FLAGS -DLUA_MULTILIB='"lib/${CMAKE_LIBRARY_ARCHITECTURE}"') endif() -# Since the assembler part does NOT maintain a frame pointer, it's -# pointless to slow down the C part by not omitting it. Debugging, -# tracebacks and unwinding are not affected -- the assembler part -# has frame unwind information and GCC emits it where needed (x64) -# or with -g. -AppendFlags(CMAKE_C_FLAGS -fomit-frame-pointer -fno-stack-protector) +AppendFlags(CMAKE_C_FLAGS -fno-stack-protector) + +if(NOT LUAJIT_DISABLE_SYSPROF AND + CMAKE_SYSTEM_NAME STREQUAL "Linux" AND + ( + CMAKE_SYSTEM_PROCESSOR STREQUAL "i386" OR + CMAKE_SYSTEM_PROCESSOR STREQUAL "i686" OR + CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64" + ) + ) + AppendFlags(CMAKE_C_FLAGS -fno-omit-frame-pointer -fasynchronous-unwind-tables) +else() + AppendFlags(CMAKE_C_FLAGS -fomit-frame-pointer) +endif() # Redefined to benefit from expanding macros in gdb. set(CMAKE_C_FLAGS_DEBUG "-g -ggdb3") diff --git a/cmake/FindLibUnwind.cmake b/cmake/FindLibUnwind.cmake new file mode 100644 index 00000000..70221c12 --- /dev/null +++ b/cmake/FindLibUnwind.cmake @@ -0,0 +1,86 @@ +#[========================================================================[.rst: +FindLibUnwind +-------- +Finds the libunwind library. + +Result Variables +^^^^^^^^^^^^^^^^ +``LIBUNWIND_FOUND`` + True if the system has the libunwind library. +``LIBUNWIND_VERSION`` + The version of the libunwind library which was found. +``LIBUNWIND_INCLUDE_DIR`` + Include directory needed to use libunwind. +``LIBUNWIND_LIBRARIES`` + Libraries needed to link to libunwind. + +Cache Variables +^^^^^^^^^^^^^^^ +``LIBUNWIND_INCLUDE_DIR`` + The directory containing ``libunwind.h``. +``LIBUNWIND_LIBRARIES`` + The paths to the libunwind libraries. +#]========================================================================] + +include(FindPackageHandleStandardArgs) +include(GetLibUnwindVersion) + +find_package(PkgConfig QUIET) +pkg_check_modules(PC_LIBUNWIND QUIET libunwind) + +find_path(LIBUNWIND_INCLUDE_DIR libunwind.h ${PC_LIBUNWIND_INCLUDE_DIRS}) +if(LIBUNWIND_INCLUDE_DIR) + include_directories(${LIBUNWIND_INCLUDE_DIR}) +endif() + +if(BUILD_STATIC AND NOT APPLE) + set(LIBUNWIND_LIBRARY_NAME libunwind.a) +else() + # Only a dynamic version of libunwind is available on macOS: also, we + # should link against the umbrella framework `System` — otherwise `ld` will + # complain that it cannot link directly with libunwind.tbd. + set(LIBUNWIND_LIBRARY_NAME System unwind) +endif() +find_library(LIBUNWIND_LIBRARY NAMES ${LIBUNWIND_LIBRARY_NAME} + PATHS ${PC_LIBUNWIND_LIBRARY_DIRS}) + +if(APPLE) + set(LIBUNWIND_LIBRARIES ${LIBUNWIND_LIBRARY}) +else() + if(BUILD_STATIC) + set(LIBUNWIND_PLATFORM_LIBRARY_NAME + "libunwind-${CMAKE_SYSTEM_PROCESSOR}.a") + else() + set(LIBUNWIND_PLATFORM_LIBRARY_NAME + "unwind-${CMAKE_SYSTEM_PROCESSOR}") + endif() + find_library(LIBUNWIND_PLATFORM_LIBRARY ${LIBUNWIND_PLATFORM_LIBRARY_NAME} + ${PC_LIBUNWIND_LIBRARY_DIRS}) + set(LIBUNWIND_LIBRARIES ${LIBUNWIND_LIBRARY} ${LIBUNWIND_PLATFORM_LIBRARY}) +endif() + +if(BUILD_STATIC) + # libunwind could have been built with liblzma dependency: + # https://github.com/libunwind/libunwind/blob/4feb1152d1c4aaafbb2d504dbe34c6db5b6fe9f2/configure.ac#L302-L317 + pkg_check_modules(PC_LIBLZMA QUIET liblzma) + find_library(LIBLZMA_LIBRARY liblzma.a ${PC_LIBLZMA_LIBRARY_DIRS}) + if(NOT LIBLZMA_LIBRARY STREQUAL "LIBLZMA_LIBRARY-NOTFOUND") + message(STATUS "liblzma found") + set(LIBUNWIND_LIBRARIES ${LIBUNWIND_LIBRARIES} ${LIBLZMA_LIBRARY}) + endif() + # Ditto, + # https://github.com/libunwind/libunwind/blob/4feb1152d1c4aaafbb2d504dbe34c6db5b6fe9f2/configure.ac#L319-L334 + set(LIBUNWIND_LIBRARIES ${LIBUNWIND_LIBRARIES} ZLIB::ZLIB) +endif() + +if(PC_LIBUNWIND_VERSION) + set(LIBUNWIND_VERSION ${PC_LIBUNWIND_VERSION}) +else() + GetLibUnwindVersion(LIBUNWIND_VERSION) +endif() + +find_package_handle_standard_args(LibUnwind + VERSION_VAR LIBUNWIND_VERSION + REQUIRED_VARS LIBUNWIND_INCLUDE_DIR LIBUNWIND_LIBRARIES) + +mark_as_advanced(LIBUNWIND_INCLUDE_DIR LIBUNWIND_LIBRARIES) diff --git a/cmake/GetLibUnwindVersion.cmake b/cmake/GetLibUnwindVersion.cmake new file mode 100644 index 00000000..af833478 --- /dev/null +++ b/cmake/GetLibUnwindVersion.cmake @@ -0,0 +1,12 @@ +function(GetLibUnwindVersion _LIBUNWIND_VERSION) + set(_LIBUNWIND_VERSION_HEADER "${LIBUNWIND_INCLUDE_DIR}/libunwind-common.h") + if(LIBUNWIND_LIBRARY AND EXISTS ${_LIBUNWIND_VERSION_HEADER}) + file(READ ${_LIBUNWIND_VERSION_HEADER} + _LIBUNWIND_VERSION_HEADER_CONTENTS) + string(REGEX MATCH + "#define UNW_VERSION_MAJOR[ \t]+([0-9]+)\n#define UNW_VERSION_MINOR[ \t]+([0-9]+)" + _VERSION_REGEX "${_LIBUNWIND_VERSION_HEADER_CONTENTS}") + set(${_LIBUNWIND_VERSION} "${CMAKE_MATCH_1}.${CMAKE_MATCH_2}" + PARENT_SCOPE) + endif() +endfunction() diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 1a3f106a..4f9ad1de 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -283,6 +283,23 @@ add_dependencies(core_shared buildvm_output) list(APPEND TARGET_LIBS m) +if(NOT LUAJIT_DISABLE_SYSPROF AND + CMAKE_SYSTEM_NAME STREQUAL "Linux" AND + ( + CMAKE_SYSTEM_PROCESSOR STREQUAL "i386" OR + CMAKE_SYSTEM_PROCESSOR STREQUAL "i686" OR + CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64" + ) + ) + find_package(LibUnwind MODULE REQUIRED) + + if(NOT LIBUNWIND_FOUND) + message(FATAL_ERROR "Libunwind is required for build with sysprof") + else() + list(APPEND TARGET_LIBS ${LIBUNWIND_LIBRARIES}) + endif() +endif() + set(LIB_OBJECTS_STATIC $ $ diff --git a/src/lj_sysprof.c b/src/lj_sysprof.c index 55cea203..04509f68 100644 --- a/src/lj_sysprof.c +++ b/src/lj_sysprof.c @@ -26,7 +26,9 @@ #include #include -#include + +#define UNW_LOCAL_ONLY +#include /* ** Number of profiler frames we need to omit during stack @@ -85,6 +87,34 @@ static struct sysprof sysprof = {0}; /* --- Stream ------------------------------------------------------------- */ +static ssize_t collect_stack(void **buffer, int size) +{ + int frame_no = 0; + unw_context_t unw_ctx; + unw_cursor_t unw_cur; + + int rc = unw_getcontext(&unw_ctx); + if (rc != 0) + return -1; + + rc = unw_init_local(&unw_cur, &unw_ctx); + if (rc != 0) + return -1; + + for (; frame_no < size; ++frame_no) { + unw_word_t ip; + rc = unw_get_reg(&unw_cur, UNW_REG_IP, &ip); + if (rc != 0) + return -1; + + buffer[frame_no] = (void *)ip; + rc = unw_step(&unw_cur); + if (rc <= 0) + break; + } + return frame_no; +} + static const uint8_t ljp_header[] = {'l', 'j', 'p', LJP_FORMAT_VERSION, 0x0, 0x0, 0x0}; @@ -197,10 +227,11 @@ static void default_backtrace_host(void *(writer)(int frame_no, void *addr)) int max_depth = sp->opt.mode == LUAM_SYSPROF_LEAF ? SYSPROF_HANDLER_STACK_DEPTH + 1 : SYSPROF_BACKTRACE_FRAME_MAX; - const int depth = backtrace(backtrace_buf, max_depth); + const int depth = collect_stack(backtrace_buf, max_depth); int level; lua_assert(depth <= max_depth); + lua_assert(depth != -1); for (level = SYSPROF_HANDLER_STACK_DEPTH; level < depth; ++level) { if (!writer(level - SYSPROF_HANDLER_STACK_DEPTH + 1, backtrace_buf[level])) return; @@ -410,20 +441,12 @@ int lj_sysprof_set_backtracer(sp_backtracer backtracer) { if (sp->state != SPS_IDLE) return PROFILE_ERRUSE; - if (backtracer == NULL) { + + if (backtracer == NULL) sp->backtracer = default_backtrace_host; - /* - ** XXX: `backtrace` is not signal-safe, according to man, - ** because it is lazy loaded on the first call, which triggers - ** allocations. We need to call `backtrace` before starting profiling - ** to avoid lazy loading. - */ - void *dummy = NULL; - backtrace(&dummy, 1); - } - else { + else sp->backtracer = backtracer; - } + if (!is_unconfigured(sp)) { sp->state = SPS_IDLE; } diff --git a/test/tarantool-tests/misclib-sysprof-capi.test.lua b/test/tarantool-tests/misclib-sysprof-capi.test.lua index afb99cf2..1117ff73 100644 --- a/test/tarantool-tests/misclib-sysprof-capi.test.lua +++ b/test/tarantool-tests/misclib-sysprof-capi.test.lua @@ -15,15 +15,11 @@ local jit = require('jit') jit.off() local test = tap.test("clib-misc-sysprof") -test:plan(2) +test:plan(4) test:ok(testsysprof.base()) test:ok(testsysprof.validation()) --- FIXME: The following two tests are disabled because sometimes --- `backtrace` dynamically loads a platform-specific unwinder, which is --- not signal-safe. ---[[ local function lua_payload(n) if n <= 1 then return n @@ -56,5 +52,4 @@ jit.on() jit.flush() test:ok(testsysprof.profile_func(payload)) ---]] os.exit(test:check() and 0 or 1) -- 2.36.1