A stack backtrace method, device and storage medium based on ShenWei BIOS

By obtaining and parsing the loading module information and stack frame structure of Shenwei BIOS, the problem that the existing stack traceback method cannot obtain function names and offsets within the module in Shenwei BIOS is solved, and efficient function call information acquisition and debugging efficiency is achieved.

CN115756647BActive Publication Date: 2025-07-25WUXI ADVANCED TECH RES INST
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202211463133.3
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-11-22
Publication Date
2025-07-25
Estimated Expiration
2042-11-22

AI Technical Summary

Technical Problem

The existing stack traceback method cannot be applied to Shenwei BIOS, and the function name and module offset cannot be obtained under the UEFI framework. The existing method does not match the function call design of Shenwei processor, resulting in poor debugging results.

Method used

By obtaining the loading information of all loading modules in the current system, using the stack frame structure in the Shenwei architecture to extract the return address and frame pointer, combining instruction scanning and correctness detection, analyzing the loading module name and intra-module offset on the call path, and using local analysis tools to obtain complete stack trace information.

Benefits of technology

It realizes the acquisition of correct and complete function call information in Shenwei BIOS, with low coupling, high modularity and good portability, simplifies the debugging process and improves debugging efficiency.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN115756647B_ABST
    Figure CN115756647B_ABST
Patent Text Reader

Abstract

The present invention discloses a stack backtracking method, device and storage medium based on Shenwei BIOS, including: obtaining the loading information of all loaded modules in the current system; extracting the return address and frame pointer by means of instruction scanning; performing correctness detection on each of the return addresses and frame pointers; using the return address with a correct detection result as the calling function address, and using the frame pointer as the calling function stack pointer to continue backtracking until the backtracking ends when all return addresses on the call path are obtained; parsing all return addresses on the call path according to each of the loading information to obtain the name of the loaded module where each return address on the call path is located and the offset within the module; using a local analysis tool to parse the name of the loaded module where each return address on the call path is located and the offset within the module to obtain complete stack backtracking information. The present invention is completely implemented based on the official interfaces provided by the UEFI framework, and has the advantages of low coupling degree, high modularity and good portability, etc.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of embedded systems, and particularly relates to a stack backtracking method, device, and storage medium based on ShenWei BIOS. Background Art

[0002] ShenWei BIOS (Basic Input Output System) is a system firmware implemented based on the UEFI (Unified Extensible Firmware Interface) framework for the ShenWei processor platform, and is used for power-on self-test, connecting the operating system, and providing an interface for connecting the operating system and hardware; the ShenWei processor platform faced by ShenWei BIOS uses an independent instruction set and is a series of processors with complete intellectual property rights in China. However, there is currently no stack backtracking method applicable to ShenWei BIOS.

[0003] Currently, the stack backtracking method applied to other ShenWei software is mainly based on the stack frame structure. By parsing the stack frame of the current function, the upper-layer function that calls the current function is found and backtracked, and the hierarchical relationship of function calls is obtained by backtracking layer by layer; however, this method requires additional space to store the mapping structure of functions and return addresses, and is applicable to software or kernels that are stored on disk and the executable file itself can carry more data. As a system firmware, ShenWei BIOS needs to be burned into a Flash Memory with a small space. In order to load more drivers and provide more services as much as possible, the UEFI framework deletes the mapping table of function names and relative offsets of functions within the module when generating the executable file. This results in that if ShenWei BIOS uses the existing stack backtracking method, the corresponding function name cannot be found for the obtained return address, and the debugging effect is greatly reduced. Moreover, to ensure the extensibility and good integration characteristics of the system firmware, the UEFI framework of ShenWei BIOS is composed of modules as units, and each required function is divided into many sub-functions and implemented separately in the form of modules; since the modules are independent of each other and the loading addresses are not fixed, the global address obtained by using the above existing stack backtracking method cannot directly find the corresponding function in the compilation environment like other ShenWei software, and the debugging value is further reduced.

