Call Stack Backtracking Method, Device, and Computer Equipment for Embedded Systems

The method uses CPU registers to trace the call stack in embedded MCU systems, addressing inefficiencies in existing methods by accurately reconstructing the call hierarchy during crashes, aiding developers in rapid issue resolution.

CN113742119BActive Publication Date: 2025-07-15SHANGHAI WINGTECH INFORMATION TECH CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202110845087.2
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-07-26
Publication Date
2025-07-15
Estimated Expiration
2041-07-26

AI Technical Summary

Technical Problem

The existing technology cannot quickly and accurately trace the function call relationship of embedded MCU software systems, resulting in low efficiency in troubleshooting downtime problems.

Method used

By obtaining the program source code file, specifying compilation commands, and downtime exception handlers, analyzing the function stack frames using FP registers and LR registers, generating the target file in ELF format, and using the address parsing tool to parse the call stack traceback information.

Benefits of technology

It realizes efficient and accurate backtracking of the call relationship of the embedded system, provides on-site information, and improves software development and testing efficiency.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN113742119B_ABST
    Figure CN113742119B_ABST
Patent Text Reader

Abstract

The present application relates to the field of computers, and provides a method, apparatus, and computer device for call stack backtracking of an embedded system. The method includes: compiling and linking a program source code file based on a specified compilation command and a crash exception handler to generate an object file in ELF format; wherein the specified compilation command is used to record the starting address of the function stack frame; when an exception occurs in the system, saving the register information and the function stack frame corresponding to the object file, and executing the crash exception handler; the register information includes: the FP register and the LR register; analyzing the function stack frame based on the FP register and the LR register through the crash exception handler to obtain a call address table of the target function where the exception occurs; using a preset address parsing tool to parse the call stack backtracking information of the addresses in the call address table in the object file. By using this method, the complete call relationship can be backtracked more efficiently and accurately.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of computer technology, and in particular, to a method, apparatus, and computer device for call stack backtracking in an embedded system. Background Art

[0002] During the software development, debugging, and testing of an embedded MCU (Microcontroller Unit), various crashing phenomena often occur due to design defects in the software. For the crashing problem of the embedded MCU software system, currently, it is mainly based on the content of the CPU (Central Processing Unit) core registers and combined with the disassembly code to troubleshoot and fix the design defects of the code. However, this method has relatively high requirements for developers, and developers need to have a relatively in-depth understanding and comprehension of the CPU architecture, registers, operation and exception mechanisms, and the compilation principle of the program. More importantly, this method cannot backtrack and deduce the complete execution call relationship upward from the position of the crashing function, so it cannot effectively and timely troubleshoot and recover the system, which has a relatively large impact on the software development and testing efficiency.

[0003] Therefore, how to quickly backtrack the call relationship of functions has become an urgent problem to be solved. Summary of the Invention

[0004] Based on this, in view of the above technical problems, it is necessary to provide a method, apparatus, and computer device for call stack backtracking in an embedded system that can efficiently and accurately backtrack the complete call relationship.

[0005] An embodiment of this application provides a method for call stack backtracking in an embedded system, and the method includes:

[0006] Obtain a program source code file, a specified compilation command, and a crashing exception handling program; wherein, the specified compilation command is used to record the starting address of the function stack frame;

[0007] Based on the specified compilation command and the crashing exception handling program, compile and link the program source code file to generate an object file in ELF format;

[0008] When the system encounters an exception, save the register information and the function stack frame corresponding to the object file, and execute the crashing exception handling program; wherein, the register information includes: an FP register for recording the starting address of the stack frame of the current function, and an LR register for recording the return address of the upper-level function that calls the current function;

[0009] Based on the FP register and the LR register, the crash exception handler analyzes the function stack frame to obtain a call address table of the target function where the exception occurs;

[0010] Using a preset address resolution tool, the call stack backtrace information of the address in the call address table in the target file is resolved.

