There is a program code

function GetStatck():TList<TCallStack>; var Level : integer; ar : Plua_Debug; CallStack : TCallStack; begin ..... Result := TList<TCallStack>.Create; ..... Level := 1; .... // Почему при попытке заполнить запись ar данными из стека, постоянно получаем 0 ? while lua_getstack(L, Level, @ar) = 1 do begin .... // СЮДА НЕ ПОПАДАЕМ .... CallStack := TCallStack.Create; .... Result.Add(CallStack); .... end; .... end; 

and the code of the main program, for example:

....

 procedure TFormMain.DoLua(); var ListCallStack: TList<TCallStack>; begin Lua.DoString('print "Hello World"'); .... // Тут хотелось бы увидеть результат, получить список данных ListCallStack := GetStatck(); .... end; 

How to get Traceback using the lua_getstack function?

Here is a simple example of using LUA to get TraceBack - for HelloWorld

 procedure TFormMain.DoLua(); ..... Lua.DoString( 'print "Hello World"' + #13#10 + 'print (debug.traceback());' 

Result:

hello stack traceback: [string "print" Hello World "..."]: 2: in main chunk

Help solve the problem, what's the problem? why let's say as an example executing such a code, the stack data is also visible ...:

 function low () mid() end function mid() high(5) end function high(val) print (debug.traceback()) end print ("Started") low() print ("Completed") 

I use Delphi 10 and wrapper for LUA (VerySimple) - http://blog.spreendigital.de/2015/02/18/verysimple-lua-2-0-a-cross-platform-lua-5-3-0-wrapper -for-delphi-xe5-xe7 /

  • What do you want to see on the stack by print "Hello World" ? The stack, in general, stores local variables of functions, return addresses from them, and what you yourself put there. What of this is present in your first program? In the second, it is clear that at the time of calling the debug.traceback() there will be three return addresses. - Alekcvp
  • Hello World wrote as an example, you can run the LUA code below with no questions .... I would like to receive debug.traceback ... using the function lua_getstack (L, Level, @ar) - Indy Putful
  • And if you do Level := 0; ? - zed
  • one
    Or maybe there is nothing in the stack? - kot-da-vinci
  • 2
    In the description of lua_getstack it says: “it receives information about the runtime stack,” that is, as I understand it, it is intended to be called in hooks. You try to call it after the end of the program, but this is not runtime. - Alekcvp

1 answer 1

Thanks to everyone who helped in the comments! Especially to the colleague Alekcvp , who pointed out directly in the comments, to an error in the implementation I specified. The problem is that the function function GetStatck () is needed : TList; , call in implemented hooks to get the stack, let's say:

 procedure LuaHook(L: lua_State; ar: Plua_Debug); cdecl; var ListCallStack : TList<TCallStack> begin // делаем проверку на BREAKPOINT... // если они удовлетворяют нашему условию то получаем стэк ... ListCallStack := GetStatck(); ... end; 

All this must be done before executing the script with the command:

 ... lua_sethook(L, LuaHook, LUA_MASKLINE, 0); luaL_dostring(L,НАШ_СКРИПТ); ...