ST program cross-platform debugging method based on instrumentation and state machine indirect compiling

By generating equivalent program logic statements and inserting debug logic statements based on indirect compilation methods based on instrumentation and state machine, the problem that existing ST language debugging methods are not applicable in small systems is solved, cross-platform debugging is realized, and the improvement of Efficiency and simplicity, suitable for small systems and multiple CPU architectures.

CN119003341APending Publication Date: 2024-11-22GUODIAN NANJING AUTOMATION
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202411024822.3
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-07-29
Publication Date
2024-11-22

AI Technical Summary

Technical Problem

The existing ST language debugging method is not applicable in small systems. It requires the installation of third-party software GDB and requires developers to be familiar with the GDB interface and ptrace system, resulting in inefficiency and does not support cross-platform.

Method used

Using the cross-platform debugging method of ST programs based on indirect compilation of instrumentation and state machine, by generating equivalent program logic statements and inserting debugging logic statements, GCC of each CPU architecture is compiled into target code to achieve cross-platform debugging, avoiding Reliance on underlying technology and complex mapping steps.

Benefits of technology

It realizes cross-platform debugging without installing third-party debugging tools, improves debugging efficiency and simplicity, reduces technical requirements for developers, is suitable for small systems, and supports one-time development and adaptation.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119003341A_ABST
    Figure CN119003341A_ABST
Patent Text Reader

Abstract

The invention discloses an ST program cross-platform debugging method based on instrumentation and indirect compiling of a state machine, and relates to the technical field of debugging in the ST program development process, the method comprises the following steps: based on an ST program, utilizing a compiler to generate an equivalent program logic statement, and based on the ST program, executing instrumentation operation on the program logic statement to obtain a debugging logic statement; based on the program logic statement and the debugging logic statement, obtaining a C program debugging version; compiling the C program debugging version into a target code by utilizing the GCC corresponding to each CPU architecture; and based on the target code, performing flow control and variable operation by utilizing runtime. According to the method, a configuration software developer is liberated from tedious instruction learning, the debugging function can be achieved only on the program language level, dependence on third-party software and complex mapping steps are avoided, and great convenience is provided for programming and debugging in the field of industrial automation.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of debugging in the process of ST program development, and in particular to a cross-platform ST program debugging method based on plug-in and state machine indirect compilation. Background Art

[0002] ST language, full name Structured Text, is a high-level programming language based on Pascal, which emphasizes the readability and modularity of the program. It is mainly used to write executable programs in industrial automation systems. ST language supports multiple data types and rich control structures, and allows the definition of functions and procedures to improve code reusability and reduce errors. The design of ST language follows the international standard IEC61131-3, and has a syntax structure similar to that of C language. It is suitable for programming complex processing that is difficult to express in ladder diagram language. At present, the compilation methods of ST language are mainly divided into direct compilation method and indirect compilation method; such as Figure 2 As shown in the figure, the direct compilation method directly compiles the ST program into machine code, which has the advantages of high compilation efficiency and running performance. However, due to the different assembly instruction sets of different CPU architectures, corresponding ST compilers need to be constructed respectively. The direct compilation method often lacks portability and has high requirements for compiler developers. Figure 3 As shown, the indirect compilation method is to first convert the ST program into an intermediate language such as C language, and then use a compiler such as GCC to compile the intermediate language into machine code. The advantage of this method is that it utilizes the cross-platform nature of C language to achieve good portability and reduces the dependence on the developer's knowledge of assembly instructions. However, due to the increase in the intermediate language conversion step, the compilation efficiency is lower than the direct compilation method, and the running performance depends on the degree of GCC's optimization of the C program, so the running performance may be low.

[0003] There are two ST language debugging methods based on indirect compilation in the prior art, including using GDB and injecting debugging instructions into the target code. Figure 4 As shown in the figure, with the help of GDB method, the ST program is compiled into a C program through the ST2C compiler, and a mapping table between ST statements and C statement blocks is constructed. Figure 5 As shown, use the ST debugger (ST-DBG, which is part of the runtime) to control GDB to debug the C program, and use the mapping table to map the debugging information to the ST program to achieve indirect debugging; Figure 6 As shown in the figure, the debugging instruction method is to inject the debugging instruction into the target code. The ST program is first translated into a C program, and then the C program is compiled into the target code using GCC. Figure 7 As shown, the ST debugger is used to track the target code through the ptrace system call, inject breakpoint instructions into the target code, and read and write registers and memory.

[0004] Among them, the GDB method is not suitable for small systems. It requires the installation of third-party software such as GDB, and requires developers to be familiar with third-party interfaces such as GDB. The mapping steps are cumbersome and inefficient. The method of injecting debugging instructions into the target code relies on the underlying technology, requiring developers to be familiar with the ptrace system and assembly language, and does not support cross-platform. Both existing debugging methods have obvious shortcomings.

[0005] Currently, no effective solution has been proposed for the problems in the related technologies. Summary of the invention