[0004] In addition, the stack backtracking method based on other processors mainly retains the information of ar registers and DRAM, and reversely restores the scene at intervals of slip when an exception occurs and calls an algorithm to reversely restore the stack call relationship. This method is contrary to the function call design of the ShenWei processor and cannot be used for stack backtracking of ShenWei BIOS. Therefore, there is an urgent need for a new method that can perform stack backtracking according to the architectural characteristics of the UEFI framework in ShenWei BIOS. Summary of the Invention

[0005] The object of the present invention is to overcome the deficiencies in the prior art, and provide a stack backtrace method, device and storage medium based on ShenWei BIOS, which can be fully implemented based on the official interfaces provided by the UEFI framework, and has technical effects such as low coupling degree, high modularity and good portability.

[0006] To achieve the above object, the present invention is implemented by the following technical solutions:

[0007] In the first aspect, the present invention provides a stack backtrace method based on ShenWei BIOS, the method includes:

[0008] Obtain the loading information of all loaded modules in the current system, and record the stack pointer and the loading addresses of the functions that call each loaded module;

[0009] Use the stack frame structure in the ShenWei architecture to backtrace the calling function, and extract the return address and frame pointer by means of instruction scanning;

[0010] Perform correctness detection on each of the extracted return addresses and frame pointers;

[0011] Use the return address with the correct detection result as the function address of the calling function, and use the frame pointer as the stack pointer of the calling function to continue backtracing until all return addresses on the call path are obtained and the backtracing ends;

[0012] Parse all the return addresses on the call path according to the respective loading information to obtain the name of the loaded module where each return address on the call path is located and the offset within the module;

[0013] Use a local analysis tool to parse the name of the loaded module where each return address on the call path is located and the offset within the module to obtain complete stack backtrace information.

[0014] Combined with the first aspect, preferably, the step of obtaining the loading information of all loaded modules in the current system includes:

[0015] Use the lookup protocol interface provided by the UEFI framework to traverse and read the content stored in the image protocol corresponding to each loaded module; the content stored in the image protocol includes the loading address, file size and module name.

[0016] Combined with the first aspect, preferably, the step of extracting the return address and frame pointer by means of instruction scanning includes:

[0017] Based on the regular encoding format of the fixed-length instructions of the ShenWei processor, use the instruction address of the current function to scan upward the previously executed instructions to find the instructions that store the frame pointer and return address when the calling function makes call preparations;

[0018] Calculate the offsets of the stored frame pointer and return address on the stack using the instruction encoding specified by the ShenWei architecture;

[0019] Use the offsets on the stack and the current stack pointer to extract the frame pointer and return address, and trace back to the stack space of the calling function.

[0020] Combined with the first aspect, preferably, the steps of performing correctness detection on each of the extracted return addresses and frame pointers include:

[0021] Detect the number of times the return address is extracted. If it is detected multiple times, it is determined that the stack frame is incorrect and the service exits;

[0022] Detect the range of the extracted stack pointer. If it is detected that the extracted stack pointer exceeds the set range, it is determined that the extraction fails or the stack top has been reached, and the service exits;

[0023] Detect the range of the extracted return address. If it is detected that the previous PC storage instruction of the currently extracted return address does not belong to a function call, it is determined that the currently extracted return address is incorrectly parsed and the service exits;

[0024] If it is detected that the number of times the return address is extracted is only once, the range of the extracted stack pointer is within the set range, and the previous PC storage instruction of the currently extracted return address belongs to a function call, it is determined that the detection results of the currently extracted return address and frame pointer are correct.

[0025] Combined with the first aspect, preferably, the steps of parsing all return addresses on the call path according to each of the loading information include:

[0026] Traverse the loading information of each of the loading modules, and compare whether the PC storage instruction of the current return address on the call path falls within the loading space of one of the loading modules;

