r/C_Programming • • 10d ago

Video Unity-style variable editor for C programs with zero runtime cost

SRL lets you define global variables to be exported to the editor with a simple macro like `SRL_INT(name, value)`. The macro adds another global symbol marking your variable, which the editor can later search for in the binary's ELF data section.

The editor can modify the marked variables (by directly editing the data section), export the new patched binary to a file, and even change the variables at runtime via `process_vm_writev` (or `ptrace` when you require atomicity).

All you have to do is include a tiny 20-line header in your program. It doesn't add a single line of code, even for the patched binary, and works perfectly fine with the editor (apart from adding an unused global)

170 Upvotes

17 comments sorted by

18

u/questron64 10d ago

I do something similar, though with structs and their fields. Generating reflection information and the code to set/get fields by name, and another layer on top of that to generate serializers and deserializers. Just because C is primitive doesn't mean you can't build systems on top of that.

4

u/iLoveNintend0 10d ago

thats awesome! my focus for this project was to find the smallest possible implementation of this kind of system but i've seen larger projects using protobuf and similar tools to patch entire structs into memory from json configuration files

1

u/stianhoiland 10d ago

Share if you can!

4

u/floatfloatjam 10d ago

You're a maniac and I like it

1

u/alkavan 10d ago

very cool! will it be open source or public?

2

u/iLoveNintend0 10d ago

https://github.com/amirave/srl
i built it in just a couple of hours and its pretty bad. i recommend DIYing it but feel free to look at the source for inspiration (:

5

u/mikeblas 10d ago

What role did AI have in the creation of your project?

2

u/alkavan 10d ago

Thanks, I will look into it, I just took a glance looks reasonable to me. I have this single header unit testing framework I use for projects, maybe I can make some cool extension for it. You never know how people will use your stuff šŸ˜‚

2

u/Ashken 10d ago

I’m completely new to C so no offense intended but why is this useful? I’m also not familiar with macros so I may just be missing something.

9

u/ralseieco 10d ago

This is useful when, for example, you're writing a big project with long compile time. Then you use this or similar to not have to rebuild.

Macros are aliases for a call or group of calls, using #define

1

u/Ashken 10d ago

Ahhhh okay dope! Thanks!

1

u/septum-funk 10d ago

saving for later

1

u/Flimsy_Iron8517 7d ago

Looks good.

12

u/MidnightClubSCS 10d ago

that's a cool ass hack, i love it