[0006] In response to the problems in the related technology, the present invention proposes a cross-platform debugging method for ST programs based on stub insertion and state machine indirect compilation, which has the advantages of not needing to install third-party debugging tools on the target machine, supporting cross-platform, achieving one-time development and adaptation everywhere, and significantly improving debugging efficiency and simplicity, thereby solving the problems in the prior art that the GDB method is not suitable for small systems, requires developers to be familiar with third-party interfaces such as GDB, has cumbersome mapping steps and low efficiency, and the method of injecting debugging instructions into the target code relies on underlying technology, requires developers to be familiar with the ptrace system and assembly language, and does not support cross-platform.

[0007] To this end, the specific technical solution adopted by the present invention is as follows: The cross-platform debugging method of ST program based on stub insertion and state machine indirect compilation comprises the following steps: S1. Based on the ST program, use the compiler to generate equivalent program logic statements, and perform a plug-in operation on the program logic statements based on the ST program to obtain a debugging logic statement; S2, based on the program logic statements and the debugging logic statements, obtain the C program debugging version; S3, using GCC corresponding to each CPU architecture, compile the C program debug version into target code; S4. Based on the target code, use runtime to perform process control and variable operations.

[0008] Further, the program logic statements are C program statements equivalent to the ST program, and the debugging logic statements are C program statements additionally inserted to realize the debugging function, including a program description table and a stub statement; Among them, the program description table includes a user-defined type description table and a POU description table; The custom type description table is used to describe the type details of each custom variable, including enumeration type, structure type and array type; The POU description table is used to describe each POU in units of POUs, including the POU type, POU name, and POU variable list; The POU type includes the PROGRAM field, FUNCTION field, and FUNCTION BLOCK field; The POU variable list includes the variable name, type and address.

[0009] Further, based on the ST program, using a compiler to generate an equivalent program logic statement, and performing a stub operation on the program logic statement based on the ST program, obtaining a debugging logic statement includes the following steps: S11, based on the compiler, input the ST program and output the program logic statement; S12. Create a custom type description table according to the ST program; S13. Create a POU description table according to the ST program; S14. Based on the program logic statements, four positions are selected to insert stub statements to obtain debugging logic statements.

[0010] Further, according to the ST program, creating a custom type description table includes the following steps: S121. Extract the TYPE field content from the ST program; S122, extracting user-defined type information based on the extracted field content; S123. Use the C language structure to describe the extracted custom type information and input it into a new .c file.

[0011] Further, according to the ST program, creating a POU description table includes the following steps: S131, extract the PROGRAM field, FUNCTION field and FUNCTION BLOCK field from the ST program; S132, based on the extracted POU type, obtaining POU definition information; S133. Use the C language structure to describe the acquired POU definition information and input it into a new .c file.

[0012] Further, based on the program logic statements, selecting four locations to insert stub statements includes the following steps: S141, select the POU starting position of the program logic statement and insert the pile statement; S142, select the POU end position of the program logic statement, and insert the pile statement; S143, select the ST statement start position of the program logic statement, and insert the pile statement; S144. Insert the variable address registration functions one by one between the POU variable definition and the program body.

[0013] Further, based on the program logic statements and the debugging logic statements, obtaining the C program debugging version includes the following steps: S21. Based on the program logic statements and the inserted debugging logic statements, four sets of dynamic data structures are generated and dynamically maintained, wherein the four sets of dynamic data structures include a breakpoint list, a process state machine, a POU call stack, and a variable address list; S22, using runtime to control the process based on four sets of dynamic data structures; S23. Based on the four sets of dynamic data structures, the runtime is used to perform variable operations and generate a C program debugging version.

[0014] Further, based on the program logic statements and the inserted debugging logic statements, four sets of dynamic data structures are generated and dynamically maintained, including the following steps: S211. Based on user instructions, dynamically create breakpoints and maintain a breakpoint list; S212, maintaining the process state machine according to the state machine transition rules, combined with the user instructions and the actual running position of the ST program; S213, according to the ST program running, trigger the stack push event and stack pop event, and maintain the POU call stack; S214. Based on the insert stub statement, obtain the address of the variable contained in each POU in the POU call stack in the RAM, and maintain a variable address list.

[0015] Furthermore, according to the four sets of dynamic data structures, using runtime to perform process control includes the following steps: S221, using runtime to implement the specific logic of the stub statement, and using the ST debugger to check the process state machine; S222, based on the query result, determine the current state of the program; S223. Control the start and stop direction of the program according to the current state of the program.

[0016] Furthermore, according to the four sets of dynamic data structures, the runtime is used to perform variable operations and generate a C program debugging version, which includes the following steps: S231. Use the ST debugger to check the variable address list; S232, based on the query result, determine the address of the variable in the RAM; S233. According to the determined address, the vector is monitored and the preparation value is written to obtain a C program debugging version.

[0017] Furthermore, using the GCC corresponding to each CPU architecture, compiling the C program debug version into target code includes the following steps: S31, determine the target platform and GCC version, and configure the compilation environment; S32, preprocess the C program debug version and compile it into assembly code; S33, assemble the assembly code into target code, and link the target code.

[0018] Furthermore, determining the target platform and GCC version and configuring the compilation environment include the following steps: S311, determine the target platform on which the ST program will run, and select a GCC compiler version that matches the target platform; S312, setting the compilation options of the compiler according to the characteristics of the target platform; S313. Prepare the compilation environment according to the characteristics of the target platform.

