Program breakpoint setting method and device, storage medium and electronic equipment

By setting up BKPT instructions and status query packages in the communication processing thread of the PLC, the breakpoint debugging function of the PLC is realized, solving the problem that the PLC based on the microkernel operating system cannot realize breakpoint debugging, and providing a technical solution for debugging function.

CN119127698APending Publication Date: 2024-12-13HANGZHOU HOLLYSYS AUTOMATION
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202411236830.4
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-09-04
Publication Date
2024-12-13

AI Technical Summary

Technical Problem

PLC based on microkernel operating system cannot realize the breakpoint debugging function, which brings difficulties to the development of upper-level functions.

Method used

By determining the control instruction type in the communication processing thread of the PLC, setting the breakpoint address in the target code area to the BKPT instruction, suspending the target running thread, and notifying the running status to the PLC programming software through the status query package, realizing the breakpoint debugging function .

Benefits of technology

The breakpoint debugging function of PLC is implemented, which solves the problem that PLC cannot implement breakpoint debugging, allowing users to debug the target program and view the variable value at breakpoints.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119127698A_ABST
    Figure CN119127698A_ABST
Patent Text Reader

Abstract

The invention discloses a program breakpoint setting method and device, a storage medium and electronic device.The method comprises the steps that under the condition that a control instruction sent by PLC programming software is obtained through a communication processing thread of a PLC, the instruction type of the control instruction is determined; under the condition that the instruction type is used for indicating that the control instruction is a breakpoint setting instruction, determining a to-be-set first breakpoint address from the control instruction; and an instruction on the first breakpoint address in a target code area is set as a breakpoint BKPT instruction, and an instruction set in the target code area forms a target program. By adopting the technical scheme, the problem that the PLC cannot realize a breakpoint debugging function is solved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of data processing technology, and in particular to a method and device for setting program breakpoints, a storage medium, and an electronic device. Background Art

[0002] Programmable Logic Controller (PLC) is widely used in the field of industrial automation and is a key device for the automated operation of enterprise production. The breakpoint debugging function is an auxiliary debugging function of PLC, which is mainly used to help troubleshoot logical errors in PLC user programs.

[0003] However, the microkernel operating system only provides five functions: thread management, inter-process communication, virtual memory space, handle space, and device basic unit (interrupt object), and no other functions. Therefore, the PLC based on the microkernel operating system cannot provide breakpoint debugging function, which brings many difficulties to the development of upper-level functions.

[0004] With regard to the problem in the related technology that PLC cannot realize the breakpoint debugging function, no effective solution has been proposed yet. Summary of the invention

[0005] The embodiments of the present application provide a method and device for setting program breakpoints, a storage medium, and an electronic device, so as to at least solve the problem that the PLC cannot implement the breakpoint debugging function.

[0006] According to an embodiment of the present application, a method for setting a program breakpoint is provided, comprising: in a case where a control instruction sent by a PLC programming software is obtained through a communication processing thread of a programmable logic controller (PLC), determining the instruction type of the control instruction; in a case where the instruction type is used to indicate that the control instruction is a breakpoint setting instruction, determining a first breakpoint address to be set from the control instruction; and setting the instruction at the first breakpoint address in a target code area as a breakpoint BKPT instruction, wherein the instruction set in the target code area constitutes a target program.

[0007] In an exemplary embodiment, setting the instruction at the first breakpoint address in the target code area as a breakpoint BKPT instruction includes: suspending the target running thread in the PLC, wherein the target running thread is used to execute the target program; when the instruction at the first breakpoint address in the target code area is a NOP instruction, replacing the NOP instruction at the first breakpoint address with the BKPT instruction, and resuming the operation of the target running thread.

[0008] In an exemplary embodiment, after setting the instruction at the first breakpoint address in the target code area as a breakpoint BKPT instruction, the method further includes: obtaining the current breakpoint address of the target program; setting the current breakpoint address in a status query packet, and sending the status query packet carrying the current breakpoint address to the PLC programming software through the communication processing thread, wherein the status query packet is used to notify the PLC programming software of the operating status of the PLC.

[0009] In an exemplary embodiment, obtaining the current breakpoint address of the target program includes: determining whether the target running thread is in a suspended state, wherein the target running thread is used to execute instructions in the target code area; if the target running thread is in a suspended state, setting the current breakpoint address to the program counter PC address of the target running thread; if the target running thread is not in a suspended state, setting the current breakpoint address to a preset address.

[0010] In an exemplary embodiment, after determining the instruction type of the control instruction, the method further includes: when the control instruction is a skip current breakpoint instruction, determining a second breakpoint address to be skipped from the skip current breakpoint instruction; when the instruction at the second breakpoint address in the target code area is the BKPT instruction, and the PC address of the target running thread is the second breakpoint address, setting the PC address of the target running thread to a specified address, wherein the specified address is equal to the second breakpoint address plus a preset offset, and the target running thread is used to execute instructions in the target code area.