[0011] In one embodiment, the compiling and linking of the program source code file based on the specified compilation command and the crash exception handler to generate an ELF format target file includes:

[0012] Compiling the program source code file using the specified compilation command to obtain a compilation result file; injecting the crash exception handler into the compilation result file to generate an ELF format target file.

[0013] In one embodiment, the analyzing of the function stack frame based on the FP register and the LR register by the crash exception handler to obtain a call address table of the target function where the exception occurs includes:

[0014] The crash exception handler obtains the FP register and the LR register from the CPU registers corresponding to the function stack frame; based on the LR register and the FP register, the call address table of the target function where the exception occurs is traced back.

[0015] In one embodiment, the tracing back based on the LR register and the FP register to obtain a call address table of the target function where the exception occurs includes:

[0016] Taking the target function where the exception occurs as the current function, and repeatedly executing the following backtracking derivation algorithm until the call address table of the target function is traced back: obtaining the starting address of the current function from the FP register corresponding to the stack frame of the current function; obtaining the return address from the LR register corresponding to the stack frame of the current function; obtaining the upper-level function that calls the current function based on the starting address, and taking the upper-level function as the new current function;

[0017] When the LR register corresponding to the stack frame of the new current function is a null value, stop the backtracking derivation algorithm, and generate a call address table based on all the starting addresses and return addresses obtained when stopping the backtracking derivation algorithm.

[0018] In one embodiment, the register information further includes: a PC register for recording the instruction address where the exception occurs, and the method further includes: determining the address of the target function where the exception occurs according to the PC register.

[0019] In one embodiment, executing the crash exception handler includes: a central processing unit (CPU) enters and executes the crash exception handler through a preset exception vector function table.

[0020] In one embodiment, the call stack backtrace information includes: the function name of the target function, the address of the target function, the addresses of the calling functions at all levels, the files to which the target function and the calling functions belong respectively, and the line numbers of the target function and the calling functions in their respective files; wherein, the calling function is a function that has a call stack relationship with the target function.

[0021] An embodiment of the present application provides a call stack backtrace device for an embedded system, and the device includes:

[0022] An acquisition module, configured to acquire a program source code file, a specified compilation command, and a crash exception handler; wherein, the specified compilation command is used to record the starting address of the function stack frame;

[0023] A file generation module, configured to compile and link the program source code file based on the specified compilation command and the crash exception handler to generate an executable and linkable object file in ELF format;

[0024] A program execution module, configured to save register information and the function stack frame corresponding to the object file when an exception occurs in the system, and execute the crash exception handler; wherein, the register information includes: an FP register for recording the starting address of the stack frame of the current function, and an LR register for recording the return address of the upper-level function that calls the current function;

[0025] An exception analysis module, configured to analyze the function stack frame through the crash exception handler based on the FP register and the LR register to obtain a call address table of the target function where the exception occurs;

[0026] A parsing module, configured to use a preset address parsing tool to parse the call stack backtrace information of the addresses in the call address table in the object file.

[0027] An embodiment of the present application provides a computer device, including a memory and a processor, where the memory stores a computer program, and when the processor executes the computer program, the steps of the call stack backtrace method for an embedded system provided in any embodiment of the present application are implemented.

[0028] An embodiment of the present application provides a computer-readable storage medium, on which a computer program is stored, and when the computer program is executed by a processor, the steps of the call stack backtrace method for an embedded system provided in any embodiment of the present application are implemented.

[0029] The call stack backtracking method, device, and computer device of the embedded system provided by the embodiments of the present application

