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 7CF166EC55; Wed, 14 Jul 2021 14:15:05 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 7CF166EC55 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1626261305; bh=cSV5fp7zhFTO2dW6Cayu815Ybh2Kspx4vbsyKtSXPXY=; h=To:Cc:Date:In-Reply-To:References:Subject:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=ZvwOg3U4c97lWThWcd59C2boanEAERt52SAAe7tkVWyHAKvhlFT+2la9rklkKEo33 VNR2gN+lgqzW1BprWVDaM5fi9qDxyB+RcTTII40Knfw/hEqliR49V1z7wd/VA6DZlD J8uEgy0mrVTDP0TQxAikaqm5AT4xi/PXPOHtWj/w= Received: from smtp59.i.mail.ru (smtp59.i.mail.ru [217.69.128.39]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id 264CF6EC5C for ; Wed, 14 Jul 2021 14:13:37 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 264CF6EC5C Received: by smtp59.i.mail.ru with esmtpa (envelope-from ) id 1m3cpc-0001Jt-BT; Wed, 14 Jul 2021 14:13:36 +0300 To: gorcunov@tarantool.org, alyapunov@tarantool.org Cc: tarantool-patches@dev.tarantool.org, Egor Elchinov Date: Wed, 14 Jul 2021 14:12:51 +0300 Message-Id: X-Mailer: git-send-email 2.31.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-7564579A: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD97BB0EF39AD2B33D5CFD6F66580F08A9E8DA110284E1A7113182A05F5380850402A1E2D413E5C3BDE03F8109E39F3A5F1C5225D1E839A7B6B13141682DE79E49E X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7AEA4A6B3AFC9B957C2099A533E45F2D0395957E7521B51C2CFCAF695D4D8E9FCEA1F7E6F0F101C6778DA827A17800CE753B96F70D0F8F35FEA1F7E6F0F101C6723150C8DA25C47586E58E00D9D99D84E1BDDB23E98D2D38BBCA57AF85F7723F28E7DA39B9A566B32E1E462CCA334FA00CC7F00164DA146DAFE8445B8C89999728AA50765F7900637F6B57BC7E64490618DEB871D839B7333395957E7521B51C2DFABB839C843B9C08941B15DA834481F8AA50765F7900637F924B32C592EA89F389733CBF5DBD5E9B5C8C57E37DE458BD9DD9810294C998ED8FC6C240DEA76428AA50765F7900637A9329C8D8F988D4ED81D268191BDAD3DBD4B6F7A4D31EC0BEA7A3FFF5B025636D81D268191BDAD3D78DA827A17800CE70180C25061743639EC76A7562686271EEC990983EF5C03292E808ACE2090B5E14AD6D5ED66289B5259CC434672EE63711DD303D21008E298D5E8D9A59859A8B6B372FE9A2E580EFC725E5C173C3A84C328505141BAE32B3435872C767BF85DA2F004C90652538430E4A6367B16DE6309 X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975CE68746B1F2AB10C68A6FD7FCC63516576F5961B0EBCD011C9C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EF309DFB797F6729CB699F904B3F4130E343918A1A30D5E7FCCB5012B2E24CD356 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D34951738D4D62E58A49D43D9775C50B9AD5D4A11E1BA619B0C2C6165225805B16C09A3A514B15AD2141D7E09C32AA3244CAD7370223249CD88344C181A342CE871F165894D92D62706927AC6DF5659F194 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojDdSFIg49M1S1cAyimssZ4g== X-Mailru-Sender: EFA0F3A8419EF2166B053C73E674FFD5D022A552E9BBBF67A91C4C582D642D08E2527C969975515C67F54F2D6EFFC80BC77752E0C033A69E17841C44D9B5D58765F2F89A5AFDB6F16C18EFA0BB12DBB0 X-Mras: Ok Subject: [Tarantool-patches] [PATCH v3 3/4] fiber: refactor lua backtrace routines 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: Egor Elchinov via Tarantool-patches Reply-To: eelchinov@tarantool.org Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" From: Egor Elchinov Lua backtrace with callback moved to new routine `lua_backtrace_cb()` similar to the `backtrace_cb()` for C backtrace. Needed for: #4002 --- src/lua/fiber.c | 170 ++++++++++++++++++++++-------------------------- 1 file changed, 76 insertions(+), 94 deletions(-) diff --git a/src/lua/fiber.c b/src/lua/fiber.c index fbf68c561..59d72fc52 100644 --- a/src/lua/fiber.c +++ b/src/lua/fiber.c @@ -205,14 +205,17 @@ struct lua_parent_tb_ctx { int tb_frame; }; +/** Lua backtrace callback type */ +typedef int (lua_backtrace_cb)(struct lua_Debug *ar, int frame_no, void *cb_ctx); + #ifdef ENABLE_BACKTRACE static void -dump_lua_frame(struct lua_State *L, lua_Debug *ar, int tb_frame) +dump_lua_frame(struct lua_State *L, const char *name, const char *src, int line, + int tb_frame) { char buf[512]; snprintf(buf, sizeof(buf), "%s in %s at line %i", - ar->name != NULL ? ar->name : "(unnamed)", - ar->source, ar->currentline); + name != NULL ? name : "(unnamed)", src, line); buf[sizeof(buf) - 1] = 0; lua_pushnumber(L, tb_frame); lua_newtable(L); @@ -222,21 +225,67 @@ dump_lua_frame(struct lua_State *L, lua_Debug *ar, int tb_frame) lua_settable(L, -3); } +static int +dump_lua_frame_cb(lua_Debug *ar, int tb_frame, void *cb_ctx) +{ + (void) tb_frame; + struct lua_fiber_tb_ctx *tb_ctx = (struct lua_fiber_tb_ctx *)cb_ctx; + + tb_ctx->tb_frame++; + dump_lua_frame(tb_ctx->L, ar->name, ar->source, ar->currentline, + tb_ctx->tb_frame); + + return 0; +} + +static int +save_lua_frame_cb(lua_Debug *ar, int tb_frame, void *cb_ctx) +{ + struct parent_bt_lua *bt = (struct parent_bt_lua *)cb_ctx; + + if (tb_frame >= PARENT_BT_LUA_LEN_MAX) + return 1; + + bt->cnt++; + bt->lines[tb_frame] = ar->currentline; + strncpy(bt->names[tb_frame], ar->name == NULL ? "(unnamed)" : ar->name, + PARENT_BT_LUA_NAME_MAX - 1); + bt->names[tb_frame][PARENT_BT_LUA_NAME_MAX - 1] = 0; + strncpy(bt->sources[tb_frame], ar->source, + PARENT_BT_LUA_NAME_MAX - 1); + bt->sources[tb_frame][PARENT_BT_LUA_NAME_MAX - 1] = 0; + + return 0; +} + static void -dump_parent_lua_frame(struct lua_State *L, const char *name, const char *src, - int currentline, int tb_frame) +lua_backtrace_foreach(struct lua_State *L, lua_backtrace_cb cb, void *cb_ctx) { - char buf[512]; - snprintf(buf, sizeof(buf), "%s in %s at line %i", - name != NULL ? name : "(unnamed)", - src, currentline); - buf[sizeof(buf) - 1] = 0; - lua_pushnumber(L, tb_frame); - lua_newtable(L); - lua_pushstring(L, "L"); - lua_pushstring(L, buf); - lua_settable(L, -3); - lua_settable(L, -3); + int lua_frame = 0, tb_frame = 0; + lua_Debug ar; + while (lua_getstack(L, lua_frame, &ar) > 0) { + /* Skip all following C-frames. */ + lua_getinfo(L, "Sln", &ar); + if (*ar.what != 'C') + break; + if (ar.name != NULL) { + /* Dump frame if it is a C built-in call. */ + if (cb(&ar, tb_frame, cb_ctx) != 0) + return; + tb_frame++; + } + lua_frame++; + } + while (lua_getstack(L, lua_frame, &ar) > 0) { + /* Trace Lua frame. */ + lua_getinfo(L, "Sln", &ar); + if (*ar.what == 'C') + break; + if (cb(&ar, tb_frame, cb_ctx) != 0) + return; + tb_frame++; + lua_frame++; + } } static int @@ -249,31 +298,9 @@ fiber_backtrace_cb(int frameno, void *frameret, const char *func, size_t offset, * https://github.com/tarantool/tarantool/issues/5326 * will not resolved, but is possible afterwards. */ - if (func != NULL && strstr(func, "lj_BC_FUNCC") == func) { + if (func != NULL && tb_ctx->R && strstr(func, "lj_BC_FUNCC") == func) { /* We are in the LUA vm. */ - lua_Debug ar; - while (tb_ctx->R && lua_getstack(tb_ctx->R, tb_ctx->lua_frame, &ar) > 0) { - /* Skip all following C-frames. */ - lua_getinfo(tb_ctx->R, "Sln", &ar); - if (*ar.what != 'C') - break; - if (ar.name != NULL) { - /* Dump frame if it is a C built-in call. */ - tb_ctx->tb_frame++; - dump_lua_frame(L, &ar, tb_ctx->tb_frame); - } - tb_ctx->lua_frame++; - } - while (tb_ctx->R && lua_getstack(tb_ctx->R, tb_ctx->lua_frame, &ar) > 0) { - /* Trace Lua frame. */ - 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++; - } + lua_backtrace_foreach(tb_ctx->R, dump_lua_frame_cb, tb_ctx); } char buf[512]; int l = snprintf(buf, sizeof(buf), "#%-2d %p in ", @@ -310,10 +337,10 @@ fiber_parent_backtrace_cb(int frameno, void *frameret, const char *func, /* We are in the LUA vm. */ while (lua_frame < bt->cnt) { tb_ctx->tb_frame++; - dump_parent_lua_frame(L, bt->names[lua_frame], - bt->sources[lua_frame], - bt->lines[lua_frame], - tb_ctx->tb_frame); + dump_lua_frame(L, bt->names[lua_frame], + bt->sources[lua_frame], + bt->lines[lua_frame], + tb_ctx->tb_frame); lua_frame++; } } @@ -338,60 +365,15 @@ fiber_parent_backtrace_cb(int frameno, void *frameret, const char *func, static int fiber_parent_bt_init(struct fiber *f, struct lua_State *L) { - int lua_frame = 0, tb_frame = 0; - lua_Debug ar; struct parent_bt_lua *bt = NULL; - - bt = (struct parent_bt_lua *)malloc(sizeof(*bt)); + bt = (struct parent_bt_lua *)calloc(1, sizeof(*bt)); if (bt == NULL){ - diag_set(OutOfMemory, sizeof(*bt), "malloc", "bt"); + diag_set(OutOfMemory, sizeof(*bt), "calloc", "bt"); return 1; } - while (tb_frame < PARENT_BT_LUA_LEN_MAX && - lua_getstack(L, lua_frame, &ar) > 0) { - /* Skip all following C-frames. */ - lua_getinfo(L, "Sln", &ar); - if (*ar.what != 'C') - break; - bt->names[tb_frame][0] = 0; - bt->sources[tb_frame][0] = 0; - if (ar.name != NULL) { - /* Dump frame if it is a C built-in call. */ - bt->lines[tb_frame] = ar.currentline; - strncpy(bt->names[tb_frame], ar.name, - PARENT_BT_LUA_NAME_MAX - 1); - bt->names[tb_frame][PARENT_BT_LUA_NAME_MAX - 1] = 0; - strncpy(bt->sources[tb_frame], ar.source, - PARENT_BT_LUA_NAME_MAX - 1); - bt->sources[tb_frame][PARENT_BT_LUA_NAME_MAX - 1] = 0; - tb_frame++; - } - lua_frame++; - } - while (tb_frame < PARENT_BT_LUA_LEN_MAX && - lua_getstack(L, lua_frame, &ar) > 0) { - /* Trace Lua frame. */ - lua_getinfo(L, "Sln", &ar); - if (*ar.what == 'C') - break; - bt->lines[tb_frame] = ar.currentline; - if (ar.name != NULL) { - strncpy(bt->names[tb_frame], ar.name, - PARENT_BT_LUA_NAME_MAX - 1); - } else { - strncpy(bt->names[tb_frame], "(unnamed)", - PARENT_BT_LUA_NAME_MAX - 1); - } - bt->names[tb_frame][PARENT_BT_LUA_NAME_MAX - 1] = 0; - strncpy(bt->sources[tb_frame], ar.source, - PARENT_BT_LUA_NAME_MAX - 1); - bt->sources[tb_frame][PARENT_BT_LUA_NAME_MAX - 1] = 0; - tb_frame++; - lua_frame++; - } - - bt->cnt = tb_frame; + bt->cnt = 0; + lua_backtrace_foreach(L, save_lua_frame_cb, bt); f->storage.lua.parent_bt = bt; return 0; @@ -1117,7 +1099,7 @@ static const struct luaL_Reg fiberlib[] = { {"top_enable", lbox_fiber_top_enable}, {"top_disable", lbox_fiber_top_disable}, #endif /* ENABLE_FIBER_TOP */ -#if ENABLE_BACKTRACE +#ifdef ENABLE_BACKTRACE {"parent_bt_enable", lbox_fiber_parent_bt_enable}, {"parent_bt_disable", lbox_fiber_parent_bt_disable}, #endif /* ENABLE_BACKTRACE */ -- 2.31.1