Stack backtracking method, device, electronic equipment and computer readable storage medium
By calculating the stack bottom position based on parameter information from the PC and SP registers, the problems of prior preparation and system crash impact in existing technologies are solved, achieving efficient stack backtracking without preparation.
Patent Information
- Application Number
- CN202110163494.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-02-05
- Publication Date
- 2026-02-13
- Estimated Expiration
- 2041-02-05
AI Technical Summary
In existing technologies, stack backtracking methods require prior preparation and cannot perform stack backtracking when the system crashes, affecting performance and storage space.
By utilizing the parameter information of the PC register, the target point is searched from the current node, and the change in stack size is recorded. Combined with the parameter information of the SP register, the stack bottom position is calculated, thus achieving stack backtracking without prior preparation.
It enables stack backtracking even in the event of a system crash, avoiding wasted storage space and improving operational efficiency and flexibility.
Smart Images

Figure CN114880149B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the technical field of stack backtracking, and particularly relates to a stack backtracking method and device, an electronic device and a computer readable storage medium. BACKGROUND
[0002] In the prior art, there are two ways to perform stack backtracking. One is stack backtracking based on FP stack frame registers. The FP register is stacked and the bottom address of the stack is saved each time a function is called, which affects the runtime performance. After the compiler is started, the FP register is not stacked, and this program cannot be used for stack backtracking in this way. The other is stack backtracking based on unwind. Each stack instruction is coded and saved in a special segment, which occupies storage space.
[0003] Both of the above-mentioned stack backtracking methods must be prepared in advance, that is, before the program crashes, the corresponding preparation work must be performed in the program. If a program does not have FP register stacking and unwind information, stack backtracking cannot be performed, and therefore the prior art needs to be improved. SUMMARY
[0004] The present application provides a stack backtracking method, device, electronic device and computer readable storage medium, which does not need to be prepared in advance and is not affected by system crashes during stack backtracking.
[0005] To solve the above-mentioned technical problem, the first technical solution provided by the present application is to provide a stack backtracking method, comprising: obtaining a current node of function execution according to parameter information of a PC register; searching a target point from the current node and recording a stack size change amount from the current node to the target point; obtaining parameter information of the SP register at the target point according to parameter information of the SP register at the current node and the stack size change amount; and obtaining a stack bottom position of the current function based on the parameter information of the SP register at the target point.
[0006] The searching of the target point from the current node and the recording of the stack size change amount from the current node to the target point comprises: calculating a sum of the parameter information of the SP register at the current node and the stack size change amount, and then obtaining the parameter information of the SP register at the target point.
[0007] The obtaining of the stack bottom position of the current function based on the parameter information of the SP register at the target point comprises: if the parameter information of the SP register at the target point is equal to the parameter information of the FP register at the target point, the target point is the stack bottom position of the current function.
[0008] The searching the target point from the current node and recording the stack size change amount from the current node to the target point comprises: searching the target point from the current node and judging the parameter information of the PC register at the current search node; if the parameter information of the PC register at the current search node is a stack operation instruction, recording the stack size change amount corresponding to the stack operation instruction; if the parameter information of the PC register at the current search node is a return instruction, the current search node is the target point; and adding the stack size change amounts corresponding to all stack operation instructions to obtain the stack size change amount from the current node to the target point.
[0009] The judging the parameter information of the PC register at the current search node further comprises: if the parameter information of the PC register at the current search node is a branch instruction, recording the addresses of all branch nodes; and selecting one branch node from all branch nodes as the current search node of the PC register.
[0010] The method further comprises: if the parameter information of the PC register at the current search node is a branch instruction and the branch node comprises a node that has been searched, deleting the current search node and corresponding search information and returning to the previous current search node.
[0011] If the parameter information of the PC register at the current search node is a branch instruction, the recording the addresses of all branch nodes further comprises: if all branch nodes have been searched, returning to the current search node and deleting the branch node and corresponding search information.
[0012] The obtaining the stack bottom position of the current function based on the parameter information of the SP register at the target point comprises: if the parameter information of the SP register at the target point is less than the parameter information of the FP register at the target point, the target point is not the stack bottom position of the current function.
[0013] The method further comprises: determining a search starting position according to the parameter information of the SP register at the target point; searching the current node from the search starting position and recording a stack size change amount from the search starting position to the current node; obtaining the parameter information of the SP register at the current node according to the parameter information of the SP register at the search starting position and the stack size change amount; and obtaining the stack bottom position of the current function based on the parameter information of the SP register at the current node.
[0014] The parameter information of the SP register at the target point is determined according to the parameter information of the SP register at the target point, including: obtaining first preset parameter information according to the parameter information of the SP register at the target point, and judging whether the first preset parameter information is located in the code segment; if yes, the first preset parameter information is the parameter information of the LR register; obtaining second preset parameter information according to the parameter information of the LR register, and judging whether the second preset parameter information is located in the code segment; if yes, the second preset parameter information is the parameter information of the PC register; and the position corresponding to the parameter information of the PC register is the search starting point position.
[0015] To solve the above technical problems, the second technical solution provided by the present application is to provide a stack backtracking device, comprising: a node determination module configured to obtain a current node of function execution according to the parameter information of the PC register; a search module configured to search a target point from the current node and record a stack size change amount from the current node to the target point; a calculation module configured to obtain the parameter information of the SP register at the target point according to the parameter information of the SP register at the current node and the stack size change amount; and a stack bottom position determination module configured to obtain a stack bottom position of the current function based on the parameter information of the SP register at the target point.
[0016] To solve the above technical problems, the third technical solution provided by the present application is to provide an electronic device, comprising: a memory and a processor, wherein the memory stores program instructions, and the processor retrieves the program instructions from the memory to execute the stack backtracking method of any one of the above.
[0017] To solve the above technical problems, the fourth technical solution provided by the present application is to provide a computer readable storage medium, which stores a program file, and the program file can be executed to implement the stack backtracking method of any one of the above.
[0018] The beneficial effects of the present application are different from those of the prior art. The present application obtains a current node of function execution according to the parameter information of the PC register, searches a target point from the current node and records a stack size change amount from the current node to the target point, obtains the parameter information of the SP register at the target point according to the parameter information of the SP register at the current node and the stack size change amount, and obtains a stack bottom position of the current function based on the parameter information of the SP register at the target point. The stack backtracking can be performed only by exporting the corresponding registers and stack data, and the stack backtracking process does not require prior preparation and is not affected by system crashes. BRIEF DESCRIPTION OF DRAWINGS
[0019] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the drawings needed to be used in the embodiments will be briefly introduced. Obviously, the drawings in the following description only constitute some embodiments of the present application, and for those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0020] Figure 1 A flowchart of an embodiment of the stack backtracking method of the present application;
[0021] Figure 2 A flowchart of an embodiment of step S12 in the method; Figure 1
[0022] Figure 3 A flowchart of another embodiment of step S12 in the method; Figure 1
[0023] A structural diagram of an embodiment of the stack backtracking device of the present application; Figure 4
[0024] A structural diagram of an embodiment of the electronic device of the present application; Figure 5
[0025] A structural diagram of an embodiment of the computer storage medium of the present application. Figure 6 DETAILED DESCRIPTION
[0026] The technical solutions in the embodiments of the present application will be described clearly and completely below with reference to the drawings in the embodiments of the present application. Obviously, the described embodiments are only some of the embodiments of the present application, rather than all the embodiments. Based on the embodiments in the present application, all other embodiments obtained by those skilled in the art without creative effort belong to the scope of protection of the present application.
[0027] The data in the stack and the data out of the stack in the function calling process should be equal, that is, the SP register pointer should be at the same position before the function calling starts and after the function returns, denoted as Stack in = Stack out , wherein Stack in is the size of the data in the stack, and Stack out is the size of the data out of the stack. The execution of a function is a directed graph structure, of which the nodes are all branches or jump codes, and the edges are sequentially executed codes. The in-stack and out-stack operations can be tracked by assembly codes, and the cumulative sizes of the in-stack and out-stack operations are Stacksize. On the basis of this fact, the values of the current PC register and the Space register are known, denoted as PC cur and SP cur , the stack backtracking can be performed. Because the execution of the function is a directed graph structure, according to the PC cur position, the corresponding node in the graph is found, and starting from the node, a directed graph search is performed, and the goal is to search for a function return point. During this period, all stack-in and stack-out operations are counted and the in-out quantity StackSize is recorded (if the stack-in is positive and the stack-out is negative, the successful search for the function return point is negative, because when returning, all stack-in data will be stack-out, including the stack-in data between the function start and the PC position). Because:
[0028] SP cur + StackSize = Stack out ;
[0029] Stack in = Stack out ;
[0030] Therefore, the SP cur pointer plus StackSize is the value of the stack-in Stack in , that is, the position of the FP. In the ARM (Advanced RISC Machine) architecture, the position of the FP minus 4 is the position of the LR. In this way, the entire stack backtracking process can be completed.
[0031] The above-mentioned directed graph search does not need to generate the entire graph structure in advance, but only needs to search from the PC position downward, and can end as soon as the function return point is searched. In fact, the entire search process is a search process of the assembly code, and only the information of the searched node needs to be recorded to complete the backtracking.
[0032] The scheme of the present application will be specifically described in combination with the accompanying drawings and specific embodiments.
[0033] Please refer to Figure 1 , which is a flowchart of the first embodiment of the stack backtracking method of the present application, including:
[0034] Step S11: obtaining the current node of the function execution according to the parameter information of the PC register.
[0035] Specifically, during the execution of the function, the parameter information of the PC register and the parameter information of the SP register are known. That is, the current node of the function execution can be obtained according to the parameter information of the PC register. In an embodiment, the parameter information of the PC register of the current node is denoted as PC cur , and the parameter information of the SP register at the current node is denoted as SP cur .
[0036] Step S12: searching the target point from the current node, and recording the stack size change amount from the current node to the target point.
[0037] Specifically, during the execution of the function, the execution of computer instructions includes three structures of sequence, branch and loop. The execution of the function is a directed graph structure. When the function is executed to a certain node, it can be judged that the current node is one of the three structures of sequence, branch and loop according to the parameter information of the PC register.
[0038] Specifically, during the search process, the parameter information of the PC register at the current search node is judged. If the parameter information of the PC register at the current search node is a stack operation instruction, it is a sequence structure, and the stack size change amount StackSize corresponding to the stack operation instruction is recorded. If the parameter information of the PC register at the current search node is a branch instruction, it is a branch structure, and the addresses of all branch nodes are recorded; one branch node is selected from all branch nodes as the current search node of the PC register. If the parameter information of the PC register at the current search node is a branch instruction, and the branch node includes a node that has been searched, it is a loop structure, and the current search node and the corresponding search information are deleted, and the last current search node is returned. If the parameter information of the PC register at the current search node is a return instruction, the current search node is the target point; the stack size change amounts corresponding to all stack operation instructions are accumulated, and then the stack size change amount from the current node to the target point is obtained.
[0039] Specifically, please refer to Figure 2 , assuming that the current node is ad06c, the target point is searched from the current node ad06c, according to the direction of the directed graph shown in Figure 2 , assuming that the current node is node ad06c, during the search, the parameter information of the PC register at the current node ad06c is judged. If the parameter information of the PC register at the current node ad06c is a stack operation instruction, it is a sequence structure, and the search is performed to node ad0b0, the stack size change amount from node ad06c to node ad0b0 is recorded, which is denoted as StackSize1. At this time, node ad0b0 is the current search node, and the parameter information of the PC register at the current search node ad0b0 is judged, as shown in Figure 2As shown, the parameter information of the PC register in the current search node ad0d4 is branch instruction, including branch ad0d4-ad0f0 and ad0d4-ad0f4. At this time, the addresses of all branch nodes (ad0f0 and ad0f4) are recorded, and one branch node is selected from all branch nodes (ad0f0 and ad0f4) as the current search node of the PC register. Assuming that the node ad0f4 is selected as the current search node of the PC register, the stack size change quantity from the node ad0d4 to the node ad0f4 is recorded, denoted as StackSize3. Further, the parameter information of the PC register in the node ad0f4 is judged, as shown in Table 2. Figure 2 As shown, the parameter information of the PC register in the current search node ad0d4 is branch instruction, including branch ad0d4-ad0f0 and ad0d4-ad0f4. At this time, the addresses of all branch nodes (ad0f0 and ad0f4) are recorded, and one branch node is selected from all branch nodes (ad0f0 and ad0f4) as the current search node of the PC register. Assuming that the node ad0f4 is selected as the current search node of the PC register, the stack size change quantity from the node ad0d4 to the node ad0f4 is recorded, denoted as StackSize3. Further, the parameter information of the PC register in the node ad0f4 is judged, as shown in Table 2. Figure 2 As shown, the parameter information of the PC register in the current search node ad0f4 is branch instruction, including branch ad0d4-ad0f0 and ad0d4-ad0f4. At this time, the addresses of all branch nodes (ad0f0 and ad0f4) are recorded, and one branch node is selected from all branch nodes (ad0f0 and ad0f4) as the current search node of the PC register. Assuming that the node ad0f4 is selected as the current search node of the PC register, the stack size change quantity from the node ad0d4 to the node ad0f4 is recorded, denoted as StackSize3. Further, the parameter information of the PC register in the node ad0f4 is judged, as shown in Table 2. Figure 2 As shown, the parameter information of the PC register in the current search node ad0f0 is stack operation instruction, which is sequential structure, and the search is to the node ad158. The stack size change quantity from the node ad0f0 to the node ad158 is recorded, denoted as StackSize4. Further, the parameter information of the PC register in the node ad158 is judged, as shown in Table 2. Figure 2As shown, the parameter information of PC register in the current search node ad158 is a stack operation instruction, and it is a sequential structure, and the search reaches node ad15c, and the stack size change amount from node ad158 to node ad15c is recorded as StackSize5. The parameter information of PC register in node ad15c is judged, and if the parameter information of PC register in node ad15c is a return instruction, then node ad15c is the target point. At this time, all stack size change amounts recorded from the current node to the target point are accumulated, and then the stack size change amount StackSize from the current node to the target point is obtained. That is, StackSize1+ StackSize2+ StackSize4+ StackSize5= StackSize.
[0040] Specifically, in an embodiment, if the parameter information of PC register in the current search node is a branch instruction, and all branch nodes are searched, then return to the current search node, and delete the branch nodes and the corresponding search information. Specifically, for example, if node ad0d4 is searched, the parameter information of PC register in the current search node ad0d4 is a branch instruction, which includes branches ad0d4-ad0f0 and ad0d4-ad0f4. If all branch nodes ad0f0 and ad0f4 are searched, then return to the current search node ad0d4, and delete the search information of all branch nodes ad0f0 and ad0f4.
[0041] Step S13: obtaining the parameter information of SP register in the target point according to the parameter information of SP register in the current node and the stack size change amount.
[0042] Specifically, the sum of the parameter information SP cur of SP register in the current node and the stack size change amount StackSize is calculated, and then the parameter information of SP register in the target point is obtained, which is specifically SP callce = SP cur + StackSize.
[0043] Step S14: obtaining the stack bottom position of the current function based on the parameter information of SP register in the target point.
[0044] Specifically, because SP cur + StackSize = Stack out , and Stack in = Stack out ; therefore, SP callce = Stack in = Stack out . Therefore, SP curThe value of the register plus StackSize is the value pushed onto the stack. in The value of . Specifically, in one embodiment, if the SP register contains parameter information SP at the target point callce If the parameter information in the FP register is equal to the value at the target point, then the target point is the bottom of the stack of the current function. That is, the location of the FP register, which is also the bottom of the stack of the current function.
[0045] In another embodiment, if the SP register contains parameter information at the target point SP callce If the parameter information in the FP register at the target point is less than the value of the parameter information at the target point, then the target point is not the bottom of the stack of the current function.
[0046] Specifically, such as Figure 3 As shown, assuming the current node is ad06c, the target point is searched from the current node ad06c, according to... Figure 2 The directed graph shown assumes the current node is ad06c. During the search, the parameters of the PC register in the current node ad06c are checked. If the parameters of the PC register in the current node ad06c are stack operation instructions, then it is a sequential structure. The search proceeds to node ad0b0, and the change in stack size from node ad06c to node ad0b0 is recorded, denoted as StackSize1. At this point, node ad0b0 is the current search node. The parameters of the PC register in the current search node ad0b0 are checked, such as... Figure 2 As shown, the parameter information of the PC register at the current search node ad0b0 is a branch instruction, including branches ad0b0-ad0d4 and ad0b0-ad160. At this point, the addresses of all branch nodes (ad0d4 and ad160) are recorded; and one branch node is selected from all branch nodes (ad0d4 and ad160) as the current search node for the PC register. Specifically, assuming node ad0d4 is selected as the current search node for the PC register, the change in stack size from node ad0b0 to node ad0d4 is recorded, denoted as StackSize2. Further checks are made on the parameter information of the PC register at node ad0d4, such as... Figure 2 As shown, the parameter information of the PC register at the current search node ad0d4 is a branch instruction, including branches ad0d4-ad0f0 and ad0d4-ad0f4. At this point, the addresses of all branch nodes (ad0f0 and ad0f4) are recorded, and one branch node is selected from all branch nodes (ad0f0 and ad0f4) as the current search node for the PC register. Assuming node ad0f4 is selected as the current search node for the PC register, the change in stack size from node ad0d4 to node ad0f4 is recorded, denoted as StackSize3. Further checks are made on the parameter information of the PC register at node ad0f4, such as...Figure 2 As shown, the parameter information of PC register in the current search node ad0f4 is branch instruction, including branch ad0f4-ad0b0 and ad0f4-ad158. Among them, the branch node includes the node ad0b0 which has been searched, and it is a loop structure. At this time, the current search node ad0f4 and the corresponding search information are deleted. Specifically, the current search node ad0f4 and the recorded stack size change amount StackSize3 are deleted. Return to the last current search node ad0d4 of the current search node ad0f4. Specifically, since the search node ad0f4 is deleted, the search node ad0d4 has only one out-degree branch, that is, ad0d4-ad0f0, and the node ad0f0 is the current search node. Further judge the parameter information of PC register in the node ad0f0, such as Figure 2 As shown, the parameter information of PC register in the current search node ad0f0 is stack operation instruction, and it is a sequential structure. Search to node ad158, and record the stack size change amount from node ad0f0 to node ad158, recorded as StackSize4. Further judge the parameter information of PC register in the node ad158, such as Figure 2 As shown, the parameter information of PC register in the current search node ad158 has no return instruction, and the search end point ad15c is unreachable. At this time, all stack size change amounts from the current node to the target point ad158 are recorded and accumulated, and then the stack size change amount from the current node to the target point is obtained. StackSize1+StackSize2+StackSize4=StackSize. If the parameter information SP of SP register in the target point is less than the parameter information of FP register in the target point, then the target point ad158 is not the stack bottom position of the current function. callce
[0047] As shown in FIG. 6, the parameter information of PC register in the current search node ad0f4 is branch instruction, including branch ad0f4-ad0b0 and ad0f4-ad158. Among them, the branch node includes the node ad0b0 which has been searched, and it is a loop structure. At this time, the current search node ad0f4 and the corresponding search information are deleted. Specifically, the current search node ad0f4 and the recorded stack size change amount StackSize3 are deleted. Return to the last current search node ad0d4 of the current search node ad0f4. Specifically, since the search node ad0f4 is deleted, the search node ad0d4 has only one out-degree branch, that is, ad0d4-ad0f0, and the node ad0f0 is the current search node. Further judge the parameter information of PC register in the node ad0f0, such as Figure 3 As shown in the diagram, there is no path that can reach the endpoint ad15c. In this case, regardless of the location of the PC register, the endpoint cannot be found using the above method. Therefore, to calculate the information needed for stack backtracking, we need to start the search from the function entry point, with the endpoint being the current PC pointer location. Finding a reachable path between them and calculating the stack operation size along the path allows for effective stack backtracking. Specifically, at this point, only the parameter information of the SP register at node ad158 is known, and this parameter information can be used to determine the search starting point. Specifically, based on the parameter information of the SP register at the target point, we obtain the first preset parameter information and determine whether it is located within the code segment. If so, the first preset parameter information is the parameter information of the LR register. Based on the parameter information of the LR register, we obtain the second preset parameter information and determine whether it is located within the code segment. If so, the second preset parameter information is the parameter information of the PC register. The location corresponding to the parameter information of the PC register is the search starting point. If it is not within the code segment, the above search process is repeated.
[0048] In one specific embodiment, 4 bytes of content are taken from the parameter information of the SP register at the target point as the first preset parameter information. If it is within the code segment, the first preset parameter information is the parameter information of the LR register. Then, 4 bytes of content are taken from the parameter information of the LR register as the second preset parameter information. If it is within the code segment, the second preset parameter information is the parameter information of the PC register. The search starting point position can be determined based on the parameter information of the PC register.
[0049] After determining the starting position of the search, search for the current node from the starting position and record the change in stack size from the starting position to the current node; obtain the parameter information of the SP register at the current node based on the parameter information of the SP register at the starting position and the change in stack size; obtain the stack bottom position of the current function based on the parameter information of the SP register at the current node.
[0050] Specifically, such as Figure 3 As shown, if the determined search starting point is ad21c, a reverse search can be performed, searching backwards from ad21c to the current node ad06c. The reverse search method is the same as the search method described above, and will not be repeated here. After obtaining the parameter information of the SP register at the current node, the parameter information Stack at the function entry point can be obtained. in Because of Stack in It is the parameter information of the FP register, which can then be used to obtain the stack bottom position of the current function.
[0051] The stack backtracking method provided in the application regards a function execution program as a directed graph, and does not need any prior preparation for stack backtracking. In any case, the stack backtracking analysis can be performed as long as the data in the corresponding registers and the stack in the memory are exported. All backtracking processes are based on calculation, and do not need recording, thus not wasting storage space. The method is convenient, inexpensive and effective.
[0052] See Figure 4 , which is a structural schematic diagram of an embodiment of the stack backtracking device of the application. The device comprises a node determining module 41, a search module 42, a calculation module 43 and a stack bottom position determining module 44.
[0053] The node determining module 41 is used to obtain the current node of function execution according to the parameter information of the PC register.
[0054] The search module 42 is used to search the target point from the current node, and record the stack size change amount from the current node to the target point.
[0055] In an embodiment, the search module 42 is used to search the target point from the current node, and judge the parameter information of the PC register in the current search node. If the parameter information of the PC register in the current search node is a stack operation instruction, the stack size change amount corresponding to the stack operation instruction is recorded. If the parameter information of the PC register in the current search node is a branch instruction, the addresses of all branch nodes are recorded. One branch node is selected from all the branch nodes as the current search node of the PC register. If the parameter information of the PC register in the current search node is a branch instruction, and the branch node includes a node that has been searched, the current search node and the corresponding search information are deleted, and the process returns to the last current search node. If the parameter information of the PC register in the current search node is a return instruction, the current search node is the target point. The stack size change amounts corresponding to all the stack operation instructions are added, and thus the stack size change amount from the current node to the target point is obtained.
[0056] The calculation module 43 is used to obtain the parameter information of the SP register in the target point according to the parameter information of the SP register in the current node and the stack size change amount. Specifically, the calculation module 43 calculates the sum of the parameter information of the SP register in the current node and the stack size change amount, and thus obtains the parameter information of the SP register in the target point.
[0057] The stack bottom position determining module 44 is used to obtain the stack bottom position of the current function based on the parameter information of the SP register in the target point. In an embodiment, if the parameter information of the SP register in the target point is equal to the parameter information of the FP register in the target point, the target point is the stack bottom position of the current function. If the parameter information of the SP register in the target point is less than the parameter information of the FP register in the target point, the target point is not the stack bottom position of the current function.
[0058] Specifically, after the stack bottom position determining module 44 determines that the target point is not the stack bottom position of the current function, the search module 42 is configured to determine a search starting position according to the parameter information of the SP register at the target point; search the current node from the search starting position, and record the stack size change quantity from the search starting position to the current node; obtain the parameter information of the SP register at the current node according to the parameter information of the SP register at the search starting position and the stack size change quantity; and obtain the stack bottom position of the current function based on the parameter information of the SP register at the current node. Further, the search module 42 obtains first preset parameter information according to the parameter information of the SP register at the target point, and determines whether the first preset parameter information is located in the code segment; if yes, the first preset parameter information is the parameter information of the LR register; obtains second preset parameter information according to the parameter information of the LR register, and determines whether the second preset parameter information is located in the code segment; if yes, the second preset parameter information is the parameter information of the PC register; and the position corresponding to the parameter information of the PC register is the search starting position.
[0059] The stack backtracking device provided in the application regards the function execution program as a directed graph, and does not need any prior preparation for stack backtracking. In any case, as long as the corresponding registers and the data in the stack in the memory are exported, stack backtracking analysis can be performed. All backtracking processes are based on calculation, and do not need recording, so that storage space is not wasted. The stack backtracking device is convenient, inexpensive and effective.
[0060] Please refer to Figure 5 The electronic device includes a memory 102 and a processor 101 connected to each other.
[0061] The memory 102 is configured to store program instructions for implementing any of the above methods.
[0062] The processor 101 is configured to execute the program instructions stored in the memory 102.
[0063] The processor 101 can also be referred to as a CPU (Central Processing Unit). The processor 101 can be an integrated circuit chip with signal processing capability. The processor 101 can also be a general-purpose processor, a DSP (Digital Signal Processor), an ASIC (Application-Specific Integrated Circuit), an FPGA (Field Programmable Gate Array) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components. The general-purpose processor can be a microprocessor or the processor can also be any conventional processor.
[0064] The memory 102 can be a memory stick, a TF card, etc., and can store all information in the electronic device of the device, including input raw data, computer programs, intermediate running results and final running results. It stores and retrieves information according to the location specified by the controller. With the memory, the electronic device has a memory function and can work normally. The memory of the electronic device can be divided into main memory (internal memory) and auxiliary memory (external memory) according to the use, and there is also a classification method of external memory and internal memory. External storage is usually a magnetic medium or an optical disc, etc., which can store information for a long time. Internal memory refers to the storage component on the motherboard, which is used to store the data and programs currently being executed, but is only used to temporarily store programs and data, and the data will be lost when the power is turned off or disconnected.
[0065] In several embodiments provided in the present application, it should be understood that the disclosed method and device can be implemented in other ways. For example, the device implementation described above is only schematic, for example, the division of the module or unit is only a logical function division, and actual implementation can have another division manner, for example, a plurality of units or components can be combined or integrated into another system, or some features can be ignored or not executed. In addition, the coupling or direct coupling or communication connection between the units or components shown or discussed can be indirect coupling or communication connection through some interface, device or unit, and can be electrical, mechanical or other forms.
[0066] The unit described as a separate component can or can not be physically separated, and the component shown as a unit can or can not be a physical unit, that is, it can be located in one place, or it can be distributed to a plurality of network units. Part or all of the units can be selected according to actual needs to achieve the purpose of the present embodiment scheme.
[0067] In addition, the functional units in each embodiment of the present application can be integrated in one processing unit, or each unit can be physically present separately, or two or more units can be integrated in one unit. The integrated unit can be realized in the form of hardware or in the form of a software functional unit.
[0068] The integrated unit, if implemented in the form of a software function unit and sold or used as an independent product, can be stored in a computer readable storage medium. Based on such understanding, the technical solutions of the present application, essentially or in part, or all or part of the technical solutions can be embodied in the form of a software product. The computer software product is stored in a storage medium, and includes a plurality of instructions for causing a computer device (which can be a personal computer, a server, or a network device, etc.) or a processor to perform all or part of the steps of the methods in the embodiments of the present application.
[0069] Please refer to Figure 6 , the structural schematic diagram of the computer readable storage medium of the present application. The storage medium of the present application stores a program file 201 capable of realizing all the above methods, wherein the program file 201 can be stored in the above storage medium in the form of a software product, including a plurality of instructions for causing a computer device (which can be a personal computer, a server, or a network device, etc.) or a processor to perform all or part of the steps of the methods in the embodiments of the present application. The foregoing storage device includes: a U disk, a mobile hard disk, a read-only memory (ROM, Read-Only Memory), a random access memory (RAM, Random Access Memory), a magnetic disk or an optical disk, and various media capable of storing program codes, or a computer, a server, a mobile phone, a tablet, and other terminal devices.
[0070] The above is only an embodiment of the present application, and does not limit the patent scope of the present application. Any equivalent structure or equivalent flow transformation, or direct or indirect application in other related technical fields, is also included in the patent protection scope of the present application.
Claims
1. A stack backtracking method, characterized in that, The method includes: The current node of function execution is obtained based on the parameter information in the PC register; Search for the target point from the current node and record the change in stack size from the current node to the target point; The parameter information of the SP register at the target point is obtained based on the parameter information of the SP register at the current node and the change in stack size; The stack bottom position of the current function is obtained based on the parameter information of the SP register at the target point; The step of searching for the target point from the current node and recording the change in stack size from the current node to the target point includes: Search for the target point from the current node and determine the parameter information of the PC register at the current search node; If the parameter information of the PC register in the current search node is a stack operation instruction, then the change in stack size corresponding to the stack operation instruction is recorded; If the parameter information of the PC register at the current search node is a return instruction, then the current search node is the target point; The stack size changes corresponding to all stack operation instructions are summed to obtain the stack size change from the current node to the target node.
2. The method according to claim 1, characterized in that, The step of searching for the target point from the current node and recording the change in stack size from the current node to the target point includes: The sum of the parameter information of the SP register at the current node and the change in stack size is calculated to obtain the parameter information of the SP register at the target point.
3. The method according to claim 2, characterized in that, The process of obtaining the stack bottom position of the current function based on the parameter information of the SP register at the target point includes: If the parameter information of the SP register at the target point is equal to the parameter information of the FP register at the target point, then the target point is the stack bottom position of the current function.
4. The method according to claim 1, characterized in that, The step of determining the parameter information of the PC register in the current search node also includes: If the parameter information of the PC register at the current search node is a branch instruction, then the addresses of all branch nodes are recorded; Select one branch node from all the branch nodes as the current search node for the PC register.
5. The method according to claim 4, characterized in that, The method further includes: If the parameter information of the PC register at the current search node is a branch instruction, and the branch node includes a node that has already been searched, then the current search node and its corresponding search information are deleted, and the process reverts to the previous current search node.
6. The method according to claim 4, characterized in that, The step of recording the addresses of all branch nodes if the parameter information of the PC register at the current search node is a branch instruction also includes: If all the branch nodes have been searched, then return to the current search node and delete the branch node and its corresponding search information.
7. The method according to claim 1, characterized in that, The process of obtaining the stack bottom position of the current function based on the parameter information of the SP register at the target point includes: If the parameter information of the SP register at the target point is less than the parameter information of the FP register at the target point, then the target point is not the bottom of the stack of the current function.
8. The method according to claim 7, characterized in that, The method further includes: The search starting point position is determined based on the parameter information of the SP register at the target point; Search for the current node from the search starting position and record the change in stack size from the search starting position to the current node; The parameter information of the SP register at the current node is obtained based on the parameter information of the SP register at the search starting point and the change in stack size; The stack bottom position of the current function is obtained based on the parameter information of the SP register at the current node.
9. The method according to claim 8, characterized in that, Determining the search starting point position based on the parameter information of the SP register at the target point includes: The first preset parameter information is obtained based on the parameter information of the SP register at the target point, and it is determined whether the first preset parameter information is located within the code segment; If so, then the first preset parameter information is the parameter information of the LR register; The second preset parameter information is obtained based on the parameter information of the LR register, and it is determined whether the second preset parameter information is located within the code segment; If so, the second preset parameter information is the parameter information of the PC register; the position corresponding to the parameter information of the PC register is the search starting position.
10. A stack backtracking device, characterized in that, include: The node determination module is used to determine the current node of function execution based on the parameter information in the PC register; The search module is used to search for a target point from the current node and record the change in stack size from the current node to the target point; The calculation module is used to obtain the parameter information of the SP register at the target point based on the parameter information of the SP register at the current node and the change in stack size; The stack bottom position determination module is used to obtain the stack bottom position of the current function based on the parameter information of the SP register at the target point; The search module is further configured to search for a target point from the current node and determine the parameter information of the PC register at the current search node; if the parameter information of the PC register at the current search node is a stack operation instruction, then the change in stack size corresponding to the stack operation instruction is recorded; if the parameter information of the PC register at the current search node is a return instruction, then the current search node is the target point; the changes in stack size corresponding to all stack operation instructions are accumulated to obtain the change in stack size from the current node to the target point.
11. An electronic device, characterized in that, include: A memory and a processor, wherein the memory stores program instructions, and the processor retrieves the program instructions from the memory to execute the stack backtracking method as described in any one of claims 1-9.
12. A computer-readable storage medium, characterized in that, The system stores a program file that can be executed to implement the stack backtracking method as described in any one of claims 1-9.
Citation Information
Patent Citations
Fault locating method and device
CN101261603A
Method, device and system for determining function call relationship
CN102109975A