[0027] If it falls within the loading space of one of the loading modules, obtain the module name and the offset within the module of the loading module where the current return address on the call path is located according to the loading information of the loading module. The value of the offset within the module is the difference between the current return address and the loading position of the module;

[0028] If it does not fall within the loading space of any of the loading modules, it is determined that the PC storage instruction of the current return address is incorrect, and an error message is output and the parsing stops.

[0029] Combined with the first aspect, preferably, the local analysis tool is the PYTHON scripting language, which is used to implement local parsing of stack backtrace information.

[0030] In connection with the first aspect, preferably, the step of using a local analysis tool to parse the names of the loading modules where each return address on the call path is located and the offsets within the modules includes:

[0031] Using the PYTHON scripting language to parse the call order of functions, the return addresses of called functions, the names of the modules where the called functions are located, and the offsets of the called functions within the modules according to the input stack backtrace information;

[0032] Spelling out the binary file generated by the current module where the called function is located before being packaged and loaded according to the name of the module where the called function is located;

[0033] Invoking the symbol resolution instruction in the tool set provided by the compiler to obtain the relative offsets of all functions within the corresponding module;

[0034] Calculating which function the return address of the called function is located in by comparing the return address of the called function with the relative offsets of each function, obtaining the corresponding function name, and printing the function name after the input information to complete the acquisition of the complete stack backtrace information.

[0035] In a second aspect, the present invention provides a stack backtrace device based on the Shenwei BIOS, and the device includes:

[0036] An acquisition module, configured to acquire the loading information of all loading modules in the current system, and record the stack pointer and the loading addresses of each loading module;

[0037] An extraction module, configured to use the stack frame structure in the Shenwei architecture to backtrace the called functions, and extract the return address and the frame pointer by means of instruction scanning;

[0038] A detection module, configured to perform correctness detection on each of the extracted return addresses and frame pointers;

[0039] A backtrace module, configured to use the return address with a correct detection result as the function address of the called function, and use the frame pointer as the stack pointer of the called function to continue backtracing until all return addresses on the call path are obtained and the backtracing ends;

[0040] A parsing module, configured to parse all return addresses on the call path according to each of the loading information to obtain the names of the loading modules where each return address on the call path is located and the offsets within the modules;

[0041] An obtaining module, configured to use a local analysis tool to parse the names of the loading modules where each return address on the call path is located and the offsets within the modules, and obtain the complete stack backtrace information.

[0042] In a third aspect, the present invention provides a stack backtrace device based on the Shenwei BIOS, including a processor and a storage medium;

[0043] The storage medium is used to store instructions;

[0044] The processor is used to operate according to the instructions to execute the steps of the stack backtrace method based on ShenWei BIOS described in any item of the first aspect.

[0045] In a fourth aspect, the present invention provides a computer-readable storage medium, on which a computer program is stored. When the computer program is executed by a processor, the steps of the stack backtrace method based on ShenWei BIOS described in any item of the first aspect are implemented.

[0046] Compared with the prior art, the beneficial effects achieved by the present invention are as follows:

[0047] 1. Inserting the stack backtrace link library formed by the method of the present invention at any position where ShenWei BIOS runs can obtain correct, complete and comprehensive function call information. Moreover, the present invention is completely implemented based on the official interfaces provided by the UEFI framework, without destroying the original framework code or significantly increasing the volume of the output file, and has the technical effects of low coupling degree, high modularity and good portability;

[0048] 2. From the user's perspective, the present invention has good information monitoring and fault tolerance capabilities, low implementation difficulty, simple usage method, clear and easy-to-understand output. For common exceptions or assertions with unknown call relationships, the present invention can quickly and correctly locate the actual site where the error occurs, greatly reducing the time for tracking code and saving time and energy;

[0049] 3. During function development, the present invention can clearly illustrate the process of the same function being called by different functions, which is convenient for debugging and understanding the code. BRIEF DESCRIPTION OF THE DRAWINGS

