Reputation: 703
I would like to write a function that will find all local variables and save them to disc. Later on, I want to load these variables and call/debug function from which the variables were saved. The goal is to speedup debugging of functions that are deep inside of my code.
I have used similar technique in Matlab, which allows to save whole workspace and later load it and continue. Can I use similar/relevant technique in C++?
Upvotes: 0
Views: 284
Reputation: 2862
What about the call history? What about the values in registers? Do you want to resume at the first line of the function or some other line.
You could try writing the stack, but that only works if no variables have constructors or pointers to malloc'd memory.
You can use setjmp() to get all the values in registers and longjmp() to restore them.
If you are on Windows, you can look into the DbgHelp API. It may have a way to enumerate all local variables.
Upvotes: 0
Reputation: 56479
You're searching for a reflector mechanism.
In C++ there isn't any reflector facility to enumerate variables, so you can not do it like Matlab.
You should make a data structure and add variables manually in the code. Then do whatever you want.
In practices programmers will use these steps:
To Save:
To load:
Upvotes: 3