[0030] First, compile and link the program source code file based on the obtained specified compilation command and crash exception handler. Among them, the specified compilation command enables the generated target file to record the starting address of the function stack frame during runtime, and the crash exception handler can quickly enter the call stack backtracking process when a system exception occurs, that is: analyze the function stack frame based on the FP register and LR register to obtain the call address table of the target function where the exception occurs; this technical solution uses the FP register and LR register of the CPU to analyze the call address table from the function stack frame, and can more efficiently and accurately backtrack the call address; further, use a preset address parsing tool to parse the complete call stack backtracking information of the address in the call address table in the target file, providing on-site information for problem analysis and search for MCU software developers, which is beneficial to helping developers quickly find and solve problems and improve software development and testing efficiency. Description of the Drawings

[0031] Figure 1 It is a schematic flowchart of the call stack backtracking method of the embedded system in one embodiment;

[0032] Figure 2 It is an application scenario diagram of the call stack backtracking method in one embodiment;

[0033] Figure 3 It is a schematic diagram showing the call stack backtracking information in one embodiment;

[0034] Figure 4 It is a structural block diagram of the call stack backtracking device of the embedded system in one embodiment;

[0035] Figure 5 It is an internal structure diagram of a computer device in one embodiment. Detailed Embodiments

[0036] In order to make the objectives, technical solutions, and advantages of the present application clearer, the present application will be further described in detail below with reference to the drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present application and are not used to limit the present application.

[0037] Regarding the crash problem of the embedded MCU software system, considering that the current exception handling method cannot locate the position where the CPU crashes and cannot accurately backtrack the complete execution call relationship. Based on this, this embodiment provides a call stack backtracking method, device, and computer device for the embedded system, which can efficiently and accurately backtrack the complete call relationship. For ease of understanding, the embodiments of the present application are introduced in detail below.

[0038] In one embodiment, as Figure 1 shown, a method for call stack backtrace of an embedded system is provided. This method can be applied to the scenario of call stack backtrace when the system encounters an abnormal crash. This method can be executed by a call stack backtrace device of the embedded system, and this device can be implemented by software and / or hardware, and is generally integrated in an electronic device. Exemplarily, an example electronic device for implementing the call stack backtrace method of the embedded system according to the embodiments of the present application can be implemented on intelligent terminals such as tablet computers and computers. As Figure 1 shown, the method in this embodiment includes the following steps:

[0039] Step S102, obtain the program source code file, the specified compilation command, and the crash exception handler. Among them, the specified compilation command can use the CPU register R11 as the frame pointer to record the starting address of the function stack frame.

[0040] Step S104, compile and link the program source code file based on the specified compilation command and the crash exception handler to generate an object file in ELF (Executable and Linkable Format) format.

[0041] In this embodiment, first, the program source code file is compiled using the specified compilation command to obtain a compilation result file. When the specified compilation command is added, that is, when the FP register is enabled to record the starting address of the current function stack frame, during the execution of the program source code file, the stack frame pointer of the current function can be obtained through the stack frame defined by the FP register, and thus the stack frame of the current function can be obtained. The above-mentioned specified compilation command can be, for example, in practical applications: the ARMCC compilation command --use_frame_pointer, and the GCC compilation command -fno-omit-frame-pointer.

[0042] Next, the crash exception handler is injected into the compilation result file to generate an object file in ELF format. Specifically, the target file can be output by linking the crash exception handler in the compilation result file through the link library. This crash exception handler can execute a stack backtrace derivation algorithm to determine the call relationship of the target function where the exception occurs.

[0043] Step S106, when the system encounters an exception, save the register information and the function stack frame corresponding to the object file, and execute the crash exception handler; among them, the register information includes: the FP register (i.e., register R11) for recording the starting address of the stack frame of the current function, and the LR register (i.e., register R14) for recording the return address of the upper-level function that calls the current function.

[0044] In this embodiment, the system generally refers to an embedded software system. During the actual operation of the system, when an abnormal system crash occurs due to a design defect in the software logic, the register information and the function stack frame corresponding to the target file can be saved. Among them, the function stack frame is used to save various information during the function call process, such as function parameters, return addresses, etc.; the register information can also include: the PC register (i.e., register R15) for recording the instruction address where the exception occurs, the SP register (i.e., register R13) for recording the bottom address of the current function's stack frame, etc., which will not be listed one by one here.