[0050] Figure 1 is a flowchart of a stack backtrace method based on ShenWei BIOS provided by an embodiment of the present invention;

[0051] Figure 2 The stack backtrace information printing obtained in the BIOS print information provided by an embodiment of the present invention;

[0052] Figure 3 The complete stack backtrace information obtained after being parsed by the local parsing tool provided by an embodiment of the present invention;

[0053] Figure 4 is a structural principle block diagram of a stack backtrace device based on ShenWei BIOS provided by an embodiment of the present invention. DETAILED DESCRIPTION OF THE EMBODIMENTS

[0054] The technical solution of the present invention will be described in detail below with reference to the accompanying drawings and specific embodiments. It should be understood that the embodiments of the present application and the specific features in the embodiments are detailed descriptions of the technical solution of the present application, rather than limitations on the technical solution of the present application. Without conflict, the technical features in the embodiments of the present application and the embodiments can be combined with each other.

[0055] The term "and / or" in this article is merely a description of the association relationship of associated objects, indicating that there can be three relationships. For example, A and / or B can represent: A exists alone, A and B exist simultaneously, and B exists alone. In addition, the character " / " in this article generally represents an "or" relationship between the front and rear associated objects.

[0056] Embodiment 1:

[0057] As Figure 1 shown, this embodiment introduces a stack backtrace method based on the Shenwei BIOS. The method includes the following steps:

[0058] Step 1: Obtain the loading information of all loaded modules in the current system, and record the stack pointer and the loading address for calling each loaded module;

[0059] Step 2: Use the stack frame structure in the Shenwei architecture to backtrace the calling function, and extract the return address and frame pointer by means of instruction scanning;

[0060] Step 3: Perform correctness detection on each of the extracted return addresses and frame pointers;

[0061] Step 4: Use the return address with the correct detection result as the function address of the calling function, and use the frame pointer as the stack pointer of the calling function to continue backtracing until all return addresses on the call path are obtained and the backtracing ends;

[0062] Step 5: Parse all the return addresses on the call path according to the respective loading information to obtain the name of the loaded module where each return address on the call path is located and the offset within the module;

[0063] Step 6: Use the local analysis tool to parse the name of the loaded module where each return address on the call path is located and the offset within the module to obtain the complete stack backtrace information.

[0064] This embodiment will introduce each step of its stack backtrace method in detail in combination with the architecture of the Shenwei BIOS, as follows.

[0065] The loading information obtained in Step 1 provided by the embodiment of the present invention includes the loading location, file size, and module name. The specific steps for obtaining it are described as follows:

[0066] Based on the design of the UEFI framework, when the dispatcher loads the binary files of each module, it generates an image protocol corresponding to each module. The image protocol includes the basic information of its corresponding module and the service interfaces provided externally. The lookup protocol interface provided by the UEFI framework can be used to look up the protocol and obtain the content stored in the protocol;

[0067] After receiving the instruction to use the stack backtrace link library, UEFI calls the link library initialization function; in the link library initialization function, the embodiments of the present invention use the lookup protocol interface provided by the UEFI framework to obtain all image protocol instances in the current system; traverse the protocol instances, and use the provided open protocol interface to open the instances, read out the current module loading position and module file size saved in the instances according to the protocol structure, and use the protocol to access the private structure when the dispatcher loads the module, obtain the read pointer of the current module binary from the private structure, scan the file using the pointer, and obtain the name of the current module; after obtaining the loading position, size and module name of the current module, store the above loading information in a global variable, open the next image protocol instance to continue obtaining the loading information of the next loaded module, and after the traversal is completed, the global variable memory stores the loading information of all modules currently loaded in the system.