[0019] Furthermore, the compiler options, including optimization level and debug information detail; Compilation environment, including library files, header files and toolchain configuration.

[0020] Furthermore, preprocessing the debug version of the C program and compiling it into assembly code includes the following steps: S321, preprocessing the C program debugging version, processing macro definitions and conditional compilation instructions; S322, checking whether there is code or library call incompatible with the target platform in the debugging version of the C program; S323. Use GCC to compile the preprocessed C program debug version into assembly code, and check whether the assembly code correctly reflects the logic of the C program.

[0021] Further, assembling the assembly code into target code and linking the target code comprises the following steps: S331, converting the assembly code into target code, and determining the number of target code files; S332. Based on the judgment result, if the number of target code files is greater than 1, all target code files are linked into one executable file using a linker of GCC; S333. Run the generated executable file on the target platform and test the debugging statements.

[0022] The beneficial effects of the present invention are: (1) The present invention inserts debugging information (insertion stubs) into the C program while compiling the ST program into the C program, and then uses the GCC corresponding to each CPU architecture to compile the C program into the target code, freeing the configuration software developers from the tedious CPU instruction learning. The debugging function can be realized only at the C language level, avoiding the reliance on third-party software and complicated mapping steps. At the same time, it solves the high requirements of the underlying technology on developers and the problem of non-cross-platform, which provides great convenience for programming and debugging in the field of industrial automation.

[0023] (2) The present invention inserts debugging logic statements into the C program instead of using GDB for indirect debugging or injecting debugging instructions into the target code. The debugger is cross-platform and achieves one-time development and adaptation everywhere without the need for separate adaptation for each CPU instruction set.

[0024] (3) The present invention does not require the installation of third-party debugging tools such as GDB on the target machine, and can also be applied to small systems. At the same time, the developers of the debugger do not need to be familiar with third-party interfaces such as GDB, and no mapping is required during debugging, thereby significantly improving the efficiency and simplicity of debugging. BRIEF DESCRIPTION OF THE DRAWINGS

[0025] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.

[0026] Figure 1 It is a flowchart of a cross-platform debugging method of an ST program based on stub insertion and state machine indirect compilation according to an embodiment of the present invention; Figure 2 It is a flowchart of the direct compilation method; Figure 3 It is a flowchart of the indirect compilation method; Figure 4 It is a diagram of the compilation process using the GDB method; Figure 5 It is a schematic diagram of the debugging framework using the GDB method; Figure 6 It is a compile flow diagram of the method of injecting debug instructions into target code; Figure 7 It is a schematic diagram of a debugging framework for injecting debugging instructions into target code; Figure 8 It is a specific implementation diagram of the ST program cross-platform debugging method based on stub insertion and state machine indirect compilation according to an embodiment of the present invention; Fig. 9 It is a schematic diagram of the composition of debugging logic statements in the ST program cross-platform debugging method based on stub and state machine indirect compilation according to an embodiment of the present invention; Fig.10 It is a schematic diagram of implementing and calling the debugging SDK in the ST program cross-platform debugging method based on stub and state machine indirect compilation according to an embodiment of the present invention; Fig.11It is an overview of the contents of debugging logic statements inserted by the compiler into the C program in the ST program cross-platform debugging method based on stub insertion and state machine indirect compilation according to an embodiment of the present invention; Fig.12 It is a schematic diagram of four sets of dynamic data structures maintained by runtime in a cross-platform debugging method of ST program based on stub and state machine indirect compilation according to an embodiment of the present invention; Fig.13 It is a schematic diagram of the process state machine transition rules in the ST program cross-platform debugging method based on stub and state machine indirect compilation according to an embodiment of the present invention; Fig.14 It is a schematic diagram of process control and variable operation in a cross-platform debugging method of an ST program based on plug-in and state machine indirect compilation according to an embodiment of the present invention. DETAILED DESCRIPTION

[0027] To further illustrate each embodiment, the present invention provides drawings, which are part of the disclosure of the present invention and are mainly used to illustrate the embodiments. They can be used in conjunction with the relevant descriptions in the specification to explain the operating principles of the embodiments. With reference to these contents, ordinary technicians in the field should be able to understand other possible implementations and advantages of the present invention. The components in the figures are not drawn to scale, and similar component symbols are generally used to represent similar components.

[0028] According to an embodiment of the present invention, a cross-platform debugging method for ST programs based on stub insertion and state machine indirect compilation is provided.

[0029] The present invention is further described with reference to the accompanying drawings and specific embodiments. Figure 1 As shown, according to one embodiment of the present invention, a cross-platform debugging method for ST programs based on indirect compilation of stubs and state machines is provided. The cross-platform debugging method for ST programs based on indirect compilation of stubs and state machines comprises the following steps: S1. Based on the ST program, use the compiler to generate equivalent program logic statements, and perform a plug-in operation on the program logic statements based on the ST program to obtain a debugging logic statement; S2, based on the program logic statements and the debugging logic statements, obtain the C program debugging version; S3, using GCC corresponding to each CPU architecture, compile the C program debug version into target code; S4. Based on the target code, use runtime to perform process control and variable operations.

