Stack backtracking method and device, equipment, storage medium and program product
By obtaining the interrupt index array and the function return address array, combining the interrupt function address array, obtaining the call relationship between program functions and interrupt processing function location in the RISC architecture, the problem of information loss in the stack after the interrupt processing function exits is solved, and the problem of information loss in the stack is realized Complete stack backtracking information acquisition and rapid analysis.
Patent Information
- Application Number
- CN202411354680.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-09-26
- Publication Date
- 2024-12-17
AI Technical Summary
In the RISC architecture, after the interrupt processing function exits, the stack frames of the interrupt processing function no longer contain the interrupt processing function, resulting in the lack of backtracking of the interrupt processing function when using GDB for stack backtracking, resulting in incomplete stack backtracking information.
By obtaining the interrupt index array and the function return address array, combining the interrupt function address array, obtaining the stack traceback information, including the call relationship between program functions and the location of the interrupt processing function, ensuring the integrity of the traceback information and not entering the debugging mode automatically triggers stack traceback.
It realizes the acquisition of complete stack backtracking information in the RISC architecture, avoids the loss of interrupt backtracking information, and quickly obtains stack backtracking information when it cannot enter debug mode.
Smart Images

Figure CN119149430A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of software debugging technology, and in particular to a stack backtracing method, device, equipment, storage medium and program product. Background Art
[0002] In a Reduced Instruction Set Computer (RISC) architecture, an interrupt may occur during program execution. At this time, the current program execution flow needs to be interrupted immediately, and an interrupt handling function is called to handle the interrupt. After the interrupt is handled, the interrupt handling function is exited and the original program is returned to continue execution. Among them, the RISC architecture can be the RISC-V architecture, where V represents the fifth generation of RISC.
[0003] During the execution of a program, the function call path is recorded through a stack. For example, a stack may include multiple stack frames. Each stack frame is a memory block allocated for each function call, which is used to store the parameters, return address and other information of the called function. The corresponding stack frame is deleted after the function is exited. By backtracking from the top of the stack to the bottom of the stack, it is possible to trace back from the current execution point to the execution starting point of the program, and then locate program errors by analyzing the function call path of the program execution process. This process is called stack backtracking.
[0004] In the related art, the stack backtrace function of GDB (GNU Debugger) is used to perform stack backtrace on each stack frame in the stack by entering the debugging mode. The obtained stack backtrace information includes the return address of each called function, and the function call path of the program execution process can be analyzed through each return address. Among them, GNU is an operating system.
[0005] However, since the stack no longer includes the stack frame of the interrupt handling function after the interrupt handling function exits execution, the method of using GDB to perform stack backtracing on the stack lacks the backtracing of the interrupt handling function, resulting in incomplete stack backtracing information. Summary of the invention
[0006] The present application provides a stack backtracking method, apparatus, device, storage medium and program product, which can solve the problems in the related art.
[0007] In a first aspect, a stack backtracing method is provided, the method comprising: in a process of executing a program, obtaining an interrupt index array, the interrupt index array comprising an interrupt address of an executed interrupt processing function, the interrupt processing function being a function called when an interrupt occurs in the program, the interrupt address indicating an address of a next instruction of the instruction in which the interrupt occurs;
[0008] When the executed program meets the stack backtrace condition, obtaining a function return address array, wherein the function return address array includes a return address of the executed program function, wherein the program function is a function called by an instruction of the program during normal execution;
[0009] Stack backtrace information is obtained based on the interrupt index array and the function return address array, wherein the stack backtrace information includes the calling relationship between program functions during the program execution process and the location of the program function where the interrupt occurs.
[0010] In a possible implementation, the method further includes: in the process of executing the program, obtaining an interrupt function address array, the interrupt function address array including the entry address of the executed interrupt handling function; obtaining stack backtrace information based on the interrupt index array and the function return address array, including: obtaining stack backtrace information based on the interrupt index array, the function return address array and the interrupt function address array, the stack backtrace information also including the calling relationship between the program function where the interrupt occurs and the interrupt handling function.
[0011] In a possible implementation, the context of the return addresses in the function return address array indicates the calling relationship between the program functions; the stack backtrace information is obtained based on the interrupt index array, the function return address array and the interrupt function address array, including: arranging the interrupt addresses in the interrupt index array into the function return address array to obtain a sorted address array, wherein the arrangement positions of the interrupt addresses belonging to the interrupt index array in the sorted address array indicate the positions of the program functions where the interruption occurs; replacing the interrupt addresses belonging to the interrupt index array in the sorted address array with the entry addresses in the interrupt function address array to obtain a replaced address array, wherein the context of the return address and the entry address in the replaced address array indicates the calling relationship between the program functions where the interruption occurs and the interrupt handling function; and using the replaced address array as the stack backtrace information.
[0012] In a possible implementation, obtaining the function return address array includes: determining the function type of the first function in which the executed program is located, and obtaining the return address of the first function according to the function type of the first function; saving the return address of the first function into the function return address array; and obtaining the function return address array when a termination condition is met.
[0013] In a possible implementation, after saving the return address of the first function into the function return address array, it also includes: when the termination condition is not met, determining the function type of the second function that calls the first function, and obtaining the return address of the second function according to the function type of the second function; saving the return address of the second function into the function return address array; and obtaining the function return address array when the termination condition is met.
[0014] In one possible implementation, determining the function type of the first function in which the executed program is located includes: when the number of interrupt occurrences is greater than a reference value, determining that the function type of the first function is an interrupt handling function, wherein the number of interrupt occurrences is increased by a first value when entering execution of the interrupt handling function, and is decreased by the first value when exiting execution of the interrupt handling function; when the number of interrupt occurrences is equal to the reference value, if the return address obtained based on the frame pointer of the program is inconsistent with the return address in the return address register of the program, determining that the function type of the first function is a leaf function; if the return address obtained based on the frame pointer is consistent with the return address in the return address register, determining that the function type of the first function is a non-leaf function.
[0015] In a possible implementation, obtaining the return address of the first function according to the function type of the first function includes: when the function type of the first function is an interrupt handling function or a non-leaf function, using the return address obtained based on the frame pointer of the program as the return address of the first function, and the frame pointer points to the stack frame base address of the first function.
[0016] In a possible implementation, after saving the return address of the first function into the function return address array, it also includes: obtaining the value of the stack pointer corresponding to the frame pointer based on the interrupt handling function or the non-leaf function, and assigning the value of the stack pointer to the frame pointer so that the assigned frame pointer points to the stack frame base address of the second function that calls the first function, and the assigned frame pointer is used to determine the function type of the second function.
[0017] In a possible implementation, obtaining the return address of the first function according to the function type of the first function includes: when the function type of the first function is a leaf function, obtaining the value of the stack pointer corresponding to the frame pointer based on the leaf function, assigning the value of the stack pointer to the frame pointer so that the assigned frame pointer points to the stack frame base address of the non-leaf function that calls the first function; using the return address in the return address register of the program and the return address obtained based on the assigned frame pointer as the return address of the first function.
[0018] In a possible implementation, after saving the return address of the first function into the function return address array, it also includes: obtaining the value of the stack pointer corresponding to the assigned frame pointer based on the non-leaf function, assigning the value of the stack pointer to the assigned frame pointer, so that the re-assigned frame pointer points to the stack frame base address of the second function that calls the non-leaf function, and the re-assigned frame pointer is used to determine the function type of the second function.
[0019] In a possible implementation, the termination condition is to satisfy any one of the following three conditions; condition one: the value of the return address saved in the function return address array is 0; condition two: the value of the stack pointer obtained based on the frame pointer is greater than or equal to the value of the top of the program stack; condition three: the number of stack backtraces is greater than the stack backtraces depth, and the stack backtraces depth indicates the maximum number of stack backtraces allowed, wherein the number of stack backtraces is added to a second value when the function return address array stores one return address.
[0020] In a second aspect, a stack backtracking device is provided, the device comprising:
[0021] A first acquisition module is used to acquire an interrupt index array during the execution of a program, wherein the interrupt index array includes an interrupt address of an executed interrupt processing function, wherein the interrupt processing function is a function called when an interrupt occurs in the program, and the interrupt address indicates an address of a next instruction of the instruction in which the interrupt occurs;
[0022] A second acquisition module is used to acquire a function return address array when the executed program meets the stack backtrace condition, wherein the function return address array includes the return address of the executed program function, and the program function is a function called by an instruction of the program during normal execution;
[0023] The third acquisition module is used to acquire stack backtrace information based on the interrupt index array and the function return address array, wherein the stack backtrace information includes the calling relationship between program functions during the program execution process and the location of the program function where the interrupt occurs.
[0024] In a possible implementation, the second acquisition module is further used to acquire an interrupt function address array during execution of the program, wherein the interrupt function address array includes an entry address of an executed interrupt handling function; the third acquisition module is used to acquire stack backtrace information based on the interrupt index array, the function return address array, and the interrupt function address array, wherein the stack backtrace information also includes a calling relationship between the interrupted program function and the interrupt handling function.
[0025] In a possible implementation, the front-end relationship of the return addresses in the function return address array indicates the calling relationship between the program functions; the third acquisition module is used to arrange the interrupt addresses in the interrupt index array into the function return address array to obtain a sorted address array, and the arrangement position of the interrupt addresses belonging to the interrupt index array in the sorted address array indicates the position of the program function where the interrupt occurs; the interrupt addresses belonging to the interrupt index array in the sorted address array are replaced with the entry addresses in the interrupt function address array to obtain a replaced address array, and the front-end relationship of the return address and the entry address in the replaced address array indicates the calling relationship between the program function where the interrupt occurs and the interrupt handling function; and the replaced address array is used as the stack backtrace information.
[0026] In a possible implementation, the second acquisition module is used to determine the function type of the first function in which the executed program is located, and to acquire the return address of the first function according to the function type of the first function; to save the return address of the first function into the function return address array; and to obtain the function return address array when a termination condition is met.
[0027] In a possible implementation, the second acquisition module is further used to determine the function type of the second function that calls the first function when the termination condition is not met, and to obtain the return address of the second function according to the function type of the second function; save the return address of the second function into the function return address array; and obtain the function return address array when the termination condition is met.
[0028] In a possible implementation, the second acquisition module is used to determine that the function type of the first function is an interrupt handling function when the number of interrupt occurrences is greater than a reference value, wherein the number of interrupt occurrences is increased by a first value when entering execution of the interrupt handling function, and is decreased by the first value when exiting execution of the interrupt handling function; when the number of interrupt occurrences is equal to the reference value, if the return address obtained based on the frame pointer of the program is inconsistent with the return address in the return address register of the program, the function type of the first function is determined to be a leaf function; if the return address obtained based on the frame pointer is consistent with the return address in the return address register, the function type of the first function is determined to be a non-leaf function.
[0029] In a possible implementation, the second acquisition module is used to use the return address obtained based on the frame pointer of the program as the return address of the first function when the function type of the first function is an interrupt handling function or a non-leaf function, and the frame pointer points to the stack frame base address of the first function.
[0030] In a possible embodiment, the second acquisition module is also used to obtain the value of the stack pointer corresponding to the frame pointer based on the interrupt handling function or the non-leaf function, and assign the value of the stack pointer to the frame pointer so that the assigned frame pointer points to the stack frame base address of the second function that calls the first function, and the assigned frame pointer is used to determine the function type of the second function.
[0031] In a possible implementation, the second acquisition module is used to, when the function type of the first function is a leaf function, obtain the value of the stack pointer corresponding to the frame pointer based on the leaf function, assign the value of the stack pointer to the frame pointer so that the assigned frame pointer points to the stack frame base address of the non-leaf function that calls the first function; and use the return address in the return address register of the program and the return address obtained based on the assigned frame pointer as the return address of the first function.
[0032] In a possible implementation, the second acquisition module is also used to obtain the value of the stack pointer corresponding to the assigned frame pointer based on the non-leaf function, and assign the value of the stack pointer to the assigned frame pointer, so that the re-assigned frame pointer points to the stack frame base address of the second function that calls the non-leaf function, and the re-assigned frame pointer is used to determine the function type of the second function.
[0033] In a possible implementation, the termination condition is to satisfy any one of the following three conditions; condition one: the value of the return address saved in the function return address array is 0; condition two: the value of the stack pointer obtained based on the frame pointer is greater than or equal to the value of the top of the program stack; condition three: the number of stack backtraces is greater than the stack backtraces depth, and the stack backtraces depth indicates the maximum number of stack backtraces allowed, wherein the number of stack backtraces is added to a second value when the function return address array stores one return address.
[0034] In a possible implementation, the stack backtracking device has the function of implementing the behavior in the method embodiment provided in the above aspects. The stack backtracking device can be a device, or a component of a device (such as a processor, a chip, or a chip system, etc.), or a logic module or software that can implement all or part of the functions of the communication device. The functions of the stack backtracking device can be implemented by hardware, or by hardware executing corresponding software implementations, and the hardware or software includes one or more modules or units corresponding to the above functions.
[0035] In a third aspect, a computer device is also provided, comprising a processor and a memory, wherein the memory stores at least one program code, and the at least one program code is loaded and executed by the processor so that the computer device implements any of the stack backtracing methods described above.
[0036] In a fourth aspect, a computer-readable storage medium is also provided, wherein at least one program code is stored in the computer-readable storage medium, and the at least one program code is loaded and executed by a processor so that a computer implements any of the stack backtracing methods described above.
[0037] In a fifth aspect, a computer program product or a computer program is also provided, wherein the computer program product or the computer program includes computer instructions, and the computer instructions are stored in a computer-readable storage medium. A processor of a computer device reads the computer instructions from the computer-readable storage medium, and the processor executes the computer instructions, so that the computer device executes any of the stack backtracking methods described above.
[0038] The technical solution provided by this application can at least bring the following beneficial effects:
[0039] The technical solution provided by the present application obtains an interrupt index array during the execution of a program. The interrupt index array includes the address of the next instruction where the interrupt occurs, so that based on the interrupt index array, it is possible to trace back the calling relationship between program functions and also trace back the locations of program functions where the interrupt occurred, without causing the loss of interrupt backtrace information, making the backtrace information more complete. In addition, the method automatically triggers the acquisition of the function return address array by whether the stack backtrace condition is met, that is, automatically triggers the acquisition of the stack backtrace information. This process does not require entering the debug mode through hardware, so that the stack backtrace information can be quickly obtained in the scenario where the debug mode cannot be entered. BRIEF DESCRIPTION OF THE DRAWINGS
[0040] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the drawings required for use in the description of the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without creative work.
[0041] Figure 1 It is a schematic diagram of an implementation environment of a stack backtracking method provided in an embodiment of the present application;
[0042] Figure 2 is a flowchart of a stack backtracking method provided by an embodiment of the present application;
[0043] Figure 3 It is a schematic diagram of a stack backtracking process provided by an embodiment of the present application;
[0044] Figure 4 is a schematic diagram of a stack backtracking device provided in an embodiment of the present application;
[0045] Figure 5 It is a structural diagram of a computer device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0046] In order to make the objectives, technical solutions and advantages of the present application clearer, the implementation methods of the present application will be further described in detail below with reference to the accompanying drawings.
[0047] In the process of executing a program, there will always be some unexpected situations. For example, when an exception occurs, the user needs to enter the debug mode and track the process of the exception by stack backtracing to locate the specific cause of the exception. In other words, when the user needs to locate whether an exception occurs in a program, it is necessary to enter the debug mode to accurately locate the location of the exception. However, in scenarios such as stuck machines, users are unable to enter the debug mode, resulting in the inability to locate the location of the exception in the program.
[0048] Among them, stack trace is a technology used to track function calls during program execution. It records the program from the current position back to the execution starting point of the program when an error or exception occurs, including the call relationship of each function and the corresponding return address. Stack traceback can help developers quickly locate the problem. The stack is a memory block used by the program to record the function call path during execution. For example, the stack can include multiple stack frames. Each stack frame is a memory block allocated for each function call, which is used to store the parameters, return address and other information of the called function. The corresponding stack frame is deleted after exiting the execution function.
[0049] For the RISC-V architecture, an interrupt may occur during program execution. At this time, it is necessary to immediately interrupt the current program execution flow and call the interrupt handling function to handle the interrupt. After the processing is completed, the interrupt handling function is exited and the original program is returned to continue execution.
[0050] In the related art, the stack backtrace function of GDB is used to perform stack backtrace on each stack frame in the stack by entering the debug mode. The obtained stack backtrace information includes the return address of each called function. The function call path of the program execution process can be analyzed through each return address.
[0051] However, this method requires entering the debug mode. If the debug mode cannot be entered, the stack backtrace information cannot be obtained in time. Even if the user can enter the debug mode, the stack no longer includes the stack frame of the interrupt handler function after the interrupt handler function exits execution, so the stack backtrace method using GDB to perform stack backtrace lacks the backtrace of the interrupt handler function, resulting in incomplete stack backtrace information.
[0052] The embodiment of the present application provides a stack backtrace method, which can obtain complete stack backtrace information without entering the debug mode. Figure 1 The schematic diagram of the implementation environment provided by the embodiment of the present application is shown. The implementation environment includes: a computer device 11. The computer device 11 is installed with a stack backtrace application, through which the stack backtrace method provided by the embodiment of the present application can be implemented.
[0053] In a possible implementation, the computer device 11 may refer to a terminal, or a server, or a processor, which is configured in an independent device. Exemplarily, the terminal may be any electronic product that can interact with a user through one or more methods such as a keyboard, a touchpad, a touch screen, a remote control, voice interaction, or a handwriting device, such as a personal computer (PC), a smart phone, a personal digital assistant (PDA), a wearable device, a handheld computer PPC (Pocket PC, PPC), a tablet computer, an intelligent car machine, etc. The server may be an independent physical server, or a server cluster or distributed system composed of multiple physical servers, or a cloud server that provides basic cloud computing services such as cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communications, middleware services, domain name services, security services, content delivery networks (CDN), and big data and artificial intelligence platforms.
[0054] Those skilled in the art should understand that the above-mentioned computer device 11 is only an example, and other existing or future computer devices may also be applicable to the present application, should also be included in the scope of protection of the present application, and are included here by reference.
[0055] Figure 2 A flowchart of a stack backtracking method provided in an embodiment of the present application, which can be applied to the above Figure 1 In the implementation environment shown, for example, the computer device 11 is applied, and the computer device 11 can be a terminal or a server, which is not limited in the present embodiment of the application. Figure 2 As shown, the stack backtracking method includes but is not limited to the following steps 201 to 203.
[0056] Step 201, during the execution of the program, obtain an interrupt index array, the interrupt index array includes the interrupt address of the executed interrupt processing function, the interrupt processing function is a function called when the program is interrupted, and the interrupt address indicates the address of the next instruction of the instruction where the interrupt occurs.
[0057] Among them, the program is composed of instructions, which are stored in the memory in a specific order. The computer device completes various tasks by executing instructions. Therefore, executing a program means executing each instruction in the program. Under normal circumstances, the computer device will execute instructions in a predetermined order, but when an interrupt occurs, an interrupt handling mechanism of the computer device is to call the interrupt handling function, which is a function used to handle the interrupt. That is, in the process of executing each instruction in the program, there is a case where the interrupt handling function is called due to an interrupt. After the interrupt is handled by the interrupt handling function, it returns to the original program to continue execution.
[0058] When the program calls the interrupt handling function, the interrupt address of the interrupt handling function will be recorded. The interrupt address is used to indicate the address of the next instruction of the instruction where the interrupt occurs, that is, the address of the next instruction in the originally executed program that is interrupted by the interrupt. Through the interrupt address, the original program can be returned after the interrupt handling function exits execution. Therefore, each time the interrupt handling function is called, the interrupt address of the interrupt handling function can be obtained, and the embodiment of the present application saves the interrupt address to the interrupt index array. The interrupt index array can be represented by back_mepc[].
[0059] In a possible implementation, when the interrupt processing function is called, in addition to obtaining the interrupt index array, the number of interrupt occurrences is also recorded. Optionally, the number of interrupt occurrences can be initialized to a reference value, and when entering the execution of the interrupt processing function, the number of interrupt occurrences is added to the first value, and when exiting the execution of the interrupt processing function, the number of interrupt occurrences is reduced by the first value. Both the reference value and the first value can be flexibly set according to the application scenario, for example, the reference value is 0 and the first value is 1.
[0060] In the RISC-V architecture, when an interrupt occurs, the hardware will automatically save the current PC in the Machine Exception Program Counter (mepc). When the interrupt is processed and returned, the hardware will automatically assign the address in mepc to the program counter (PC), which is used to point to the address of the instruction being executed or the address of the instruction to be executed, thereby restoring the correct execution process of the program. Therefore, for the RISC-V architecture, during the execution of the program, when entering the interrupt processing function, the instruction address in mepc is saved in back_mepc, and intr_tick is incremented by one; when exiting the interrupt processing function, intr_tick is decremented by one. The variable intr_tick represents the number of interrupts.
[0061] In a possible implementation, the method also obtains an interrupt function address array during the execution of the program, and the interrupt function address array includes the entry address of the executed interrupt processing function. The entry address of the interrupt processing function is the address of the instruction to enter the execution of the interrupt processing function, and the entry address can indicate the function type of the interrupt processing function, and the function type of the interrupt processing function can refer to the interrupt type of the interrupt. The interrupt function address array can be represented by back_intr_ra[]. The depth of the interrupt function address array is the same as the depth of the interrupt index array, which is the total number of times the interrupt processing function is called after an interrupt occurs during the execution of the program.
[0062] In the RISC-V architecture, the interrupt handling mechanism includes direct mode and vectored mode. Direct mode means that when an interrupt occurs, the interrupt handling function is directly executed through the base address field. In this direct mode, the base address field directly points to the entry address of the interrupt handling function corresponding to the interrupt type that caused the interrupt. Therefore, the entry address of the interrupt handling function can be obtained by reading the value of the base address field.
[0063] Vector mode means that when an interrupt occurs, the interrupt vector table (IVT) is determined by the base address field, and the entry address of the interrupt handling function corresponding to the interrupt type is indexed from the interrupt vector table according to the interrupt type. In this vector mode, the base address field points to a fixed interrupt vector table, and the entry address of the interrupt handling function of each entry in the interrupt vector table corresponds to an interrupt type. Therefore, the interrupt vector table can be determined by reading the value of the base address field, and the entry address of the interrupt handling function can be obtained in the interrupt vector table according to the interrupt type.
[0064] In an embodiment of the present application, the base address field may be stored in a machine trap vector base address register (MachineTrap Vector Base-Address Register, mtvec). Exemplarily, mtvec.Base (i.e., the base address of mtvec) is used to save the address to be jumped to when an interrupt occurs. In direct mode, the address in mtvec.Base is the entry address of all interrupt handling functions, and the address in mtvec.Base is saved in back_intr_ra; in vector mode, the address in mtvec.Base indicates the entry address of the interrupt vector, and the machine cause register (mcause) is used to save the interrupt source number indicating the interrupt type. In this case, the address in mtvec.Base+4*mcause is the entry address of the interrupt handling function, and the address in mtvec.Base+4*mcause is saved in back_intr_ra.
[0065] In addition, for the configurable core-local interrupt controller (Smclic) scenario, the machine trap vector table (mtvt) register is used to store the entry address of the interrupt vector table in the clic mode. At this time, the address in mtvt.Base+4*mcause is the entry address of the interrupt handling function, and the address in mtvt.Base+4*mcause is saved in back_intr_ra.
[0066] Step 202, when the executed program meets the stack backtrace condition, obtain a function return address array, the function return address array includes the return address of the executed program function, and the program function is a function called by an instruction of the program during normal execution.
[0067] In the embodiment of the present application, the situation where the stack backtracking condition is met refers to the situation where the stack backtracking is required, for example, an error or exception occurs in the executed program, or the executed program causes the machine to jam. When the stack backtracking condition is met, the stack is backtracked starting from the execution position of the current program to obtain the function return address layer by layer. This process does not require entering the debug mode, and the function return address array can be directly obtained by serial port printing. Serial port printing refers to the process of sending the function return address array to a computer device or other device for display or processing through a serial interface (serial port for short).
[0068] The function return address array includes the return address of the program function called during the program execution. The program function is the function called by the instruction during the normal execution of the program, not the function called due to unexpected circumstances such as interruption. The implementation method of obtaining the function return address array is similar to the stack backtrace function of GDB. Please refer to the relevant content of the stack backtrace function of GDB.
[0069] In a possible implementation, a method for obtaining a function return address array may include determining a function type of a first function in which an executed program is located, obtaining a return address of the first function according to the function type of the first function, and saving the return address of the first function in a function return address array. In the process of obtaining the function return address array, the number of stack backtraces is also recorded, and the number of stack backtraces indicates the number of layers for backtracing back layer by layer to obtain the return address. For example, the number of stack backtraces is initialized to a reference value, and each time a return address is saved in the function return address array, the number of stack backtraces is increased by a second value. Both the reference value and the second value can be flexibly set according to the application scenario, for example, the reference value is 0 and the second value is 1.
[0070] Optionally, a method for determining the function type of the first function in which the executed program is located may include, when the number of interrupt occurrences is greater than a reference value, determining that the function type of the first function is an interrupt handling function; when the number of interrupt occurrences is equal to the reference value, if the return address obtained based on the program's frame pointer (frame pointer, fp) is inconsistent with the return address in the program's return address (return address, ra) register, determining that the function type of the first function is a leaf function; if the return address obtained based on the frame pointer is consistent with the return address in the return address register, determining that the function type of the first function is a non-leaf function.
[0071] Since the number of interrupt occurrences is greater than the reference value when entering the interrupt handling function and is equal to the reference value when exiting the interrupt handling function, the number of interrupt occurrences can accurately determine whether the first function is an interrupt handling function. When the first function is not an interrupt handling function, the fp and ra registers are used to further determine whether the first function is a leaf function or a non-leaf function. A leaf function refers to a function that no longer calls other functions. A leaf function does not allocate stack space, that is, the stack does not include the stack frame corresponding to the leaf function; a non-leaf function refers to a function that may call other functions. A non-leaf function allocates stack space, that is, the stack includes the stack frame corresponding to the non-leaf function.
[0072] Among them, the frame pointer fp can also be called s0, and s0 indicates the bottom address of the stack of the calling function. The ra register is used to store the address of the next instruction of the calling function instruction, that is, the return address. The return address is also saved in the stack frame of the stack, so the return address saved in the stack frame can be obtained based on s0. For example, in the RISC-V architecture, the value in the memory address of s0-4 indicates the return address, and s0-n means s0 goes back n bytes. In this case, it is determined whether the return address in the memory address of s0-4 is inconsistent with the return address in the ra register.
[0073] In the embodiment of the present application, the function types include interrupt handling functions, leaf functions, and non-leaf functions, and the methods for obtaining the return address are different for different function types. Therefore, the methods for obtaining the return address of the first function according to the function type of the first function and saving the return address of the first function into the function return address array include the following three cases.
[0074] Case 1: the function type of the first function is an interrupt processing function.
[0075] In this case, the return address obtained based on the program's frame pointer is used as the return address of the first function, and the frame pointer now points to the stack frame base address of the first function. Exemplarily, the function return address array can be represented by back_ra[]. The return address in the memory address of s0-4 is saved in back_ra, and the number of stack backtraces is increased by one.
[0076] Optionally, after the return address in the memory address of s0-4 is saved in back_ra, the value of the stack pointer (Stack Pointer, SP) corresponding to the frame pointer is obtained based on the interrupt handling function, and the value of the stack pointer is assigned to the frame pointer, so that the assigned frame pointer points to the stack frame base address of the second function that calls the first function. Then, the function type of the second function can be determined according to the assigned frame pointer, so as to achieve the effect of tracing back the return addresses of functions at all levels layer by layer. For the interrupt handling function, exemplarily, the value in the memory address of s0-20 indicates the stack pointer, and the value in the memory address of s0-20 is assigned to s0.
[0077] Case 2: The function type of the first function is a non-leaf function.
[0078] In the second case, the return address obtained based on the program frame pointer is used as the return address of the first function, and the frame pointer now points to the stack frame base address of the first function. Exemplarily, the return address in the memory address of s0-4 is saved in back_ra, and the number of stack backtraces is increased by one.
[0079] Optionally, after the return address in the memory address of s0-4 is saved in back_ra, the value of the stack pointer corresponding to the frame pointer is obtained based on the non-leaf function, and the value of the stack pointer is assigned to the frame pointer, so that the assigned frame pointer points to the stack frame base address of the second function that calls the first function. For a non-leaf function, illustratively, the value in the memory address of s0-8 indicates a stack pointer, and the value in the memory address of s0-8 is assigned to s0.
[0080] Case three: the function type of the first function is a leaf function.
[0081] In this case three, the value of the stack pointer corresponding to the frame pointer is obtained based on the leaf function, and the value of the stack pointer is assigned to the frame pointer so that the assigned frame pointer points to the stack frame base address of the non-leaf function that calls the first function; the return address in the program's return address register and the return address obtained based on the assigned frame pointer are used as the return address of the first function. Optionally, after the return address of the first function is saved in the function return address array, the value of the stack pointer corresponding to the assigned frame pointer is obtained based on the non-leaf function, and the value of the stack pointer is assigned to the assigned frame pointer so that the frame pointer after re-assignment points to the stack frame base address of the second function that calls the non-leaf function, and the frame pointer after re-assignment is used to determine the function type of the second function.
[0082] Exemplarily, since the leaf function does not allocate stack space, the return address in the ra register is first saved to back_ra, and the number of stack backtraces is increased by one. At this time, the value in the memory address of s0-4 indicates SP; the value in the memory address of s0-4 is assigned to s0. At this time, s0 indicates the stack frame base address of the non-leaf function that calls the leaf function, and the value in the memory address of s0-4 indicates the return address of the non-leaf function; then the return address in the memory address of s0-4 is saved to back_ra, and the number of stack backtraces is increased by one; the value in the memory address of s0-8 is assigned to s0.
[0083] Therefore, through the above three situations, the return address of the first function can be obtained and saved in the function return address array, so that the function return address array includes the return address of the first function.
[0084] Next, it is determined whether the termination condition is met. If the termination condition is met, the final function return address array is obtained; if the termination condition is not met, the function type of the second function that calls the first function is determined, and the return address of the second function is obtained according to the function type of the second function; the return address of the second function is saved in the function return address array; until the termination condition is met. The relevant implementation methods of the second function are the same as those of the first function, and will not be repeated here.
[0085] Optionally, the termination condition is to meet any one of the following three conditions. Condition 1: The value of the return address saved in the function return address array is 0; Condition 2: The value of the stack pointer obtained based on the frame pointer is greater than or equal to the value of the program stack top; Condition 3: The number of stack backtraces is greater than the stack backtraces depth, and the stack backtraces depth indicates the maximum number of stack backtraces allowed. The stack backtraces depth can be flexibly set according to the application scenario, and the embodiment of the present application does not limit it. Among them, the number of stack backtraces can be represented by depth, and the stack backtraces depth can be represented by back_depth.
[0086] Step 203 , obtaining stack backtrace information based on the interrupt index array and the function return address array, wherein the stack backtrace information includes the calling relationship between program functions during program execution and the location of the program function where the interrupt occurs.
[0087] In an embodiment of the present application, the function return address array is the return address of each program function obtained layer by layer through the stack pointer, so the front and back relationship of the return addresses in the function return address array indicates the call relationship between the program functions. Acquiring stack backtrace information based on the interrupt index array and the function return address array may include arranging the interrupt addresses in the interrupt index array into the function return address array to obtain a sorted address array, and using the sorted address array as the stack backtrace information. The arrangement position of the interrupt addresses belonging to the interrupt index array in the sorted address array indicates the position of the program function where the interrupt occurs.
[0088] Optionally, the interrupt addresses in the interrupt index array are arranged in the function return address array according to the address size. For example, through the function return address array, it can be concluded that function 1 calls function 2, and through the interrupt index array, the address of the next instruction in the program interrupted by the interrupt can be obtained, that is, the interruption location, thereby determining the program execution path, the interruption location, and the number of interruptions through statistics.
[0089] In the case where the interrupt function address array is also obtained, obtaining stack backtrace information based on the interrupt index array and the function return address array includes: obtaining stack backtrace information based on the interrupt index array, the function return address array and the interrupt function address array, and the stack backtrace information also includes the calling relationship between the program function where the interrupt occurs and the interrupt processing function. In this case, after obtaining the sorted address array, the interrupt address belonging to the interrupt index array in the sorted address array is replaced with the entry address in the interrupt function address array to obtain the replaced address array, and the ante-posterior relationship between the return address and the entry address in the replaced address array indicates the calling relationship between the program function where the interrupt occurs and the interrupt processing function; the replaced address array is used as the stack backtrace information.
[0090] The method provided by the embodiment of the present application obtains an interrupt index array during the execution of a program. The interrupt index array includes the address of the next instruction where the interrupt occurs, so that based on the interrupt index array, it is possible to trace back the calling relationship between program functions and also trace back the locations of program functions where the interrupt occurred, without causing the loss of interrupt backtrace information, making the backtrace information more complete. In addition, the method automatically triggers the acquisition of the function return address array by whether the stack backtrace condition is met, that is, automatically triggers the acquisition of the stack backtrace information. This process does not require entering the debug mode through hardware, so that the stack backtrace information can be quickly obtained in the scenario where the debug mode cannot be entered.
[0091] Next, the interrupt handling mechanism for the vector mode of the RISC-V architecture is combined with Figure 3 The stack backtracking method provided in the embodiment of the present application is illustrated by way of example.
[0092] 101, declare and initialize the stack backtrace count (depth), stack backtrace depth (back_depth), stack backtrace interrupt index array (back_mepc), stack backtrace function return address array (back_ra) and stack backtrace interrupt function address array (back_intr_ra), where the number of array elements can be the stack backtrace depth (back_depth), and go to 102.
[0093] 102. For all interrupt handling functions during the program execution, when entering the interrupt handling function, mepc is saved to back_mepc, the value of the function address of the interrupt handling function (mtvec.Base+4*mcause) is saved to back_intr_ra, and intr_tick is increased by one; when exiting the interrupt handling function, intr_tick is reduced by one and enter 103.
[0094] 103 , at the program execution position where stack backtracing is required, call the stack backtracing function interface (ie, execute the function interface of the above steps 202 and 203 ), and enter 104 .
[0095] 104 , judging whether the currently executed instruction (ie, the program execution flow) is located in the interrupt processing function according to whether intr_tick is greater than 0, if it is greater than 0, it is located in the interrupt processing function and proceeding to 105 , if it is equal to 0, it is not located in the interrupt processing function and proceeding to 107 .
[0096] 105. When the program execution flow is in the interrupt handling function, the value of the function address (mtvec.BASE+4*mcause) of the interrupt handling function is saved in back_intr_ra, and mepc is saved in back_mepc, that is, back_intr_ra[depth]=mtvec.Base+4*mcause, back_mepc[depth]=mepc; depth is increased by one, that is, depth++, and enter 106.
[0097] 106, read the value from the memory address of s0-4 (i.e., the return address) and store it in back_ra, i.e., back_ra[depth]=s0-4; read the value amplitude from the memory address of s0-20 and give it to sp, and assign the value of sp to s0, i.e., sp=s0-20, s0=sp; add one to depth, i.e., depth++, and enter 107.
[0098] 107, judging whether the current function is a leaf function according to whether the value read from the s0-4 memory address is consistent with the current value of the ra register, if not, it is a leaf function and proceeds to 108, if consistent, it is a non-leaf function and proceeds to 109.
[0099] 108, save the current value of the ra register to back_ra, that is, back_ra[depth]=ra; read the value from the memory address of s0-4 and assign it to sp, and assign sp to s0, that is, sp=s0-4, s0=sp; add one to depth, that is, depth++, and enter 109.
[0100] 109, read the value from the memory address s0-4 (i.e., the return address) and save it to back_ra, i.e., back_ra[depth]=s0-4; read the value from the memory address s0-8 and assign it to sp, and assign sp to s0, i.e., sp=s0-8, s0=sp; add one to depth, i.e., depth++, and enter 110.
[0101] 110, determine whether the obtained back_ra[depth] is 0 (back_ra[depth]=0), or whether the value read from the memory address of s0-8 (i.e., the stack pointer sp) is greater than or equal to the program stack top (stacktop) (s0-8>stacktop), or whether depth is greater than back_depth (depth>back_depth). If any of them is satisfied, go to 111, otherwise return to 107.
[0102] 111, sort the return addresses in back_mepc and back_ra according to a rule, for example, perform ascending arrangement and combination to obtain arranged back_ra; and replace the return addresses originally belonging to back_mepc in the arranged back_ra with the entry addresses in back_intr_ra one by one to obtain replaced back_ra.
[0103] 112, output the stack backtrace information (ie, back_ra after replacement), and end the stack backtrace process.
[0104] In summary, the stack backtrace method can be implemented using software such as assembly or C language. There is no need to enter the debugging mode. It can directly print through the serial port to start stack backtrace from the exception or interrupt, obtain the function return address layer by layer, and count the location and number of interrupts in vector mode. It can obtain complete stack backtrace information to help users quickly analyze the occurrence of exceptions and locate problems.
[0105] See also Figure 4 , an embodiment of the present application provides a stack backtracking device, the device comprising:
[0106] A first acquisition module 401 is used to acquire an interrupt index array during the execution of a program, wherein the interrupt index array includes an interrupt address of an executed interrupt processing function, wherein the interrupt processing function is a function called when an interrupt occurs in the program, and the interrupt address indicates an address of a next instruction of the instruction in which the interrupt occurs;
[0107] A second acquisition module 402 is used to acquire a function return address array when the executed program meets the stack backtracking condition, where the function return address array includes the return address of the executed program function, where the program function is a function called by an instruction of the program during normal execution;
[0108] The third acquisition module 403 is used to acquire stack backtrace information based on the interrupt index array and the function return address array. The stack backtrace information includes the calling relationship between program functions during program execution and the location of the program function where the interrupt occurs.
[0109] In one possible implementation, the second acquisition module 402 is also used to acquire an interrupt function address array during program execution, and the interrupt function address array includes an entry address of the executed interrupt handling function; the third acquisition module 403 is used to acquire stack backtrace information based on the interrupt index array, the function return address array and the interrupt function address array, and the stack backtrace information also includes a calling relationship between the program function where the interrupt occurs and the interrupt handling function.
[0110] In one possible implementation, the context of the return addresses in the function return address array indicates the calling relationship between program functions; the third acquisition module 403 is used to arrange the interrupt addresses in the interrupt index array into the function return address array to obtain a sorted address array, and the arrangement position of the interrupt addresses belonging to the interrupt index array in the sorted address array indicates the position of the program function where the interrupt occurs; the interrupt addresses belonging to the interrupt index array in the sorted address array are replaced with the entry addresses in the interrupt function address array to obtain a replaced address array, and the context of the return address and the entry address in the replaced address array indicates the calling relationship between the program function where the interrupt occurs and the interrupt handling function; the replaced address array is used as stack backtrace information.
[0111] In one possible implementation, the second acquisition module 402 is used to determine the function type of the first function where the executed program is located, and to obtain the return address of the first function according to the function type of the first function; to save the return address of the first function into a function return address array; and to obtain the function return address array when the termination condition is met.
[0112] In a possible implementation, the second acquisition module 402 is also used to determine the function type of the second function that calls the first function when the termination condition is not met, and to obtain the return address of the second function according to the function type of the second function; save the return address of the second function into a function return address array; and obtain the function return address array when the termination condition is met.
[0113] In one possible implementation, the second acquisition module 402 is used to determine that the function type of the first function is an interrupt handling function when the number of interrupt occurrences is greater than a reference value, wherein the number of interrupt occurrences is increased by a first value when entering the execution of the interrupt handling function, and is decreased by the first value when exiting the execution of the interrupt handling function; when the number of interrupt occurrences is equal to the reference value, if the return address obtained based on the frame pointer of the program is inconsistent with the return address in the return address register of the program, the function type of the first function is determined to be a leaf function; if the return address obtained based on the frame pointer is consistent with the return address in the return address register, the function type of the first function is determined to be a non-leaf function.
[0114] In one possible implementation, the second acquisition module 402 is used to use the return address obtained based on the program's frame pointer as the return address of the first function when the function type of the first function is an interrupt handling function or a non-leaf function, and the frame pointer points to the stack frame base address of the first function.
[0115] In one possible implementation, the second acquisition module 402 is also used to obtain the value of the stack pointer corresponding to the frame pointer based on the interrupt handling function or the non-leaf function, and assign the value of the stack pointer to the frame pointer so that the assigned frame pointer points to the stack frame base address of the second function that calls the first function, and the assigned frame pointer is used to determine the function type of the second function.
[0116] In one possible implementation, the second acquisition module 402 is used to obtain the value of the stack pointer corresponding to the frame pointer based on the leaf function when the function type of the first function is a leaf function, and assign the value of the stack pointer to the frame pointer so that the assigned frame pointer points to the stack frame base address of the non-leaf function that calls the first function; and use the return address in the program's return address register and the return address obtained based on the assigned frame pointer as the return address of the first function.
[0117] In one possible implementation, the second acquisition module 402 is also used to obtain the value of the stack pointer corresponding to the assigned frame pointer based on the non-leaf function, and assign the value of the stack pointer to the assigned frame pointer, so that the re-assigned frame pointer points to the stack frame base address of the second function that calls the non-leaf function, and the re-assigned frame pointer is used to determine the function type of the second function.
[0118] In a possible implementation, the termination condition is to satisfy any one of the following three conditions; condition one: the value of the return address saved in the function return address array is 0; condition two: the value of the stack pointer obtained based on the frame pointer is greater than or equal to the value of the top of the program stack; condition three: the number of stack backtraces is greater than the stack backtraces depth, and the stack backtraces depth indicates the maximum number of stack backtraces allowed, wherein the number of stack backtraces is increased by a second value when the function return address array stores one return address.
[0119] It should be understood that the device provided in the above embodiment only uses the division of the above functional modules as an example to implement its functions. In actual applications, the above functions can be assigned to different functional modules as needed, that is, the internal structure of the device is divided into different functional modules to complete all or part of the functions described above. In addition, the device and method embodiments provided in the above embodiment belong to the same concept, and their specific implementation process and beneficial effects are detailed in the method embodiment, which will not be repeated here.
[0120] Please refer to Figure 5 , which shows a schematic diagram of the structure of a computer device provided by an embodiment of the present application. The computer device may be a terminal, such as a smart phone, a tablet computer, a vehicle-mounted terminal, a laptop computer or a desktop computer. The terminal may also be called a user device, a portable terminal, a laptop terminal, a desktop terminal or other names.
[0121] Typically, the terminal includes: a processor 701 and a memory 702 .
[0122] The processor 701 may include one or more processing cores, such as a 4-core processor, an 8-core processor, etc. The processor 701 may be implemented in at least one hardware form of DSP (Digital Signal Processing), FPGA (Field-Programmable Gate Array), and PLA (Programmable Logic Array). The processor 701 may also include a main processor and a coprocessor. The main processor is a processor for processing data in an awake state, also known as a CPU (Central Processing Unit); the coprocessor is a low-power processor for processing data in a standby state. In some embodiments, the processor 701 may be integrated with a GPU (Graphics Processing Unit), which is responsible for rendering and drawing the content to be displayed on the display screen. In some embodiments, the processor 701 may also include an AI (Artificial Intelligence) processor, which is used to process computing operations related to machine learning.
[0123] The memory 702 may include one or more computer-readable storage media, which may be non-transitory. The memory 702 may also include a high-speed random access memory, and a non-volatile memory, such as one or more disk storage devices, flash memory storage devices. In some embodiments, the non-transitory computer-readable storage medium in the memory 702 is used to store at least one instruction, which is used to be executed by the processor 701 to implement the stack backtracking method provided in the method embodiment of the present application.
[0124] In some embodiments, the terminal may further optionally include: a peripheral device interface 703 and at least one peripheral device. The processor 701, the memory 702 and the peripheral device interface 703 may be connected via a bus or a signal line. Each peripheral device may be connected to the peripheral device interface 703 via a bus, a signal line or a circuit board. Specifically, the peripheral device includes: at least one of a radio frequency circuit 704, a display screen 705, a camera assembly 706, an audio circuit 707 and a power supply 708.
[0125] The peripheral device interface 703 may be used to connect at least one peripheral device related to I / O (Input / Output) to the processor 701 and the memory 702. In some embodiments, the processor 701, the memory 702, and the peripheral device interface 703 are integrated on the same chip or circuit board; in some other embodiments, any one or two of the processor 701, the memory 702, and the peripheral device interface 703 may be implemented on a separate chip or circuit board, which is not limited in this embodiment.
[0126] The radio frequency circuit 704 is used to receive and transmit RF (Radio Frequency) signals, also known as electromagnetic signals. The radio frequency circuit 704 communicates with communication networks and other communication devices through electromagnetic signals. The radio frequency circuit 704 converts electrical signals into electromagnetic signals for transmission, or converts received electromagnetic signals into electrical signals. Optionally, the radio frequency circuit 704 includes: an antenna system, an RF transceiver, one or more amplifiers, a tuner, an oscillator, a digital signal processor, a codec chipset, a user identity module card, and the like. The radio frequency circuit 704 can communicate with other terminals through at least one wireless communication protocol. The wireless communication protocol includes, but is not limited to: a metropolitan area network, various generations of mobile communication networks (2G, 3G, 4G and 5G), a wireless local area network and / or a wireless fidelity (Wireless Fidelity, WiFi) network. In some embodiments, the radio frequency circuit 704 may also include circuits related to NFC (Near Field Communication, short-range wireless communication), which is not limited in this application.
[0127] The display screen 705 is used to display the UI (User Interface). The UI may include graphics, text, icons, videos and any combination thereof. When the display screen 705 is a touch display screen, the display screen 705 also has the ability to collect touch signals on the surface or above the surface of the display screen 705. The touch signal can be input to the processor 701 as a control signal for processing. At this time, the display screen 705 can also be used to provide virtual buttons and / or virtual keyboards, also known as soft buttons and / or soft keyboards. In some embodiments, the display screen 705 can be one, set on the front panel of the terminal; in other embodiments, the display screen 705 can be at least two, respectively set on different surfaces of the terminal or in a folding design; in some other embodiments, the display screen 705 can be a flexible display screen, set on the curved surface or folding surface of the terminal. Even, the display screen 705 can also be set to a non-rectangular irregular shape, that is, a special-shaped screen. The display screen 705 can be made of materials such as LCD (Liquid Crystal Display), OLED (Organic Light-Emitting Diode) and the like.
[0128] The camera assembly 706 is used to capture images or videos. Optionally, the camera assembly 706 includes a front camera and a rear camera. Typically, the front camera is arranged on the front panel of the terminal, and the rear camera is arranged on the back of the terminal. In some embodiments, there are at least two rear cameras, which are any one of a main camera, a depth of field camera, a wide-angle camera, and a telephoto camera, so as to realize the fusion of the main camera and the depth of field camera to realize the background blur function, the fusion of the main camera and the wide-angle camera to realize panoramic shooting and VR (Virtual Reality) shooting function or other fusion shooting functions. In some embodiments, the camera assembly 706 may also include a flash. The flash can be a monochrome temperature flash or a dual-color temperature flash. A dual-color temperature flash refers to a combination of a warm light flash and a cold light flash, which can be used for light compensation at different color temperatures.
[0129] The audio circuit 707 may include a microphone and a speaker. The microphone is used to collect sound waves from the user and the environment, and convert the sound waves into electrical signals and input them into the processor 701 for processing, or input them into the radio frequency circuit 704 to achieve voice communication. For the purpose of stereo acquisition or noise reduction, there may be multiple microphones, which are respectively arranged at different parts of the terminal. The microphone may also be an array microphone or an omnidirectional acquisition microphone. The speaker is used to convert the electrical signal from the processor 701 or the radio frequency circuit 704 into sound waves. The speaker may be a traditional film speaker or a piezoelectric ceramic speaker. When the speaker is a piezoelectric ceramic speaker, it can not only convert the electrical signal into sound waves audible to humans, but also convert the electrical signal into sound waves inaudible to humans for purposes such as ranging. In some embodiments, the audio circuit 707 may also include a headphone jack.
[0130] The power supply 708 is used to power various components in the terminal. The power supply 708 can be an alternating current, a direct current, a disposable battery, or a rechargeable battery. When the power supply 708 includes a rechargeable battery, the rechargeable battery can support wired charging or wireless charging. The rechargeable battery can also be used to support fast charging technology.
[0131] In some embodiments, the terminal further includes one or more sensors 709 , including but not limited to: an acceleration sensor 710 , a gyroscope sensor 711 , a pressure sensor 712 , an optical sensor 713 , and a proximity sensor 714 .
[0132] The acceleration sensor 710 can detect the magnitude of acceleration on the three coordinate axes of the coordinate system established by the terminal. For example, the acceleration sensor 710 can be used to detect the components of gravity acceleration on the three coordinate axes. The processor 701 can control the display screen 705 to display the user interface in a horizontal view or a vertical view according to the gravity acceleration signal collected by the acceleration sensor 710. The acceleration sensor 710 can also be used for collecting game or user motion data.
[0133] The gyroscope sensor 711 can detect the body direction and rotation angle of the terminal, and the gyroscope sensor 711 can cooperate with the acceleration sensor 710 to collect the user's 3D actions on the terminal. The processor 701 can implement the following functions based on the data collected by the gyroscope sensor 711: motion sensing (such as changing the UI according to the user's tilt operation), image stabilization during shooting, game control, and inertial navigation.
[0134] The pressure sensor 712 can be set in the side frame of the terminal and / or the lower layer of the display screen 705. When the pressure sensor 712 is set in the side frame of the terminal, it can detect the user's holding signal of the terminal, and the processor 701 performs left and right hand recognition or shortcut operation according to the holding signal collected by the pressure sensor 712. When the pressure sensor 712 is set in the lower layer of the display screen 705, the processor 701 controls the operability controls on the UI interface according to the user's pressure operation on the display screen 705. The operability controls include at least one of a button control, a scroll bar control, an icon control, and a menu control.
[0135] The optical sensor 713 is used to collect the ambient light intensity. In one embodiment, the processor 701 can control the display brightness of the display screen 705 according to the ambient light intensity collected by the optical sensor 713. Specifically, when the ambient light intensity is high, the display brightness of the display screen 705 is increased; when the ambient light intensity is low, the display brightness of the display screen 705 is reduced. In another embodiment, the processor 701 can also dynamically adjust the shooting parameters of the camera assembly 706 according to the ambient light intensity collected by the optical sensor 713.
[0136] The proximity sensor 714, also known as a distance sensor, is usually arranged on the front panel of the terminal. The proximity sensor 714 is used to collect the distance between the user and the front of the terminal. In one embodiment, when the proximity sensor 714 detects that the distance between the user and the front of the terminal is gradually decreasing, the processor 701 controls the display screen 705 to switch from the screen-on state to the screen-off state; when the proximity sensor 714 detects that the distance between the user and the front of the terminal is gradually increasing, the processor 701 controls the display screen 705 to switch from the screen-off state to the screen-on state.
[0137] Those skilled in the art will understand that Figure 5 The structure shown in the figure does not constitute a limitation on the computer device, and may include more or less components than shown in the figure, or combine some components, or adopt a different component arrangement.
[0138] In an exemplary embodiment, a computer device is also provided, the computer device comprising a processor and a memory, wherein at least one program code is stored in the memory. The at least one program code is loaded and executed by one or more processors, so that the computer device implements any of the above stack backtracking methods.
[0139] In an exemplary embodiment, a computer-readable storage medium is further provided, in which at least one program code is stored. The at least one program code is loaded and executed by a processor of a computer device to enable the computer to implement any of the above stack backtracing methods.
[0140] Optionally, the computer-readable storage medium may be a read-only memory (ROM), a random access memory (RAM), a compact disc (CD-ROM), a magnetic tape, a floppy disk, an optical data storage device, etc.
[0141] In an exemplary embodiment, a computer program product or a computer program is also provided, the computer program product or the computer program comprising computer instructions, the computer instructions being stored in a computer-readable storage medium. A processor of a computer device reads the computer instructions from the computer-readable storage medium, and the processor executes the computer instructions, so that the computer device performs any of the above stack backtracking methods.
[0142] The terms "first", "second", "third" and "fourth" etc. in the specification and claims of the present application and the drawings are used to distinguish different objects, rather than to describe a specific order. In addition, the terms "including" and "having" and any variations thereof are intended to cover non-exclusive inclusions. For example, a process, method, system, product or device that includes a series of steps or units is not limited to the listed steps or units, but optionally includes steps or units that are not listed, or optionally includes other steps or units inherent to these processes, methods, products or devices.
[0143] The above description is only an optional embodiment of the present application and is not intended to limit the present application. Any modifications, equivalent substitutions, improvements, etc. made within the principles of the present application shall be included in the protection scope of the present application.
Claims
1. A stack backtracking method, characterized in that: The method comprises: During the execution of the program, an interrupt index array is obtained, wherein the interrupt index array includes an interrupt address of an executed interrupt processing function, wherein the interrupt processing function is a function called when an interrupt occurs in the program, and the interrupt address indicates an address of a next instruction of the instruction in which the interrupt occurs; When the executed program meets the stack backtrace condition, obtaining a function return address array, wherein the function return address array includes a return address of the executed program function, wherein the program function is a function called by an instruction of the program during normal execution; Stack backtrace information is obtained based on the interrupt index array and the function return address array, wherein the stack backtrace information includes the calling relationship between program functions during the program execution process and the location of the program function where the interrupt occurs.
2. The method according to claim 1, characterized in that The method further comprises: In the process of executing the program, an interrupt function address array is obtained, wherein the interrupt function address array includes an entry address of an executed interrupt processing function; The acquiring stack backtrace information based on the interrupt index array and the function return address array includes: The stack backtrace information is obtained based on the interrupt index array, the function return address array and the interrupt function address array. The stack backtrace information also includes the calling relationship between the interrupted program function and the interrupt handling function.
3. The method according to claim 2, characterized in that The context of the return addresses in the function return address array indicates the calling relationship between the program functions; the acquiring of the stack backtrace information based on the interrupt index array, the function return address array and the interrupt function address array comprises: Arrange the interrupt addresses in the interrupt index array into the function return address array to obtain a sorted address array, wherein the arrangement positions of the interrupt addresses belonging to the interrupt index array in the sorted address array indicate the positions of the program functions where the interrupt occurs; Replacing the interrupt address in the sorted address array that belongs to the interrupt index array with the entry address in the interrupt function address array to obtain a replaced address array, wherein the chronological relationship between the return address and the entry address in the replaced address array indicates the calling relationship between the interrupted program function and the interrupt handling function; The replaced address array is used as the stack backtrace information.
4. The method according to any one of claims 1 to 3, characterized in that: The acquisition function returns an address array, including: Determine a function type of a first function in which the executed program is located, and obtain a return address of the first function according to the function type of the first function; Saving the return address of the first function into the function return address array; When the termination condition is reached, the function return address array is obtained.
5. The method according to claim 4, characterized in that After the return address of the first function is saved in the function return address array, the method further includes: If the termination condition is not met, determining a function type of a second function that calls the first function, and obtaining a return address of the second function according to the function type of the second function; Saving the return address of the second function into the function return address array; When the termination condition is reached, the function return address array is obtained.
6. The method according to claim 4, characterized in that The determining the function type of the first function of the executed program includes: When the number of interrupt occurrences is greater than a reference value, determining that the function type of the first function is an interrupt processing function, wherein the number of interrupt occurrences is increased by a first value when entering the execution of the interrupt processing function, and is decreased by the first value when exiting the execution of the interrupt processing function; When the number of interrupt occurrences is equal to the reference value, if the return address obtained based on the frame pointer of the program is inconsistent with the return address in the return address register of the program, the function type of the first function is determined to be a leaf function; if the return address obtained based on the frame pointer is consistent with the return address in the return address register, the function type of the first function is determined to be a non-leaf function.
7. The method according to claim 4, characterized in that The acquiring the return address of the first function according to the function type of the first function includes: When the function type of the first function is an interrupt handling function or a non-leaf function, the return address obtained based on the frame pointer of the program is used as the return address of the first function, and the frame pointer points to the stack frame base address of the first function.
8. The method according to claim 7, characterized in that After the return address of the first function is saved in the function return address array, the method further includes: Based on the interrupt handling function or the non-leaf function, the value of the stack pointer corresponding to the frame pointer is obtained, and the value of the stack pointer is assigned to the frame pointer so that the assigned frame pointer points to the stack frame base address of the second function that calls the first function. The assigned frame pointer is used to determine the function type of the second function.
9. The method according to claim 4, characterized in that The acquiring the return address of the first function according to the function type of the first function includes: In the case that the function type of the first function is a leaf function, the value of the stack pointer corresponding to the frame pointer is obtained based on the leaf function, and the value of the stack pointer is assigned to the frame pointer so that the assigned frame pointer points to the stack frame base address of the non-leaf function that calls the first function; the return address in the return address register of the program and the return address obtained based on the assigned frame pointer are used as the return address of the first function.
10. The method according to claim 9, characterized in that After the return address of the first function is saved in the function return address array, the method further includes: Based on the non-leaf function, the value of the stack pointer corresponding to the assigned frame pointer is obtained, and the value of the stack pointer is assigned to the assigned frame pointer, so that the re-assigned frame pointer points to the stack frame base address of the second function that calls the non-leaf function, and the re-assigned frame pointer is used to determine the function type of the second function.
11. The method according to claim 4, characterized in that The termination condition is to satisfy any one of the following three conditions: Condition 1: The value of the return address saved in the function return address array is 0; Condition 2: The value of the stack pointer obtained based on the frame pointer is greater than or equal to the value at the top of the program stack; Condition three: the number of stack backtraces is greater than the stack backtraces depth, the stack backtraces depth indicates the maximum allowed number of stack backtraces, wherein the number of stack backtraces is increased by a second value when the function return address array stores one return address.
12. A stack backtracking device, characterized in that: The device comprises: A first acquisition module is used to acquire an interrupt index array during the execution of a program, wherein the interrupt index array includes an interrupt address of an executed interrupt processing function, wherein the interrupt processing function is a function called when an interrupt occurs in the program, and the interrupt address indicates an address of a next instruction of the instruction in which the interrupt occurs; A second acquisition module is used to acquire a function return address array when the executed program meets the stack backtrace condition, wherein the function return address array includes the return address of the executed program function, and the program function is a function called by an instruction of the program during normal execution; The third acquisition module is used to acquire stack backtrace information based on the interrupt index array and the function return address array, wherein the stack backtrace information includes the calling relationship between program functions during the program execution process and the location of the program function where the interrupt occurs.
13. A computer device, characterized in that: The computer device includes a processor and a memory, wherein the memory stores at least one computer program or instruction, and the at least one computer program or instruction is loaded and executed by the processor so that the computer device implements the stack backtracing method according to any one of claims 1 to 11.
14. A computer-readable storage medium, characterized in that: At least one program code is stored in the computer-readable storage medium, and the at least one program code is loaded and executed by the processor to enable the computer to implement the stack backtracking method according to any one of claims 1 to 11.
15. A computer program product, characterized in that The computer program product comprises: a computer program code, wherein the computer program code is loaded and executed by a computer so as to enable the computer to implement the stack backtracking method according to any one of claims 1 to 11.