[0045] When the system sends an exception, it also includes that the CPU enters and executes the system crash exception handling program through a preset exception vector function table, and then executes the following step S108.

[0046] Step S108: Analyze the function stack frame based on the FP register and the LR register through the system crash exception handling program to obtain the call address table of the target function where the exception occurs.

[0047] In this embodiment, through the FP register, the value of the LR register (abbreviation: LR value) stored in the target function stack frame can be found. The LR value represents the return address of the target function; at the same time, the value of the FP register of the upper-level function stored in the target function stack frame (abbreviation: FP value) can also be found. This FP value points to the bottom of the upper-level function's stack. In this way, the values of the LR register and the LR register stored in the upper-level function stack frame can be found in the same way. Based on this, in this embodiment, with the FP register as the core, the LR value and the LR value stored in each function stack frame are determined in turn, the return address of the target function and the bottom address of the upper-level function's stack are calculated, so as to find the call address table of each level of function, thereby constituting a complete call stack backtracking process.

[0048] Step S110: Use a preset address parsing tool to parse the call stack backtracking information of the addresses in the call address table in the target file.

[0049] Among them, the address parsing tool can be, for example, addr2line.exe. Using this address parsing tool addr2line.exe, the corresponding relationship between the addresses in the call address table and the target file is parsed to obtain complete and accurate call stack backtracking information. The call stack backtracking information can include, but is not limited to: the function name of the target function, the address of the target function, the addresses of each level of caller functions, the files to which the target function and the caller functions belong, and the line numbers of the target function and the caller functions in their respective files; among them, the caller function is a function that has a call stack relationship with the target function.

[0050] The call stack backtracking method for an embedded system provided by an embodiment of the present application first compiles and links a program source code file based on the obtained specified compilation command and crash exception handler. Among them, the specified compilation command enables the generated target file to record the starting address of the function stack frame during runtime, and the crash exception handler can quickly enter the call stack backtracking process when a system exception occurs, that is: analyze the function stack frame based on the FP register and LR register to obtain the call address table of the target function where the exception occurs; this technical solution uses the FP register and LR register of the CPU to analyze and obtain the call address table from the function stack frame, and can more efficiently, accurately, and completely backtrack the call address; further, use a preset address parsing tool to parse the call stack backtracking information of the address in the call address table in the target file, providing on-site information for MCU software developers to analyze and find problems, which is beneficial to helping developers quickly find and solve problems and improve software development and testing efficiency.

[0051] Another call stack backtracking method for an embedded system provided by an embodiment of the present application includes:

[0052] Step 1, compile and link the program source code file based on the previously obtained specified compilation command and crash exception handler to generate a target file in ELF format.

[0053] Step 2, when a system exception occurs, save the register information and the function stack frame corresponding to the target file, and execute the crash exception handler; the register information at least includes: PC register, FP register, and LR register.

[0054] Step 3, obtain the above PC register, FP register, and LR register from the CPU registers corresponding to the function stack frame through the crash exception handler.

[0055] Step 4, determine the address of the target function where the exception occurs according to the PC register.

[0056] Step 5, backtrack the call address table of the target function where the exception occurs according to the LR register and FP register. The specific implementation manner of this embodiment can be referred to as follows.

[0057] Take the target function where the exception occurs as the current function, and repeatedly execute the backtracking derivation algorithm shown in (1)-(3) below until the call address table of the target function is backtracked:

[0058] (1) Obtain the starting address of the current function from the FP register corresponding to the stack frame of the current function. It can be understood that when the backtracking derivation algorithm is first executed, the current function is the target function where the exception occurs.

[0059] (2) Obtain the return address from the LR register corresponding to the stack frame of the current function.