[0011] In an exemplary embodiment, after determining the instruction type of the control instruction, the method further includes: when the control instruction is a delete breakpoint instruction, determining a third breakpoint address to be deleted from the delete breakpoint instruction; suspending a target running thread in the PLC, wherein the target running thread is used to execute the target program; when the instruction at the third breakpoint address in the target code area is the BKPT instruction, replacing the BKPT instruction at the third breakpoint address with a NOP instruction, and resuming the operation of the target running thread.

[0012] In an exemplary embodiment, before obtaining the control instructions sent by the PLC programming software, the method also includes: obtaining the assembly code sent by the PLC programming software, wherein each assembly code block in the assembly code has a NOP instruction at the end; and storing the assembly code in the target code area.

[0013] According to another embodiment of the embodiments of the present application, a program breakpoint setting device is also provided, including: a first determination module, used to determine the instruction type of the control instruction when the control instruction sent by the PLC programming software is obtained through the communication processing thread of the programmable logic controller PLC; a second determination module, used to determine the first breakpoint address to be set from the breakpoint setting instruction when the instruction type is used to indicate that the control instruction is a breakpoint setting instruction; a setting module, used to set the instruction at the first breakpoint address in the target code area as a breakpoint BKPT instruction, wherein the instruction set in the target code area constitutes a target program.

[0014] According to another aspect of the embodiments of the present application, a computer-readable storage medium is provided, in which a computer program is stored, wherein the computer program is configured to execute the above-mentioned method for setting program breakpoints during runtime.

[0015] According to another aspect of an embodiment of the present application, an electronic device is provided, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the method for setting program breakpoints through the computer program.

[0016] In an embodiment of the present application, when the communication processing thread of the PLC obtains the control instruction for setting a breakpoint sent by the PLC programming software, the first breakpoint address to be set is determined from the control instruction, and the instruction at the first breakpoint address in the target code area is set as a breakpoint BKPT instruction, wherein the instruction set in the target code area constitutes a target program, so that when the target program runs to the instruction at the first breakpoint address, the CPU is triggered to enter an exception and the target program is suspended. The above-mentioned technical solution is adopted to solve the problem that the PLC cannot realize the breakpoint debugging function, and the technical effect of realizing the breakpoint debugging function of the PLC is achieved. BRIEF DESCRIPTION OF THE DRAWINGS

[0017] The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with the present application and, together with the description, serve to explain the principles of the present application.

[0018] In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, for ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative labor.

[0019] Figure 1 It is a hardware environment diagram of a method for setting program breakpoints in an embodiment of the present application;

[0020] Figure 2 is a flow chart of a method for setting program breakpoints according to an embodiment of the present application;

[0021] Figure 3 is a structural schematic diagram of a PLC according to an embodiment of the present application;

[0022] Figure 4 It is a structural block diagram of a device for setting program breakpoints according to an embodiment of the present application. DETAILED DESCRIPTION

[0023] In order to enable those skilled in the art to better understand the solution of the present application, the technical solution in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work should fall within the scope of protection of the present application.

[0024] It should be noted that the terms "first", "second", etc. in the specification and claims of the present application and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It should be understood that the data used in this way can be interchangeable where appropriate, so that the embodiments of the present application described herein can be implemented in an order other than those illustrated or described herein. In addition, the terms "including" and "having" and any of their variations are intended to cover non-exclusive inclusions, for example, a process, method, subsystem, product or device comprising a series of steps or units is not necessarily limited to those steps or units clearly listed, but may include other steps or units that are not clearly listed or inherent to these processes, methods, products or devices.

[0025] According to one aspect of an embodiment of the present application, a method for setting a program breakpoint is provided. The method for setting a program breakpoint is widely used in scenarios such as software system operation. Optionally, in this embodiment, the method for setting a program breakpoint can be applied to Figure 1 In the hardware environment composed of the terminal device 102 and the server 104 shown in FIG. Figure 1 As shown, the server 104 is connected to the terminal device 102 via a network, and can be used to provide services (such as application services, etc.) for the terminal or a client installed on the terminal. A database can be set on the server or independently of the server to provide data storage services for the server 104. Cloud computing and / or edge computing services can be configured on the server or independently of the server to provide data computing services for the server 104.

[0026] The above network may include but is not limited to at least one of the following: wired network, wireless network. The above wired network may include but is not limited to at least one of the following: wide area network, metropolitan area network, local area network, and the above wireless network may include but is not limited to at least one of the following: WIFI (Wireless Fidelity), Bluetooth. The terminal device 102 may not be limited to smart devices such as PCs, mobile phones, tablet computers, and engineering equipment such as motors.

[0027] In this embodiment, a method for setting a program breakpoint is provided, which is applied to the above-mentioned terminal device (such as a PLC controller). Figure 2 : is a flow chart of a method for setting a program breakpoint according to an embodiment of the present application, the process includes the following steps:

[0028] Step S202, when a control instruction sent by the PLC programming software is obtained through the communication processing thread of the programmable logic controller PLC, determining the instruction type of the control instruction;

[0029] As an optional example, the control instruction includes but is not limited to a set breakpoint instruction (used to instruct setting a breakpoint), a skip current breakpoint instruction (used to instruct skipping the current breakpoint), and a delete breakpoint instruction (used to instruct deleting a breakpoint).