[0068] It should be noted that based on the underlying architecture design of Shenwei and specific compilation options specified when compiling the BIOS, when calling a function, the stack pointer expands downward to create the space required for the new function, and the frame pointer (i.e., the stack pointer of the calling function) and its corresponding function return address (i.e., the address of the next instruction after the call in the calling function) are stored on the stack of the called function, and can be obtained by accessing the specified offset through the stack pointer of the called function; therefore, the method of using a scan instruction to extract the offsets of the frame pointer and return address stored on the stack in step 2 provided by the embodiments of the present invention specifically includes:

[0069] Step 2.1: Based on the regular encoding format of the fixed-length instructions of the Shenwei processor (i.e., the property that the addresses between instructions in the same function are connected and the instruction lengths are the same), use the instruction address of the current function to scan the previously executed instructions upward to find the instructions that store the frame pointer and return address when the calling function makes call preparations;

[0070] Step 2.3: Use the instruction encoding specified by the Shenwei architecture to calculate the offsets of the stored frame pointer and return address on the stack;

[0071] Step 2.4: Use the offsets on the stack and the current stack pointer to extract the frame pointer and return address, and backtrack to the stack space of the calling function.

[0072] As an embodiment of the present invention, when performing stack backtrace service, the present invention prevents the display of incorrect stack backtrace information in special cases such as compilation option modification and accidental destruction of frame pointers through the correctness detection in step 3; the steps for performing correctness detection on each of the extracted return addresses and frame pointers include:

[0073] Step 3.1: Detect the number of times of extraction of the return address. If it is detected multiple times, it is determined that the stack frame is incorrect and the service exits;

[0074] Step 3.2: Detect the range of the extracted stack pointer. If it is detected that the extracted stack pointer exceeds the set range, it is determined that the extraction fails or has reached the stack top, and the service exits;

[0075] Step 3.3: Detect the range of the extracted return address. If it is detected that the previous PC storage instruction of the currently extracted return address does not belong to a function call, it is determined that the currently extracted return address is incorrectly parsed and the service exits;

[0076] Based on the detections in steps 3.1 to 3.3, if it is detected that the number of times of extraction of the return address is only once, the range of the extracted stack pointer is within the set range, and the previous PC storage instruction of the currently extracted return address belongs to a function call, it is determined that the detection results of the currently extracted return address and frame pointer are correct.

[0077] Further, based on the characteristics of the CPU instruction stream, the extracted function return address is the global offset from the system perspective. After obtaining the correct function return address information stream, in step 4 of this embodiment, the loaded module information table obtained in step 1 is used to parse the return address; among them, the specific steps for parsing all return addresses on the call path according to each of the loading information include:

[0078] Step 4.1: Traverse the loading information of each of the loaded modules, and compare whether the PC storage instruction of the current return address on the call path falls within the loading space of one of the loaded modules;

[0079] Step 4.2: If it falls within the loading space of one of the loaded modules, obtain the module name and the offset within the module of the loaded module where the current return address on the call path is located according to the loading information of the loaded module, and the value of the offset within the module is the difference between the current return address and the loading position of the module;

[0080] Step 4.3: If it does not fall within the loading space of any of the loaded modules, it is determined that the PC storage instruction of the current return address is incorrect, an error message is output, and the parsing stops.

[0081] It should be further noted that after obtaining the stack backtrace information, in this embodiment, the tool implemented locally in the BIOS code of the present invention is used to resolve the function names; although the UEFI framework deletes the mapping table recording the offsets of functions within modules when packaging the executable file, the binary files generated before packaging for all modules can be resolved locally in the code through specific instructions (nm instruction on the Linux platform) of the toolset provided by the compiler, and the relative offsets of all functions within the modules where the functions are located can be obtained. Among them, the specific steps of using the local analysis tool in step 6 to resolve the names and offsets within the modules of the loading modules where the return addresses on the call path are located include:

[0082] Step 6.1: Use the PYTHON scripting language to resolve the call order of functions, the return addresses of the calling functions, the names of the modules where the calling functions are located, and the offsets within the modules of the calling functions according to the input stack backtrace information;