[0060] (3) Obtain the upper-level function that called the current function based on the starting address, and use the upper-level function as the new current function.

[0061] Repeat the above steps (1)-(3) until the LR register corresponding to the stack frame of the new current function is a null value, indicating that the current function has no upper-level function. In this case, the backtracking derivation algorithm can be stopped, and a call address table can be generated based on all the starting addresses and return addresses obtained when the backtracking derivation algorithm stops. The call address table can record the function stack call relationship related to the target function, as well as the starting addresses and return addresses of each level of function involved in the function stack call relationship. Of course, information such as the function name of each level of function and the bottom address of the function stack frame can also be further recorded.

[0062] Step six, use a preset address parsing tool to parse the call stack backtracking information of the addresses in the call address table in the target file.

[0063] To better understand the above steps five and six, as Figure 2 shown, here is an example of an application scenario of system abnormal shutdown. In this example, assume that when the CPU executes to the 97th line of the function func3(), a crash shutdown exception occurs; in this case, the CPU hardware will automatically push the registers R0 - R3, R12, R14, R15, etc. onto the stack. The PC value recorded in the register R15 can point to the instruction address where the shutdown exception occurs. Based on this address, the target function where the exception occurs can be determined, that is, Figure 2 the function func3() in

[0064] The LR value in the abnormal stack push points to the return address of the calling function of the function func3(), that is, the instruction address at the 59th line in func2(). Since the subroutine call instruction (BL or BLX) of the ARM architecture is 4 bytes, the instruction address of the call site can be obtained by subtracting 4 from the LR value: the 58th line of the function func2().

[0065] Based on the FP value of the CPU register, the starting address of the stack frame of the function func3() can be located, and then the starting position of the stack frame of the upper-level calling function func2() can be read. Through the LR value of the function stack frame here, the return address of the calling function func1() of the function func2() can be obtained, that is, the 19th line of the func1() function. Subtracting 4 from this LR value can obtain the instruction address of the call site: the 18th line of the function func1().

[0066] The return address of the calling function at the upper level is obtained through the LR value in the function stack frame, and the stack frame position of the calling function at the upper level is obtained through the FP value. Thus, a chain structure from bottom to top can be formed. The complete call stack relationship is recorded by this chain structure. By traversing and backtracking the chain structure, a complete set of call address tables can be deduced.

[0067] Through the above set of call address tables, using the upper computer tool addr2line and combining with the target file in ELF format, the final parsed output is as Figure 3 shown complete call stack backtrace information.

[0068] In summary, the call stack backtracking method for the embedded system provided by the embodiment of the present application uses the PC value to locate the address of the target function where the crash occurs, and uses the FP value and LR value to backtrack the call address table corresponding to the target function, which can efficiently and accurately implement crash location and backtrack the call address table; further combine the address parsing tool to parse out the complete call stack backtrace information, so as to provide on-site information for problem analysis and search for MCU software developers, which is helpful to help developers quickly find and solve problems and improve software development and testing efficiency.

[0069] It should be understood that although Figure 1 the steps in the flowchart of Figure 1 are shown in sequence according to the indication of the arrows, these steps do not necessarily execute in the order indicated by the arrows. Unless otherwise clearly stated in this article, the execution of these steps has no strict order limit, and these steps can be executed in other orders. Moreover, Figure 1 at least a part of the steps in

[0070] In one embodiment, as Figure 4 shown, a call stack backtracking device for an embedded system is provided, including: an acquisition module 402, a file generation module 404, a program execution module 406, an exception analysis module 408, and a parsing module 410, where:

[0071] The acquisition module 402 is used to acquire the program source code file, the specified compilation command, and the crash exception handling program; wherein, the specified compilation command is used to record the starting address of the function stack frame;

[0072] The file generation module 404 is used to compile and link the program source code file based on the specified compilation command and the crash exception handling program to generate an executable and linkable target file in ELF format;

