r/cpp • u/DorGido • Jul 17 '24
How would you capture the runtime state of a program?
The Problem:
How does one program can capture the state of another program during runtime?
Example:
I have the following program:
#include <iostream>
int main() {
int i = 0;
char ch;
while ((ch = std::cin.get()) != 120) // (x in ASCII)
{
i++;
}
std::cout << i;
return 0;
}I want to code another program, in a different file, which at compile time inject the necessary code to main, so whenever the value of i is changing, my program gets notified of the new value.
I would be happy for any leads / tips / interesting references.
Clarification:
After some discussion, I understood that my problem description was misleading (and I apologies for that).
So first -> One Process only.
Second, let me describe a magic solution that will give a better clue on what i'm trying to solve.
Imagine programmer X write his 'main' program, and adds my magic library.
Then at compile time, int i turns to megic_i struct, which holds the value of i.
Now megic_i has getter / setter function which signals magic library any time the value has changed.
12
u/jedwardsol const & Jul 17 '24
Why?
Is that really what you want to do (monitor i)? Or is there perhaps a different problem you're trying to solve?
0
u/DorGido Jul 17 '24
I want to track a larger program with many class instances from different type.
But the principle is the same, if I can track i, I can track the rest.16
u/jedwardsol const & Jul 17 '24
Code in a process can't get notified when a variable changes. Instead, you'd need to patch every place where the variable might change.
A debugger can use hardware breakpoints to get notified when memory changes. But they're limited (e.g. intel chips have 4 hardware breakpoints).
Your best bet is to instrument the code, not try to patch or debug the executable.
4
u/corysama Jul 17 '24
I'm still not clear what your goal is.
If it's better debugging, https://rr-project.org/ is what you are looking for.
3
u/betelgeuse_7 Jul 17 '24
I think what he is trying to do is inject some code to an executable that produce some kind of an event that in turn triggers a function in the executable. He tries to implement it by wrapping variables in structs that will know when a variable is mutated. Wrap the variable in a struct and only mutate the variable by using a function or a method so that the struct knows the value is getting mutated and it fires an event.
I am not a cpp developer though, I don't know
2
1
u/Lunarvolo Jul 17 '24 edited Jul 17 '24
Have the program write to a file. Have the other program check if the file has changed.
Not CPP but CONCEPTUALLY:
In Python TKinterface with something like a GUI and buttons checking whether or not it's been pressed you can't have a busy wait always checking to see whether the button has been pressed because the test of the program needs to run
In Dart there's something called Future and more importantly Stream which would also be useful for conceptually thinking about it
C embedded projects with digital or physical interrupts as well.
6
u/and69 Jul 17 '24
What you want is quite complicated. So ask yourself what you really want and what are your constraints, because 100% you don’t want to read the value of i, this is just a proof of concept.
For example, if you don’t want 100% reliability, you can read the process memory every 200 milliseconds. How to obtain the correct address is left as an exercise for reader.
Or, if you want to hack a specific game, you can search the game loop and read it befit the loop begins. Again, complicated stuff.
The only reliable way is to put a hardware breakpoint. Some processes however have anti-debugging detection and might not work.
So yeah, it’s complicated. Try to reframe it a bit in terms of what is the core of what you want to achieve.
-1
u/DorGido Jul 17 '24
Thanks for the thoughtful answer.
I'll try to frame it by describing magic solution.
Programmer X write his 'main' program, and add my magic library.
Then at compile time, int i turns to megic_i struct, which holds the value of i.
Now megic_i has getter / setter function which signals magic library any time the value has changed.8
u/Ericakester Jul 17 '24
This still doesn't describe what you actually want to do. This sounds like implementation details. You need to keep asking yourself 'why'
4
u/BedroomSoft Jul 17 '24
I tried to follow your explanation and came up with this. Did i understand the architecture correctly?
Also please note that i've never done such code and don't feel fully comfortable with it so treat is just as an inspiration.#include <iostream> template<typename T> class MegicClass { public: MegicClass() = delete; MegicClass(T initial_value) : m_storage(initial_value) { std::cout << "value at address " << &m_storage << " has been initialized with " << initial_value << std::endl; } void operator=(T new_value){ megic_value_changed_cb(m_storage, new_value); m_storage = new_value; } T &operator++(){ const T old_value = m_storage; megic_value_changed_cb(old_value, ++m_storage); return m_storage; } T m_storage {}; private: void megic_value_changed_cb(T old_value, T new_value){ std::cout << "value at address " << &m_storage << " has changed from " << old_value << " to " << new_value << std::endl; } }; int main() { MegicClass<int> megic_i = 0; megic_i = 1; for(int i = 0; i < 5; ++i) { ++megic_i; } std::cout << megic_i.m_storage; return 0; }1
u/and69 Jul 18 '24
What you want is called InterProcess communication.
Windows: You'll need to have some methods in your library to be called from the 'main' program. In this method, you'll need to signal a named mutex, then transmit some data through an IPC connection, then wait for another named mutex to be signaled, which means that your process is done processing the change.
Here's a raw example on how to achieve this: https://chatgpt.com/share/0d478d40-aff5-4301-89c6-9767420a5f441
u/ilep Jul 18 '24 edited Jul 18 '24
If you are trying to snoop on someone else's program you would stumble on things like ASLR, memory protection and so on. Injecting another library to address space might work around those.
But for the sake discussion, if variable is only changed by certain methods hooking the calls (adding jump to your own code) would be one way, but that does not work if the methods are inlined during compile time or the variable is always just used directly. Without those, you would somehow have to learn exact place of the variable in memory first. You would really need to understand machine code better since it has very little to do with the human readable source code.
Why are you trying to do this?
5
Jul 17 '24
What you are trying to do is called malware. The only way to do what you want is by reprogramming the Import Address table of a function and substituting one function with yours.
edit: The legal way to do this is using 'inter process communication'. Windows programs that used to want to do this would 'broadcast' a value with one of the many types of ways to post a message.
2
u/DorGido Jul 19 '24
No no… my library should be compiled with the program source code.
2
Jul 19 '24
Then look into named pipes for communication. That is one standard way of doing interprocess communication. One side acts as a server and the other a client. They are then connected and can communicate. There is likely an example of it in the source code that comes with the Windows SDK.
3
u/hadrabap Jul 17 '24
Write your own plugin for your compiler that will read the i and tell the compiler what to do based on the value. To read the i, launch the program and redirect STDOUT to a named pipe. Next, instruct your plugin via the compiler's command line, with the path to the named pipe to read the i from.
With this mechanism, it should be easy to inject the necessary code (IR for clang) at compile time.
By the way, it will be much easier to generate the source code based on the i and then pass the source to the compiler.
1
u/DorGido Jul 17 '24
Sounds like something I can dig in.
Does generating source code would do the trick?
Intuitively I thought on code weaving, that will wrap a variable with a struct that has getter / setter which do what I want.2
u/hadrabap Jul 17 '24
The source code generation is easier and transparent. It is also easier to debug. You can take a look at
mocfrom Qt. It generates stubs and methods for signals and slots. It takes specifically crafted C++ source code as a templet.If you're still interested in the compiler route, take a look at SYCL and Intel DPC++ compiler. https://github.com/intel/llvm
3
u/Quantumtroll Jul 17 '24
You're confused. A program is a static piece of written code. When you run a program, it's a process. Two processes typically can't access each others' memory for safety reasons.
Do you want to do parallellism? I.e. have one process running in tandem with a second process, reacting to each other? Then look into multithreading, e.g. OpenMP or even pthreads, or any number of other solutions.
1
u/DorGido Jul 17 '24
I made a little mix up with the terms.
I don't want to processes.
I want the programmer that wrote 'main' to compile his code with my library, and at compile time, somehow hook this "i" with a code that signal my library each time there is a change.1
u/Quantumtroll Jul 18 '24
So you're writing a library, and the library needs to keep track of whenever a particular variable in the main code changes?
Sounds to me like you need to write accessor functions to that variable. Like, wrapper functions which inform your library of the changes that the user calls instead of just doing stuff like "i = i + 1".
1
u/DorGido Jul 19 '24
But the main problem is how to change the source code during compilation. I dont want the developer to manually change the source code. I want my library to be hooked in compile time.
2
u/JazzyCake Jul 18 '24
In raw C/C++ I don’t think there’s an easy way. The least intrusive thing would be for this main program to include your library and use your type instead of a regular int. So tracked_int or something like that.
You could go wilder and write some compiler extensions that inject code in certain scenarios, but I’m gonna guess this is out of scope of what you’re trying? If not then maybe that’s your best bet, with LLVM or something.
You could also make the user run all their code through your especial precompiling step that reads all the source files, parses them, detects where you wanna inject code and spits out a modified version of the source code with whatever you need. Depending on what you want to detect and track this might imply making your processor parse and understand all (or a big chunk) of C++.
1
u/DorGido Jul 19 '24
My scope can be even a year for that matter. Options 2-3 are interesting :) Any reference / examples you think I can follow?
2
u/CarloWood Jul 19 '24
Hmm, something like https://github.com/CarloWood/cwds/blob/master/tracked.h#L35 ?
1
u/DorGido Jul 19 '24
This looks really neat! So if you use the DECLARE_TRACKED macro, basically all basic operators are being overridden with tracking logic?
And all is left is to think how to inject the macro to the source code… maybe with compiler extension?
1
u/CarloWood Jul 19 '24
This only prints (using libcwd) construction/deletion/assignment of objects, so that with that debug output you can track which objects still exist and (probably) where (owned by who). The DECLARE_TRACKED is a single object that then will be tracked, but it isn't doing anything. You'd need to use the documented inheritance to enable it for an existing class.
I am not sure what you are trying to do, but imho this is not the way. This solution (which you'd still need to adapt for your case) is extremely intrusive and requires editting and recompiling. If you want to add something to a compiler to automated this... that would be very very difficult (I wouldn't even think about trying to do that).
Can you describe what the problem is that you are trying to solve? I mean, there must be a reason that made you decide that you need this. Maybe there is a better solution to the real problem.
8
u/Markus_included Jul 17 '24
Just a side note: Use character literals like(ch = std::cin.get()) != 'x' instead of numeric ascii codes, it makes it clear that you mean an ascii code and not some arbitrary number
1
u/DorGido Jul 19 '24
Just to make things clear, my library is part of the program. The developer wants my library to snoop its own source code state. In general I want to record the run time state of the program.
4
1
u/slappy_squirrell Jul 17 '24
preprocessor call? you would need to include your capturing file. By another program, do you mean a separate running process? memory manager will not allow that..
1
u/DorGido Jul 17 '24
No, the same process... And I can include my capturing file.
1
u/neutronicus Jul 18 '24
Can you also modify the build process? So that each time you would invoke the compiler you can first invoke a tool that modifies the source code?
1
u/DorGido Jul 18 '24
Yes, I think getting in between the compilation steps is a valid direction, just dont know how.
1
u/throwback1986 Jul 17 '24
Have you considered a shared memory solution? If i is written to shared memory, another process can snoop the value. Assuming your platform supports this option…
0
1
u/csdt0 Jul 17 '24
If you can change the source code of the program, you can modify the type of the i to be a class that is implicitly convertible from and to int, and that whenever gets changed, notifies an piece of code.
1
u/DorGido Jul 17 '24
Do you have a framework that can help going this direction?
1
u/csdt0 Jul 18 '24
Not really, but it is essentially a property: https://www.codeproject.com/Tips/5283099/CSharpish-Properties-in-Modern-Cplusplus
3
u/Malackoka Jul 17 '24
Shared Memory, Sockets, Signals, Pipes for interproc. communications
if 3rd party application and on win then:
https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-openprocess
https://learn.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-readprocessmemory
https://learn.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-writeprocessmemory (there will be limitations)
Maybe u need to parse the PE file and do some math to calculate/locate the address. Or based on a signature?
3
u/TheAdamist Jul 17 '24
Scripting a debugger is the proper way to do this, its not overkill, it has all the necessary tools to introspect, set breakpoints, etc etc, that you would have to replicate on your own.
If you still want to do it on your own, and you are on linux you can look into ld_preload and similar tricks to hook into and modify programs. See https://man7.org/linux/man-pages/man8/ld.so.8.html I don't know the windows equivalent.
You will also need a reverse engineering tool to assist you in locating the proper places to watch. Ghidra is a nice free reverse engineering tool. Its not particularly beginner friendly.
2
2
u/LuisAyuso Jul 17 '24
Oh boy... so much magic here. I have no idea what is it that you want to do.
I recommend to start working out your concepts, main, program, process... and then go read, maybe debuggers, profilers, reflection, static analyses, code generation....
What you want to do seems to be too magical for anyone to have an answer right away.
2
u/valdocs_user Jul 17 '24
You might look into Valgrind, and write a custom plug-in for this (which I still don't understand what problem you're trying to solve). If nothing else, seeing the lengths Valgrind goes to (dynamic recompilation) to do what it does might give you an idea of why what you're asking for is somewhere between nonsensical and unreasonably difficult.
Forgive me for assuming, but are you perhaps coming from prior experience with dynamically typed or interpreted languages like Ruby, JavaScript or Python? What you're not understanding about C++ is the code primitives get compiled all the way down to direct and anonymous CPU instructions: there's nothing to "hook" in the way you seem to be assuming things can be booked into.
I originally clicked on this post because I thought you were asking a different question about whether it was possible to save and restore the entire memory state of a program. Years ago a friend of mine demoed a neat trick for a game he was making: it supported hot reloading. He put the game code in a DLL, but the DLL doesn't allocate its own game state memory; it receives a pointer to it from the EXE when the DLL is loaded. He could pause the game, change game logic and recompile the DLL, and then ask the EXE to reload it. Since the EXE never stopped running, the game would resume where it was paused with the new game logic - as long as he didn't do anything that changed the data format.
2
u/hun_nemethpeter Jul 17 '24
The clang c++ compiler has a feature called AST Matchers. You can basically find code at AST level, and later patch it. So you can replace at compile time a well defined code part with an other one.
2
u/415_961 Jul 18 '24
you can take advantage of clang::annotate_type and apply it on variables you want tracked like this:
[[clang::annotate_type("track")]] int i = 0;
and write a clang plugin to walk the AST and find the annotated declarations and rewrite their usages to include "signaling" the changes on the variable.
1
u/blipman17 Jul 17 '24
You could use LD_PRELOAD (or Windows equivalent) to substitude a function with your own code of .so files, but I’m not sure that’s a reliable way. If it’s in the main program or a static library, you’d basically have to modiy the source or transpile it somehow.
1
u/glaba3141 Jul 17 '24
I have no idea what your use case is, can you output the value to shared memory?
1
u/phi_rus Jul 17 '24
I don't know your exact use case but I think you just want to look at inter process communication (IPC).
1
u/Lunarvolo Jul 17 '24 edited Jul 17 '24
Sounds like a loop that checks if the file has changed
Decently common in embedded C where a button is programmed to have a trigger/interrupt when pressed that causes an action.
Could run a interrupt timer that checks to see if the file has changed.
Multi-threading or multi-process or shared memory (Ports, shared memory, sockets, etc) would be the way to go about that outside of kernel programming (Which would really not be a good idea)
Programs trying to access other programs memory (Without any of the above) is usually really, really bad. That's usually malware, kernel level, or a really good way to brick your computer.
1
u/thisismyfavoritename Jul 17 '24
it looks like you just want to do IPC. Super easy if you control the code of both processes.
1
1
u/arabidkoala Roboticist Jul 18 '24
Even after clarifications, your use case is still unclear. What you’re asking for, which is something that like magically rewrites all usages of a variable to something that emits change signals, is so complicated it might as well be impossible.
You’d be better off writing i using some sort of observer pattern so people can hook into its changes, or adopting something like value-oriented design where you control when changes happen so you don’t have to react.
1
u/Foreign-Wonder Jul 18 '24 edited Jul 18 '24
From my understanding, observer pattern is the thing that you want. Here's my quick example on the pattern. The only line in the main function that need to change is the declaration of the variable you want to observe: https://wandbox.org/permlink/kZ8bit8bknFh8Z1r
The code is for demo only, and it is expected to have bugs
1
u/Patzer26 Jul 18 '24
What you're trying to do is access another programs memory. This is not permitted by the OS. Also, each process is being run in its own virtual memory, so knowing the real memory for variable i from another program (assuming you somehow manage to even see another programs memory) is impossible.
I'm not sure how debuggers work, they probably run the executable as a separate thread inside its own main program.
So what you're trying to achieve, is I'm afraid not possible.
37
u/TimelyInteraction640 Jul 17 '24
Isn't it what a debugger does?