[0030] In addition, it should be noted that ST is a programming language written specifically for industrial automation systems and programmable logic controllers (PLCs). Its design and implementation follow the international standard IEC61131-3 and has a similar grammatical structure to Pascal. The characteristics of ST include: ① Structured Text (ST), which emphasizes the readability and modularity of the program; ② Based on Pascal, although ST is based on Pascal syntax, it has been optimized and modified to meet the needs of industrial control; ③ Support for multiple data types, ST supports multiple data types such as integers, real numbers, Boolean values, and also provides a series of data type conversions; ④ Control structures, including conditional statements, loop statements, repetition statements, etc., are used to control the execution flow of the program; ⑤ Operators and expressions, supporting basic mathematical operators such as addition, subtraction, multiplication, division, etc., as well as more complex expressions; ⑥ Functions and procedures, allowing the definition of functions and procedures to simplify code reuse and reduce errors; ⑦ Compatibility with other programming languages, although ST has its own characteristics, it is also compatible with some syntax and concepts of other programming languages, such as bit operations in C.

[0031] A programmable logic controller (PLC) is a digital computing controller with a microprocessor for automated control. It can load control instructions into memory at any time for storage and execution. A programmable controller consists of functional units such as a CPU, instruction and data memory, input / output interface, power supply, and digital-to-analog conversion. Early programmable logic controllers only had the function of logic control, so they were named programmable logic controllers. Later, with continuous development, these computer modules with simple functions at the beginning have various functions including logic control, timing control, analog control, and multi-machine communication.

[0032] GNU Debugger (GDB) is a program debugging tool widely used in UNIX and UNIX-like operating systems. As part of the GNU tool chain, GDB supports programs written in multiple programming languages, especially C, C++, and Fortran. GDB works with tools such as GCC (GNU Compiler Collection) to provide developers with a powerful development environment.

[0033] Specifically, ST language debugging includes the following: 1) Process control, including breakpoint, next step, entering POU and exiting POU; 2) Variable operations, including variable monitoring and preparing value writing.

[0034] Specifically, to implement the debugging of the above two types of ST languages, such as Figure 8 As shown, the present invention does not use GDB for indirect debugging, nor does it change the target code. Instead, it inserts debugging logic statements into the C code during the compilation phase, and uses these debugging logic statements in the C language form as support to implement debugging of ST.

[0035] Specifically, "process control" and "variable operation" are implemented by the compiler and runtime. To this end, the present invention uses the compiler to insert a "program description table" and a "stub statement", and uses the runtime to dynamically maintain four sets of dynamic data structures in RAM.

[0036] Specifically, program logic statements are prior art. That is, without considering debugging, the input of the compiler is the ST program, and the output is the C program. The output C program is the so-called program logic statement, which is completely equivalent to the ST program and has the same meaning, just like inputting "book" and outputting "book".

[0037] Specifically, Fig. 9 As shown, the debugging logic statements are divided into two categories: "program description table" and "stub statement". In this embodiment, Fig.10 As shown, the runtime is used to implement the specific logic of the above-mentioned "stub statement" (hereinafter referred to as the "debugging package SDK"), and a state machine is constructed during the running phase to support these dynamic logics.

[0038] In one embodiment, the program logic statements are C program statements equivalent to the ST program, and the debugging logic statements are C program statements additionally inserted to implement the debugging function, including a program description table and a stub statement; Among them, the program description table includes a user-defined type description table and a POU description table; The custom type description table is used to describe the type details of each custom variable, including enumeration type, structure type and array type; The POU description table is used to describe each POU in units of POUs, including the POU type, POU name, and POU variable list; The POU type includes the PROGRAM field, FUNCTION field, and FUNCTION BLOCK field; The POU variable list includes the variable name, type and address.

[0039] Specifically, in this embodiment, the program description table includes a "custom type description table" and a "POU description table", and the contents of these two types of tables are based on the international standard IEC61131-3.

[0040] In one embodiment, based on the ST program, using a compiler to generate an equivalent program logic statement, and performing a stub operation on the program logic statement based on the ST program to obtain a debugging logic statement includes the following steps: S11, based on the compiler, input the ST program and output the program logic statement; S12. Create a custom type description table according to the ST program; S13. Create a POU description table according to the ST program; S14. Based on the program logic statements, select four locations to insert stub statements.

[0041] Specifically, in this embodiment, in order to implement "process control" and "variable operation", the compiler inserts a "program description table" and a "stub statement" at a specific location of the C program.

[0042] In one embodiment, according to the ST program, creating a user-defined type description table includes the following steps: S121. Extract the TYPE field content from the ST program; S122, extracting user-defined type information based on the extracted field content; S123. Use the C language structure to describe the extracted custom type information and input it into a new .c file.

[0043] Specifically, the "custom type description table" describes in detail the details of each custom variable type. The types are divided into three categories, including enumerations, structures and arrays. In this embodiment, the steps for the compiler to create a "custom type description table" are: extract the "TYPE" field content from the ST program and extract the custom type information; describe this information with a C language structure and input it into a new .c file.