[0073] A program execution module 406, configured to save register information and the function stack frame corresponding to the target file and execute a crash exception handler when an exception occurs in the system; wherein, the register information includes: an FP register for recording the starting address of the stack frame of the current function, and an LR register for recording the return address of the upper-level function that calls the current function.

[0074] An exception analysis module 408, configured to analyze the function stack frame based on the FP register and the LR register through the crash exception handler to obtain a call address table of the target function where the exception occurs.

[0075] A parsing module 410, configured to use a preset address parsing tool to parse the call stack backtrace information of the addresses in the call address table in the target file.

[0076] In one embodiment, the above file generation module 404 is specifically configured to:

[0077] Compile the program source code file using a specified compilation command to obtain a compilation result file; inject the crash exception handler into the compilation result file to generate a target file in ELF format.

[0078] In one embodiment, the above exception analysis module 408 includes:

[0079] An acquisition unit, configured to obtain the FP register and the LR register from the CPU registers corresponding to the function stack frame through the crash exception handler.

[0080] An analysis unit, configured to backtrack based on the LR register and the FP register to obtain a call address table of the target function where the exception occurs.

[0081] In one embodiment, the above analysis unit is specifically configured to:

[0082] Take the target function where the exception occurs as the current function, and repeatedly execute the following backtracking derivation algorithm until the call address table of the target function is obtained: obtain the starting address of the current function from the FP register corresponding to the stack frame of the current function; obtain the return address from the LR register corresponding to the stack frame of the current function; obtain the upper-level function that calls the current function based on the starting address, and take the upper-level function as the new current function.

[0083] When the LR register corresponding to the stack frame of the new current function is a null value, stop the backtracking derivation algorithm, and generate a call address table based on all the starting addresses and return addresses obtained when the backtracking derivation algorithm is stopped.

[0084] In one embodiment, the above register information further includes a PC register for recording the instruction address where an exception occurs. The above exception analysis module 408 further includes an address determination unit, and this address determination unit is configured to: determine the address of the target function where the exception occurs according to the PC register.

[0085] In one embodiment, the above program execution module 406 is specifically configured to: the CPU enters and executes the crash exception handling program through a preset exception vector function table.

[0086] In one embodiment, the above call stack backtrace information includes: the function name of the target function, the address of the target function, the addresses of the calling functions at all levels, the files to which the target function and the calling functions belong respectively, and the line numbers of the target function and the calling functions in their respective files; wherein, the calling function is a function having a call stack relationship with the target function.

[0087] The call stack backtrace device of the embedded system provided by the embodiments of the present application first compiles and links the program source code file based on the obtained specified compilation command and the crash exception handling program. Among them, the specified compilation command enables the generated target file to record the starting address of the function stack frame during runtime, and the crash exception handling program can quickly enter the call stack backtrace process when a system exception occurs, that is: analyze the function stack frame based on the FP register and the LR register to obtain the call address table of the target function where the exception occurs; this technical solution uses the FP register and the LR register of the CPU to analyze the call address table from the function stack frame, and can more efficiently and accurately backtrace the call address; further, use a preset address parsing tool to parse out the complete call stack backtrace information of the addresses in the call address table in the target file, providing on-site information for problem analysis and search for MCU software developers, which is beneficial to helping developers quickly find and solve problems, and improving software development and testing efficiency.

[0088] For the specific limitations of the call stack backtrace device of the embedded system, reference can be made to the limitations on the call stack backtrace method of the embedded system described above, and details will not be repeated here. Each module in the above call stack backtrace device of the embedded system can be implemented in whole or in part by software, hardware and their combination. The above modules can be embedded in the processor in the computer device in hardware form or be independent of it, or can be stored in the memory in the computer device in software form, so as to facilitate the processor to call and execute the operations corresponding to the above respective modules.