[0083] Step 6.2: Spell out the binary file generated before packaging and loading for the module where the current calling function is located according to the name of the module where the calling function is located;

[0084] Step 6.3: Call the symbol resolution instruction in the toolset provided by the compiler to obtain the relative offsets of all functions within the corresponding module;

[0085] Step 6.4: Calculate which function the return address of the calling function is located in by comparing the return address of the calling function with the relative offsets of each function, obtain the corresponding function name, and print the function name after the input information to complete the acquisition of the complete stack backtrace information.

[0086] Refer to Figure 2 and Figure 3 This is a case where the stack backtrace link library formed by inserting the method of the present invention at any place in the ShenWei BIOS in this embodiment is used for information backtrace; Figure 2 This is the stack backtrace information obtained in the BIOS print information in step 5 of this embodiment. The meanings of the parameters from left to right are the call order, the return address (PC) of the calling function, the module (Module) where the calling function is located, and the offset (offset) within the module where the calling function is located; among them, the missing lines #3 and #4 are UEFI framework functions. As Figure 3In step 6 of this embodiment, regarding the printing of the complete stack trace information obtained after being parsed by the local parsing tool, the meanings of the parameters from left to right are the call order, the return address (PC) of the calling function, the module where the calling function is located (Module), the offset within the module where the calling function is located, and the function name of the calling function (Function nanme); among them, the missing lines #3 and #4 are UEFI framework functions. The test results show that the present invention has good information monitoring and fault tolerance capabilities, low implementation difficulty, simple usage method, clear and easy-to-understand output. For common exceptions or assertions with unknown call relationships, the present invention can quickly and correctly locate the actual location of the error occurrence, greatly reducing the time for tracing code and saving time and effort; moreover, the present invention is completely implemented based on the official interfaces provided by the UEFI framework, without destroying the original framework code or significantly increasing the volume of the output file, and has the technical effects of low coupling degree, high modularity, and good portability, and is suitable for popularization and use.

[0087] Embodiment 2:

[0088] As Figure 4 shown, an embodiment of the present invention provides a stack trace device based on Shenwei BIOS, which can be used to implement the method described in Embodiment 1, and specifically includes:

[0089] An acquisition module, configured to acquire the loading information of all loaded modules in the current system, and record the stack pointer and the loading addresses of each loaded module called;

[0090] An extraction module, configured to use the stack frame structure in the Shenwei architecture to backtrace the calling function, and extract the return address and the frame pointer by means of instruction scanning;

[0091] A detection module, configured to perform correctness detection on each of the extracted return addresses and frame pointers;

[0092] A backtrace module, configured to use the return address with a correct detection result as the function address of the calling function, and use the frame pointer as the stack pointer of the calling function to continue backtracing until all return addresses on the call path are obtained and the backtracing ends;

[0093] An analysis module, configured to analyze all return addresses on the call path according to each of the loading information to obtain the name of the loaded module where each return address on the call path is located and the offset within the module;

[0094] An obtaining module, configured to use a local analysis tool to analyze the name of the loaded module where each return address on the call path is located and the offset within the module, and obtain the complete stack trace information.

[0095] The stack backtracking device based on the ShenWei BIOS provided in the embodiments of the present invention and the stack backtracking method based on the ShenWei BIOS provided in Embodiment 1 are based on the same technical concept, and can produce beneficial effects as described in Embodiment 1. For the content not described in detail in this embodiment, reference can be made to Embodiment 1.

[0096] Embodiment 3:

[0097] The embodiments of the present invention provide a stack backtracking device based on the ShenWei BIOS, including a processor and a storage medium;

[0098] The storage medium is used to store instructions;

[0099] The processor is used to operate according to the instructions to execute the steps of any one of the methods in Embodiment 1.

[0100] Embodiment 4:

[0101] The embodiments of the present invention provide a computer-readable storage medium, on which a computer program is stored. When the computer program is executed by a processor, the steps of any one of the methods in Embodiment 1 are implemented.