[0044] In one embodiment, according to the ST program, creating a POU description table includes the following steps: S131, extract the PROGRAM field, FUNCTION field and FUNCTION BLOCK field from the ST program; S132, based on the extracted POU type, obtaining POU definition information; S133. Use the C language structure to describe the acquired POU definition information and input it into a new .c file.

[0045] Specifically, the "POU description table" describes each POU in detail in units of POUs, and includes POU types (including PROGRAM, FUNCTION BLOCK, and FUNCTION), POU names, and POU variable lists (including variable names, types, and addresses). In this embodiment, the compiler creates the "POU description table" by extracting the contents of the "PROGRAM", "FUNCTION", and "FUNCTION BLOCK" fields from the ST program to obtain POU definition information; describing this information in a C language structure and inputting it into a new .c file.

[0046] In one embodiment, based on the program logic statements, selecting four locations to insert stub statements includes the following steps: S141, select the POU starting position of the program logic statement and insert the pile statement; S142, select the POU end position of the program logic statement, and insert the pile statement; S143, select the ST statement start position of the program logic statement, and insert the pile statement; S144. Insert the variable address registration functions one by one between the POU variable definition and the program body.

[0047] Specifically, Fig.11 As shown, the compiler inserts stub function calls at four locations of the C program, including the start of the POU, the end of the POU, the start of the ST statement, and inserts the address registration functions of the variables one by one between the POU variable definition and the program body.

[0048] In one embodiment, obtaining a C program debugging version based on program logic statements and debugging logic statements includes the following steps: S21. Based on the program logic statements and the inserted debugging logic statements, four sets of dynamic data structures are generated and dynamically maintained, wherein the four sets of dynamic data structures include a breakpoint list, a process state machine, a POU call stack, and a variable address list; S22, using runtime to control the process based on four sets of dynamic data structures; S23. Based on the four sets of dynamic data structures, the runtime is used to perform variable operations and generate a C program debugging version.

[0049] Specifically, Fig.12 As shown in the figure, the runtime dynamically maintains four sets of dynamic data structures in RAM (including breakpoint list, process state machine, POU call stack and variable address list).

[0050] In one embodiment, based on program logic statements and inserted debugging logic statements, generating four sets of dynamic data structures and dynamically maintaining them includes the following steps: S211. Based on user instructions, dynamically create breakpoints and maintain a breakpoint list; Specifically, users dynamically create breakpoints when debugging ST programs, and the runtime maintains a breakpoint list based on user instructions.

[0051] S212, maintaining the process state machine according to the state machine transition rules, combined with the user instructions and the actual running position of the ST program; Specifically, the runtime maintains the process state machine according to the user instructions and the actual location of the ST program. The state machine transition rules are as follows: Fig.13 As shown, the so-called "state machine" is to define a series of "states" ( Fig.13 circles) and "events" ( Fig.13 The text on the arrow) is used to connect two states with an arrow, for example ①-A->② means that if event A occurs when the system is in state 1, the system state transitions to 2 ("transition" means "become"); begin and end are the initial and end states respectively, and the program is not running. The present invention does not discuss these two situations.

[0052] Specifically, the running status is divided into two categories. One is that the program is in the pause state, waiting for user commands (want_cmd); the other is that the program is in the running state, expecting to run to a suitable position and pause, which includes four sub-states: ① expecting to run to the next breakpoint position (want_bp); ② expecting to enter the called POU (want_push); ③ expecting to jump out of the POU (want_pop); ④ expecting to run to the next line of code (want_next).

[0053] Specifically, the meaning of each state is as follows: Begin: program start; End: program exit; Want_cmd: wait for user instructions; Want_bp: wait for running to breakpoint; Want_next: wait for running to next statement; Want_push: wait for running to POU stack; Want_pop: wait for running to POU stack.

[0054] Specifically, events are divided into two categories. One is when the program is in the stopped state and the user enters a specific command, an event is generated, including: ① The user enters run (cmd_run) to start the program; ② The user enters continue (cmd_continue) to run the program to the next breakpoint; ③ The user enters next (cmd_next) to run the program to the next line of code; ④ The user enters step (cmd_step) to enter the called POU; ⑤ The user enters finish (cmd_finish) to jump out of the current POU.

[0055] Specifically, another type of event is when the program is in the running state. When it runs to the stub statement, an event is generated, including: ① When it runs to the breakpoint, a meet_bp event is generated; ② When it runs to a new line of code, a meet_st_line event is generated; ③ When it runs to the POU push position, a meet_push event is generated; ④ When it runs to the set POU pop position, a meet_this_pop event is generated; ⑤ When it runs to the non-set POU pop position, a meet_other_pop event is generated.

[0056] Specifically, the meaning of each event is as follows: cmd_run: the user inputs run to start the program; cmd_continue: the user inputs continue to run the program to the next breakpoint; cmd_next: the user inputs next to run the program to the next line of code; cmd_step: the user inputs step to let the program enter the called POU; cmd_finish: the user inputs finish to let the program jump out of the current POU; meet_bp: run to the breakpoint; meet_st_line: run to a new line of code; meet_push: run to the POU push position; meet_this_pop: run to the set POU pop position; meet_other_pop: run to a non-set POU pop position.