[0089] In one embodiment, a computer device is provided. This computer device can be a terminal, and its internal structure diagram can be as Figure 5As shown in the figure. The computer device includes a processor, a memory, a communication interface, a display screen, and an input device connected by a system bus. Among them, the processor of the computer device is used to provide computing and control capabilities. The memory of the computer device includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system and computer programs. The internal memory provides an environment for the operation of the operating system and computer programs in the non-volatile storage medium. The communication interface of the computer device is used to communicate with an external terminal in a wired or wireless manner, and the wireless manner can be implemented through WIFI, a carrier network, near field communication (NFC), or other technologies. When the computer program is executed by the processor, it implements a method for backtracking the call stack of an embedded system. The display screen of the computer device can be a liquid crystal display screen or an electronic ink display screen, and the input device of the computer device can be a touch layer covered on the display screen, or a button, a trackball, or a touchpad provided on the casing of the computer device, or an external keyboard, touchpad, or mouse, etc.

[0090] Those skilled in the art can understand that Figure 5 the structure shown in the figure is only a block diagram of some structures related to the solution of this application, and does not constitute a limitation on the computer device to which the solution of this application is applied. The specific computer device may include more or fewer components than those shown in the figure, or combine some components, or have a different component arrangement.

[0091] In one embodiment, the call stack backtracking device of the embedded system provided in this application can be implemented in the form of a computer program, and the computer program can run on a computer device as shown in Figure 5 the figure. Each program module constituting the call stack backtracking device of the embedded system can be stored in the memory of the computer device. For example, Figure 4 the acquisition module 402, the file generation module 404, the program execution module 406, the exception analysis module 408, and the parsing module 410 shown in the figure. The computer program composed of each program module enables the processor to execute the steps in the call stack backtracking method of the embedded system in each embodiment of this application described in this specification.

[0092] Those of ordinary skill in the art can understand that all or part of the processes in the methods of the above embodiments can be completed by instructing relevant hardware through a computer program. The computer program can be stored in a non-volatile computer-readable storage medium. When the computer program is executed, it can include the processes of the embodiments of the above various methods. Among them, any reference to a memory, database, or other medium used in the various embodiments provided in this application can include at least one of non-volatile and volatile memories. Non-volatile memory can include read-only memory (ROM), magnetic tape, floppy disk, flash memory, or optical memory, etc. Volatile memory can include random access memory (RAM) or an external cache memory. By way of illustration and not limitation, RAM is available in many forms, such as static random access memory (SRAM) and dynamic random access memory (DRAM), etc.

[0093] The technical features of the above embodiments can be combined arbitrarily. For the sake of concise description, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, it should be considered as the scope described in this specification.

[0094] The above-described embodiments merely represent several implementation manners of this application. The description is relatively specific and detailed, but it should not be construed as a limitation on the scope of the invention patent. It should be noted that for those of ordinary skill in the art, without departing from the concept of this application, several modifications and improvements can still be made, and these all belong to the protection scope of this application. Therefore, the protection scope of the patent of this application should be subject to the appended claims.

Claims

1. A call stack backtracking method for an embedded system, characterized in that, The method includes: Obtaining a program source code file, a specified compilation command, and a crash exception handler; wherein, the specified compilation command is used to record the starting address of the function stack frame; Compiling and linking the program source code file based on the specified compilation command and the crash exception handler to generate an object file in the executable and linkable ELF format; When an exception occurs in the system, saving the register information and the function stack frame corresponding to the object file, and executing the crash exception handler; wherein, the register information includes: an FP register for recording the starting address of the stack frame of the current function, and an LR register for recording the return address of the upper-level function that called the current function; Analyzing the function stack frame by the crash exception handler based on the FP register and the LR register to obtain a call address table of the target function where the exception occurred; Using a preset address resolution tool to resolve the call stack backtrace information of the addresses in the call address table in the object file; Among them, the analyzing the function stack frame by the crash exception handler based on the FP register and the LR register to obtain a call address table of the target function where the exception occurred includes: Taking the target function where the exception occurred as the current function, and repeatedly executing the following backtracking derivation algorithm until the call address table of the target function is obtained: Obtaining the starting address of the current function from the FP register corresponding to the stack frame of the current function; Obtaining the return address from the LR register corresponding to the stack frame of the current function; Obtaining the upper-level function that called the current function according to the starting address, and taking the upper-level function as the new current function; When the LR register corresponding to the stack frame of the new current function is a null value, stopping the backtracking derivation algorithm, and generating a call address table based on all the starting addresses and return addresses obtained when stopping the backtracking derivation algorithm.

