A method, system, electronic device and storage medium for tracking problems
By forming a call tree and re-running each node, combined with deepcopy and pickle technology, the problem of Python program tracking in long processes is solved, and the ability to quickly locate and reproduce problems is achieved.
Patent Information
- Application Number
- CN202011435462.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-12-10
- Publication Date
- 2024-08-23
- Estimated Expiration
- 2040-12-10
AI Technical Summary
When processing long processes, existing technology is difficult to effectively track problems in deeply nested data structures in Python programs. Especially when processing millions of medical records, it is difficult to locate the root cause of the problem.
By traversing all functions and classes, forming a call tree, recording the input and output parameters of each node, and re-running to track the process of each node, using the pysnooper tool to record parameters, using the deepcopy mechanism and pickle serialization to support data changes and Memory management.
It speeds up the initial problem locating steps, simplifies the tracking of problems within a single function, solves problems that are difficult to track in long processes, and improves debugging efficiency.
Smart Images

Figure CN112506780B
Abstract
Description
Technical Field
[0001] The present application relates to the field of detection technology, and in particular to a method, system, electronic device and storage medium for problem tracking. Background Art
[0002] At present, Python is a popular programming language. Python is also used in scenarios where deeply nested data structures are processed. However, troubleshooting and debugging are inevitable, but also more difficult. For example, in the scenario of processing millions of medical records: each medical record contains a lot of information, which is relatively complex and often represented by a multi-layer nested structure. The processing results and intermediate processes of the medical records are also complex structures. If the final result is found to be abnormal, the processing process is very long, and it is difficult to locate the root cause of the problem in which step.
[0003] In the Python standard library, there are trace tools and the third-party open source pysnooper tool for problem tracking. However, the disadvantage of the trace tool is that although it records the jumps of the program, it does not record the values of the variables and data, which is not enough for debugging. Because the most important thing when debugging and troubleshooting is to check the values of data and variables, check whether an intermediate variable does not meet expectations, or whether a certain data has an error, the trace tool cannot provide help. The third-party tool pysnooper is different from the standard trace tool. It focuses on recording the details of the changes in variables and data within a single function, rather than the call status. Its typical usage method is to decorate a target function with a decorator and specify it as the function to be traced. After running the software, the detailed changes of each variable in the function will be printed out, which is very useful for debugging a single function. However, its limitation is that it only focuses on a single or several functions, rather than printing all line jumps.
[0004] With regard to the problem in related technologies that it is difficult to track problems when the processing flow is very long, no effective solution has been proposed so far. Summary of the invention
[0005] The main purpose of the present application is to provide a method, system, electronic device and storage medium for problem tracking to solve the problem in the related art that it is difficult to track problems when the processing flow is very long.
[0006] In order to achieve the above objectives, in a first aspect, the present application provides a method for problem tracking, the steps of which are as follows:
[0007] Traverse all functions and all classes in the software;
[0008] Record the calling relationships between all the functions and all the classes to form a tree structure, recorded as a call tree, and record the input parameters and output parameters of each node in the tree structure;
[0009] Re-pass the input parameters of each node in the tree structure to all the functions and all the classes, re-run it once, track its running process, and complete the problem tracking of each node in the tree structure.
[0010] The steps of recording the calling relationships between all functions and all classes to form a tree structure and recording the input parameters and output parameters of each node in the tree structure are as follows:
[0011] Record the current function or current class as the parent node of the tree structure, and record the function name or class name of the current function or current class;
[0012] When a sub-function or sub-class of the current function or current class is called, a sub-function or sub-class of the tree structure is newly created, and the sub-function or sub-class is recorded as a child node;
[0013] Record the function name or class name of the sub-function or sub-class, and use the sub-function or sub-class as the current node of the tree structure;
[0014] Record the input and output parameters of the current node.
[0015] The steps of re-passing the input parameters of each node in the tree structure to all the functions and all the classes are as follows:
[0016] Re-pass the current node input parameters to the current node, that is, to the current sub-function or sub-class;
[0017] Re-run the current sub-function or sub-class and record the return value or exception;
[0018] Set the current node as a new parent node, and continue to traverse the child functions or subclasses of the new parent node;
[0019] Until all functions and all classes are completed, the problem tracking of each node in the tree structure is completed.
[0020] The recording of the input parameters and output parameters of the current node uses the pysnooper tool to record the input parameters and output parameters.
[0021] The input parameters and output parameters are recorded on the call tree.
[0022] When re-running the current sub-function or sub-class, if data in the software is changed, the deepcopy mechanism needs to be enabled before re-running.
[0023] Each node in the tree structure is serialized using pickle and saved on disk.
[0024] In a second aspect, the present application also provides a problem tracking system, which is implemented by the problem tracking method, and includes: a traversal module, a call tree module and an operation module;
[0025] The traversal module, the book calling module and the running module are connected in sequence;
[0026] The traversal module is used to traverse all functions and all classes in the software;
[0027] The call tree module is used to record the call relationships between all functions and all classes to form a tree structure, called a call tree, and record the input parameters and output parameters of each node in the tree structure;
[0028] The running module is used to re-pass the input parameters of each node in the tree structure to all the functions and all, re-run once, track its running process, and complete the problem tracking of each node in the tree structure.
[0029] An electronic device comprises a memory and a processor, wherein the memory is used to store a computer program, and the processor runs the computer program to enable the computer device to execute the problem tracking method.
[0030] A readable storage medium stores computer program instructions. When the computer program instructions are read and executed by a processor, the problem tracking method is executed.
[0031] Beneficial technical effects:
[0032] The present application proposes a method, system, electronic device and storage medium for problem tracking, which speeds up the steps of initially locating the problem and makes it simple and easy to track and reproduce the problem within a single function, solving the problem in the related art that it is difficult to track the problem when the processing flow is very long. BRIEF DESCRIPTION OF THE DRAWINGS
[0033] The drawings constituting a part of this application are used to provide a further understanding of this application, so that other features, purposes and advantages of this application become more obvious. The schematic embodiment drawings and their descriptions of this application are used to explain this application and do not constitute an improper limitation on this application. In the drawings:
[0034] Figure 1is a flow chart of a method for problem tracking provided according to an embodiment of the present application;
[0035] Figure 2 It is a flowchart of forming a tree structure provided according to an embodiment of the present application;
[0036] Figure 3 It is a flow chart of the operation process of re-running and tracking provided in an embodiment of the present application;
[0037] Figure 4 It is a system principle block diagram of a problem tracking provided according to an embodiment of the present application. DETAILED DESCRIPTION
[0038] 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.
[0039] 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 sequential order. It should be understood that the data used in this way can be interchanged where appropriate, so that the embodiments of the present application described here. In addition, the terms "including" and "having" and any of their variations are intended to cover non-exclusive inclusions, for example, a process, method, system, 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.
[0040] In the present application, the terms "upper", "lower", "left", "right", "front", "back", "top", "bottom", "inner", "outer", "middle", "vertical", "horizontal", "lateral", "longitudinal" and the like indicate positions or positional relationships based on the positions or positional relationships shown in the drawings. These terms are mainly used to better describe the present application and its embodiments, and are not used to limit the indicated devices, elements or components to have a specific orientation, or to be constructed and operated in a specific orientation.
[0041] In addition, some of the above terms may be used to express other meanings in addition to indicating orientation or positional relationship. For example, the term "on" may also be used to express a certain dependency or connection relationship in some cases. For those of ordinary skill in the art, the specific meanings of these terms in this application can be understood according to specific circumstances.
[0042] In addition, the term "plurality" shall mean two or more.
[0043] It should be noted that, in the absence of conflict, the embodiments and features in the embodiments of the present application can be combined with each other. The present application will be described in detail below with reference to the accompanying drawings and in combination with the embodiments.
[0044] In the first aspect, the present application provides a method for tracking a problem, such as Figure 1 As shown, the steps are as follows:
[0045] Step S1: traverse all functions and all classes in the software;
[0046] Step S2: Record the calling relationships between all functions and all classes to form a tree structure, recorded as a call tree, and record the input parameters and output parameters of each node in the tree structure;
[0047] Step S3: passing the input parameters of each node in the tree structure to all the functions and all the classes, re-running once, tracking the running process, and completing the problem tracking of each node in the tree structure.
[0048] Record the calling relationships between all the functions and all the classes to form a tree structure, and record the input parameters and output parameters of each node in the tree structure, such as Figure 2 As shown, the steps are as follows:
[0049] Step S21: Record the current function or current class as the parent node of the tree structure, and record the function name or class name of the current function or current class;
[0050] Step S22: when a sub-function or sub-class of the current function or current class is called, a sub-function or sub-class of the tree structure is newly created, and the sub-function or sub-class is recorded as a child node;
[0051] Step S23: recording the function name or class name of the sub-function or sub-class, and taking the sub-function or sub-class as the current node of the tree structure;
[0052] Step S24: Record the input parameters and output parameters of the current node.
[0053] The input parameters of each node in the tree structure are re-passed to all the functions and all the classes, and the operation is re-run once, and the operation process is tracked to complete the problem tracking of each node in the tree structure, such as Figure 3 As shown, the steps are as follows:
[0054] Step S31: re-pass the current node input parameter to the current node, that is, pass it to the current sub-function or sub-class;
[0055] Step S32: re-run the current sub-function or sub-class and record the return value or exception;
[0056] Step S33: setting the current node as a new parent node, and continuing to traverse the child functions or subclasses of the new parent node;
[0057] Step S34: until all functions and all classes are completed, the problem tracking of each node in the tree structure is completed.
[0058] The recording of the input parameters and output parameters of the current node uses the pysnooper tool to record the input parameters and output parameters.
[0059] The input parameters and output parameters are recorded on the call tree.
[0060] When re-running the current sub-function or sub-class, if data in the software is changed, the deepcopy mechanism needs to be enabled before re-running.
[0061] Functions that modify state and their considerations:
[0062] This application provides a mechanism to deepcopy the parameters and return values when they are recorded on the call tree. The purpose of this is to deal with stateful functions. Data structures such as dict and list in Python are mutable by default, that is, after being recorded in the call tree and before the call tree is checked after the program is finished running, those data are changed by other parts of the software, and when they are checked, they are no longer the original appearance at runtime. Deepcopying the data can avoid this problem. Even if the software is modified, the copied part will generally not be modified.
[0063] However, this function also has defects, which will invalidate the logic that depends on the memory address. After an object is copied, there will be two copies of the data representing the same content in the memory. If the software logic depends on the address of its object in the memory, the address of the object has changed during the probing. Typical operations that depend on addresses include the is operator and the id function in Python. Therefore, whether to turn on this option requires the debugger to determine based on the specific situation of the software. In other words, if the data may be changed, you can consider turning on the deepcopy mechanism, but you must consider the impact of address changes.
[0064] Each node in the tree structure is serialized using pickle and saved on disk.
[0065] Memory usage and its considerations:
[0066] One of the problems caused by recording all parameters and return values in the call tree is that the memory usage will increase greatly. Objects that should be garbage collected are still referenced in the call tree and will not be garbage collected, and the memory will not be released. The deepcopy mode has greater memory pressure, and the copied objects themselves occupy more memory.
[0067] Therefore, pytrace provides a mechanism for serializing objects, allowing nodes of the call tree to be serialized with pickle and saved on disk. When exploring the call tree, it is restored from disk. In this way, memory is no longer affected.
[0068] This also has limitations, and debuggers need to pay attention to it. Pickle's own serialization is conditional, and the parts that cannot be serialized cannot use this mechanism. It is necessary to decide whether to enable this mechanism based on the specific situation.
[0069] However, in most cases, general debugging is debugging a single piece of data. Although the number of nested layers is large and the content is complex, the memory of the development machine is more than enough. Saving the call tree and its intermediate data is almost the same as opening a large software.
[0070] The embodiments of this application are described as follows:
[0071] 1. Notes on the call tree:
[0072] A tree is used to record the running status of the program, which is called a call tree.
[0073] It allows you to see the call details of the program at a glance (except local variables).
[0074] Each call of a function (method is also considered a function) corresponds to a tree node.
[0075] The parent node p of a node s means that the function of p called the function of s during execution.
[0076] There is an order between the child nodes of a node, which represents the execution order. For example, p has three child nodes, s0, s1, and s2, which means that during the execution of p's function, the function of s0, the function of s1, and the function of s2 are called in sequence.
[0077] The node contains the specific information of the call, including input (i.e. parameters) and output (i.e. return value or exception).
[0078] 2. The process of generating a call tree:
[0079] When the program starts, before any business logic function is called, a special root node is initialized, which does not correspond to any function and has no input or output. The root node is regarded as the "current node".
[0080] Before calling a function, create a new node, record the input, append it after the "current node", and use the node as the new "current node"
[0081] After calling a function, record the output (return value or exception) and use the parent node of the "current node" as the new "current node"
[0082] In this way the entire call tree can be generated.
[0083] In a second aspect, the present application also provides a system for problem tracking, which is implemented by using the problem tracking method, such as Figure 4 As shown, it includes: a traversal module, a call tree module and a running module;
[0084] The traversal module, the book calling module and the running module are connected in sequence;
[0085] The traversal module is used to traverse all functions and all classes in the software;
[0086] The call tree module is used to record the call relationships between all functions and all classes to form a tree structure, called a call tree, and record the input parameters and output parameters of each node in the tree structure;
[0087] The running module is used to re-pass the input parameters of each node in the tree structure to all the functions and all, re-run once, track its running process, and complete the problem tracking of each node in the tree structure.
[0088] An electronic device comprises a memory and a processor, wherein the memory is used to store a computer program, and the processor runs the computer program to enable the computer device to execute the problem tracking method.
[0089] A readable storage medium stores computer program instructions. When the computer program instructions are read and executed by a processor, the problem tracking method is executed.
[0090] The above description is only the preferred embodiment of the present application and is not intended to limit the present application. For those skilled in the art, the present application may have various modifications and variations. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present application shall be included in the protection scope of the present application.
Claims
1. A method for problem tracking, characterized in that: include: Traverse all functions and all classes in the software; Record the calling relationships between all the functions and all the classes to form a tree structure, recorded as a call tree, and record the input parameters and output parameters of each node in the tree structure; Pass the input parameters of each node in the tree structure to all the functions and all the classes, re-run it once, track its running process, and complete the problem tracking of each node in the tree structure; The steps of recording the calling relationships between all functions and all classes to form a tree structure and recording the input parameters and output parameters of each node in the tree structure are as follows: Record the current function or current class as the parent node of the tree structure, and record the function name or class name of the current function or current class; When a sub-function or sub-class of the current function or current class is called, a sub-function or sub-class of the tree structure is newly created, and the sub-function or sub-class is recorded as a child node; Record the function name or class name of the sub-function or sub-class, and use the sub-function or sub-class as the current node of the tree structure; Record the input parameters and output parameters of the current node; The steps of re-passing the input parameters of each node in the tree structure to all the functions and all the classes are as follows: Re-pass the current node input parameters to the current node, that is, to the current sub-function or sub-class; Re-run the current sub-function or sub-class and record the return value or exception; Set the current node as a new parent node, and continue to traverse the child functions or subclasses of the new parent node; Until all functions and all classes are completed, the problem tracking of each node in the tree structure is completed.
2. The problem tracking method according to claim 1, characterized in that: The recording of the input parameters and output parameters of the current node uses the pysnooper tool to record the input parameters and output parameters.
3. The problem tracking method according to claim 2, characterized in that: The input parameters and output parameters are recorded on the call tree.
4. The problem tracking method according to claim 1, characterized in that: When re-running the current sub-function or sub-class, if data in the software is changed, the deepcopy mechanism needs to be enabled before re-running.
5. The problem tracking method according to claim 1, characterized in that: Each node in the tree structure is serialized using pickle and saved on disk.
6. A problem tracking system, characterized in that: A method for problem tracking as claimed in any one of claims 1 to 5, comprising: a traversal module, a call tree module and an operation module; The traversal module, the book calling module and the running module are connected in sequence; The traversal module is used to traverse all functions and all classes in the software; The call tree module is used to record the call relationships between all functions and all classes to form a tree structure, called a call tree, and record the input parameters and output parameters of each node in the tree structure; The running module is used to re-pass the input parameters of each node in the tree structure to all the functions and all, re-run once, track the running process, and complete the problem tracking of each node in the tree structure; The steps of recording the calling relationships between all functions and all classes to form a tree structure and recording the input parameters and output parameters of each node in the tree structure are as follows: Record the current function or current class as the parent node of the tree structure, and record the function name or class name of the current function or current class; When a sub-function or sub-class of the current function or current class is called, a sub-function or sub-class of the tree structure is newly created, and the sub-function or sub-class is recorded as a child node; Record the function name or class name of the sub-function or sub-class, and use the sub-function or sub-class as the current node of the tree structure; Record the input parameters and output parameters of the current node; The steps of re-passing the input parameters of each node in the tree structure to all the functions and all the classes are as follows: Re-pass the current node input parameters to the current node, that is, to the current sub-function or sub-class; Re-run the current sub-function or sub-class and record the return value or exception; Set the current node as a new parent node, and continue to traverse the child functions or subclasses of the new parent node; Until all functions and all classes are completed, the problem tracking of each node in the tree structure is completed.
7. An electronic device, characterized in that: The electronic device comprises a memory and a processor, wherein the memory is used to store a computer program, and the processor runs the computer program to enable the computer device to execute the problem tracking method according to any one of claims 1-5.
8. A readable storage medium, characterized in that: The readable storage medium stores computer program instructions, and when the computer program instructions are read and executed by a processor, the problem tracking method according to any one of claims 1 to 5 is executed.
