From f2d29c22f86a88bfa0d0cbd9141e6ae60f840cb0 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Sat, 5 Mar 2022 22:03:11 -0800 Subject: [PATCH] a simple hack to make caller apparent Teliva isn't yet smart enough to know the caller of an indirect function where the function being called goes through a local variable. I'd expected fixing this to be a long death march. However, there's a shockingly easy fix: just make every indirect call go through an additional direct function call. My policy for zet.tlv was that function 'main' could open any file. This stopped working since I introduced spawn_main. But with this commit it's working again. I can also drop all my special-casing of 'main' since it's now a regular Lua call. We still can't rely on the caller of an indirect call. That affects start_reading and start_writing, which really need to be part of the framework. --- src/task.lua | 8 +++++++- src/teliva.c | 6 +----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/task.lua b/src/task.lua index 3b00ef6..dbe788f 100644 --- a/src/task.lua +++ b/src/task.lua @@ -382,7 +382,7 @@ _M.NOP = NOP -- Specific to Teliva function spawn_main() - task.spawn(main) + task.spawn(call_main) task.scheduler() assert(false, "Teliva's scheduler ran out of work; this shouldn't happen.\n".. "Either a channel is blocked forever or you're reading past\n".. @@ -391,6 +391,12 @@ function spawn_main() curses.getch() end +-- This function exists only to make the call to 'main' visible to Teliva. +-- Teliva can't yet recognize the caller of indirect calls. +function call_main() + main() +end + ---------------------------------------------------------------------------- ---------------------------------------------------------------------------- -- Tests diff --git a/src/teliva.c b/src/teliva.c index 2e9c9d9..52c1ae3 100644 --- a/src/teliva.c +++ b/src/teliva.c @@ -322,7 +322,6 @@ void save_caller(lua_State* L, const char* name, int call_graph_depth) { lua_getstack(L, 1, &ar); lua_getinfo(L, "n", &ar); if (ar.name) save_caller_as(L, name, ar.name); - else if (call_graph_depth == 2) save_caller_as(L, name, "main"); // the way Teliva calls `main` messes with debug info } char* caller(lua_State* L) { @@ -396,9 +395,6 @@ restart: clear(); luaL_newmetatable(L, "__teliva_call_graph_depth"); int cgt = lua_gettop(L); - // special-case: we don't instrument the call to main, but it's always at depth 1 - lua_pushinteger(L, 1); - lua_setfield(L, cgt, "main"); // segment definitions by depth lua_getglobal(L, "teliva_program"); int history_array = lua_gettop(L); @@ -514,7 +510,7 @@ restart: y += 2; mvprintw(y, 0, "functions: "); y++; - for (int depth = 1; ; ++depth) { + for (int depth = /*ignore call_main*/2; ; ++depth) { mvaddstr(y, 0, " "); bool drew_anything = false; index_within_level = 0;