Function call tracking method and system, storage medium and electronic equipment
By obtaining the function call chain based on stack traceback, the problem of resource overhead and processing speed reduction caused by function call tracing in the prior art is solved, and efficient function call tracing and multi-threaded analysis are realized.
Patent Information
- Application Number
- CN202411718017.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-27
- Publication Date
- 2025-03-21
AI Technical Summary
In function call tracking, the prior art requires additional execution of recording instructions at the entrance and exit of the function, resulting in additional overhead of computing and storing resources, and when the overall function volume of the system is large, the device processing speed will decrease and the storage resources will increase.
The function call tracking method based on stack backtracking is adopted. By obtaining the top of the target stack and backtracking the target stack, the call chain data and function name of the function are obtained without the need to reserve specific code before and after the function in advance.
This method reduces the overhead of computing and storage resource for function call tracking, improves the processing speed of the device, and reduces the usage of storage resources. At the same time, it can obtain function call chains of different threads and analyze the working conditions of each thread.
Smart Images

Figure CN119668727A_ABST
Abstract
Description
Technical Field
[0001] The present application belongs to the field of computer technology and relates to a function call tracing method, and in particular to a function call tracing method, system, storage medium and electronic device based on stack backtrace. Background Art
[0002] Function call tracing is of great significance in the fields of computers and so on. By tracing the function calls, we can obtain information such as the function call order, hierarchical relationship, and specific execution path, which can be applied to software debugging, performance analysis, error location and other scenarios. In the prior art, it is mainly to additionally mark the running position of the program at the function entry and exit, and save it in a pre-reserved cache space, and obtain and parse the function call chain from the cache space when necessary to achieve function call tracing. However, this method requires the execution of a small number of additional recording instructions at the entry and exit of the function, which will result in additional overhead of computing and storage resources. When the overall function volume of the system is large, this will cause a significant decrease in the processing speed of the device and a significant increase in storage resources. Summary of the invention
[0003] Embodiments of the present application provide a function call tracing method, system, storage medium, and electronic device for improving the processing speed of a device and reducing storage resource overhead.
[0004] In a first aspect, an embodiment of the present application provides a function call tracing method based on stack backtracing, the method comprising: obtaining the top of a target stack and determining the target stack based on the top of the target stack; performing stack backtracing on the target stack to obtain the content of the target stack; obtaining the call chain data of the function based on the content of the target stack and the range of the code segment, the call chain data including the address information of the function; obtaining the corresponding function name based on the call chain data and the instruction storage address of each of the functions.
[0005] In an implementation manner of the first aspect, obtaining the target stack top includes: for a suspended thread, obtaining the target stack top according to a thread control block of the thread.
[0006] In an implementation of the first aspect, obtaining the target stack top includes: for a currently running function, obtaining the target stack top according to the position of a local variable applied for by the function.
[0007] In an implementation manner of the first aspect, obtaining the target stack top includes: when an interrupt is triggered, obtaining the target stack top according to a callback function of the interrupt.
[0008] In an implementation manner of the first aspect, the method further includes: disassembling the compiled code file to obtain instructions and instruction storage addresses of each function.
[0009] In an implementation manner of the first aspect, the code segment is stored in a continuous memory space.
[0010] In an implementation of the first aspect, obtaining call chain data based on the content of the target stack and the range of the code segment includes: obtaining the starting address and the ending address of the memory space; comparing the content of the target stack with the data in the memory space to obtain the data in the target stack located in the memory space as the call chain data.
[0011] In the second aspect, an embodiment of the present application provides a function call tracing system based on stack backtracing, the system comprising: a stack top and stack determination module, used to obtain a target stack top and determine a target stack based on the target stack top; a stack backtracing module, used to perform stack backtracing on the target stack to obtain the content of the target stack; a call chain data acquisition module, used to obtain the call chain data of the function based on the content of the target stack and the scope of the code segment, the call chain data including the address information of the function; a function name acquisition module, used to obtain the corresponding function name based on the call chain data and the instruction storage address of each of the functions.
[0012] In a third aspect, an embodiment of the present application provides a computer-readable storage medium having a computer program stored thereon, and when the computer program is executed by a processor, the method described in any one of the first aspects of the embodiments of the present application is implemented.
[0013] In a fourth aspect, an embodiment of the present application provides an electronic device, comprising: a memory storing a computer program; and a processor communicatively connected to the memory, for executing any of the methods described in the first aspect of the embodiment of the present application when the computer program is called.
[0014] As described above, in the embodiment of the present application, the function call chain is obtained based on stack backtracing, and there is no need to reserve specific codes before and after the function in advance, which has the advantages of short time consumption and low memory overhead.
[0015] In addition, since the call chain data is naturally stored in the stack, there is no need to use a new buffer to record the data in the embodiment of the present application, which helps to further reduce storage resource overhead.
[0016] Furthermore, since different threads hold different stacks, in the embodiment of the present application, the function call chains of different threads can be obtained, which is conducive to analyzing the working status of each thread. BRIEF DESCRIPTION OF THE DRAWINGS
[0017] Figure 1 Shown is a flowchart of a function call tracing method provided in an embodiment of the present application.
[0018] Figure 2 Shown is a flowchart for obtaining call chain data in an embodiment of the present application.
[0019] Figure 3 Shown is a flow chart of an application method of the function call tracing method provided in an embodiment of the present application.
[0020] Figure 4 Shown is a flowchart of another application method of the function call tracing method provided in an embodiment of the present application.
[0021] Figure 5 Shown is a structural diagram of a function call tracing system provided in an embodiment of the present application.
[0022] Figure 6 Shown is a schematic diagram of the structure of an electronic device provided in an embodiment of the present application.
[0023] Component number description
[0024] 5 Function Call Tracing System
[0025] 51 Stack top and stack determination module
[0026] 52 Stack Backtrace Module
[0027] 53 Call chain data acquisition module
[0028] 54 Function name acquisition module
[0029] 600 Electronic equipment
[0030] 601 Processor
[0031] 602 Memory
[0032] 6021 Operating System
[0033] 6022 Applications
[0034] 603 Network Interface
[0035] 604 bus system
[0036] 605 User Interface DETAILED DESCRIPTION
[0037] The following describes the embodiments of the present application through specific examples, and those skilled in the art can easily understand other advantages and effects of the present application from the contents disclosed in this specification. The present application can also be implemented or applied through other different specific embodiments, and the details in this specification can also be modified or changed in various ways based on different viewpoints and applications without departing from the spirit of the present application. It should be noted that the following embodiments and features in the embodiments can be combined with each other without conflict.
[0038] It should be noted that the illustrations provided in the following embodiments are only used to illustrate the basic concept of the present application in a schematic manner, and therefore the illustrations only show components related to the present application rather than being drawn according to the number, shape and size of components in actual implementation. In actual implementation, the type, quantity and proportion of each component may be changed arbitrarily, and the component layout may also be more complicated.
[0039] In the embodiments of the present application, the words "exemplary" or "for example" represent examples, illustrations or descriptions. Any embodiment or design described as "exemplary" or "for example" in the present application should not be interpreted as being more preferred or more advantageous than other embodiments or designs. Specifically, the use of words such as "exemplary" or "for example" is intended to present related concepts in a specific way.
[0040] In the embodiments of the present application, "at least one" refers to one or more, and "plurality" refers to two or more. "And / or" describes the association relationship of associated objects, indicating that three relationships may exist. For example, A and / or B can represent: A exists alone, A and B exist at the same time, and B exists alone, where A and B can be singular or plural. The character " / " generally indicates that the previous and next associated objects are in an "or" relationship. "At least one of the following" or similar expressions refers to any combination of these items, including any combination of single or plural items. For example, at least one of a, b or c can represent: a, b, c, ab, ac, bc or abc, where a, b, c can be single or multiple.
[0041] In some technical solutions, the running position of the program is additionally marked at the function entry and exit, and it is saved in a pre-reserved cache space. When necessary, the function call chain is obtained and parsed from the cache space to realize function call tracking. However, this method requires an additional small number of recording instructions to be executed at the entry and exit of the function, which will result in additional overhead of computing and storage resources. When the overall function volume of the system is large and the function call chain is deep, this will cause the processing speed of the device to drop significantly and the storage resources to increase significantly. In addition, in these technical solutions, the call information will be stored in a preset cache space, which requires additional memory, and the size of the cache space is often difficult to accurately determine, and usually requires relevant personnel to preset according to the complexity of the system. Furthermore, in the case of multiple threads, the above solution cannot distinguish the call status of each thread, and can only obtain 1 total function call chain, which is often difficult to meet actual needs.
[0042] At least to address the above problems, the embodiments of the present application provide a function call tracing method, system, storage medium and electronic device. The technical solution in the embodiments of the present application will be described in detail below in conjunction with the accompanying drawings in the embodiments of the present application.
[0043] Figure 1 The flowchart of the function call tracking method provided by the embodiment of the present application is shown. Figure 1 As shown, the function call tracing method provided in the embodiment of the present application includes the following steps S11 to S14.
[0044] S11, obtaining the target stack top and determining the target stack according to the target stack top, wherein the target stack is a stack to be traced for function calls, and the target stack top is the stack top of the target stack.
[0045] Specifically, for an operating system, it is usually necessary to run multiple threads, and each thread has a corresponding stack. In addition to these stacks corresponding to threads, the system may also include a system stack that serves interrupt functions. In order to obtain function call chains of different threads, one or more stacks in the system may be selected as target stacks in step S11. Since the stack space of a thread is applied for manually, the corresponding target stack may be determined by judging the space to which the top of the target stack belongs, and then judging the thread to which the target stack belongs.
[0046] S12, perform stack backtracing on the target stack to obtain the content of the target stack. The content of the target stack includes information such as stack frame, local variables, function parameters, and return address. The stack frame is a memory block allocated by the system for the function in the stack each time the function is called. It records the context of the function execution, such as the function's entry parameters and local variables, and the return address points to the location of the calling function so that the function returns to the correct location after execution. When a function is called, the address returned later is stored on the target stack. Therefore, the call chain information of the function can be directly obtained based on the content of the target stack.
[0047] Exemplarily, stack backtracing may be performed through a debugger or a stack tracing API in a programming language, but the present application is not limited thereto.
[0048] S13, according to the content of the target stack and the scope of the code segment, the call chain data of the function is obtained, and the call chain data includes the address information of the function. Specifically, the content of the target stack includes information such as stack frames, local variables, function parameters, and return addresses. In order to obtain the call chain data, the content of the target stack needs to be screened according to the scope of the code segment. For example, data belonging to the scope of the code segment can be obtained from the content of the target stack as the call chain data.
[0049] S14, according to the call chain data and the instruction storage address of each function, the corresponding function name is obtained to associate the function name with the call chain data. For example, the call chain data obtained in step S13 may correspond to three functions, but it is difficult to determine which function these call chain data specifically correspond to based only on the content of the target stack. Based on this, the function name corresponding to the call chain data can be obtained in step S14, thereby obtaining an intuitive function call chain.
[0050] As described above, in an embodiment of the present application, the stack corresponding to one or more threads of interest can be first determined as the target stack, and then the call chain data of the function can be obtained according to the content of the target stack, and the function name corresponding to these call chain data can be obtained. This method does not require the reservation of specific codes before and after the function in advance, and has the advantages of short time consumption and low memory overhead. In addition, since the call chain data is naturally stored in the target stack, there is no need to use a new buffer to record the data, which is conducive to reducing storage resource overhead. Furthermore, compared with the solution of mixing the function call chains of all threads together for analysis, the function call chain of each thread can be obtained in the embodiment of the present application, which is conducive to analyzing the working status of each thread.
[0051] In some implementations, the method for obtaining the target stack top includes: for a suspended thread, obtaining the target stack top according to a thread control block (TCB) of the thread.
[0052] A suspended thread is a thread that has temporarily stopped executing for some reason. When it is suspended, the execution state and context of the thread are saved so that it can be resumed and continued later. Exemplarily, the reasons why a thread is suspended include the thread waiting for resources, I / O operation blocking, system calls or active calls to thread synchronization mechanisms, such as sleep(), wait(), etc.
[0053] The thread control block is a data structure used to manage threads in the operating system kernel. It contains various runtime information of the thread, such as the thread's unique identifier, priority, program counter, register contents, stack pointer, and thread state (running, ready, waiting, etc.). When a thread is suspended, its current running context, including the stack pointer, register state, etc., will be saved in the thread control block so that these states can be reloaded and continued when the thread is resumed.
[0054] Exemplarily, the target stack top can be obtained according to the thread control block by reading the stack pointer. For example, the stack pointer in the TCB can be accessed through the debugging tool or API of the operating system (such as the pthread library in Linux or the GetThreadContext function of Windows) and located at the target stack top.
[0055] In some implementations, obtaining the target stack top includes: for a currently running function, obtaining the target stack top according to the position of the local variables applied by the function. Specifically, when a function is called, the system allocates a stack frame for it, and the stack frame is located at the top of the target stack and is used to store information such as the local variables, parameters, and return address of the function. Therefore, for a currently running function, the target stack top can be obtained according to the position of the local variables applied by the function.
[0056] In some implementations, obtaining the target stack top includes: when an interrupt is triggered, obtaining the target stack top according to a callback function of the interrupt.
[0057] An interrupt interrupts the execution of the current program and executes the handler associated with the interrupt instead. Common interrupt types include hardware interrupts (such as keyboard input, interrupt signals) and software interrupts (such as system calls). A callback function is a special function that is automatically called by the system when an interrupt occurs to handle the interrupt event. When an interrupt occurs, the currently executed function is paused and the system saves the current context information, including the stack pointer, register status, etc. The target stack top can be obtained according to the interrupt callback function.
[0058] In some implementations, the function call tracing method provided by the embodiments of the present application may also include: disassembling the compiled code file to obtain the instructions of each function and the instruction storage address. Specifically, by disassembling the compiled code file, the specific instructions of each function and the storage address of its instructions in the memory can be obtained. Since the function's call chain data contains the function's address information, the function name and the storage address of the function's instructions in the memory can be obtained by disassembling, and the function name corresponding to each call chain data can be obtained by comparing the address information with the storage address.
[0059] In some implementations, the code segment is stored in a continuous memory space. For example, a link script can be used to reasonably arrange the positions of the code segment, data segment, etc. of the program to avoid the code segment being intertwined with other non-code segments, so that the code segment independently and completely occupies a continuous memory space. The program can obtain the starting address and the ending address of the memory space through the link script.
[0060] See also Figure 2 In some implementations, obtaining call chain data according to the content of the target stack and the scope of the code segment includes the following steps S21 and S22.
[0061] S21, obtaining the starting address and the ending address of the memory space. For example, the starting address and the ending address of the memory space may be obtained through a link script, but the present application is not limited thereto.
[0062] S22, comparing the content of the target stack with the data in the memory space to obtain the data in the target stack located in the memory space as the call chain data.
[0063] In some implementations, the function call tracing method provided in the embodiments of the present application can be used to help quickly locate the root cause of a problem when an error or exception occurs in a program. Figure 3 A method for processing program exceptions is shown, and the method includes the following steps S31 to S35.
[0064] S31, using an exception capture mechanism (such as a try-catch statement) to capture exception information.
[0065] S32, obtaining a function call chain based on the function call tracing method provided in an embodiment of the present application.
[0066] S33, recording the function call chain and exception information into a log file, which can provide a complete context for subsequent exception analysis and debugging. Exemplarily, the log file may include error timestamp, exception type and message, function list, and other contents.
[0067] S34, analyzing the function call sequence from the main program to the error point based on the function call chain to determine the function that causes the exception, and then obtaining the cause of the exception.
[0068] S35, repair the abnormality according to the cause of the abnormality.
[0069] In some implementations, the function call tracing method provided by the embodiments of the present application can be used to optimize software. Figure 4 An optimization method is shown, which includes the following steps S41 to S43.
[0070] S41, obtaining a function call chain based on the function call tracing method provided in an embodiment of the present application.
[0071] S42, obtaining time-consuming functions and call chains according to the function call chains.
[0072] S43, take corresponding optimization measures to optimize the functions and call chains that consume more time.
[0073] For example, it is possible to check whether the functions in the call chain have repeated calls or unnecessary recursions, and reduce the time consumption by reducing unnecessary calls.
[0074] For example, for functions that take a long time to execute in a function call chain, multithreading or parallel computing can be used to reduce the time consumption.
[0075] In some implementations, the function call tracing method provided in the embodiments of the present application can be used to obtain the architecture of the software and optimize the architecture of the software.
[0076] For example, the actual working mode of the system can be obtained by analyzing the function call chain, and the module division, dependency relationship and mutual cooperation mode of the system can be obtained by tracing the function call sequence when the program is running.
[0077] The protection scope of the function call tracing method provided in the embodiment of the present application is not limited to the execution order of the steps listed in the present embodiment. All solutions implemented by adding, reducing or replacing steps in the prior art based on the principles of the present application are included in the protection scope of the present application.
[0078] An embodiment of the present application also provides a function call tracing system, which can implement the function call tracing method described in the present application. However, the implementation device of the function call tracing method described in the present application includes but is not limited to the structure of the function call tracing system listed in the present embodiment. All structural deformations and replacements of the prior art made according to the principles of the present application are included in the protection scope of the present application.
[0079] Figure 5 is a schematic block diagram of a function call tracing system provided in an embodiment of the present application. Figure 5 As shown, the function call tracking system 5 includes a stack top and stack determination module 51, a stack backtracking module 52, a call chain data acquisition module 53 and a function name acquisition module 54. The stack top and stack determination module 51 is used to obtain the target stack top and determine the target stack according to the target stack top. The stack backtracking module 52 is used to perform stack backtracking on the target stack to obtain the content of the target stack. The call chain data acquisition module 53 is used to obtain the call chain data of the function according to the content of the target stack and the range of the code segment, and the call chain data includes the address information of the function. The function name acquisition module 54 is used to obtain the corresponding function name according to the call chain data and the instruction storage address of each function.
[0080] It should be understood that the specific process of each module executing the above corresponding steps has been described in detail in the above method embodiment, and for the sake of brevity, it will not be repeated here.
[0081] The embodiment of the present application also provides a computer-readable storage medium, on which a computer program is stored, and when the computer program is executed by the processor, the function call tracking method provided by the embodiment of the present application is implemented. A person of ordinary skill in the art can understand that all or part of the steps in the method for implementing the above embodiment can be completed by instructing the processor through a program, and the program can be stored in a computer-readable storage medium, and the storage medium is a non-transitory medium, such as a random access memory, a read-only memory, a flash memory, a hard disk, a solid-state hard disk, a magnetic tape, a floppy disk, an optical disc, and any combination thereof. The above storage medium can be any available medium that can be accessed by a computer or a data storage device such as a server or a data center that includes one or more available media integrated. The available medium can be a magnetic medium (for example, a floppy disk, a hard disk, a tape), an optical medium (for example, a digital video disc (DVD)), or a semiconductor medium (for example, a solid-state disk (SSD)), etc.
[0082] An embodiment of the present application also provides an electronic device. Figure 6 is a schematic block diagram of an electronic device provided in an embodiment of the present application. Figure 6 As shown, the electronic device 600 includes: at least one processor 601, a memory 602, at least one network interface 603 and a user interface 605. The various components in the electronic device 600 are coupled together through a bus system 604. It can be understood that the bus system 604 is used to realize the connection and communication between these components. In addition to the data bus, the bus system 604 also includes a power bus, a control bus and a status signal bus. However, for the sake of clarity, the bus system 604 is not shown in FIG. Figure 6 In the specification, various buses are labeled as bus systems.
[0083] The user interface 605 may include a display, a keyboard, a mouse, a trackball, a click gun, keys, buttons, a touch pad or a touch screen.
[0084] It is understood that the memory 602 can be a volatile memory or a non-volatile memory, and can also include both volatile and non-volatile memories. Among them, the non-volatile memory can be a read-only memory (ROM), a programmable read-only memory (PROM), which is used as an external cache. By way of exemplary but not limiting explanation, many forms of RAM are available, such as static random access memory (SRAM), synchronous static random access memory (SSRAM). The memory described in the embodiments of the present application is intended to include but is not limited to these and any other suitable categories of memory.
[0085] The memory 602 in the embodiment of the present application is used to store various categories of data to support the operation of the electronic device 600. Examples of these data include: any executable program for operating on the electronic device 600, such as an operating system 6021 and an application 6022; the operating system 6021 includes various system programs, such as a framework layer, a core library layer, a driver layer, etc., for implementing various basic services and processing hardware-based tasks. The application 6022 may include various applications, such as a media player (Media Player), a browser (Browser), etc., for implementing various application services. The function call tracking method provided in the embodiment of the present application may be included in the application 6022.
[0086] The method disclosed in the above embodiment of the present application can be applied to the processor 601, or implemented by the processor 601. The processor 601 may be an integrated circuit chip with signal processing capabilities. In the implementation process, each step of the above method can be completed by an integrated logic circuit of hardware in the processor 601 or an instruction in the form of software. The above processor 601 may be a general-purpose processor, a digital signal processor (DSP, Digital Signal Processor), or other programmable logic devices, discrete gates or transistor logic devices, discrete hardware components, etc. The processor 601 can implement or execute the various methods, steps and logic block diagrams disclosed in the embodiments of the present application. The general-purpose processor 601 may be a microprocessor or any conventional processor, etc. In combination with the steps of the accessory optimization method provided in the embodiment of the present application, it can be directly embodied as a hardware decoding processor to execute, or it can be executed by a combination of hardware and software modules in the decoding processor. The software module can be located in a storage medium, which is located in a memory, and the processor reads the information in the memory and completes the steps of the above method in combination with its hardware.
[0087] In an exemplary embodiment, the electronic device 600 may be implemented by one or more application specific integrated circuits (ASIC), DSP, programmable logic device (PLD), complex programmable logic device (CPLD) to execute the aforementioned method.
[0088] The terms "component", "module", "system", etc. used in this specification are used to represent computer-related entities, hardware, firmware, a combination of hardware and software, software, or software in execution. For example, a component can be, but is not limited to, a process running on a processor, a processor, an object, an executable file, an execution thread, a program and / or a computer. By way of illustration, both applications running on a computing device and a computing device can be components. One or more components may reside in a process and / or an execution thread, and a component may be located on a computer and / or distributed between two or more computers. In addition, these components may be executed from various computer-readable media having various data structures stored thereon. Components may, for example, communicate through local and / or remote processes according to signals having one or more data packets (e.g., data from two components interacting with another component between a local system, a distributed system and / or a network, such as the Internet interacting with other systems through signals).
[0089] Those of ordinary skill in the art will appreciate that the various illustrative logical blocks and steps described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, or in a combination of computer software and electronic hardware. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professional and technical personnel may use different methods to implement the described functions for each specific application, but such implementation should not be considered beyond the scope of this application.
[0090] In the several embodiments provided in the present application, it should be understood that the disclosed systems, devices and methods can be implemented in other ways. For example, the device embodiments described above are only schematic. For example, the division of units is only a logical function division. There may be other division methods in actual implementation. For example, multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of devices or units, which can be electrical, mechanical or other forms.
[0091] The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0092] In addition, each functional unit in each embodiment of the present application may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit.
[0093] In the above embodiments, the functions of each functional unit can be implemented in whole or in part by software, hardware, firmware or any combination thereof. When implemented using software, it can be implemented in whole or in part in the form of a computer program product. The computer program product includes one or more computer instructions (programs). When loading and executing computer program instructions (programs) on a computer, the process or function according to the embodiment of the present application is generated in whole or in part. The computer can be a general-purpose computer, a special-purpose computer, a computer network, or other programmable devices. Computer instructions can be stored in a computer-readable storage medium, or transmitted from one computer-readable storage medium to another computer-readable storage medium, for example, computer instructions can be transmitted from a website site, computer, server or data center by wired (e.g., coaxial cable, optical fiber, digital subscriber line (digital subscriber line, DSL)) or wireless (e.g., infrared, wireless, microwave, etc.) mode to another website site, computer, server or data center.
[0094] If the function is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present application, or the part that contributes to the prior art or the part of the technical solution, can be embodied in the form of a software product, which is stored in a storage medium and includes several instructions for a computer device (which can be a personal computer, server, or network device, etc.) to perform all or part of the steps of the various embodiments of the present application. The aforementioned storage medium includes: various media that can store program codes, such as a USB flash drive, a mobile hard disk, a read-only memory, a random access memory, a magnetic disk or an optical disk.
[0095] In summary, in the embodiment of the present application, the position of the code segment is reasonably arranged, and the function call chain is traced back according to the stack information naturally formed when the program is running. This method does not need to reserve specific codes before and after the function in advance, and has the advantages of short time consumption and low memory overhead. In addition, since the call chain data is naturally stored in the stack, there is no need to use a new buffer to record the data in the embodiment of the present application, which is conducive to further reducing the storage resource overhead. Furthermore, since different threads hold different stacks, the function call chains of different threads can be obtained in the embodiment of the present application, which is conducive to analyzing the working conditions of each thread. Therefore, the present application effectively overcomes the various shortcomings in the prior art and has a high industrial utilization value.
[0096] The above embodiments are merely illustrative of the principles and effects of the present application and are not intended to limit the present application. Anyone familiar with the technology may modify or change the above embodiments without violating the spirit and scope of the present application. Therefore, all equivalent modifications or changes made by a person of ordinary skill in the art without departing from the spirit and technical ideas disclosed in the present application shall still be covered by the claims of the present application.
Claims
1. A function call tracing method based on stack backtracing, characterized in that: The method comprises: Obtaining a target stack top and determining a target stack according to the target stack top; Performing stack backtracing on the target stack to obtain the content of the target stack; Acquire call chain data of the function according to the content of the target stack and the range of the code segment, wherein the call chain data includes address information of the function; The corresponding function name is obtained according to the call chain data and the instruction storage address of each function.
2. The method according to claim 1, characterized in that Acquiring the target stack top includes: for a suspended thread, acquiring the target stack top according to a thread control block of the thread.
3. The method according to claim 1, characterized in that Obtaining the target stack top includes: for a currently running function, obtaining the target stack top according to the position of a local variable applied for by the function.
4. The method according to claim 1, characterized in that Acquiring the target stack top includes: when an interrupt is triggered, acquiring the target stack top according to a callback function of the interrupt.
5. The method according to claim 1, characterized in that The method further comprises: disassembling the compiled code file to obtain the instructions of each function and the instruction storage address.
6. The method according to claim 1, characterized in that The code segment is stored in a continuous memory space.
7. The method according to claim 6, characterized in that Acquiring call chain data according to the content of the target stack and the scope of the code segment includes: Obtaining the starting address and the ending address of the memory space; The content of the target stack is compared with the data in the memory space to obtain the data in the target stack located in the memory space as the call chain data.
8. A function call tracking system based on stack backtrace, characterized in that: The system comprises: A stack top and stack determination module, used to obtain a target stack top and determine a target stack according to the target stack top; A stack backtracking module, used for performing stack backtracking on the target stack to obtain the content of the target stack; A call chain data acquisition module, used to acquire the call chain data of the function according to the content of the target stack and the range of the code segment, wherein the call chain data includes the address information of the function; The function name acquisition module is used to acquire the corresponding function name according to the call chain data and the instruction storage address of each function.
9. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the method according to any one of claims 1 to 7 is implemented.
10. An electronic device, characterized in that: The electronic device comprises: A memory storing a computer program; A processor is communicatively connected to the memory, and executes the method according to any one of claims 1 to 7 when calling the computer program.