73 points · by fercircularbuf · 2y ago · 35 comments
I started this project to solve this problem by giving a simple gui to a few core features of lldb through the lldb api. Experimenting with changing colors, where blocks in the level go, how tall the player is, gravity is all now just a simple toggle or text field edit. The variables are modifiable while the program is running.
The project is still in its early stages, and so far only targets macOS.
In my case, I went to implement this for Windows, but bit more advanced with inline scripting support. Basically, you insert a snippet anywhere in the code that can call various functions and one of them is `view()` which will show all locals (or a chosen object out of scope) in a Visual Studio window (it's a VS extension [0], website [1]). Calling it again will update the view with the new state. The scripting language underneath is Angelscript and it understands symbols inside your entire project (e.g you could do `view(Namespace::g_variable)` or even call functions from the program etc. The snippet hook is then JIT'ed into the target function by relocating it using info from the PDB. This took me ~1y to implement, and it's still a research phase project.
[0] https://marketplace.visualstudio.com/items?itemName=donadigo...
[1] https://d-0.dev/
The best experience I've had with this kind of thing has been Godot. Not only do you get to tweak values at runtime, you can also change the code at runtime and see changes reflected live. It's so awesome being able to hit a breakpoint in a running game, step through some code, change the code, and then continue stepping to see the changes.
This is generally a tool you would likely need to build into a game regardless for exactly the reason you mentioned, rebuilding to change gameplay variables isn't exactly a great experience.
I personally just use a simple terminal that I can open and close to adjust variables but eith hoe easy imgui is I don't see any reason to not do a gui window for that.
Optimising IR without dropping debug info is difficult. I don't believe it to be intractable, some of the Sony developers are active in improving this for LLVM. Debug info is roughly a bunch of side tables of data from which one can recompute values which were in the original program and are not in the executing one. I would speculate that a compilation mode which treated any degradation of that as a miscompile could be done with minor to no runtime cost, at serious engineering cost.
Perhaps easier would be to restructure the program so that specific variables are always available to the debugger. Deliberately hoist them to escaped global variables, roughly.
The other interesting line of attack here is a JIT. The students probably don't change all the parameters simultaneously. Call graph walk + incremental compilation of parts affected by a given variable is probably tractable. A sibling mentioned hot code replacement, that's a similar sort of hack (you block function inlining, pad functions with some nops, then overwrite in place if the new definition fits in the space).
Thanks for the link, interesting stuff.
There are also plenty of game devs expressing the idea of "never use STL, because that makes performance unusable in debug builds". Which implies that many of them are not completely against debug builds (for debug/development purpose).
The problem of variables being optimized away is mostly for local variables. This tool will most likely used on global/long lifetime objects representing overall game state and configuration, instead of temporary variables while stepping through a function. (used)Global variables and class properties are unlikely to be optimized away or mangled in away that makes them hard to inspect.
This was created to help students while learning. In those kinds of projects performance is somewhat to be less of an issue, unless you make a mistake which would ruin performance regardless of optimization level.
Well, not for the shipping/release/prod builds, sure. But for dev? I dunno, from my vantage would rather keep it, if not for debug/inspect then just for compile speeds and ensuring a certain baseline perf even-without-optimizations... (but then, I'm not triple-A or have any games to my record =)
<https://blog.voxagon.se/2018/03/13/hot-reloading-hardcoded-p...>
In other words, rather than having a graphical interface for tweaking values at runtime, you interface with it the same way you normally would—by changing the constants in the original source file, but the difference between the ordinary stop-and-recompile way of doing it is that the changes get picked up live, so there is no stop-and-recompile. (This isn't exactly novel; webdevs have had hot code reloading to various degrees for a while, but the conventional static languages folks have generally needed to rely on support in sophisticated IDEs to get the same thing, and many end up going without.)
For those who want the graphical control panel, I suppose you could do the complement: when a value is tweaked in-game, it could overwrite the existing value in the source file so you don't have to tweak in-game and then manually copy them out to source once you've landed on the values you like.
Not really, building and using shared libraries to do this has been trivial for decades.
Most setups will likely stream both code and assets to wherever the game is running while you're developing as well, but I wouldn't necessarily call that trivial. Recompiling a shared library and hot reloading it if it's changed is (on the order of 20 or so lines), though, and I wouldn't expect any game to be developed without at least some code being reloaded this way.
While I haven't seen it as much, developing servers this way is also very doable, depending on how much you have to change data structures that have to be kept around.
Edit:
See https://www.youtube.com/watch?v=WMSBRk5WG58 for an example of how trivial it is to get this using just your OS facilities (`stat` + `LoadLibrary`/`dlopen`). None of this stuff is new and it's certainly been used with C/C++ or any language like it for a very, very long time.
Usually you would want to hot reload the code sections though and allow the application to change the data section which is significantly more intuitive than changing source code to change data.
Edit:
On that note most game engines serialise state to disk, pretty sure it would be easy to just read the state from like a yaml file and update the state if you really don't want to write an interface.
TLDR: There are significantly easier solutions to "how do I update the value of a variable at runtime" than hooking into the running process and changing the variable in memory.
This looks cool but I'm not sure how it is especially superior to functionality that is easily available through your favorite debugger (note: I'm speaking from a standpoint of Qt Creator + gdb; YMMV depending on your IDE/debugger setup), especially given that Hook seems to be mainly targeting macOS.
Is it the include of LLDB.h?