jaintapauljp
Is it possible in openplc runtime to track variables and functions associated with a structred text file?

For example lets say this is a structured text

 
(* File: LightControl.ST *)
PROGRAM LightControl
VAR
LightOn : BOOL; (* Output: Turns the light ON or OFF *)
MotionDetected : BOOL; (* Input: TRUE if motion is detected *)
END_VAR
(* Control logic for light *)
IF MotionDetected THEN
LightOn := TRUE; (* Turn ON the light when motion is detected *)
ELSE
LightOn := FALSE; (* Turn OFF the light when no motion is detected *)
END_IF;
END_PROGRAM
 
I believe this is compiled to C, and the C code is executed in runtime. I want to track/log which functions/variables are responsible for modifying the variables `LightOn` or `MotionDetected`. Can I replace the matiec compiler with my custom llvm compiler to do so?
Reply 0 0
fg71
I believe there is 3 parts how OpenPLC Editor is working.
 
Translate IEC 61131-3 standard (Structured Text) to Ansi C code.
GNU C++ is compiling the code to a binary format
dfu-util make it possible to upload the binary file to a bootloader.
Reply 0 0
jaintapauljp
In the second step, is it possible to track the variables and functions responsible for modification of the variables in ST file? Also can you point me the files where the first two steps happen?
Reply 0 0
fg71
When you compile a Project (with OpenPLC Editor) you will find a build folder there you have saved your Project ,
you could find all files include structured text file, c code and headers
 
You could look at the log when you compile your plc program in OpenPLC Editor.
Reply 0 0
jaintapauljp
I am not tlaking about the openplc editor, I am talking about the openplc runtime repo
Reply 0 0
fg71
OpenPLC runtime if you check the script folder you will find it use gnu g++ to compile plc program
Its as simple as download the source at GitHub and find what you are looking for.
Reply 0 0
jaintapauljp
Thanks for the response. I found it. I was looking if I can track the variables thru execution
Reply 0 0
thiagoralves
I imagine you want to do this for cyber security research reasons. You can make use of the debug facilities to track variables (see debug.cpp file) but there isn’t any structure to track function calls. This may require some form of code instrumentation or post processing after MatIEC compilation.
Reply 0 0
jaintapauljp
I imagine you want to do this for cyber security research reasons. You can make use of the debug facilities to track variables (see debug.cpp file) but there isn’t any structure to track function calls. This may require some form of code instrumentation or post processing after MatIEC compilation.

Yes I am aware of the instrumentation. What I don't understand is what kind of logic the instrumentation needs to track only the functions that read/writes the variables in the original structured text

Reply 0 0
thiagoralves
Well, there is no instrumentation for that on the runtime. You will have to write that yourself. As I said, the only thing you can track with the debugger that is already in place is the value of variables, not which function reads/writes to them
Reply 0 0
jaintapauljp
Well, there is no instrumentation for that on the runtime. You will have to write that yourself. As I said, the only thing you can track with the debugger that is already in place is the value of variables, not which function reads/writes to them
I am aware I need to write my custom instrumentation. I am just confused about what logic I need to put in the past while searching for functions so that I don't need to perform trial and error.
Reply 0 0
jaintapauljp
Example: I already have a custom llvm pass : https://github.com/icstracker/icstracker/blob/main/instrumentation_pass/instrument_pass.cpp

Currently it logs literally every function call , I just want it to record only those that are responsible for reading/writing the variables
Reply 0 0
Reply