[0057] S213, according to the ST program running, trigger the stack push event and stack pop event, and maintain the POU call stack; Specifically, when the runtime runs the ST program, it maintains the POU call stack. When the program enters a POU, a push event is triggered, and when the program exits, a pop event is triggered.

[0058] Specifically, the functions of the call stack are: responding to user queries on the POU call stack; providing a basis for adding and deleting temporary variable information in the variable address list.

[0059] S214. Based on the insert stub statement, obtain the address of the variable contained in each POU in the POU call stack in the RAM, and maintain a variable address list.

[0060] Specifically, the runtime maintains a list of POU variable addresses during runtime. This table is created by the runtime and is based on POUs. The table records the addresses of the variables contained in each POU in the above POU call stack in RAM. The variable addresses in each POU are: ①PROGRAM variable address is the resident address; ②FUNCTION BLOCK variable address is the current instance address; ③FUNCTION variable address is the address of the temporary variable when the PLC runs to the FUNCTION.

[0061] Specifically, the variable address is provided by the compiler in the process of "inserting variable address registration functions one by one between the POU variable definition and the program body" in step S144.

[0062] In one embodiment, based on four sets of dynamic data structures, using runtime to perform process control includes the following steps: S221, using runtime to implement the specific logic of the stub statement, and using the ST debugger to check the process state machine; S222, based on the query result, determine the current state of the program; S223. Control the start and stop direction of the program according to the current state of the program.

[0063] Specifically, Fig.14 As shown, when the program runs to the stub statement, the "Debug SDK" generates a corresponding debugging event, which causes changes in the process state machine, POU call stack and variable address list.

[0064] Specifically, the ST debugger (ST-DBG) is embedded in the runtime.

[0065] Specifically, the debugging control module in the ST debugger determines the current state of the program by consulting the "process state machine" to control the start and stop direction of the program.

[0066] In one embodiment, according to four sets of dynamic data structures, using runtime to perform variable operations, generating a C program debugging version includes the following steps: S231. Use the ST debugger to check the variable address list; S232, based on the query result, determine the address of the variable in the RAM; S233. According to the determined address, the vector is monitored and the preparation value is written to obtain a C program debugging version.

[0067] Specifically, the variable operation module in the ST debugger can accurately find the address of the variable in RAM by consulting the "variable address list", thereby realizing monitoring and preparing value writing.

[0068] In one embodiment, using GCC corresponding to each CPU architecture, compiling the C program debug version into target code includes the following steps: S31, determine the target platform and GCC version, and configure the compilation environment; S32, preprocess the C program debug version and compile it into assembly code; S33, assemble the assembly code into target code, and link the target code.

