From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from localhost (localhost [127.0.0.1]) by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTP id 307962B4F4 for ; Thu, 4 Oct 2018 03:44:21 -0400 (EDT) Received: from turing.freelists.org ([127.0.0.1]) by localhost (turing.freelists.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 4WI_n7AZmXQY for ; Thu, 4 Oct 2018 03:44:21 -0400 (EDT) Received: from smtp57.i.mail.ru (smtp57.i.mail.ru [217.69.128.37]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTPS id E0F802B46E for ; Thu, 4 Oct 2018 03:44:20 -0400 (EDT) From: Georgy Kirichenko Subject: [tarantool-patches] [PATCH 2/3] Proper unwind for currently executing fiber Date: Thu, 4 Oct 2018 10:44:16 +0300 Message-Id: <3bf7292e6ae0c5b7b892c204457c630c73bd8108.1538638419.git.georgy@tarantool.org> In-Reply-To: <20180926173625.GO30528@chai> References: <7b844650e2e0799813e82b32ea2d637982bc3395.1537535602.git.georgy@tarantool.org> <20180925233402.GJ3137@chai> <14423179.bJO8PXla9P@home.lan> <20180926173625.GO30528@chai> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: tarantool-patches-bounce@freelists.org Errors-to: tarantool-patches-bounce@freelists.org Reply-To: tarantool-patches@freelists.org List-help: List-unsubscribe: List-software: Ecartis version 1.0.0 List-Id: tarantool-patches List-subscribe: List-owner: List-post: List-archive: To: tarantool-patches@freelists.org Cc: Georgy Kirichenko Each yielded fiber has a preserved coro state stored in a corresponding variable however an executing fiber has a volatile state placed in CPU registers (stack pointer, instruction pointer and non-volatile registers) and corresponding context-storing variable value is invalid. For already yielded fiber we have to use a special asm-written handler to make a temporary switch to the preserved state and capture executing context what is not needed for executing fiber. After the patch for the executing fiber NULL is passed to the backtrace function as coro context and then backtrace function could decide should it use special context-switching handler or might just use unw_getcontext from the unwind library. --- src/backtrace.cc | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/backtrace.cc b/src/backtrace.cc index 2512bc045..d59fd5879 100644 --- a/src/backtrace.cc +++ b/src/backtrace.cc @@ -363,12 +363,32 @@ __asm__ volatile( #endif } +/* + * Call `cb' callback for each `coro_ctx' contained frame or the current + * executed coroutine if `coro_ctx' is NULL. A coro_context is a structure + * created on each coroutine yield to store execution context so for an on-CPU + * coroutine there is no valid coro_context could be defined and NULL is + * passed. +*/ void backtrace_foreach(backtrace_cb cb, coro_context *coro_ctx, void *cb_ctx) { unw_cursor_t unw_cur; unw_context_t unw_ctx; - coro_unwcontext(&unw_ctx, coro_ctx); + if (coro_ctx == NULL) { + /* + * Current executing coroutine and simple unw_getcontext + * should function. + */ + unw_getcontext(&unw_ctx); + } else { + /* + * Execution context is stored in the coro_ctx + * so use special context-switching handler to + * capture an unwind context. + */ + coro_unwcontext(&unw_ctx, coro_ctx); + } unw_init_local(&unw_cur, &unw_ctx); int frame_no = 0; unw_word_t sp = 0, old_sp = 0, ip, offset; -- 2.19.0