[0030] As an optional example, the structure diagram of the PLC is as follows Figure 3 As shown (specifically Figure 3 PLC controller module in the ).

[0031] Step S204, when the instruction type indicates that the control instruction is a breakpoint setting instruction, determining a first breakpoint address to be set from the breakpoint setting instruction;

[0032] Step S206: setting the instruction at the first breakpoint address in the target code area as a breakpoint BKPT instruction, wherein the instruction set in the target code area constitutes a target program.

[0033] As an optional example, the target code area is an International Electrotechnical Commission (IEC) program code area, and the target program is an IEC program.

[0034] It should be noted that the user can use the PLC programming software to set the IEC program breakpoints, and then the PLC programming software sends the breakpoint information to the PLC controller, and then the communication processing thread of the PLC controller software of the PLC controller (such as Figure 3After component 25 in the program recognizes that the function of adding a breakpoint is to add a breakpoint, the com_bkpt_add function is called to add a breakpoint. The method of adding a breakpoint by the com_bkpt_add function is simply summarized as follows: the NOP instruction at the position of the parameter addr (i.e., the first breakpoint address mentioned above, the breakpoint information carries the parameter addr) in the target code area is modified to the BKPT instruction of the CPU. The user can use the same method to add multiple breakpoints.

[0035] It should be noted that when the user starts the PLC operation using the PLC programming software, the IEC operation thread in the PLC ( Figure 3 Component 26 in the IEC logic starts to execute. When the IEC operation thread executes the IEC program to the breakpoint position, it encounters the BKPT instruction of the CPU. Since the BKPT instruction is a CPU exception instruction, it will trigger the CPU to enter an exception. In the CPU exception handling program, the kernel of the microkernel operating system determines that the exception is caused by BKPT, suspends the IEC operation thread, and selects other threads to run.

[0036] Through the above steps S202-S206, when the communication processing thread of the PLC obtains the control instruction for setting a breakpoint sent by the PLC programming software, the first breakpoint address to be set is determined from the control instruction, and the instruction at the first breakpoint address in the target code area is set as the breakpoint BKPT instruction, wherein the instruction set in the target code area constitutes the target program, so that when the target program runs to the instruction at the first breakpoint address, the CPU is triggered to enter an exception and the target program is suspended. Then, the above technical solution is adopted to solve the problem that the PLC cannot realize the breakpoint debugging function, and the technical effect of realizing the breakpoint debugging function of the PLC is achieved.

[0037] In an exemplary embodiment, the above step S206 can be implemented in the following manner: suspend the target running thread in the PLC, wherein the target running thread is used to execute the target program; when the instruction at the first breakpoint address in the target code area is a NOP instruction, replace the NOP instruction at the first breakpoint address with the BKPT instruction, and resume the operation of the target running thread.

[0038] As an optional example, the target running thread is the IEC running thread. The above step S206 can be implemented by the com_bkpt_add (unsigned long addr) function. This function belongs to the communication processing thread of the PLC controller software (such as Figure 3 Component 25 in the figure). The function logic is:

[0039] (a) Call os_api_task_suspend to suspend the IEC operation thread;

[0040] (b) Find the IEC program code area of ​​the IEC logic, check whether the address addr is a NOP instruction, and if it is a NOP instruction, modify the NOP instruction to the BKPT instruction of the CPU (it should be noted that both the NOP instruction and the BKPT instruction are 4 bytes, so the modification will not cause the instruction address to be misaligned), and then refresh the instruction cache of the IEC program code area.

[0041] (c) Call os_api_task_resume to resume the IEC operation thread.

[0042] In an exemplary embodiment, after setting the instruction at the first breakpoint address in the target code area as a breakpoint BKPT instruction, the following steps S11-S12 are further performed:

[0043] Step S11: Obtain the current breakpoint address of the target program;

[0044] As an optional example, the communication processing thread of the PLC controller software can call the os_api_bkpt_get function to obtain the current breakpoint address of the target program.

[0045] Step S12: Set the current breakpoint address in a status query packet, and send the status query packet carrying the current breakpoint address to the PLC programming software through the communication processing thread, wherein the status query packet is used to notify the PLC programming software of the operating status of the PLC.

[0046] For a better understanding, the following is a specific description. When the IEC operation thread is suspended due to an exception in the execution of the BKPT instruction, the PC address of the IEC operation thread (i.e., the current breakpoint address of the target program) is passed to the user space. In the status query packet of each cycle between the PLC programming software and the PLC controller, the communication processing thread of the PLC controller software calls the os_api_bkpt_get function to obtain the PC address of the IEC operation thread, and then the PLC controller software fills the PC address of the IEC operation thread into the status query packet and sends it to the PLC programming software. After the PLC programming software receives the status query packet of the PLC controller, if it finds that the PC of the IEC operation thread is not 0, it determines that the IEC program has encountered a breakpoint and stopped running and displays it to the user. At this time, from the user's perspective, the IEC operation thread stops running, and the user can view the variable value when the breakpoint occurs through the PLC programming software (the statement before the breakpoint has been executed, and the statement after the breakpoint has not been executed), and debug the logic of the IEC statement accordingly.

[0047] In an exemplary embodiment, the above step S11 can be implemented in the following manner: determine whether the target running thread is in a suspended state, wherein the target running thread is used to execute instructions in the target code area; when the target running thread is in a suspended state, set the current breakpoint address to the program counter PC address of the target running thread; when the target running thread is not in a suspended state, set the current breakpoint address to a preset address.

[0048] As an optional example, the above preset address is 0.

[0049] For a better understanding, the following is a specific description. Getting the current breakpoint address is mainly achieved through two functions, the interface function and the implementation function.

[0050] The interface function is: int os_api_bkpt_get(unsigned long taskID, unsigned long*addr); the implementation function is: int os_lib_bkpt_get(unsigned long taskID, unsigned long*addr);

[0051] The os_api_bkpt_get function belongs to the OS interface (such as Figure 3 Component 22 shown), os_lib_bkpt_get function belongs to the OS management thread (such as Figure 3 The interface function finally calls the implementation function, and the logic of the implementation function is:

[0052] (a) Check whether the IEC running thread is suspended.

[0053] (b) If the IEC running thread is not suspended, the return parameter addr = 0 and the function returns successfully;

[0054] (c) If the EC running thread is suspended, read the PC value of the EC running thread, fill the PC value into the return parameter addr, and the function returns successfully.

[0055] In an exemplary embodiment, after the above step S202, the method further includes:

[0056] Step S21: when the control instruction is a skip current breakpoint instruction, determining a second breakpoint address to be skipped from the skip current breakpoint instruction;

[0057] Step S22: When the instruction at the second breakpoint address in the target code area is the BKPT instruction and the PC address of the target running thread is the second breakpoint address, the PC address of the target running thread is set to a specified address, wherein the specified address is equal to the second breakpoint address plus a preset offset.

[0058] It should be noted that the preset offset is the length of the BKPT instruction, that is, 4 bytes.

[0059] For better understanding, the following is a specific description: the user uses the PLC programming software to skip the current breakpoint of the IEC program, and then the PLC programming software sends the command to skip the current breakpoint to the PLC controller. The communication processing thread of the PLC controller software ( Figure 3 After the component 25) shown in the figure recognizes that it is a function to skip the current breakpoint, it calls the com_bkpt_skip function to skip the current breakpoint.

[0060] It should be noted that PLC implements the function of skipping the current breakpoint mainly through three functions, and the function prototypes are: int com_bkpt_skip(unsigned long addr); int os_api_bkpt_skip(unsigned longtaskID, unsigned long addr); int os_lib_bkpt_skip(unsigned long taskID, unsignedlong addr).

[0061] Among them, the com_bkpt_skip function belongs to the communication processing thread of the PLC controller software (such as Figure 3 25 in the figure). The os_api_bkpt_skip function belongs to the OS interface (such as Figure 3 Component 22 in the os_lib_bkpt_skip function belongs to the OS management thread (such as Figure 3 Component 18 in.

[0062] The com_bkpt_skip function first obtains the taskID of the IEC operation thread, then calls the os_api_bkpt_skip function, which finally calls the os_lib_bkpt_skip function. The function logic of os_lib_bkpt_skip is:

[0063] (a) Check whether the address addr in the IEC program code area is a BKPT instruction. If it is not a BKPT instruction, the skip breakpoint command is invalid and the program returns directly. If it is a BKPT instruction, the skip breakpoint command is valid and the program continues to the next step.

[0064] (b) Check whether the current PC of the taskID thread is equal to addr, that is, confirm again whether the IEC thread stops at addr. If not, return directly. If yes, proceed to the next step.

[0065] (c) Calculate the next instruction address of PC: addrNew = addr + 4. It should be noted that since the original instruction at the addr position is NOP and the current instruction is BKPT, this instruction can be simply skipped.

[0066] (d) Call the os_api_set_task_register function to set the PC pointer of the IEC operation thread to addrNew.

[0067] (e) Call the os_api_task_resume function to resume the operation of the IEC operation thread.

[0068] In an exemplary embodiment, after the above step S202, the method further includes:

[0069] Step S31: when the control instruction is a breakpoint deletion instruction, determining a third breakpoint address to be deleted from the breakpoint deletion instruction;

[0070] Step S32: suspending a target running thread in the PLC, wherein the target running thread is used to execute the target program;

[0071] Step S33: when the instruction at the third breakpoint address in the target code area is the BKPT instruction, replace the BKPT instruction at the third breakpoint address with a NOP instruction, and resume the operation of the target running thread.

[0072] For better understanding, the following is a specific description: the user uses the PLC programming software to delete the IEC program breakpoint, and then the PLC programming software sends the breakpoint deletion information to the PLC controller. The communication processing thread of the PLC controller software (such as Figure 3 After component 25) in the program recognizes that it is a breakpoint deletion function, it calls the com_bkpt_del function to delete a breakpoint.

[0073] It should be noted that the function prototype for deleting a breakpoint is: com_bkpt_del(unsigned long addr);

[0074] This function belongs to the communication processing thread of the PLC controller software (such as Figure 3 Component 25 in the figure). The function logic is:

[0075] (a) Call os_api_task_suspend to suspend the IEC operation thread;

[0076] (b) Find the IEC program code area of ​​the IEC logic and check whether the address addr in the IEC program code area is a BKPT instruction. If it is a BKPT instruction, modify the BKPT instruction to the NOP instruction of the CPU and refresh the instruction cache of the IEC program code area.

[0077] (c) Call os_api_task_resume to resume the IEC operation thread;

[0078] In an exemplary embodiment, before obtaining the control instructions sent by the PLC programming software, the method also includes: obtaining the assembly code sent by the PLC programming software, wherein each assembly code block in the assembly code has a NOP instruction at the end; and storing the assembly code in the target code area.

[0079] It should be noted that the IEC logic written by the user in the PLC programming software is a high-level language statement. The PLC controller cannot directly execute high-level language statements. It must be compiled by the PLC programming software and converted into assembly statements before it can be executed by the PLC controller. For each line of IEC logic written by the user, the PLC programming software compiles it into several or dozens of lines of assembly statements (invisible to the user) during compilation. To simplify the description, this application refers to these assembly statements corresponding to each line of IEC logic as an assembly code block. In order to simplify the overall complexity of the breakpoint debugging function, the PLC programming software must automatically insert the CPU empty instruction NOP at the end of each assembly code block during compilation.

[0080] In order to better understand the process of the above-mentioned method for setting program breakpoints, the process of the implementation method for setting the above-mentioned program breakpoints is described below in combination with an optional embodiment, but it is not used to limit the technical solution of the embodiment of the present application.

[0081] The present application is a method design and software implementation of a PLC controller software breakpoint debugging function based on a microkernel operating system. The components added or modified include: a communication processing thread of the PLC controller software (such as Figure 3 Component 25 in the PLC controller software), the IEC operation thread (such as Figure 3 Component 26 in), OS software interface (such as Figure 3 Component 22 in), OS management thread (such as Figure 3 Component 18 in), microkernel operating system kernel (such as Figure 3 Component 12 in.

[0082] The method described in this application is effective for PLC programming software (such as Figure 3 Component 1) in the specification also makes some conventions, and the methods described in this application rely on these conventions.

[0083] The PLC in this application adopts a high-security microkernel operating system, which only provides five functions: thread management, inter-process communication, virtual memory space, handle space, and device basic unit (interrupt object), and no other functions.

[0084] The PLC controller software breakpoint debugging function of the present application is to add a set of breakpoint debugging function functions on the basis of the PLC controller software based on the microkernel operating system. This set of functions cooperates with each other in certain steps to realize the breakpoint debugging function of the PLC controller based on the microkernel operating system.

[0085] A set of functions added to the breakpoint debugging function of the PLC controller based on the microkernel operating system are:

[0086] (1)int com_bkpt_add(unsigned long addr);

[0087] (2)int os_api_bkpt_get(unsigned long taskID, unsigned long*addr);

[0088] (3)int os_lib_bkpt_get(unsigned long taskID, unsigned long*addr);

[0089] (4)int com_bkpt_skip(unsigned long addr);

[0090] (5)int os_api_bkpt_skip(unsigned long taskID, unsigned long addr);

[0091] (6)int os_lib_bkpt_skip(unsigned long taskID, unsigned long addr);

[0092] (7)int com_bkpt_del(unsigned long addr);

[0093] It should be noted that, in addition to the above functions, this application also modifies the microkernel operating system kernel (such as Figure 1Component 12 in the example adds a check for BKPT exceptions in its exception handler. If the exception address is a BKPT instruction, the task is only processed similar to suspend, and is not marked as having crashed and cannot be executed again.

[0094] The design method of the breakpoint debugging function of the PLC controller based on the microkernel operating system includes the following steps:

[0095] (1) Setting breakpoints. The user uses the PLC programming software to set the IEC program breakpoints, and then the PLC programming software sends the breakpoint setting information to the PLC controller. After the communication processing thread of the PLC controller software recognizes that it is adding a breakpoint function, it calls the com_bkpt_add function to add a breakpoint. The method of adding a breakpoint using the com_bkpt_add function can be simply summarized as: modifying the NOP instruction at the parameter addr position in the IEC program code area to the CPU's BKPT instruction. It should be noted that users can use the same method to add multiple breakpoints.

[0096] (2) Stop running when encountering a breakpoint. The user uses the PLC programming software to start the PLC operation, and the IEC operation thread starts to execute the IEC logic. When the IEC operation thread executes the IEC program to the breakpoint position, it encounters the CPU's BKPT instruction. Since the BKPT instruction is a CPU exception instruction, it triggers the CPU to enter an exception. In the CPU exception handler, the kernel of the microkernel operating system determines that the exception is caused by BKPT, suspends the IEC operation thread, and selects other threads to run.

[0097] (3) Get the current breakpoint address. When the IEC operation thread is suspended due to an exception in executing the BKPT instruction, the PC address of the IEC operation thread is passed to the user space. In the status query packet of each cycle between the PLC programming software and the PLC controller, the communication processing thread of the PLC controller software calls the os_api_bkpt_get function. If the IEC operation thread is suspended, its PC value will be returned in addr, otherwise addr = 0. The PLC controller software fills the return result of the os_api_bkpt_get function into the status query packet and sends it to the PLC programming software. After the PLC programming software receives the status query packet of the PLC controller, if it finds that the return result of the os_api_bkpt_get function is not 0, it determines that the IEC program has encountered a breakpoint and stopped running and displays it to the user. At this time, from the user's perspective, the IEC thread stops running. The user can view the variable value when the breakpoint occurs through the PLC programming software (the statement before the breakpoint has been executed, and the statement after the breakpoint has not been executed), and debug the logic of the IEC statement accordingly.

[0098] (4) Skip the current breakpoint. The user uses the PLC programming software to skip the current breakpoint of the IEC program, and then the PLC programming software sends the command to skip the current breakpoint to the PLC controller. After the communication processing thread of the PLC controller software recognizes that it is the skip current breakpoint function, it calls the com_bkpt_skip function to skip the current breakpoint. The method of the com_bkpt_skip function to skip the current breakpoint can be simply summarized as follows: call the function os_api_bkpt_skip with addr as the parameter to modify the PC pointer of the IEC operation thread and trigger it to continue running until the next breakpoint is encountered.

[0099] (5) Breakpoint deletion. The user uses the PLC programming software to delete the IEC program breakpoint, and then the PLC programming software sends the breakpoint deletion information to the PLC controller. After the communication processing thread of the PLC controller software recognizes that it is a breakpoint deletion function, it calls the com_bkpt_del function to delete a breakpoint. The method of deleting a breakpoint by the com_bkpt_del function can be simply summarized as: directly modify the BKPT instruction at the parameter addr position in the IEC program code area to the CPU's NOP instruction. The user can use the same method to delete multiple breakpoints.

[0100] It should be noted that in order to simplify the overall complexity of the breakpoint debugging function, the breakpoint debugging function of the PLC controller based on the microkernel operating system must meet the following four constraints to work properly:

[0101] (1) Automatically insert NOP instructions. The IEC logic written by the user in the PLC programming software is a high-level language statement. The PLC controller cannot directly execute high-level language statements. It must be compiled by the PLC programming software and converted into assembly statements before it can be executed by the PLC controller. For each line of IEC logic written by the user, the PLC programming software compiles it into several or dozens of lines of assembly statements (invisible to the user) during compilation. To simplify the description, this application refers to these assembly statements corresponding to each line of IEC logic as an assembly code block. In order to simplify the overall complexity of the breakpoint debugging function, the PLC programming software must automatically insert the CPU empty instruction NOP at the end of each assembly code block during compilation.

[0102] (2) Breakpoints can only be set at NOP addresses. When the user sets a breakpoint at a line of IEC logic, the PLC programming software needs to find the address addr of the NOP instruction in the assembly code block corresponding to the line of IEC logic, and send the breakpoint setting command and the address addr to the PLC controller. When executing com_bkpt_add, the PLC controller will check whether the address addr is a NOP instruction. If it is a NOP instruction, the breakpoint setting command is valid; if it is not a NOP instruction, the breakpoint setting command is invalid.

[0103] (3) Breakpoints can only be skipped at NOP addresses. When the user skips a breakpoint at a line of IEC logic, the PLC programming software needs to find the address addr of the NOP instruction in the assembly code block corresponding to the line of IEC logic, and send the skip breakpoint command and the address addr to the PLC controller. When executing com_bkpt_skip, the PLC controller will check whether the address addr is a BKPT instruction. If it is a BKPT instruction, the skip breakpoint command is valid; if it is not a BKPT instruction, the skip breakpoint command is invalid.

[0104] (4) Breakpoints can only be deleted at NOP addresses. When the user skips a breakpoint at a line of IEC logic, the PLC programming software needs to find the address addr of the NOP instruction in the assembly code block corresponding to the line of IEC logic, and send the breakpoint deletion command and the address addr to the PLC controller. When executing com_bkpt_del, the PLC controller will check whether the address addr is a BKPT instruction. If it is a BKPT instruction, the breakpoint deletion command is valid; if it is not a BKPT instruction, the breakpoint deletion command is invalid.

[0105] It should be noted that the microkernel operating system only provides five functions: thread management, inter-process communication, virtual memory space, handle space, and device basic unit (interrupt object). There are no other functions, which brings many difficulties to the development of upper-level functions. Based on these five basic functions, this application research integrates the characteristics of microkernel operating system kernel exception management, operating system management thread, PLC communication processing thread, PLC IEC operation thread, and PLC programming software modules, and designs a method for implementing the breakpoint debugging function of the PLC controller software based on the microkernel operating system, and finally implements the coding and passes the verification.

[0106] It can be clearly understood that the method according to the above embodiment can be implemented by means of software plus a necessary general hardware platform, and of course it can also be implemented by hardware, but in many cases the former is a better implementation method. Based on this understanding, the technical solution of the present application is essentially or the part that contributes to the prior art can be embodied in the form of a software product, which is stored in a storage medium (such as ROM / RAM, disk, CD), including a number of instructions for a terminal device (which can be a mobile phone, computer, server, or network device, etc.) to execute the methods of each embodiment of the present application.

[0107] Figure 4 is a structural block diagram of a program breakpoint setting device according to an embodiment of the present application; Figure 4 As shown, including:

[0108] A first determination module 42 is used to determine the instruction type of the control instruction when a control instruction sent by the PLC programming software is obtained through the communication processing thread of the programmable logic controller PLC;

[0109] A second determining module 44 is used to determine a first breakpoint address to be set from the control instruction when the instruction type indicates that the control instruction is a breakpoint setting instruction;

[0110] The setting module 46 is used to set the instruction at the first breakpoint address in the target code area as a breakpoint BKPT instruction, wherein the instruction set in the target code area constitutes a target program.

[0111] Through the above-mentioned device, when the communication processing thread of the PLC obtains the control instruction for setting a breakpoint sent by the PLC programming software, the first breakpoint address to be set is determined from the control instruction, and the instruction at the first breakpoint address in the target code area is set as the breakpoint BKPT instruction, wherein the instruction set in the target code area constitutes the target program, so that when the target program runs to the instruction at the first breakpoint address, the CPU is triggered to enter an exception and the target program is suspended. Then, the above-mentioned technical solution is adopted to solve the problem that the PLC cannot realize the breakpoint debugging function, and the technical effect of realizing the breakpoint debugging function of the PLC is achieved.

[0112] In an exemplary embodiment, the setting module 46 is also used to suspend the target running thread in the PLC, wherein the target running thread is used to execute the target program; when the instruction at the first breakpoint address in the target code area is a NOP instruction, the NOP instruction at the first breakpoint address is replaced with the BKPT instruction, and the operation of the target running thread is resumed.

[0113] In an exemplary embodiment, the device also includes: a first processing module, used to obtain the current breakpoint address of the target program after setting the instruction at the first breakpoint address in the target code area as a breakpoint BKPT instruction; setting the current breakpoint address in a status query packet, and sending the status query packet carrying the current breakpoint address to the PLC programming software through the communication processing thread, wherein the status query packet is used to notify the PLC programming software of the operating status of the PLC.

[0114] In an exemplary embodiment, the first processing module is also used to determine whether the target running thread is in a suspended state, wherein the target running thread is used to execute instructions in the target code area; when the target running thread is in a suspended state, the current breakpoint address is set to the program counter PC address of the target running thread; when the target running thread is not in a suspended state, the current breakpoint address is set to a preset address.

[0115] In an exemplary embodiment, the device also includes: a second processing module, which is used to, after determining the instruction type of the control instruction, determine the second breakpoint address to be skipped from the skip current breakpoint instruction when the control instruction is a skip current breakpoint instruction; and when the instruction at the second breakpoint address in the target code area is the BKPT instruction and the PC address of the target running thread is the second breakpoint address, set the PC address of the target running thread to a specified address, wherein the specified address is equal to the second breakpoint address plus a preset offset.

[0116] In an exemplary embodiment, the device also includes: a third processing module, which is used to determine the third breakpoint address to be deleted from the delete breakpoint instruction after determining the instruction type of the control instruction, when the control instruction is a delete breakpoint instruction; suspend the target running thread in the PLC, wherein the target running thread is used to execute the target program; when the instruction at the third breakpoint address in the target code area is the BKPT instruction, replace the BKPT instruction at the third breakpoint address with a NOP instruction, and resume the operation of the target running thread.

[0117] In an exemplary embodiment, the device also includes: a storage module, used to obtain the assembly code sent by the PLC programming software before obtaining the control instructions sent by the PLC programming software, wherein each assembly code block in the assembly code has a NOP instruction at the end; and store the assembly code in the target code area.

[0118] An embodiment of the present application further provides a storage medium, which includes a stored program, wherein the program executes any of the above methods when it is run.

[0119] Optionally, in this embodiment, the storage medium may be configured to store program codes for executing the following steps:

[0120] S1, when a control instruction sent by a programmable logic controller (PLC) programming software is obtained through a communication processing thread of the programmable logic controller (PLC), determining an instruction type of the control instruction;

[0121] S2, when the instruction type is used to indicate that the control instruction is a breakpoint setting instruction, determining a first breakpoint address to be set from the control instruction;

[0122] S3, setting the instruction at the first breakpoint address in the target code area as a breakpoint BKPT instruction, wherein the instruction set in the target code area constitutes a target program.

[0123] An embodiment of the present application further provides an electronic device, including a memory and a processor, wherein a computer program is stored in the memory, and the processor is configured to run the computer program to execute the steps in any one of the above method embodiments.

[0124] Optionally, the electronic device may further include a transmission device and an input / output device, wherein the transmission device is connected to the processor, and the input / output device is connected to the processor.

[0125] Optionally, in this embodiment, the processor may be configured to perform the following steps through a computer program:

[0126] S1, when a control instruction sent by a programmable logic controller (PLC) programming software is obtained through a communication processing thread of the programmable logic controller (PLC), determining an instruction type of the control instruction;

[0127] S2, when the instruction type is used to indicate that the control instruction is a breakpoint setting instruction, determining a first breakpoint address to be set from the control instruction;

[0128] S3, setting the instruction at the first breakpoint address in the target code area as a breakpoint BKPT instruction, wherein the instruction set in the target code area constitutes a target program.

[0129] Optionally, in this embodiment, the above-mentioned storage medium may include but is not limited to: a USB flash drive, a read-only memory (ROM), a random access memory (RAM), a mobile hard disk, a magnetic disk or an optical disk, and other media that can store program codes.

[0130] Optionally, the specific examples in this embodiment may refer to the examples described in the above embodiments and optional implementation modes, and this embodiment will not be described in detail here.

[0131] Obviously, those skilled in the art should understand that the above modules or steps of the present application can be implemented by a general computing device, they can be concentrated on a single computing device, or distributed on a network composed of multiple computing devices, and optionally, they can be implemented by a program code executable by a computing device, so that they can be stored in a storage device and executed by the computing device, and in some cases, the steps shown or described can be executed in a different order from that herein, or they can be made into individual integrated circuit modules, or multiple modules or steps therein can be made into a single integrated circuit module for implementation. Thus, the present application is not limited to any specific combination of hardware and software.

[0132] The above is only a preferred implementation of the present application. It should be pointed out that for ordinary technicians in this technical field, several improvements and modifications can be made without departing from the principles of the present application. These improvements and modifications should also be regarded as the scope of protection of the present application.

Claims

1. A method for setting a program breakpoint, characterized in that: include: In the case where a control instruction sent by a programmable logic controller (PLC) programming software is obtained through a communication processing thread of the programmable logic controller (PLC), determining an instruction type of the control instruction; In a case where the instruction type is used to indicate that the control instruction is a breakpoint setting instruction, determining a first breakpoint address to be set from the control instruction; The instruction at the first breakpoint address in the target code area is set as a breakpoint BKPT instruction, wherein the instruction set in the target code area constitutes a target program.

2. The method according to claim 1, characterized in that Setting the instruction at the first breakpoint address in the target code area as a breakpoint BKPT instruction includes: Suspending a target running thread in the PLC, wherein the target running thread is used to execute the target program; In the case where the instruction at the first breakpoint address in the target code area is a NOP instruction, the NOP instruction at the first breakpoint address is replaced with the BKPT instruction, and the operation of the target running thread is resumed.

3. The method according to claim 1, characterized in that After setting the instruction at the first breakpoint address in the target code area as a breakpoint BKPT instruction, the method further includes: Get the current breakpoint address of the target program; The current breakpoint address is set in a status query packet, and the status query packet carrying the current breakpoint address is sent to the PLC programming software through the communication processing thread, wherein the status query packet is used to notify the PLC programming software of the operating status of the PLC.

4. The method according to claim 3, characterized in that Get the current breakpoint address of the target program, including: Determining whether a target running thread is in a suspended state, wherein the target running thread is used to execute instructions in the target code area; When the target running thread is in a suspended state, setting the current breakpoint address to the program counter PC address of the target running thread; When the target running thread is not in a suspended state, the current breakpoint address is set to a preset address.

5. The method according to claim 1, characterized in that: After determining the instruction type of the control instruction, the method further includes: In a case where the control instruction is a skip current breakpoint instruction, determining a second breakpoint address to be skipped from the skip current breakpoint instruction; When the instruction at the second breakpoint address in the target code area is the BKPT instruction and the PC address of the target running thread is the second breakpoint address, the PC address of the target running thread is set to a specified address, wherein the specified address is equal to the second breakpoint address plus a preset offset, and wherein the target running thread is used to execute the target program.

6. The method according to claim 1, characterized in that After determining the instruction type of the control instruction, the method further includes: In a case where the control instruction is a delete breakpoint instruction, determining a third breakpoint address to be deleted from the delete breakpoint instruction; Suspending a target running thread in the PLC, wherein the target running thread is used to execute the target program; In the case where the instruction at the third breakpoint address in the target code area is the BKPT instruction, the BKPT instruction at the third breakpoint address is replaced with a NOP instruction, and the operation of the target running thread is resumed.

7. The method according to any one of claims 1 to 6, characterized in that Before obtaining the control instruction sent by the PLC programming software, the method further includes: Obtaining the assembly code sent by the PLC programming software, wherein the tail of each assembly code block in the assembly code has a NOP instruction; The assembly code is stored in the target code area.

8. A device for setting program breakpoints, characterized in that: include: A first determination module is used to determine the instruction type of the control instruction when a control instruction sent by the PLC programming software is obtained through the communication processing thread of the programmable logic controller PLC; A second determining module, configured to determine a first breakpoint address to be set from the breakpoint setting instruction when the instruction type indicates that the control instruction is a breakpoint setting instruction; A setting module is used to set the instruction at the first breakpoint address in the target code area as a breakpoint BKPT instruction, wherein the instruction set in the target code area constitutes a target program.

9. A computer-readable storage medium, characterized in that: The computer-readable storage medium includes a stored program, wherein the program executes the method described in any one of claims 1 to 7 when executed.

10. An electronic device comprising a memory and a processor, characterized in that: A computer program is stored in the memory, and the processor is configured to execute the method according to any one of claims 1 to 7 through the computer program.