[0069] In addition, it should be noted that the GNU Compiler Collection (GCC) is a powerful compiler that supports multiple programming languages ​​​​and platforms. When using GCC for compiling, different command line options can be used to control different aspects of the compilation process, such as: ①-E option, this option tells GCC to perform the preprocessing stage. The preprocessor will process the macro definitions, conditional compilation instructions (such as #ifdef, #ifndef, #endif), etc. in the source code file, and then output the results to the standard output or a specified file. The -E option is usually used to view the preprocessed source code rather than to generate target code; ②-S option, this option instructs GCC not to generate assembly code files during the compilation process, but to generate target code directly from the source code. This is usually used to optimize code size because it omits all possible symbol information and only generates the final machine code. When using the -S option, GCC will not call The compiler does not use the assembler, but handles the generation of assembly code internally; ③-o option, this option is used to specify the name of the output file. This option can be used when you want to save the compiled result (such as an object code file or an executable file) as a specific file name; ④-O0 option, this option is used to specify the optimization level. -O0 means no optimization, which is usually used for debugging purposes because it can generate machine code that is closer to the source code, making debugging easier. The compiler does not try to optimize the performance of the code, but maintains the original structure of the code so that the debugger can accurately map each step of the program execution to the source code; ⑤-g option, this option is used to generate debugging information. When the -g option is used, GCC will generate additional debugging information during the compilation process. This information can be used by the debugger to provide more detailed debugging functions, such as viewing the value of variables, single-stepping programs, etc.

[0070] All the options used in this embodiment are standard functions of the GCC compiler and belong to the prior art. They provide developers with the ability to perform fine control during the compilation process to meet different development and debugging requirements.

[0071] In one embodiment, determining the target platform and GCC version and configuring the compilation environment includes the following steps: S311, determine the target platform on which the ST program will run, and select a GCC compiler version that matches the target platform; S312, setting the compilation options of the compiler according to the characteristics of the target platform; S313. Prepare the compilation environment according to the characteristics of the target platform.

[0072] Specifically, determine the target hardware platform on which the ST program will run, such as the x86, ARM, or MIPS architecture, select the GCC compiler version that matches the target platform, and ensure that the compiler supports the required CPU architecture and operating system.

[0073] Specifically, according to the characteristics of the target platform, set the compiler options, including optimization level and detail of debugging information, and then prepare the compilation environment, including necessary library files, header files, and toolchain configuration.

[0074] Specifically, in this embodiment, the target platform is an embedded device of ARM architecture, and finally the GCC ARM Embedded compiler is selected, and then the compilation options are configured for the ARM platform, including enabling the -O0 optimization level for debugging, and adding the -g option to generate debugging information.

[0075] In one embodiment, the compilation options of the compiler include optimization level and debugging information detail; Compilation environment, including library files, header files and toolchain configuration.

[0076] In one embodiment, preprocessing the debug version of the C program and compiling it into assembly code includes the following steps: S321, preprocessing the C program debugging version, processing macro definitions and conditional compilation instructions; S322, checking whether there is code or library call incompatible with the target platform in the debugging version of the C program; S323. Use GCC to compile the preprocessed C program debug version into assembly code, and check whether the assembly code correctly reflects the logic of the C program.

[0077] Specifically, the debugging version of the C program is preprocessed to process macro definitions and conditional compilation instructions, and it is checked whether there are codes or library calls in the debugging version of the C program that are incompatible with the target platform. Then, GCC is used to compile the preprocessed debugging version of the C program into assembly code, and it is checked whether the assembly code correctly reflects the logic of the C program.

[0078] Specifically, in the above embodiment, the -E option of GCC is used to preprocess the debug version of the C program to generate a preprocessed intermediate file, and then the -S option of GCC is used to remove all possible symbol information to reduce the size of the target file finally generated.

[0079] In one embodiment, assembling the assembly code into object code and linking the object code comprises the following steps: S331, converting the assembly code into target code, and determining the number of target code files; S332. Based on the judgment result, if the number of target code files is greater than 1, all target code files are linked into one executable file using a linker of GCC; S333. Run the generated executable file on the target platform and test the debugging statements.

[0080] Specifically, the assembly code is converted into target code, i.e. machine code, to ensure that the target code can be correctly executed on the target platform. If there are multiple target code files, the GCC linker is used to link them into an executable file to resolve symbol conflicts and dependency issues that may arise during the linking process.

[0081] Specifically, in the above embodiment, the target code object file is generated using the -c option of GCC, and the output file name is specified using the -o option, and then the name of the final executable file is specified using the -o option of GCC, and the necessary libraries are linked using the -l option.

[0082] Specifically, finally, run the generated executable file on the target platform to verify its functionality and performance, and test the debugging statements to ensure that they can provide correct information during the debugging process.

[0083] In summary, with the help of the above technical solution of the present invention, while compiling the ST program into a C program, debugging information (insertion) is inserted into the C program, and then the C program is compiled into the target code using the GCC corresponding to each CPU architecture, freeing the configuration software developer from the tedious CPU instruction learning, and the debugging function can be realized only at the C language level, avoiding dependence on third-party software and complex mapping steps, while solving the high requirements of the underlying technology on developers and the problem of non-cross-platform, which provides great convenience for programming and debugging in the field of industrial automation; at the same time, the present invention inserts debugging logic statements into the C program instead of indirectly debugging with the help of GDB or injecting debugging instructions into the target code. The debugger has cross-platform characteristics, realizes one-time development and adaptation everywhere, and does not need to be adapted separately for each CPU instruction set; in addition, the present invention does not need to install third-party debugging tools such as GDB on the target machine, and small systems can also be applied. With the help of the present invention, the developers of the debugger do not need to be familiar with third-party interfaces such as GDB, and no mapping is required during debugging, thereby significantly improving the efficiency and simplicity of debugging.

[0084] The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principle of the present invention should be included in the protection scope of the present invention.

Claims

1. ST program cross-platform debugging method based on stub and state machine indirect compilation, It is characterized in that The ST program cross-platform debugging method based on stub and state machine indirect compilation includes the following steps: S1. Based on the ST program, use the compiler to generate equivalent program logic statements, and perform a plug-in operation on the program logic statements based on the ST program to obtain a debugging logic statement; S2, based on the program logic statements and the debugging logic statements, obtain the C program debugging version; S3, using GCC corresponding to each CPU architecture, compile the C program debug version into target code; S4. Based on the target code, use runtime to perform process control and variable operations.

2. The ST program cross-platform debugging method based on plug-in and state machine indirect compilation according to claim 1, It is characterized in that The program logic statements are C program statements equivalent to ST programs, and the debugging logic statements are C program statements additionally inserted to implement debugging functions, including a program description table and a stub statement; Wherein, the program description table includes a user-defined type description table and a POU description table; The user-defined type description table is used to describe the type details of each user-defined variable, including enumeration type, structure type and array type; The POU description table is used to describe each POU in units of POUs, including POU type, POU name and POU variable list; The POU type includes a PROGRAM field, a FUNCTION field and a FUNCTION BLOCK field; The POU variable list includes variable name, type and address.

3. The ST program cross-platform debugging method based on plug-in and state machine indirect compilation according to claim 2, It is characterized in that The method of generating equivalent program logic statements based on the ST program by using a compiler, and performing a plugging operation on the program logic statements based on the ST program to obtain the debugging logic statements includes the following steps: S11, based on the compiler, input the ST program and output the program logic statement; S12. Create a custom type description table according to the ST program; S13. Create a POU description table according to the ST program; S14. Based on the program logic statements, four positions are selected to insert stub statements to obtain debugging logic statements.

4. The ST program cross-platform debugging method based on plug-in and state machine indirect compilation according to claim 3, It is characterized in that According to the ST program, creating a custom type description table includes the following steps: S121. Extract the TYPE field content from the ST program; S122, extracting user-defined type information based on the extracted field content; S123. Use the C language structure to describe the extracted custom type information and input it into a new .c file.

5. The ST program cross-platform debugging method based on plug-in and state machine indirect compilation according to claim 3, It is characterized in that According to the ST program, creating a POU description table includes the following steps: S131, extract the PROGRAM field, FUNCTION field and FUNCTION BLOCK field from the ST program; S132, based on the extracted POU type, obtaining POU definition information; S133. Use the C language structure to describe the acquired POU definition information and input it into a new .c file.

6. The ST program cross-platform debugging method based on plug-in and state machine indirect compilation according to claim 3, It is characterized in that The method of selecting four positions to insert pile statements based on program logic statements comprises the following steps: S141, select the POU starting position of the program logic statement and insert the pile statement; S142, select the POU end position of the program logic statement, and insert the pile statement; S143, select the ST statement start position of the program logic statement, and insert the pile statement; S144. Insert the variable address registration functions one by one between the POU variable definition and the program body.

7. The ST program cross-platform debugging method based on plug-in and state machine indirect compilation according to claim 1, It is characterized in that The method of obtaining the C program debugging version based on the program logic statement and the debugging logic statement includes the following steps: S21. Based on the program logic statements and the inserted debugging logic statements, four sets of dynamic data structures are generated and dynamically maintained, wherein the four sets of dynamic data structures include a breakpoint list, a process state machine, a POU call stack, and a variable address list; S22, using runtime to control the process based on four sets of dynamic data structures; S23. Based on the four sets of dynamic data structures, the runtime is used to perform variable operations and generate a C program debugging version.

8. The ST program cross-platform debugging method based on plug-in and state machine indirect compilation according to claim 7, It is characterized in that The process of generating four sets of dynamic data structures based on program logic statements and inserted debugging logic statements and dynamically maintaining them includes the following steps: S211. Based on user instructions, dynamically create breakpoints and maintain a breakpoint list; S212, maintaining the process state machine according to the state machine transition rules, combined with the user instructions and the actual running position of the ST program; S213, according to the ST program running, trigger the stack push event and stack pop event, and maintain the POU call stack; S214. Based on the insert stub statement, obtain the address of the variable contained in each POU in the POU call stack in the RAM, and maintain a variable address list.

9. The ST program cross-platform debugging method based on plug-in and state machine indirect compilation according to claim 7, It is characterized in that The process control using runtime according to the four sets of dynamic data structures includes the following steps: S221, using runtime to implement the specific logic of the stub statement, and using the ST debugger to check the process state machine; S222, based on the query result, determine the current state of the program; S223. Control the start and stop direction of the program according to the current state of the program.

10. The ST program cross-platform debugging method based on plug-in and state machine indirect compilation according to claim 7, It is characterized in that The method of generating a C program debugging version by using runtime to perform variable operations based on four sets of dynamic data structures includes the following steps: S231. Use the ST debugger to check the variable address list; S232, based on the query result, determine the address of the variable in the RAM; S233. According to the determined address, the vector is monitored and the preparation value is written to obtain a C program debugging version.

11. The ST program cross-platform debugging method based on plug-in and state machine indirect compilation according to claim 1, It is characterized in that The method of compiling the C program debug version into target code using GCC corresponding to each CPU architecture includes the following steps: S31, determine the target platform and GCC version, and configure the compilation environment; S32, preprocess the C program debug version and compile it into assembly code; S33, assemble the assembly code into target code, and link the target code.

12. The ST program cross-platform debugging method based on plug-in and state machine indirect compilation according to claim 11, It is characterized in that Determining the target platform and GCC version and configuring the compilation environment includes the following steps: S311, determine the target platform on which the ST program will run, and select a GCC compiler version that matches the target platform; S312, setting the compilation options of the compiler according to the characteristics of the target platform; S313. Prepare the compilation environment according to the characteristics of the target platform.

13. The ST program cross-platform debugging method based on plug-in and state machine indirect compilation according to claim 12, It is characterized in that Compilation options of the compiler, including optimization level and debugging information detail; The compilation environment includes library files, header files and tool chain configuration.

14. The ST program cross-platform debugging method based on plug-in and state machine indirect compilation according to claim 11, It is characterized in that The preprocessing of the C program debug version and compiling it into assembly code includes the following steps: S321, preprocessing the C program debugging version, processing macro definitions and conditional compilation instructions; S322, checking whether there is code or library call incompatible with the target platform in the debugging version of the C program; S323. Use GCC to compile the preprocessed C program debug version into assembly code, and check whether the assembly code correctly reflects the logic of the C program.

15. The ST program cross-platform debugging method based on plug-in and state machine indirect compilation according to claim 11, It is characterized in that The step of assembling the assembly code into target code and linking the target code comprises the following steps: S331, converting the assembly code into target code, and determining the number of target code files; S332. Based on the judgment result, if the number of target code files is greater than 1, all target code files are linked into one executable file using a linker of GCC; S333. Run the generated executable file on the target platform and test the debugging statements.