[0102] Those skilled in the art should understand that the embodiments of the present application can be provided as a method, a system, or a computer program product. Therefore, the present application can adopt the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware aspects. Moreover, the present application can adopt the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.

[0103] The present application is described with reference to the flowcharts and / or block diagrams of methods, devices (systems), and computer program products according to the embodiments of the present application. It should be understood that each flow and / or block in the flowchart and / or block diagram can be implemented by computer program instructions, and the combination of the flows and / or blocks in the flowchart and / or block diagram can also be implemented by computer program instructions. These computer program instructions can be provided to the processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing devices to generate a machine, so that the instructions executed by the processor of the computer or other programmable data processing devices generate a device for realizing the functions specified in one Figure 1 one flow or multiple flows and / or blocks Figure 1 or multiple blocks.

[0104] These computer program instructions can also be stored in a computer-readable memory that can direct a computer or other programmable data processing device to work in a specific manner, so that the instructions stored in the computer-readable memory generate a manufactured article including an instruction device, and the instruction device realizes the functions in the processFigure 1 one process or multiple processes and / or boxes Figure 1 the functions specified in one box or multiple boxes

[0105] These computer program instructions can also be loaded onto a computer or other programmable data processing device, so that a series of operation steps are executed on the computer or other programmable device to generate a computer-implemented process. Thus, the instructions executed on the computer or other programmable device provide steps for implementing the functions specified in one process or multiple processes and / or boxes Figure 1 one process or multiple processes and / or boxes Figure 1 the steps of the functions specified in one box or multiple boxes

[0106] The above are only the preferred embodiments of the present invention. It should be noted that for those of ordinary skill in the art, without departing from the technical principle of the present invention, several improvements and modifications can be made, and these improvements and modifications should also be regarded as the protection scope of the present invention.

Claims

1. A stack backtracking method based on ShenWei BIOS, characterized in that, The method includes: Obtaining the loading information of all loaded modules in the current system, and recording the stack pointer and the loading address for calling each loaded module; Using the stack frame structure in the ShenWei architecture to backtrace the calling function, and extracting the return address and frame pointer by means of instruction scanning; Performing correctness detection on each of the extracted return addresses and frame pointers; Taking the return address with a correct detection result as the function address of the calling function, and using the frame pointer as the stack pointer of the calling function to continue backtracing until all return addresses on the call path are obtained and the backtracing ends; Parsing all return addresses on the call path according to each of the loading information to obtain the name of the loaded module where each return address on the call path is located and the offset within the module; Using a local analysis tool to parse the name of the loaded module where each return address on the call path is located and the offset within the module to obtain complete stack backtrace information.

2. The stack backtracking method based on ShenWei BIOS according to claim 1, characterized in that The step of obtaining the loading information of all loaded modules in the current system includes: Using the lookup protocol interface provided by the UEFI framework to traverse and read the content stored in the image protocol corresponding to each loaded module; the content stored in the image protocol includes the loading address, file size, and module name.

3. The stack backtrace method based on ShenWei BIOS according to claim 1, wherein, The step of extracting the return address and frame pointer by means of instruction scanning includes: Based on the regular encoding format of the fixed-length instructions of the ShenWei processor, using the instruction address of the current function to scan upward the previously executed instructions to find the instructions that store the frame pointer and return address when the calling function is preparing for the call; Calculating the offsets of the stored frame pointer and return address on the stack using the instruction encoding specified by the ShenWei architecture; Using the offsets on the stack and the current stack pointer to extract the frame pointer and return address, and backtracing to the stack space of the calling function.

