Method and system for realizing monitoring function of PLC (Programmable Logic Controller) compiler
By generating global variables and obtaining dwarf data operation codes, the PLC compiler can realize real-time monitoring of local variables of the PLC program, solving the problem of difficulty in real-time monitoring of variable values in the existing technology, improving debugging efficiency and simplifying the program structure.
Patent Information
- Application Number
- CN202310154978.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2023-02-22
- Publication Date
- 2024-08-23
AI Technical Summary
It is difficult for existing PLC compilers to monitor variable values in real time while the program is running, and breakpoints need to be set to obtain variable values, which affects debugging efficiency.
By generating global variables, we obtain the intermediate code of the PLC program and the storage location of local variables, obtain the dwarf data operation code, and add it to the intermediate code of the global variable to achieve real-time monitoring of local variable values and avoid interrupting program operation.
It realizes real-time acquisition of variable values without interrupting the program when the PLC program is running, supports abnormal detection, and simplifies the program structure.
Smart Images

Figure CN118535161A
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to a method and system for realizing a monitoring function of a PLC compiler. Background Art
[0002] Programmable logic controller (PLC for short) is a digital logic controller used for automatic control, including logic control, timing control, analog control and other functions, which is widely used in the field of industrial control. In industrial production, PLC programming can execute logical operations, sequential control, timing and other instructions. When an error occurs in the running result of the PLC program, it is necessary to use the monitoring function to view the value of the variable during the program running process to determine the location of the program abnormality. Due to the real-time nature of the PLC system, it is usually necessary to obtain the value of the program when it is running. Although the value of the variable can be obtained during the program debugging process, it is necessary to set a breakpoint to stop the program. Therefore, how to monitor the value of the variable when the PLC program is running is a function that the PLC compiler urgently needs to implement.
[0003] No patents or public knowledge related to the technology of the present invention have been found yet. Summary of the invention
[0004] In view of the defects in the prior art, the object of the present invention is to provide a method and system for realizing the monitoring function of a PLC compiler.
[0005] A method for implementing a PLC compiler monitoring function according to the present invention includes:
[0006] Step S1: Generate multiple global variables at the beginning of the PLC program
[0007] Step S2: Obtain the storage location of the intermediate code and local variables of the PLC program;
[0008] Step S3: Obtain the dwarf data operation code of the local variables of the PLC program;
[0009] Step S4: Add the dwarf data operation code to the global variable intermediate code;
[0010] Step S5: Output the value of the global variable when the PLC program is running as the process value of the corresponding local variable, and the PLC compiler monitors the value of the local variable.
[0011] Preferably, in step S1:
[0012] The starting position is before the start of the PLC program; the number of global variables is the number of local variables that need to be monitored in the program; global variables are not assigned values, and declaration operations are performed;
[0013] The number of local variables that need to be monitored is determined by the source program and the user. The user specifies which local variables need to be monitored based on the local variables in the program.
[0014] Preferably, in step S2:
[0015] The compiler front end obtains the intermediate code of the PLC program through lexical analysis, syntax analysis and semantic analysis. The PLC compiler front end converts the PLC source code into llvm intermediate code; the intermediate code after conversion contains metadata information of global variables and local variables;
[0016] Get the storage location of the local variables of the PLC program. All local variables are stored in the stack. The storage location is RBP-x, where RBP is the RBP register and -x is the offset of x bits relative to the RBP register. According to the declaration order of local variables, the storage location of each variable is RBP-x.
[0017] Preferably, in step S3:
[0018] Get the dwarf data operation code of the PLC local variable. The operation code is obtained according to the storage location of the local variable, including the register position and offset of the local variable. According to the storage location RBP-x of the local variable, call the createExpression interface in the llvm framework and pass in the two vectors llvm::dwarf::DW_OP_breg6 and -x. Among them, llvm::dwarf::DW_OP_breg6 means obtaining data information based on register 6. The interface returns the dwarf data operation code.
[0019] Preferably, in step S4:
[0020] Add dwarf opcodes to complex expressions in the global variable intermediate code. In the llvm intermediate code, each global variable contains a complex expression, and a dwarf opcode is added at the end.
[0021] A PLC compiler monitoring function implementation system provided by the present invention includes:
[0022] Module M1: Generate multiple global variables at the beginning of the PLC program
[0023] Module M2: Get the storage location of the intermediate code and local variables of the PLC program;
[0024] Module M3: Get the dwarf data operation code of the local variables of the PLC program;
[0025] Module M4: Add dwarf data opcode to the global variable intermediate code;
[0026] Module M5: When the PLC program is running, the value of the global variable is output as the process value of the corresponding local variable, and the PLC compiler monitors the value of the local variable.
[0027] Preferably, in the module M1:
[0028] The starting position is before the start of the PLC program; the number of global variables is the number of local variables that need to be monitored in the program; global variables are not assigned values, and declaration operations are performed;
[0029] The number of local variables that need to be monitored is determined by the source program and the user. The user specifies which local variables need to be monitored based on the local variables in the program.
[0030] Preferably, in the module M2:
[0031] The compiler front end obtains the intermediate code of the PLC program through lexical analysis, syntax analysis and semantic analysis. The PLC compiler front end converts the PLC source code into llvm intermediate code; the intermediate code after conversion contains metadata information of global variables and local variables;
[0032] Get the storage location of the local variables of the PLC program. All local variables are stored in the stack. The storage location is RBP-x, where RBP is the RBP register and -x is the offset of x bits relative to the RBP register. According to the declaration order of local variables, the storage location of each variable is RBP-x.
[0033] Preferably, in the module M3:
[0034] Get the dwarf data operation code of the PLC local variable. The operation code is obtained according to the storage location of the local variable, including the register position and offset of the local variable. According to the storage location RBP-x of the local variable, call the createExpression interface in the llvm framework and pass in the two vectors llvm::dwarf::DW_OP_breg6 and -x. Among them, llvm::dwarf::DW_OP_breg6 means obtaining data information based on register 6. The interface returns the dwarf data operation code.
[0035] Preferably, in the module M4:
[0036] Add dwarf opcodes to complex expressions in the global variable intermediate code. In the llvm intermediate code, each global variable contains a complex expression, and a dwarf opcode is added at the end.
[0037] Compared with the prior art, the present invention has the following beneficial effects:
[0038] 1. The present invention provides a method for realizing the monitoring function of a PLC compiler based on dwarf data, which can obtain the values of variables in the program in real time without interrupting the program when the PLC program is running;
[0039] 2. When an error or anomaly occurs in the program of the present invention, the user can detect the anomaly of the program through the value of the variable;
[0040] 3. The present invention does not need to set breakpoints to stop the program, and the structure is simpler. BRIEF DESCRIPTION OF THE DRAWINGS
[0041] Other features, objects and advantages of the present invention will become more apparent from the detailed description of non-limiting embodiments made with reference to the following drawings:
[0042] Figure 1 The figure is a flowchart of the method for implementing the monitoring function of the PLC compiler. DETAILED DESCRIPTION
[0043] The present invention is described in detail below in conjunction with specific embodiments. The following embodiments will help those skilled in the art to further understand the present invention, but are not intended to limit the present invention in any form. It should be noted that, for those of ordinary skill in the art, several changes and improvements can also be made without departing from the concept of the present invention. These all belong to the protection scope of the present invention.
[0044] Embodiment 1:
[0045] The purpose of the present invention is to provide a method for realizing the monitoring function of a PLC compiler based on dwarf data, which can obtain the value of a variable when a PLC program is running, so as to perform abnormality detection on the PLC program.
[0046] A method for implementing a PLC compiler monitoring function according to the present invention includes:
[0047] Step S1: Generate multiple global variables at the beginning of the PLC program
[0048] Specifically, in step S1:
[0049] The starting position is before the start of the PLC program; the number of global variables is the number of local variables that need to be monitored in the program; global variables are not assigned values, and declaration operations are performed;
[0050] The number of local variables that need to be monitored is determined by the source program and the user. The user specifies which local variables need to be monitored based on the local variables in the program.
[0051] Step S2: Obtain the storage location of the intermediate code and local variables of the PLC program;
[0052] Specifically, in step S2:
[0053] The compiler front end obtains the intermediate code of the PLC program through lexical analysis, syntax analysis and semantic analysis. The PLC compiler front end converts the PLC source code into llvm intermediate code; the intermediate code after conversion contains metadata information of global variables and local variables;
[0054] Get the storage location of the local variables of the PLC program. All local variables are stored in the stack. The storage location is RBP-x, where RBP is the RBP register and -x is the offset of x bits relative to the RBP register. According to the declaration order of local variables, the storage location of each variable is RBP-x.
[0055] Step S3: Obtain the dwarf data operation code of the local variables of the PLC program;
[0056] Specifically, in step S3:
[0057] Get the dwarf data operation code of the PLC local variable. The operation code is obtained according to the storage location of the local variable, including the register position and offset of the local variable. According to the storage location RBP-x of the local variable, call the createExpression interface in the llvm framework and pass in the two vectors llvm::dwarf::DW_OP_breg6 and -x. Among them, llvm::dwarf::DW_OP_breg6 means obtaining data information based on register 6. The interface returns the dwarf data operation code.
[0058] Step S4: Add the dwarf data operation code to the global variable intermediate code;
[0059] Specifically, in step S4:
[0060] Add dwarf opcodes to complex expressions in the global variable intermediate code. In the llvm intermediate code, each global variable contains a complex expression, and a dwarf opcode is added at the end.
[0061] Step S5: Output the value of the global variable when the PLC program is running as the process value of the corresponding local variable, and the PLC compiler monitors the value of the local variable.
[0062] Embodiment 2:
[0063] Embodiment 2 is a preferred example of Embodiment 1, and is used to illustrate the present invention in more detail.
[0064] The present invention also provides a PLC compiler monitoring function implementation system, which can be implemented by executing the process steps of the PLC compiler monitoring function implementation method, that is, those skilled in the art can understand the PLC compiler monitoring function implementation method as a preferred implementation method of the PLC compiler monitoring function implementation system.
[0065] A PLC compiler monitoring function implementation system provided by the present invention includes:
[0066] Module M1: Generate multiple global variables at the beginning of the PLC program
[0067] Specifically, in the module M1:
[0068] The starting position is before the start of the PLC program; the number of global variables is the number of local variables that need to be monitored in the program; global variables are not assigned values, and declaration operations are performed;
[0069] The number of local variables that need to be monitored is determined by the source program and the user. The user specifies which local variables need to be monitored based on the local variables in the program.
[0070] Module M2: Get the storage location of the intermediate code and local variables of the PLC program;
[0071] Specifically, in the module M2:
[0072] The compiler front end obtains the intermediate code of the PLC program through lexical analysis, syntax analysis and semantic analysis. The PLC compiler front end converts the PLC source code into llvm intermediate code; the intermediate code after conversion contains metadata information of global variables and local variables;
[0073] Get the storage location of the local variables of the PLC program. All local variables are stored in the stack. The storage location is RBP-x, where RBP is the RBP register and -x is the offset of x bits relative to the RBP register. According to the declaration order of local variables, the storage location of each variable is RBP-x.
[0074] Module M3: Get the dwarf data operation code of the local variables of the PLC program;
[0075] Specifically, in the module M3:
[0076] Get the dwarf data operation code of the PLC local variable. The operation code is obtained according to the storage location of the local variable, including the register position and offset of the local variable. According to the storage location RBP-x of the local variable, call the createExpression interface in the llvm framework and pass in the two vectors llvm::dwarf::DW_OP_breg6 and -x. Among them, llvm::dwarf::DW_OP_breg6 means obtaining data information based on register 6. The interface returns the dwarf data operation code.
[0077] Module M4: Add dwarf data opcode to the global variable intermediate code;
[0078] Specifically, in the module M4:
[0079] Add dwarf opcodes to complex expressions in the global variable intermediate code. In the llvm intermediate code, each global variable contains a complex expression, and a dwarf opcode is added at the end.
[0080] Module M5: When the PLC program is running, the value of the global variable is output as the process value of the corresponding local variable, and the PLC compiler monitors the value of the local variable.
[0081] Embodiment 3:
[0082] Embodiment 3 is a preferred example of Embodiment 1, and is used to illustrate the present invention in more detail.
[0083] On a machine with the llvm compiler installed, the compiler front end is responsible for lexical analysis, syntax analysis, and semantic analysis of the PLC source program, and then adds metadata information to the variables by calling the llvm framework to obtain the intermediate code. The back end is responsible for converting the intermediate code into assembly code related to the target architecture. Figure 1 The present invention provides a method for implementing the monitoring function of a PLC compiler. The technical solution includes the following steps (the process is as follows Figure 1 ):
[0084] Step 1: Generate multiple global variables at the start of the PLC program. The start position refers to before the start of the PLC program; the number of global variables is the number of local variables that need to be monitored in the program. Specifically, global variables do not need to be assigned values, only declaration operations are required.
[0085] The number of local variables is determined by the source program and the user. The user specifies which local variables need to be monitored based on the local variables in the program.
[0086] Local variables are variables inside a function in a program and are only used in that function. Global variables apply to the entire program and can also be used in other functions.
[0087] Step 2: The compiler front end obtains the intermediate code of the PLC program through lexical analysis, syntax analysis, and semantic analysis. The PLC compiler front end converts the PLC source code into llvm intermediate code. Specifically, the intermediate code after conversion contains metadata information of global variables and local variables.
[0088] Step 3: Get the storage location of the local variables of the PLC program. Specifically, all local variables are stored in the stack, and their storage location can generally be expressed as RBP-x, where RBP represents the RBP register, and -x represents the offset relative to the RBP register as x bits. The offset is regular and is the length of the variable, increasing in sequence from the order of variable declaration. For example, if two int type variables are declared, the offset of the first variable is 4, and the offset of the second variable is 8. According to the declaration order of the local variables, the storage location of each variable is expressed in the form of RBP-x.
[0089] Step 4: Get the dwarf data opcode of the PLC local variable. The opcode needs to be obtained based on the storage location of the local variable, including the register location and offset of the local variable. Specifically: according to the storage location RBP-x of the local variable, call the createExpression interface in the llvm framework, and pass in two vectors llvm::dwarf::DW_OP_breg6 and -x, where llvm::dwarf::DW_OP_breg6 means obtaining data information based on register 6 (RBP register), and the result returned by the interface is the dwarf data opcode.
[0090] Step 5: Add dwarf opcodes to the complex expressions in the global variable intermediate code. The code form of the complex expression is: !DIExpression(), which is used to pass in a dwarf opcode to act on the global variable. In the llvm intermediate code generated in step 2, each global variable contains a complex expression (DIExpression), and the dwarf opcode obtained in step 4 is added to the end.
[0091] Step 6: Output the value of the global variable when the PLC program is running as the process value of the corresponding local variable. Obtain the value of the global variable according to the complex expression in step 5. When the program is running, output these values to obtain the program running process value of the corresponding local variable. The PLC compiler monitors the value of the local variable.
[0092] In a specific example, steps 1-6 are described as follows:
[0093] The program has a global variable G and a function called main. There is a local variable A in the function. A is given an initial value of 10, and then A is incremented by 10.
[0094] As shown above, G is a global variable that has not been assigned a value, and A is a local variable that needs to be monitored. The intermediate code converted in step 2 includes:
[0095] DIExpression code of the global variable G; metadata of G, which declares the name of G, the corresponding source code location and data type.
[0096] After steps 4-5, the storage location of the local variable A can be expressed as RBP-4. The createExpression interface in the llvm framework is called, and the two vectors llvm::dwarf::DW_OP_breg6 and -4 are passed in. The obtained dwarf opcode is added to DIExpression. At this time, the intermediate code is as follows:
[0097] Add G's dwarf opcode to the DIExpression code of the global variable G; G's metadata, which declares G's name, corresponding source code location and data type.
[0098] Through the above intermediate code, the compiler backend converts it into assembly code related to the target architecture and executes it. The value of the global variable G is 11, which is the corresponding value of the local variable A.
[0099] Those skilled in the art know that, in addition to implementing the system, device and its various modules provided by the present invention in a purely computer-readable program code, it is entirely possible to implement the same program in the form of logic gates, switches, application-specific integrated circuits, programmable logic controllers and embedded microcontrollers by logically programming the method steps. Therefore, the system, device and its various modules provided by the present invention can be considered as a hardware component, and the modules included therein for implementing various programs can also be considered as structures within the hardware component; the modules for implementing various functions can also be considered as both software programs for implementing the method and structures within the hardware component.
[0100] The above describes the specific embodiments of the present invention. It should be understood that the present invention is not limited to the above specific embodiments, and those skilled in the art can make various changes or modifications within the scope of the claims, which does not affect the essence of the present invention. In the absence of conflict, the embodiments of the present application and the features in the embodiments can be combined with each other arbitrarily.
Claims
1. A method for implementing a PLC compiler monitoring function, characterized in that: include: Step S1: Generate multiple global variables at the beginning of the PLC program Step S2: Obtain the storage location of the intermediate code and local variables of the PLC program; Step S3: Obtain the dwarf data operation code of the local variables of the PLC program; Step S4: Add the dwarf data operation code to the global variable intermediate code; Step S5: Output the value of the global variable when the PLC program is running as the process value of the corresponding local variable, and the PLC compiler monitors the value of the local variable.
2. The method for realizing the monitoring function of a PLC compiler according to claim 1, characterized in that: In step S1: The starting position is before the start of the PLC program; the number of global variables is the number of local variables that need to be monitored in the program; global variables are not assigned values, and declaration operations are performed; The number of local variables that need to be monitored is determined by the source program and the user. The user specifies which local variables need to be monitored based on the local variables in the program.
3. The method for realizing the monitoring function of a PLC compiler according to claim 1, characterized in that: In step S2: The compiler front end obtains the intermediate code of the PLC program through lexical analysis, syntax analysis and semantic analysis. The PLC compiler front end converts the PLC source code into llvm intermediate code; the intermediate code after conversion contains metadata information of global variables and local variables; Get the storage location of the local variables of the PLC program. All local variables are stored in the stack. The storage location is RBP-x, where RBP is the RBP register and -x is the offset of x bits relative to the RBP register. According to the declaration order of local variables, the storage location of each variable is RBP-x.
4. The method for realizing the monitoring function of a PLC compiler according to claim 1, characterized in that: In step S3: Get the dwarf data operation code of the PLC local variable. The operation code is obtained according to the storage location of the local variable, including the register position and offset of the local variable. According to the storage location RBP-x of the local variable, call the createExpression interface in the llvm framework and pass in the two vectors llvm::dwarf::DW_OP_breg6 and -x. Among them, llvm::dwarf::DW_OP_breg6 means obtaining data information based on register 6. The interface returns the dwarf data operation code.
5. The method for realizing the monitoring function of a PLC compiler according to claim 1, characterized in that: In step S4: Add dwarf opcodes to complex expressions in the global variable intermediate code. In the llvm intermediate code, each global variable contains a complex expression, and a dwarf opcode is added at the end.
6. A PLC compiler monitoring function implementation system, characterized in that: include: Module M1: Generate multiple global variables at the beginning of the PLC program Module M2: Get the storage location of the intermediate code and local variables of the PLC program; Module M3: Get the dwarf data operation code of the local variables of the PLC program; Module M4: Add dwarf data opcode to the global variable intermediate code; Module M5: When the PLC program is running, the value of the global variable is output as the process value of the corresponding local variable, and the PLC compiler monitors the value of the local variable.
7. The PLC compiler monitoring function implementation system according to claim 6, characterized in that: In the module M1: The starting position is before the start of the PLC program; the number of global variables is the number of local variables that need to be monitored in the program; global variables are not assigned values, and declaration operations are performed; The number of local variables that need to be monitored is determined by the source program and the user. The user specifies which local variables need to be monitored based on the local variables in the program.
8. The PLC compiler monitoring function implementation system according to claim 6, characterized in that: In the module M2: The compiler front end obtains the intermediate code of the PLC program through lexical analysis, syntax analysis and semantic analysis. The PLC compiler front end converts the PLC source code into llvm intermediate code; the intermediate code after conversion contains metadata information of global variables and local variables; Get the storage location of the local variables of the PLC program. All local variables are stored in the stack. The storage location is RBP-x, where RBP is the RBP register and -x is the offset of x bits relative to the RBP register. According to the declaration order of local variables, the storage location of each variable is RBP-x.
9. The PLC compiler monitoring function implementation system according to claim 6, characterized in that: In the module M3: Get the dwarf data operation code of the PLC local variable. The operation code is obtained according to the storage location of the local variable, including the register position and offset of the local variable. According to the storage location RBP-x of the local variable, call the createExpression interface in the llvm framework and pass in the two vectors llvm::dwarf::DW_OP_breg6 and -x. Among them, llvm::dwarf::DW_OP_breg6 means obtaining data information based on register 6. The interface returns the dwarf data operation code.
10. The PLC compiler monitoring function implementation system according to claim 6, characterized in that: In the module M4: Add dwarf opcodes to complex expressions in the global variable intermediate code. In the llvm intermediate code, each global variable contains a complex expression, and a dwarf opcode is added at the end.
