Method for non-intrusive acquisition of method execution chain
By loading custom agents and bytecode enhancement tools to insert monitoring codes, record method execution information, solve the problem of code modification and performance impact when obtaining method execution chains in the existing technology, and realize a non-invasive and low-cost method Execution chain acquisition, suitable for production environment.
Patent Information
- Application Number
- CN202411122971.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-15
- Publication Date
- 2025-01-07
AI Technical Summary
The prior art requires modifying the object code or using debugging tools when obtaining method execution chain information, which affects performance and increases development costs, and is not suitable for use in the production environment.
By loading a custom agent, insert monitoring code before the target program starts, record method execution information and store it, remove it after it is finished, use bytecode enhancement tool to insert class files, automatically trigger monitoring code, and record the start and end information of the method , the splicing formation method execution chain.
It realizes the acquisition of method execution chain without modifying the target code, which reduces the cost of code modification and performance impact, is suitable for production environments, and avoids the dependence of third-party frameworks.
Smart Images

Figure CN119271292A_ABST
Abstract
Description
Technical Field
[0001] The invention belongs to the field of computer technology, and in particular relates to a method for non-invasively acquiring a method execution chain. Background Art
[0002] In software development, debugging and performance optimization are very important links. The method execution chain information can help developers understand the execution process and performance bottlenecks of the program. Therefore, obtaining the method execution chain information is very useful. Currently, there are generally the following ways to obtain the method execution chain information:
[0003] One way is to add log output in the target code to record the method call chain information, but this method requires modifying the target code and may affect the performance and stability of the program, which is not suitable for applications in production environments; another way is to use debugging tools to monitor the execution process of the program at runtime and obtain method call chain information, but this method requires debugging the program, and the use of debugging tools also requires learning costs, which is not suitable for quickly locating problems; another way is to use many open source frameworks on the market to implement it, such as: OpenTracing, Zipkin, etc., but they all need to be integrated into the existing system, increasing the development, debugging and deployment costs. Summary of the invention
[0004] In view of this, the present invention aims to propose a method for non-invasively acquiring a method execution chain, in order to solve at least one of the above-mentioned technical problems.
[0005] To achieve the above object, the technical solution of the present invention is achieved as follows:
[0006] A method for non-invasively obtaining a method execution chain comprises the following steps:
[0007] Before starting the target program, loading a custom agent program, wherein the agent program is provided with a monitoring code;
[0008] Insert monitoring code into the target program, obtain methods and execution information in the target program, and store the information in the cache;
[0009] After the target program is executed, the monitoring code is removed from the target program;
[0010] The methods and execution information of the stored target program are read from the cache, and the methods are spliced according to the execution information to obtain the method execution chain of the target program.
[0011] Furthermore, the process of loading the customized agent program includes:
[0012] Creating a class, wherein the class includes at least one automatic calling method, and the monitoring code is set in the automatic calling method;
[0013] Create a manifest file, wherein the manifest file includes at least one key-value pair, wherein the key and value of the key-value pair are the class name and the method name of the automatically called method respectively;
[0014] Package the class and manifest files into a resource bundle as an agent.
[0015] Furthermore, the process of obtaining the method in the target program and its execution information includes:
[0016] Use a bytecode enhancement tool to traverse the class files of the target program, insert the monitoring code into the class files, and record relevant information of each method in the class files;
[0017] When the target program executes to the insertion point of the monitoring code, the monitoring code is automatically triggered; when the method in the target program is executed, the monitoring code records the start information of the current method; when the method in the target program ends, the monitoring code records the end information of the current method and calculates the execution time.
[0018] Furthermore, the relevant information of each method includes:
[0019] The timestamp of the start, the name and its parameter information, the return value, and the end time of execution in the target program.
[0020] Furthermore, the process of splicing the methods according to the execution information to obtain the method execution chain of the target program includes:
[0021] Get all execution information from the cache and arrange the execution information in time sequence;
[0022] Traverse the execution information of each method one by one, extract the method name, input parameters, and return value of each method;
[0023] The information obtained from each extraction is processed in a unified format to obtain a string, the format of which is: method name (incoming parameter) return value; if there are multiple incoming parameters, each parameter is separated by a comma; if there are no incoming parameters, a null value is filled in;
[0024] Add the string to the result list, and the multiple strings corresponding to each method are arranged in the order of calling;
[0025] The multiple character strings corresponding to the various methods are sequentially connected according to the calling sequence to obtain the method execution chain of the target program.
[0026] Compared with the prior art, the method for non-intrusive acquisition of method execution chain described in the present invention has the following beneficial effects:
[0027] The method call chain can be obtained without modifying the target code, making it easier for developers to debug the code and optimize performance.
[0028] Compared with traditional debugging methods, this method can greatly reduce the cost and risk of code modification and avoid dependence on the use of third-party frameworks;
[0029] At the same time, stack cache is used, which can greatly reduce the performance impact on the target program and is suitable for applications in production environments. BRIEF DESCRIPTION OF THE DRAWINGS
[0030] The accompanying drawings constituting a part of the present invention are used to provide a further understanding of the present invention. The exemplary embodiments of the present invention and their descriptions are used to explain the present invention and do not constitute an improper limitation of the present invention. In the accompanying drawings:
[0031] Figure 1 The present invention is a flowchart of a method for non-intrusive acquisition of a method execution chain according to an embodiment of the present invention. DETAILED DESCRIPTION
[0032] It should be noted that, in the absence of conflict, the embodiments of the present invention and the features in the embodiments may be combined with each other.
[0033] In the description of the present invention, it should be understood that the terms "center", "longitudinal", "lateral", "up", "down", "front", "back", "left", "right", "vertical", "horizontal", "top", "bottom", "inside", "outside" and the like indicate positions or positional relationships based on the positions or positional relationships shown in the accompanying drawings, and are only for the convenience of describing the present invention and simplifying the description, rather than indicating or implying that the device or element referred to must have a specific orientation, be constructed and operated in a specific orientation, and therefore cannot be understood as limiting the present invention. In addition, the terms "first", "second", and the like are only used for descriptive purposes, and cannot be understood as indicating or implying relative importance or implicitly indicating the number of technical features indicated. Thus, features defined as "first", "second", and the like may explicitly or implicitly include one or more of the features. In the description of the present invention, unless otherwise specified, "multiple" means two or more.
[0034] In the description of the present invention, it should be noted that, unless otherwise clearly specified and limited, the terms "installed", "connected", and "connected" should be understood in a broad sense, for example, it can be a fixed connection, a detachable connection, or an integral connection; it can be a mechanical connection or an electrical connection; it can be a direct connection, or it can be indirectly connected through an intermediate medium, or it can be the internal communication of two components. For ordinary technicians in this field, the specific meanings of the above terms in the present invention can be understood by specific circumstances.
[0035] The present invention will be described in detail below with reference to the accompanying drawings and in conjunction with embodiments.
[0036] A method for non-invasively obtaining a method execution chain comprises the following steps:
[0037] S1. Before starting the target program, load a custom agent program, wherein the agent program is provided with a monitoring code;
[0038] S2, inserting the monitoring code into the target program, obtaining the methods and execution information in the target program, and storing the information in the cache;
[0039] S3, after the target program is executed, the monitoring code is removed from the target program;
[0040] S4. Read the methods and execution information of the stored target program from the cache, and splice the methods according to the execution information to obtain the method execution chain of the target program.
[0041] The process of loading the customized agent program includes:
[0042] Creating a class, wherein the class includes at least one automatic calling method, and the monitoring code is set in the automatic calling method;
[0043] Create a manifest file, wherein the manifest file includes at least one key-value pair, wherein the key and value of the key-value pair are the class name and the method name of the automatically called method respectively;
[0044] Package the class and manifest files into a resource bundle as an agent.
[0045] The process of obtaining the method in the target program and its execution information includes:
[0046] Use a bytecode enhancement tool to traverse the class files of the target program, insert the monitoring code into the class files, and record relevant information of each method in the class files;
[0047] When the target program executes to the insertion point of the monitoring code, the monitoring code is automatically triggered; when the method in the target program is executed, the monitoring code records the start information of the current method; when the method in the target program ends, the monitoring code records the end information of the current method and calculates the execution time.
[0048] The relevant information of each method includes:
[0049] The timestamp of the start, the name and its parameter information, the return value, and the end time of execution in the target program.
[0050] The process of splicing the methods according to the execution information to obtain the method execution chain of the target program includes:
[0051] Get all execution information from the cache and arrange the execution information in time sequence;
[0052] Traverse the execution information of each method one by one, extract the method name, input parameters, and return value of each method;
[0053] The information obtained from each extraction is processed in a unified format to obtain a string, the format of which is: method name (incoming parameter) return value; if there are multiple incoming parameters, each parameter is separated by a comma; if there are no incoming parameters, a null value is filled in;
[0054] Add the string to the result list, and the multiple strings corresponding to each method are arranged in the order of calling;
[0055] The multiple character strings corresponding to the various methods are sequentially connected according to the calling sequence to obtain the method execution chain of the target program.
[0056] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit it. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or replace some or all of the technical features therein by equivalents. These modifications or replacements do not deviate the essence of the corresponding technical solutions from the scope of the technical solutions of the embodiments of the present invention, and they should all be included in the scope of the claims and specification of the present invention.
[0057] 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. A method for non-invasively obtaining a method execution chain, characterized in that: The steps include: Before starting the target program, loading a custom agent program, wherein the agent program is provided with a monitoring code; Insert monitoring code into the target program, obtain methods and execution information in the target program, and store the information in the cache; After the target program is executed, the monitoring code is removed from the target program; The methods and execution information of the stored target program are read from the cache, and the methods are spliced according to the execution information to obtain the method execution chain of the target program.
2. The method for non-intrusive acquisition of method execution chains according to claim 1, characterized in that: The process of loading the customized agent program includes: Creating a class, wherein the class includes at least one automatic calling method, and the monitoring code is set in the automatic calling method; Create a manifest file, wherein the manifest file includes at least one key-value pair, wherein the key and value of the key-value pair are the class name and the method name of the automatically called method respectively; Package the class and manifest files into a resource bundle as an agent.
3. The method for non-intrusive acquisition of method execution chain according to claim 1, characterized in that: The process of obtaining the method in the target program and its execution information includes: Use a bytecode enhancement tool to traverse the class files of the target program, insert the monitoring code into the class files, and record relevant information of each method in the class files; When the target program executes to the insertion point of the monitoring code, the monitoring code is automatically triggered; when the method in the target program is executed, the monitoring code records the start information of the current method; when the method in the target program ends, the monitoring code records the end information of the current method and calculates the execution time.
4. The method for non-intrusive acquisition of method execution chain according to claim 3, characterized in that: The relevant information of each method includes: The timestamp of the start, the name and its parameter information, the return value, and the end time of execution in the target program.
5. The method for non-intrusive acquisition of method execution chain according to claim 1, characterized in that: The process of splicing the methods according to the execution information to obtain the method execution chain of the target program includes: Get all execution information from the cache and arrange the execution information in time sequence; Traverse the execution information of each method one by one, extract the method name, input parameters, and return value of each method; The information obtained from each extraction is processed in a unified format to obtain a string, the format of which is: method name (incoming parameter) return value; if there are multiple incoming parameters, each parameter is separated by a comma; if there are no incoming parameters, a null value is filled in; Add the string to the result list, and the multiple strings corresponding to each method are arranged in the order of calling; The multiple character strings corresponding to the various methods are sequentially connected according to the calling sequence to obtain the method execution chain of the target program.