2. The method according to claim 1, wherein The compiling and linking the program source code file based on the specified compilation command and the crash exception handler to generate an object file in the ELF format includes: Compiling the program source code file using the specified compilation command to obtain a compilation result file; Injecting the crash exception handler into the compilation result file to generate an object file in the ELF format.

3. The method according to claim 1, wherein The analyzing the function stack frame by the crash exception handler based on the FP register and the LR register to obtain a call address table of the target function where the exception occurred includes: Obtaining the FP register and the LR register from the CPU registers corresponding to the function stack frame by the crash exception handler; Backtracking to obtain a call address table of the target function where the exception occurred according to the LR register and the FP register.

4. The method according to claim 1, wherein The register information further includes: a PC register for recording the instruction address where the exception occurred, and the method further includes: Determining the address of the target function where the exception occurred according to the PC register.

5. The method according to claim 1, characterized in that, The executing the crash exception handler includes: The central processing unit (CPU) enters and executes the crash exception handler through a preset exception vector function table.

6. The method according to claim 1, wherein The call stack backtrace information includes: the function name of the target function, the address of the target function, the addresses of the calling functions at all levels, the files to which the target function and the calling functions belong respectively, and the line numbers of the target function and the calling functions in their respective files; wherein, the calling function is a function having a call stack relationship with the target function.

7. A call stack backtracking device for an embedded system, characterized in that, The device includes: An acquisition module, configured to acquire a program source code file, a specified compilation command, and a crash exception handler; wherein, the specified compilation command is used to record the starting address of the function stack frame. A file generation module, configured to compile and link the program source code file based on the specified compilation command and the crash exception handler to generate an executable and linkable ELF format object file. A program execution module, configured to save the register information and the function stack frame corresponding to the object file when an exception occurs in the system, and execute the crash exception handler; wherein, the register information includes: an FP register for recording the starting address of the stack frame of the current function, and an LR register for recording the return address of the upper-level function that calls the current function. An exception analysis module, configured to analyze the function stack frame through the crash exception handler based on the FP register and the LR register to obtain a call address table of the target function where the exception occurs. A parsing module, configured to use a preset address parsing tool to parse the call stack backtrace information of the addresses in the call address table in the object file. Wherein, the exception analysis module is further configured to: Take the target function where the exception occurs as the current function, and repeatedly execute the following backtracking derivation algorithm until the call address table of the target function is obtained: Obtain the starting address of the current function from the FP register corresponding to the stack frame of the current function. Obtain the return address from the LR register corresponding to the stack frame of the current function. Obtain the upper-level function that calls the current function according to the starting address, and take the upper-level function as the new current function. When the LR register corresponding to the stack frame of the new current function is a null value, stop the backtracking derivation algorithm, and generate a call address table based on all the starting addresses and return addresses obtained when the backtracking derivation algorithm is stopped.

8. A computer device, comprising a memory and a processor, the memory storing a computer program, characterized in that, When the processor executes the computer program, it implements the steps of the method according to any one of claims 1 to 6.

9. A computer-readable storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by a processor, it implements the steps of the method according to any one of claims 1 to 6.

Citation Information

Patent Citations

  • Error tracking method of embedded system and device thereof

    CN101539883A

  • Method, system and terminal equipment for outputting function execution streams

    CN102646051A