4. The stack backtracking method based on ShenWei BIOS according to any one of claims 1 to 3, characterized in that, The step of performing correctness detection on each of the extracted return addresses and frame pointers includes: Detecting the number of times of extraction of the return address. If it is detected multiple times, it is determined that the stack frame is incorrect and the service is exited; Detecting the range of the extracted stack pointer. If it is detected that the extracted stack pointer exceeds the set range, it is determined that the extraction fails or has reached the stack top and the service is exited; Detecting the range of the extracted return address. If it is detected that the previous PC storage instruction of the currently extracted return address does not belong to a function call, it is determined that the currently extracted return address is incorrectly parsed and the service is exited; If it is detected that the number of times of extraction of the return address is only once, the range of the extracted stack pointer is within the set range, and the previous PC storage instruction of the currently extracted return address belongs to a function call, it is determined that the detection results of the currently extracted return address and frame pointer are correct.

5. The stack backtrace method based on ShenWei BIOS according to claim 4, wherein The step of parsing all return addresses on the call path according to each of the loading information includes: Traversing the loading information of each of the loaded modules, and comparing whether the PC storage instruction of the current return address on the call path falls within the loading space of one of the loaded modules; If it falls within the loading space of one of the loaded modules, obtaining the module name of the loaded module where the current return address on the call path is located and the offset within the module according to the loading information of the loaded module, and the value of the offset within the module is the difference between the current return address and the loading position of the module; If it does not fall within the loading space of any loading module, it is determined that the PC storage instruction of the current return address is incorrect, an error message is output, and the parsing is stopped.

6. The stack backtrace method based on ShenWei BIOS according to claim 1 or 5, characterized in that The local analysis tool is a PYTHON scripting language, which is used to implement local parsing of stack backtrace information.

7. The stack backtrace method based on ShenWei BIOS according to claim 6, wherein, The steps of using the local analysis tool to parse the names of the loading modules where each return address is located on the call path and the offsets within the modules include: Using the PYTHON scripting language to parse the call order of functions, the return addresses of called functions, the names of the modules where the called functions are located, and the offsets of the called functions within the modules according to the input stack backtrace information; Spelling out the binary file generated before packaging and loading for the module where the current called function is located according to the name of the module where the called function is located; Calling the symbol parsing instruction in the tool set provided by the compiler to obtain the relative offsets of all functions within the corresponding module; Calculating which function the return address of the called function is located in by comparing the return address of the called function with each function's relative offset, obtaining the corresponding function name, and printing the function name after the input information to complete the acquisition of the complete stack backtrace information.

8. A stack backtrace device based on ShenWei BIOS, characterized in that, The device includes: An acquisition module, which is used to acquire the loading information of all loading modules in the current system and record the stack pointer and the loading addresses of each loading module called; An extraction module, which is used to backtrace the called functions by using the stack frame structure in the ShenWei architecture and extract the return address and the frame pointer by means of instruction scanning; A detection module, which is used to perform correctness detection on each of the extracted return addresses and frame pointers; A backtrace module, which is used to use the return address with a correct detection result as the function address of the called function and use the frame pointer as the stack pointer of the called function to continue backtracing until all return addresses on the call path are obtained and the backtracing ends; A parsing module, which is used to parse all return addresses on the call path according to each of the loading information to obtain the names of the loading modules where each return address on the call path is located and the offsets within the modules; An obtaining module, which is used to use the local analysis tool to parse the names of the loading modules where each return address on the call path is located and the offsets within the modules to obtain the complete stack backtrace information.

9. A stack backtrace device based on ShenWei BIOS, characterized in that, It includes a processor and a storage medium; The storage medium is used to store instructions; The processor is used to operate according to the instructions to execute the steps of the stack backtrace method based on ShenWei BIOS according to any one of claims 1 to 7.

10. A computer-readable storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by the processor, it implements the steps of the stack backtrace method based on ShenWei BIOS according to any one of claims 1 to 7.

Citation Information

Patent Citations

  • Error tracking method of embedded system and device thereof

    CN101539883A

  • Function calling relation backtracking method and system based on Xtensa DSP

    CN110007984A