You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Interacting with debugger commands at runtime from C++ #87
First, I want to thank you all for the incredible work on this project—it's outstanding!
I followed the guide for embedding debugger.lua into C++ code and generated the necessary header file to define the setup and configuration. Currently, I’d like to interact with the debugger at runtime directly from C++, without requiring user input (e.g., typing "s" for step and pressing ENTER). I’ve tried using the read and write handlers, but I still end up in scenarios where the debugger shell opens, and I have to manually continue with "c".
Could anyone provide guidance on the best approach for handling debugger input and output in a Lua state created from C++? My goal is to redirect debugger I/O through JSON packets.
Hrm. Can you share some of your code maybe? While not a particularly interesting example, I do use this functionality to run the automated tests. There's not really much to it, it just just uses the overridden I/O to send commands from a list and check for expected output: https://github.com/slembcke/debugger.lua/blob/master/test/test_util.lua#L32
After this I modified the default functions to read and write using JSON encoding:
localfunctiondbg_write(str)
print("dbg_write called with input: ", str) -- Debug print to see the inputlocaloutput=json.encode({ result=str })
print("Encoded JSON: ", output) -- Debug print to check the JSON encodingclient:send(output.."\n")
end
and
localfunctiondbg_read(prompt)
print("dbg_read called with prompt: ", prompt) -- Debug print to see the input promptdbg.write(prompt)
localinput=client:receive()
print("Received input: ", input) -- Debug print to see the raw input received from the clientlocalsuccess, command=pcall(json.decode, input)
ifsuccessthenprint("Decoded JSON: ", command) -- Debug print to check the decoded JSON objectreturncommandelsedbg_writeln(COLOR_RED.."Invalid JSON input"..COLOR_RESET)
print("JSON decoding failed.") -- Debug print to indicate failurereturnnilendend
What I want to do now is match the already implemented methods (like step and locals) given the JSON data, and I was thinking on something like this:
(...alldebugger.luacode)
-- Function to process and match commandslocalfunctionprocess_command(command)
-- Check if the command is a table with a 'cmd' attributeiftype(command) =="table" andcommand.cmdthenlocalfunc, arg=match_command(command.cmd) -- Match the commandiffuncthenreturnfunc(arg) -- Execute the command and return the resultelsereturn"Unknown command" -- Return an error message for unrecognized commandsend-- If the command is a string, directly match and process itelseiftype(command) =="string" thenlocalfunc, arg=match_command(command) -- Match the commandiffuncthenreturnfunc(arg) -- Execute the command and return the resultelsereturn"Unknown command" -- Return an error message for unrecognized commandsendendend-- Function to read and process commands in the main looplocalfunctionmain_loop()
whiletruedo-- Read command from the clientlocalcommand=dbg_read(GREEN_CARET)
ifcommandthen-- Debug: print received command for inspectionprint("Received command: ", command)
-- Process the command and get the resultlocalresult=process_command(command)
-- Debug: print the result for inspectionprint("Result of command: ", result)
-- Write the result back to the clientdbg_writeln(result)
endendend-- Start the main loop to continuously read commands and process themmain_loop()
returndbg
I came to this conclusion after having not a little difficulty integrating c++-side interaction with the debugger shell in an automated way, so I moved to the Lua side. I would like to be able to have the debugger always listening for JSON commands without the need to interact with the shell.
Activity
slembcke commentedon Nov 13, 2024
Hrm. Can you share some of your code maybe? While not a particularly interesting example, I do use this functionality to run the automated tests. There's not really much to it, it just just uses the overridden I/O to send commands from a list and check for expected output:
https://github.com/slembcke/debugger.lua/blob/master/test/test_util.lua#L32
gsisinna commentedon Nov 14, 2024
Yes thank you, I'll try to give an idea of what I would like to achieve.
I tried modifying the
debugger.luaa bit to receive JSON packets as input instead of just writing from terminal and then pressing ENTER:After this I modified the default functions to read and write using JSON encoding:
and
What I want to do now is match the already implemented methods (like step and locals) given the JSON data, and I was thinking on something like this:
I came to this conclusion after having not a little difficulty integrating c++-side interaction with the debugger shell in an automated way, so I moved to the Lua side. I would like to be able to have the debugger always listening for JSON commands without the need to interact with the shell.
Thanks again!