Is there a way to determine the parameter values ​​passed to the Lua function from a call to the debugger that handles the call event?

I wrote a Lua script that uses the debug API ( debug.sethook ) to intercept calls and returns. I use it to print a beautifully formatted call tree, which is very useful for debugging.

In the hook handler function, I increment or decrement the indentLevel global variable indentLevel on whether the event is a “call” or “return” (or “tail return”). Then I use debug.getinfo to get information about the calling function and upload it to stdout at the current indent level.

For call events, I would also like to print the values ​​of the parameters that were passed to a particular call. Presumably, I could do this in the implementation of the C / C ++ hook handler function by looking at the Lua stack.

Does anyone know if there is a way to determine parameter values ​​from a debug handler function in Lua?

+4
source share
1 answer

I think you need debug.getlocal . From the manual:

This function returns the name and value of a local variable with the index of a local function at the level level from the stack. (The first parameter or local variable has index 1, etc., until the last active local variable.) The function returns nil if there is no local variable with the given index and causes an error when called with a level outside the allowable range. (You can call debug.getinfo to check if the level is valid.)

Variable names starting with '(' (open parentheses) are internal variables (loop control variables, temporary and local C functions).

I have not tried this myself, but it looks like it should reveal what you need to know. One thing that does not dig me out of the documentation is how to determine how many parameters were actually passed, but this may be among the trivialities revealed by debug.getinfo .

+1
source

All Articles