I would like to monitor, record, specific variables in an application but I am not sure what is the right way to approach this problem.
The application is a mix of C(5%) and C++(95%) and it is relatively big, around 5 million lines of code. The target OS is CentOS 7 and my application is always compiled with debugging flags (-g3 -ggdb) and I can add/remove flags if necessary to achieve what I am looking for. In the ideal scenario I don't want to instrument the code in the main application, mainly due to the size of the app, and/or add any overhead to my application.
I have not started implementing anything yet but I have two different approaches:
Write a second program that given a variable name, this variable is declared in my main program, it:
- Parses the debug symbol of the main program
- Finds the address of the variable we are interested in
- Somehow manages to access the virtual address space of the main application and reads the value of the variable using the address we have found in the previous step
I am still not sure about how I can handle different data types, maybe I can somehow infer the data type by parsing the main program but I am now sure.
I understand that the OS won't let me easily access the memory of another process, but there should be a trick. What is that trick? Is there a more straight forward way of doing this? What is the overhead associated with this? Is there a way to also write into a variable?
Add the logic in the main application:
- Pass the variable name over network to the application
- The app reads and parses its own binary
- Finds the address of the variable we are interested and we are done
The second approach is more interesting because everything is in its address space, so no trick is requuired. How ever I still have to know the type of the variable.
I am sure others have had similar problems but I don't know what this process is called. What should I search for to get more information? I understand this is a very general question, any pointers are highly appreciated.