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 7889D2B94F for ; Tue, 25 Sep 2018 19:38:50 -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 bfcD-DhhW0GZ for ; Tue, 25 Sep 2018 19:38:50 -0400 (EDT) Received: from smtp16.mail.ru (smtp16.mail.ru [94.100.176.153]) (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 2B2692B8BC for ; Tue, 25 Sep 2018 19:38:50 -0400 (EDT) Date: Wed, 26 Sep 2018 02:38:47 +0300 From: Konstantin Osipov Subject: [tarantool-patches] Re: [PATCH 3/3] Show names of Lua functions in backtraces Message-ID: <20180925233847.GK3137@chai> References: <76f2b7de46d4a1d55a3d2b7b39eb87172749eed6.1537535602.git.georgy@tarantool.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <76f2b7de46d4a1d55a3d2b7b39eb87172749eed6.1537535602.git.georgy@tarantool.org> 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 * Georgy Kirichenko [18/09/22 00:21]: > Trace corresponding Lua state as well as normal C stack frames while > fiber backtracing. This might be useful for debugging purposes. > > Fixes: #3538 > --- > src/lua/fiber.c | 86 +++++++++++++++++++++++++++++++++++------ > test/app/fiber.result | 33 ++++++++++++++++ > test/app/fiber.test.lua | 12 ++++++ > 3 files changed, 120 insertions(+), 11 deletions(-) > > diff --git a/src/lua/fiber.c b/src/lua/fiber.c > index 147add89b..289d240f6 100644 > --- a/src/lua/fiber.c > +++ b/src/lua/fiber.c > @@ -178,20 +178,79 @@ lbox_fiber_id(struct lua_State *L) > return 1; > } > > +/** > + * Lua fiber traceback context. > + */ > +struct lua_fiber_tb_ctx { > + /* Lua stack to push values. */ > + struct lua_State *L; > + /* Lua stack to trace. */ > + struct lua_State *R; > + /* Current Lua frame. */ > + int lua_frame; > + /* Count of traced frames (both C and Lua). */ > + int tb_frame; > +}; > + > #ifdef ENABLE_BACKTRACE > -static int > -fiber_backtrace_cb(int frameno, void *frameret, const char *func, size_t offset, void *cb_ctx) > +static void > +dump_lua_frame(struct lua_State *L, lua_Debug *ar, int tb_frame, int lua_frame) > { > char buf[512]; > - int l = snprintf(buf, sizeof(buf), "#%-2d %p in ", frameno, frameret); > - if (func) > - snprintf(buf + l, sizeof(buf) - l, "%s+%zu", func, offset); > - else > - snprintf(buf + l, sizeof(buf) - l, "?"); > - struct lua_State *L = (struct lua_State*)cb_ctx; > - lua_pushnumber(L, frameno + 1); > + snprintf(buf, sizeof(buf), "LUA:#%d %s in %s at line %i", > + lua_frame + 1, > + ar->name != NULL ? ar->name : "(unnamed)", > + ar->source, ar->currentline); I have only a stupid comment, and it's about formatting. I think we should not change the formatting of C frames, it should be left without a prefix. > + lua_pushnumber(L, tb_frame); > lua_pushstring(L, buf); > lua_settable(L, -3); > +} > + > +static int > +fiber_backtrace_cb(int frameno, void *frameret, const char *func, size_t offset, void *cb_ctx) > +{ > + struct lua_fiber_tb_ctx *tb_ctx = (struct lua_fiber_tb_ctx *)cb_ctx; > + struct lua_State *L = tb_ctx->L; > + if (strstr(func, "lj_BC_FUNCC") == func) { > + // LUA code. > + lua_Debug ar; > + while (tb_ctx->R && lua_getstack(tb_ctx->R, tb_ctx->lua_frame, &ar) > 0) { > + // Skip all C-frames. Please use C style comments. > + lua_getinfo(tb_ctx->R, "Sln", &ar); > + if (*ar.what != 'C') > + break; > + if (ar.name != NULL) { > + // Dump frame if it is a built-in call > + tb_ctx->tb_frame++; > + dump_lua_frame(L, &ar, tb_ctx->tb_frame, > + tb_ctx->lua_frame); > + } > + tb_ctx->lua_frame++; > + } > + while (tb_ctx->R && lua_getstack(tb_ctx->R, tb_ctx->lua_frame, &ar) > 0) { > + // Trace Lua frames. > + lua_getinfo(tb_ctx->R, "Sln", &ar); > + if (*ar.what == 'C') { > + break; > + } > + tb_ctx->tb_frame++; > + dump_lua_frame(L, &ar, tb_ctx->tb_frame, > + tb_ctx->lua_frame); > + tb_ctx->lua_frame++; > + } > + } else { > + char buf[512]; > + int l = snprintf(buf, sizeof(buf), "C:#%d %p in ", > + frameno + 1, frameret); > + if (func) > + snprintf(buf + l, sizeof(buf) - l, "%s+%zu", func, offset); > + else > + snprintf(buf + l, sizeof(buf) - l, "?"); > + tb_ctx->tb_frame++; > + lua_pushnumber(L, tb_ctx->tb_frame); > + lua_pushstring(L, buf); > + lua_settable(L, -3); > + } > return 0; > } > #endif > @@ -229,10 +288,15 @@ lbox_fiber_statof(struct fiber *f, void *cb_ctx, bool backtrace) > > if (backtrace) { > #ifdef ENABLE_BACKTRACE > + struct lua_fiber_tb_ctx tb_ctx; > + tb_ctx.L = L; > + tb_ctx.R = f->storage.lua.stack; > + tb_ctx.lua_frame = 0; > + tb_ctx.tb_frame = 0; > lua_pushstring(L, "backtrace"); > lua_newtable(L); > - if (f != fiber()) > - backtrace_foreach(fiber_backtrace_cb, &f->ctx, L); > + backtrace_foreach(fiber_backtrace_cb, > + f != fiber() ? &f->ctx : NULL, &tb_ctx); > lua_settable(L, -3); > #endif /* ENABLE_BACKTRACE */ > } -- Konstantin Osipov, Moscow, Russia, +7 903 626 22 32 http://tarantool.io - www.twitter.com/kostja_osipov