Procedure call stack creation method, stack backtracking method and apparatus
By adding boundary functions during program execution and saving their return addresses and labels, the stack frame type can be directly determined, solving the performance problem of identifying different types of stack frames in the prior art and achieving a faster stack backtracking process.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- HUAWEI TECH CO LTD
- Filing Date
- 2021-09-08
- Publication Date
- 2026-05-26
AI Technical Summary
During program execution, existing technologies require maintaining a PC data table or data linked list at runtime when identifying call stacks of different types of stack frames. This results in a significant performance impact and slower speed in non-stack backtracking scenarios.
By adding boundary functions during program execution and saving their return addresses and corresponding labels, the stack frame type can be directly determined using the set of return addresses, avoiding the need to maintain large data tables or data structures in the runtime library and reducing table lookup time.
Without affecting non-stack backtracking scenarios, it significantly improves stack backtracking performance and shortens stack backtracking time.
Smart Images

Figure CN115774583B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of information technology, and in particular to a method for creating a program call stack, a stack backtracking method, and an apparatus. Background Technology
[0002] The call stack is the memory space allocated for the code and data in the executable file during program execution. The call stack consists of multiple stack frames, each corresponding to a function. The functions corresponding to two adjacent stack frames have a calling relationship.
[0003] In program code, when a function written in one high-level programming language calls a function written in another high-level programming language, or when the high-level programming language corresponding to the runtime library is different from the high-level programming language corresponding to the program code, different types of stack frames will appear in the call stack maintained by the program code during program execution. In this scenario, if stack tracing is required, the identification of the types of each stack frame in the call stack becomes a key factor affecting the performance of stack tracing.
[0004] In the prior art, for the call stack with different stack frames, the stack frame type of each stack frame in the call stack is identified in two main ways during the stack backtracking process: (1) searching based on the address of the Program Count (PC) register; (2) based on the data linked list of the same stack frame type maintained at runtime.
[0005] When using the existing technology to identify stack frame types, it is necessary to maintain a PC data table or data linked list during runtime, which has a significant impact on non-stack backtracking scenarios. Furthermore, the stack backtracking speed using the first method is relatively slow. Summary of the Invention
[0006] This application provides a method for creating a program call stack, a method for stack backtracking, and an apparatus that can maximize stack backtracking performance without affecting non-stack backtracking scenarios.
[0007] In a first aspect, this application provides a method for creating a program call stack, the method comprising: obtaining a first program, the first program including a first function; adding a second function and creating a call stack during the execution of the first program by calling a runtime library, wherein the call stack includes a first stack frame corresponding to the first function, a second stack frame corresponding to the second function, and a third stack frame corresponding to a third function, wherein there is a call relationship between the first function and the second function, and a call relationship between the second function and the third function, the third function being a function in the runtime library or a function in the first program, and the high-level programming language corresponding to the first function and the high-level programming language corresponding to the third function being different; saving a first return address and a label corresponding to the first return address, wherein the first return address is the return address contained in the stack frame corresponding to the function executed later among the first function and the third function, and the label corresponding to the first return address indicates the stack frame type of the first stack frame, the stack frame type of the second stack frame, and the stack frame type of the third stack frame.
[0008] The type of stack frame is matched with the type of high-level programming language of the function corresponding to the stack frame; for example, when the function corresponding to a stack frame is written in Java, then the stack frame type is Java stack frame.
[0009] The first program may include multiple functions written in one or more high-level programming languages.
[0010] It should be understood that in the process of running the first program in the embodiments of this application, a second function will be added between the first function and the third function, so that there is a calling relationship between the first function and the second function, and between the second function and the third function. That is to say, it indicates that the first function and the third function have a calling relationship in the high-level language code.
[0011] In one feasible implementation, the second function is generated by a compiler or by hand-written assembly and stored in a compiled high-level language code library or a compiled runtime code library.
[0012] The second function can be assembly instructions (i.e., represented by assembly language) generated by a compiler and hand-written assembler. These assembly instructions can then be translated into a binary executable file (or machine code) by an assembler. Using a compiler or hand-written assembler to generate the corresponding added boundary function (i.e., the second function), instead of implementing it in high-level language code (i.e., the first program), improves the reusability of the boundary function.
[0013] It should be understood that for three functions with the following calling relationship: the first function calls the second function, and the second function calls the third function, during program execution, the first function is executed first. During the execution of the first function, the second function is called to execute the second function. During the execution of the second function, the third function is called to execute the third function. Therefore, in this scenario, from a timing perspective, the third function is the function that is executed last.
[0014] From a technical perspective, for functions with two call relationships in high-level language code, namely the first function and the third function, when the corresponding high-level programming languages are different, during the creation of the call stack for the first program containing the first function, a function (i.e., the second function) is added between the first and third functions, and a first return address pointing to the second function and the label corresponding to the first return address are stored. Since the label corresponding to the first return address indicates the stack frame type of the first stack frame, the second stack frame, and the third stack frame, for subsequent stack backtracking scenarios, the stack frame types of the first, second, and third stack frames can be identified based on the label corresponding to the first return address. Compared to the existing technology that requires maintaining a large return address data table or data structure in the runtime library during stack backtracking and determining the stack frame type by looking up the table, the embodiments of this application can eliminate the maintenance overhead of the return address data table or data structure in the runtime library and the required table lookup time, thereby effectively shortening the stack backtracking time and significantly improving stack backtracking performance without affecting non-stack backtracking scenarios.
[0015] In one feasible implementation, the first function calls the second function, the second function calls the third function, and the first return address is the return address contained in the third stack frame; or, the third function calls the second function, the second function calls the first function, and the first return address is the return address contained in the first stack frame.
[0016] From a technical perspective, the first, second, and third functions can have the two calling relationships mentioned above. However, the first return address is the return address in the stack frame corresponding to the function executed later. Therefore, in the subsequent stack backtracking process, since the return address in the stack frame corresponding to the function executed later is obtained first, the stack frame type of the stack frame corresponding to the function executed earlier can be determined based on the label corresponding to the return address, effectively shortening the stack backtracking time and improving the stack backtracking performance.
[0017] In one feasible implementation, the first return address is stored in a set of return addresses, where each return address points to a function that is a boundary function. The calling function of the boundary function and the called function of the boundary function correspond to different types of high-level programming languages. The label corresponding to each return address indicates the stack frame type of the stack frame corresponding to the function pointed to by each return address, as well as the stack frame type of the stack frame corresponding to the calling function of the function pointed to by each return address.
[0018] It should be understood that for any other three functions that have the same relationship as the first, second, and third functions mentioned above, the return address and the label corresponding to the last executed function among the other three functions will also be stored in the return address set.
[0019] From a technical perspective, using a return address set to store the return addresses of all boundary functions (i.e., functions added during program execution) allows for the determination of the stack frame type by directly judging whether the return address in the current stack frame is located in the return address set during subsequent stack backtracking. This effectively shortens the stack backtracking time and improves stack backtracking performance compared to existing technologies.
[0020] In one feasible implementation, the call stack includes a fourth stack frame corresponding to a fourth function, the stack frame type of the fourth stack frame is a first type, and the stack frame type of the fourth stack frame is related to the type of high-level programming language corresponding to the fourth function; the method further includes: performing stack backtracking on the fourth stack frame, including: obtaining a second return address in the fourth stack frame, the second return address pointing to a fifth function, the fifth function being the function that calls the fourth function; when the second return address belongs to the set of return addresses, setting the stack frame type of the fifth stack frame to the second type, the fifth stack frame being the stack frame corresponding to the fifth function, the second type indicating that the fifth function is the boundary function; and setting the stack frame type of the sixth stack frame to the third type, the sixth stack frame being the stack frame corresponding to the sixth function, the sixth function being the function that calls the fifth function, the stack frame type of the sixth stack frame being different from the stack frame type of the fourth stack frame; when the return address in the fourth stack frame does not belong to the set of return addresses, setting the stack frame type of the fifth stack frame to the first type, the fifth stack frame being the stack frame corresponding to the fifth function.
[0021] Here, the boundary function is the function added during the execution of the first program, and the fourth stack frame is the stack frame corresponding to any non-boundary function in the call stack mentioned above.
[0022] Specifically, when the first function calls the second function, and the second function calls the third function, during the stack backtracking process, the fourth, fifth, and sixth stack frames can be the third stack frame, the second stack frame, and the first stack frame, respectively; when the third function calls the second function, and the second function calls the first function, during the stack backtracking process, the fourth, fifth, and sixth stack frames can be the first, second, and third stack frames, respectively.
[0023] From a technical perspective, since the return address pointing to the boundary function is added to the return address set during the creation of the call stack, the stack frame type of the relevant stack frame can be determined directly based on whether the return address belongs to the return address set and the label corresponding to the return address in the return address set during stack backtracking. Compared with the existing technology of maintaining a large return address data table or data structure in the runtime library and determining the stack frame type by looking up the table, the embodiments of this application can eliminate the maintenance overhead of the return address data table or data structure in the runtime library and the required table lookup time, thereby effectively shortening the stack backtracking time. It can significantly improve the stack backtracking performance without affecting non-stack backtracking scenarios.
[0024] In one possible implementation, the second type and the third type are two stack frame types indicated by the label corresponding to the second return address.
[0025] The tag corresponding to the second return address can be a data packet format, and the information in the packet can indicate the stack frame type corresponding to the fourth, fifth, and sixth stack frames. This application does not limit the order of the packet information indicating the stack frame type of the fourth, fifth, and sixth stack frames respectively.
[0026] In one feasible implementation, the frame pointer FP in the fourth stack frame is obtained; if the FP points to the bottom of the call stack, the stack backtracking process of the fourth stack frame is terminated.
[0027] From a technical perspective, before determining the type of the corresponding stack frame based on the return address, FP can be used to determine whether the current stack frame is located at the bottom of the call stack, thereby ensuring that the stack backtracking process ends smoothly when it reaches the bottom of the call stack.
[0028] In one possible implementation, when the fourth stack frame is the stack frame at the top of the call stack, the first type is related to the type of high-level programming language of the runtime library.
[0029] From a technical perspective, when initiating the stack backtracking process, since the function corresponding to the stack frame at the top of the call stack is a function in the runtime library, by setting the type of the stack frame at the top of the call stack to the same type as the high-level programming language corresponding to the function in the runtime library, it is ensured that when no boundary function is encountered subsequently, the type of the stack frame corresponding to the calling function of the current stack frame is directly set to the first type, thereby ensuring that the stack frame type of each stack frame is accurately identified during the stack backtracking process.
[0030] Secondly, this application provides a stack backtracking method for a call stack, wherein the call stack includes a fourth stack frame corresponding to a fourth function, the stack frame type of the fourth stack frame is a first type, and the stack frame type of the fourth stack frame is related to the type of high-level programming language corresponding to the fourth function. The method includes: obtaining a second return address in the fourth stack frame, the second return address pointing to a fifth function, the fifth function being the function that calls the fourth function; when the second return address belongs to a set of return addresses, setting the stack frame type of the fifth stack frame to the second type, wherein each return address in the set of return addresses points to a boundary function, the boundary function being a boundary function. The calling function of the boundary function and the called function of the boundary function correspond to different types of high-level programming languages. The fifth stack frame is the stack frame corresponding to the fifth function. The second type indicates that the fifth function is the boundary function. The stack frame type of the sixth stack frame is set to the third type. The sixth stack frame is the stack frame corresponding to the sixth function, which is the function that calls the fifth function. The stack frame type of the sixth stack frame is different from that of the fourth stack frame. When the second return address does not belong to the return address set, the stack frame type of the fifth stack frame is set to the first type. The fifth stack frame is the stack frame corresponding to the fifth function.
[0031] In one feasible implementation, the second type and the third type are two stack frame types indicated by the label corresponding to the second return address; the label corresponding to each return address indicates the stack frame type of the stack frame corresponding to the function pointed to by each return address, and indicates the stack frame type of the stack frame corresponding to the caller function of the function pointed to by each return address.
[0032] In one feasible implementation, the frame pointer FP in the fourth stack frame is obtained; if the FP points to the bottom of the call stack, the stack backtracking process is terminated.
[0033] In one feasible implementation, when the fourth stack frame is the stack frame at the top of the call stack, the first type is related to the type of high-level programming language of the runtime library.
[0034] In one feasible implementation, the call stack is created during the execution of a first program by calling the runtime library. The first program includes a first function. The call stack includes a first stack frame corresponding to the first function, a second stack frame corresponding to a second function, and a third stack frame corresponding to a third function. The second function is a function added during the creation of the call stack. There is a call relationship between the first function and the second function, and a call relationship between the second function and the third function. The third function is either a function in the first program or a function in the runtime library, and the high-level programming language corresponding to the first function is different from the high-level programming language corresponding to the third function. A first return address and a label corresponding to the first return address are stored. The first return address is the return address contained in the stack frame corresponding to the function executed later among the first function and the third function. The label corresponding to the first return address indicates the stack frame type of the first stack frame, the stack frame type of the second stack frame, and the stack frame type of the third stack frame. The first return address belongs to the set of return addresses.
[0035] In one feasible implementation, the first function calls the second function, the second function calls the third function, and the first return address is the return address contained in the third stack frame; or, the third function calls the second function, the second function calls the first function, and the first return address is the return address in the first stack frame.
[0036] In one feasible implementation, the second function is generated by a compiler or by hand-written assembly and stored in a compiled high-level language code library or a compiled runtime code library.
[0037] Thirdly, this application provides a program call stack creation apparatus, the apparatus comprising: an acquisition unit for acquiring a first program, the first program including a first function; a processing unit for adding a second function and creating a call stack during the execution of the first program by calling a runtime library, wherein the call stack includes a first stack frame corresponding to the first function, a second stack frame corresponding to the second function, and a third stack frame corresponding to a third function, wherein there is a call relationship between the first function and the second function, and a call relationship between the second function and the third function, the third function being a function in the runtime library or a function in the first program, and the high-level programming language corresponding to the first function and the high-level programming language corresponding to the third function being different; and a storage unit for storing a first return address and a tag corresponding to the first return address, wherein the first return address is the return address contained in the stack frame corresponding to the function executed later among the first function and the third function, and the tag corresponding to the first return address indicates the stack frame type of the first stack frame, the stack frame type of the second stack frame, and the stack frame type of the third stack frame.
[0038] In one feasible implementation, the first function calls the second function, the second function calls the third function, and the first return address is the return address contained in the third stack frame; or, the third function calls the second function, the second function calls the first function, and the first return address is the return address contained in the first stack frame.
[0039] In one feasible implementation, the second function is generated by a compiler or by hand-written assembly and stored in a compiled high-level language code library or a compiled runtime code library.
[0040] In one feasible implementation, the first return address is stored in a set of return addresses, where each return address points to a function that is a boundary function. The calling function of the boundary function and the called function of the boundary function correspond to different types of high-level programming languages. The label corresponding to each return address indicates the stack frame type of the stack frame corresponding to the function pointed to by each return address, as well as the stack frame type of the stack frame corresponding to the calling function of the function pointed to by each return address.
[0041] In one feasible implementation, the call stack includes a fourth stack frame corresponding to the fourth function. The stack frame type of the fourth stack frame is a first type, and the stack frame type of the fourth stack frame is related to the type of high-level programming language corresponding to the fourth function. The processing unit is further configured to perform stack backtracking on the fourth stack frame. During the stack backtracking process, the processing unit is specifically configured to: instruct the acquisition unit to acquire the second return address in the fourth stack frame, the second return address pointing to the fifth function, the fifth function being the function that calls the fourth function; and when the second return address belongs to the set of return addresses... The stack frame type of the fifth stack frame is set to the second type, where the fifth stack frame is the stack frame corresponding to the fifth function, and the second type indicates that the fifth function is the boundary function. The stack frame type of the sixth stack frame is set to the third type, where the sixth stack frame is the stack frame corresponding to the sixth function, which is the function that calls the fifth function, and the stack frame type of the sixth stack frame is different from the stack frame type of the fourth stack frame. When the return address in the fourth stack frame does not belong to the return address set, the stack frame type of the fifth stack frame is set to the first type, where the fifth stack frame is the stack frame corresponding to the fifth function.
[0042] In one possible implementation, the second type and the third type are two stack frame types indicated by the label corresponding to the second return address.
[0043] In one feasible implementation, the acquisition unit is further configured to acquire the frame pointer FP in the fourth stack frame; the processing unit is further configured to terminate the stack backtracking process of the fourth stack frame when the FP points to the bottom of the call stack.
[0044] In one possible implementation, when the fourth stack frame is the stack frame at the top of the call stack, the first type is related to the type of high-level programming language of the runtime library.
[0045] Fourthly, this application provides a stack backtracking device, wherein the call stack includes a fourth stack frame corresponding to a fourth function, the stack frame type of the fourth stack frame is a first type, and the stack frame type of the fourth stack frame is related to the type of high-level programming language corresponding to the fourth function; the device includes: an acquisition unit, configured to acquire a second return address in the fourth stack frame, the second return address pointing to a fifth function, the fifth function being the function that calls the fourth function; and a processing unit, configured to set the stack frame type of the fifth stack frame to the second type when the second return address belongs to a set of return addresses, wherein each return address in the set of return addresses points to a boundary function. The calling function and the called function of the boundary function correspond to different types of high-level programming languages. The fifth stack frame is the stack frame corresponding to the fifth function. The second type indicates that the fifth function is the boundary function. The stack frame type of the sixth stack frame is set to the third type. The sixth stack frame is the stack frame corresponding to the sixth function, which is the function that calls the fifth function. The stack frame type of the sixth stack frame is different from that of the fourth stack frame. Alternatively, when the second return address does not belong to the set of return addresses, the processing unit sets the stack frame type of the fifth stack frame to the first type. The fifth stack frame is the stack frame corresponding to the fifth function.
[0046] In one feasible implementation, the second type and the third type are two stack frame types indicated by the label corresponding to the second return address; the label corresponding to each return address indicates the stack frame type of the stack frame corresponding to the function pointed to by each return address, and indicates the stack frame type of the stack frame corresponding to the caller function of the function pointed to by each return address.
[0047] In one feasible implementation, the acquisition unit is further configured to acquire the frame pointer FP in the fourth stack frame; the processing unit is further configured to terminate the stack backtracking process of the fourth stack frame when the FP points to the bottom of the call stack.
[0048] In one feasible implementation, when the fourth stack frame is the stack frame at the top of the call stack, the first type is related to the type of high-level programming language of the runtime library.
[0049] In one feasible implementation, the call stack is created during the execution of a first program by calling the runtime library. The first program includes a first function. The call stack includes a first stack frame corresponding to the first function, a second stack frame corresponding to a second function, and a third stack frame corresponding to a third function. The second function is a function added during the creation of the call stack. There is a call relationship between the first function and the second function, and a call relationship between the second function and the third function. The third function is either a function in the first program or a function in the runtime library, and the high-level programming language corresponding to the first function is different from the high-level programming language corresponding to the third function. The device further includes a storage unit for storing a first return address and a tag corresponding to the first return address. The first return address is the return address contained in the stack frame corresponding to the later-executed function among the first function and the third function. The tag corresponding to the first return address indicates the stack frame type of the first stack frame, the stack frame type of the second stack frame, and the stack frame type of the third stack frame. The first return address belongs to the set of return addresses.
[0050] In one feasible implementation, the first function calls the second function, the second function calls the third function, and the first return address is the return address contained in the third stack frame; or, the third function calls the second function, the second function calls the first function, and the first return address is the return address in the first stack frame.
[0051] In one feasible implementation, the second function is generated by a compiler or by hand-written assembly and stored in a compiled high-level language code library or a compiled runtime code library.
[0052] Fifthly, this application provides a chip system comprising at least one processor, a memory, and an interface circuit, wherein the memory, the interface circuit, and the at least one processor are interconnected via circuits, and the at least one memory stores instructions; when the instructions are executed by the processor, any of the methods described in the first aspect and / or the second aspect above are implemented.
[0053] In a sixth aspect, this application provides a computer-readable storage medium storing program instructions that, when executed on one or more processors, implement the method described in any one of the first and / or second aspects.
[0054] In a seventh aspect, this application provides a computer program product, characterized in that, when the computer program product is run on a computer device, the method described in any one of the first and / or second aspects is implemented. Attached Figure Description
[0055] The accompanying drawings used in the embodiments of this application are described below.
[0056] Figure 1 This is a schematic diagram of a system architecture in an embodiment of this application;
[0057] Figure 2 This is a schematic diagram of a program call stack structure in one embodiment of this application;
[0058] Figure 3 This is a flowchart illustrating a method for creating a program call stack according to an embodiment of this application;
[0059] Figure 4 This is a flowchart illustrating stack backtracking using a program call stack in an embodiment of this application;
[0060] Figure 5 This is a schematic diagram of a stack backtracking process in an embodiment of this application;
[0061] Figure 6 This is a flowchart illustrating a stack backtracking method in an embodiment of this application;
[0062] Figure 7 This is a schematic diagram of the structure of a program call stack creation device according to an embodiment of this application;
[0063] Figure 8 This is a schematic diagram of the structure of a stack backtracking device in an embodiment of this application;
[0064] Figure 9 This is a schematic diagram of the hardware structure of a computer device according to an embodiment of this application. Detailed Implementation
[0065] The embodiments of this application are described below with reference to the accompanying drawings. The terms "first," "second," and "third" in the specification, claims, and accompanying drawings are used to distinguish different objects, not to describe a specific order; similarly, "fourth," "fifth," and "sixth" are also used to distinguish different objects, not to describe a specific order. Furthermore, the terms "comprising" and "having," and any variations thereof, are intended to cover non-exclusive inclusion. For example, a process, method, system, product, or device that includes a series of steps or units is not limited to the listed steps or units, but may optionally include steps or units not listed, or may optionally include other steps or units inherent to these processes, methods, products, or devices. The reference to "embodiment" herein means that a specific feature, structure, or characteristic described in connection with an embodiment may be included in at least one embodiment of this application. The appearance of this phrase in various places in the specification does not necessarily refer to the same embodiment, nor is it a separate or alternative embodiment mutually exclusive with other embodiments. It will be explicitly and implicitly understood by those skilled in the art that the embodiments described herein can be combined with other embodiments.
[0066] First, the relevant terms in the embodiments of this application will be explained:
[0067] (1) Stack frame: refers to the portion of stack space allocated separately for a function call. The stack frame corresponding to each function can be used to store the function's local variables, the parameters passed to the called function, the return value of the function, and the return address of the function.
[0068] (2) Stack backtracking: also known as stack return. It refers to the process of tracing back the stack frames of each function level based on the program call stack.
[0069] (3) Hybrid stack: When the high-level language code contains functions written in different high-level programming languages, or when the programming language of the high-level language code is different from the programming language of the runtime function, the call stack created during the execution of this high-level language code contains different types of stack frames. This type of call stack is called a hybrid stack. In the hybrid stack, the type of each stack frame matches the high-level programming language of the function corresponding to that stack frame.
[0070] (4) Return address: The return address refers to the address of the instruction that the calling function should continue to execute after the called function returns. That is, the address of the instruction following the call point instruction (the instruction corresponding to the code that made the function call). In other words, the return address contained in the stack frame corresponding to the called function points to the calling function.
[0071] (5) Runtime library: also known as runtime environment, runtime system, or simply runtime, it is a code library that supports the execution of high-level language code. In most scenarios, its programming language is different from the high-level language that needs to be executed.
[0072] (6) Runtime functions: Functions in the runtime. In most cases, functions in the runtime library are different from functions in the high-level language program that needs to be executed.
[0073] (7) Compiler: A tool used to compile the high-level language code to be executed and the runtime code library that supports the execution of the high-level language code. After compilation, both the high-level language code and the runtime code library become binary executable programs.
[0074] (8) Frame Pointer (FP): Points to the bottom of the current stack frame.
[0075] (9) Stack Pointer (SP): Points to the top of the call stack.
[0076] (10) Code Logic: Product code written directly by programmers in a high-level language, enabling the code to achieve the expected behavior in the product. The product can be various terminal devices, such as mobile phones, computers, watches, wristbands, or in-vehicle terminals.
[0077] (11) High-Level Programming Language (HLPL): A high-level programming language is a machine-independent, procedural, or object-oriented programming language. It is based on everyday human language, using easily understood characters and words, and has high readability, making it accessible even to those with limited computer knowledge. Examples include Basic, Java, C, C++, and Python. Assembly language, on the other hand, is a programming language that falls between high-level programming languages and machine language (also known as machine code), and is generally not considered a high-level programming language.
[0078] The following is a brief introduction to the prior art related to this application. In the prior art, there are two main methods for identifying the stack frame type during stack backtracking in a hybrid stack:
[0079] (I) PC Address Search
[0080] This approach requires creating and maintaining a large PC data table in the runtime library. This table stores the mapping between the return address and the stack frame type for each stack frame. During stack backtracking, the return address of each function is searched in the data table to determine the stack frame type of the stack frame containing that return address.
[0081] Because this method requires runtime maintenance of the PC data table, this table needs to be updated every time the system library (which stores the machine code compiled from high-level language code) is loaded or unloaded. This data table is only used during stack backtracking; in non-stack backtracking scenarios, it adds extra runtime overhead. Furthermore, during stack backtracking, the return address in each stack frame needs to be searched in the PC data table to determine the type of the stack frame, leading to poor stack back performance.
[0082] (ii) Maintaining data linked lists in the runtime library
[0083] This solution requires maintaining a stack frame-type data linked list in the runtime library. Each time business code from the system library is loaded into the runtime, the stack frame information of the high-level language in the system library is inserted into this linked list. When the business code returns from the runtime to the system library, the information in the linked list is deleted. When the business code in the system library initiates a stack push, the stack information of the high-level language is retrieved directly from the stack frame-type data linked list during the stack backtracking operation, and the stack push begins.
[0084] Because the linked list data needs to be updated every time the business code calls a function in the runtime library, and this update operation is a meaningless overhead if no stack backtracking operation is subsequently initiated, this solution has a significant impact on the performance of non-stack backtracking scenarios.
[0085] Please see Figure 1 , Figure 1 This is a schematic diagram of a system architecture according to an embodiment of this application. Figure 1 As shown, the system architecture 100 includes a system library 110 and a runtime library 120. The system library 110 contains business code 111 for execution (i.e., the second program in the following embodiments), which is machine code obtained by compiling high-level language code (i.e., the first program in the following embodiments). The runtime library 120 provides a corresponding runtime environment for the execution of the business code 111; specifically, the runtime library 120 can provide corresponding interfaces for the business code 111 to call functions 121 in the runtime library during execution.
[0086] During the execution of business code 111, a corresponding call stack is created for business code 111. This call stack includes multiple stack frames, each of which corresponds to a function in the business code or runtime library. During the execution of the business code, the stack backtracking process may or may not be executed depending on specific business requirements.
[0087] The application scenarios of the embodiments of this application are described below.
[0088] The embodiments of this application can be applied to various scenarios that require stack backtracking, such as program crashes or call stack exceptions, specifically scenarios that require stack backtracking, such as garbage collection (GC).
[0089] (1) Program crash
[0090] When a program crashes during execution, a stack backtrace can be performed to print and save information about the erroneous program, thus pinpointing exactly which function level crashed during the call.
[0091] (2) Call stack exception
[0092] When the program does not crash during execution and the code can execute normally, but an expected exception occurs during the execution process, stack backtracking can be performed to obtain relevant information about the entire execution process.
[0093] Garbage collection refers to the process of running garbage collection (GC) algorithms to release superblocks in memory. Specifically, it involves moving valid pages from multiple superblocks to a free superblock to release those superblocks and make them available for subsequent writes of valid data.
[0094] Please see Figure 2 , Figure 2 This is a schematic diagram of a program call stack structure according to an embodiment of this application. Figure 2 As shown, the program call stack can include multiple stack frames. The following description uses three adjacent stack frames (stack frame 1, stack frame 2, and stack frame 3) as an example to illustrate the structure of the call stack.
[0095] like Figure 2 As shown, from bottom to top, the stack consists of stack frame 3, stack frame 2, and stack frame 1, corresponding to functions 3, 2, and 1 respectively. Stack frame 3 stores parameters related to function 3, stack frame 2 stores parameters related to function 2, and stack frame 1 stores parameters related to function 1. The calling relationship of these three functions is: function 3 calls function 2, and function 2 calls function 1.
[0096] Each stack frame contains a return address, a frame pointer, and N parameters, where N is a positive integer. The return address in each stack frame points to the calling function of the function corresponding to that stack frame. For example, during a call between function 2 and function 1, after the call ends, the memory address of the instruction following the call point instruction is mapped to return address 1. Return address 1 returns to function 2 after function 1 finishes execution, allowing function 2 to continue running. The stack pointer FP points to the bottom of the current stack frame; for example, frame pointer 1 points to the bottom address of stack frame 1.
[0097] Please see Figure 3 , Figure 3This is a flowchart illustrating a method for creating a program call stack according to an embodiment of this application. Figure 3 As shown, method 300 includes steps S310, S320 and S330.
[0098] Step S310: Obtain a first program, which includes a first function.
[0099] The first program can be a program written by a program developer using a high-level programming language, such as Basic, Java, C, C++, or Python, etc., and this application is not limited to this. The first program can include multiple layers of functions with calling relationships. These multiple layers of functions with calling relationships can be written in one high-level programming language or multiple different high-level programming languages, and this application is not limited to this.
[0100] Step S320: During the process of calling the runtime library to run the first program, a second function is added and a call stack is created. The call stack includes a first stack frame corresponding to the first function, a second stack frame corresponding to the second function, and a third stack frame corresponding to the third function. There is a call relationship between the first function and the second function, and a call relationship between the second function and the third function. The third function is a function in the runtime library or a function in the first program. The high-level programming language corresponding to the first function is different from the high-level programming language corresponding to the third function.
[0101] Specifically, the process of adding the second function and creating the call stack during the execution of the first program by calling the runtime library includes: compiling the first program written in a high-level language to obtain a compiled binary executable program; and simultaneously compiling the runtime library written in the high-level language to obtain a compiled runtime code library (binary). Then, the processor or other hardware calls the compiled runtime code library to run the binary executable file corresponding to the first program and creates a call stack, which includes the stack frame corresponding to each function executed during the execution.
[0102] The compilation process described above may include a preprocessing stage, a compilation stage, an assembly stage, and a linking stage. This application will not elaborate on the compilation process.
[0103] Among them, a binary executable program is a machine language instruction that can be directly executed by hardware such as a processor, i.e., a binary executable file.
[0104] The runtime library provides the necessary runtime environment for the first program to run. Specifically, the runtime library may contain the code libraries, frameworks, platforms, etc., required for the execution of the first program. It should be noted that when running the first program, the runtime library also needs to be compiled, and then the compiled runtime library is used to provide the necessary runtime environment for the first program to run.
[0105] Specifically, during the execution of the first program based on the compiled runtime library, the interface provided by the runtime library can be called to invoke the corresponding functions in the compiled runtime library to support the execution of the first program.
[0106] The first function can be any function in the first program.
[0107] In this context, the first function and the third function are two functions with a calling relationship in the high-level language code. Specifically, the first function and the third function can be two functions with a calling relationship in the first program, or the first function can be a function in the first program, and the third function can be a function in the runtime library.
[0108] The aforementioned process of adding a second function and creating a call stack during the execution of the first program by calling the runtime library specifically includes: for the first and third functions that have a calling relationship in high-level languages and are written in different high-level languages, a second function is added between the first and third functions during execution. Simultaneously, a corresponding second stack frame is maintained for the second function in the call stack.
[0109] Among them, the second function corresponding to the second stack frame has a calling relationship with the first function corresponding to the first stack frame, and the second function corresponding to the second stack frame has a calling relationship with the third function corresponding to the third stack frame.
[0110] In one feasible implementation, the first function calls the second function, the second function calls the third function, and the first return address is the return address contained in the third stack frame; or, the third function calls the second function, the second function calls the first function, and the first return address is the return address contained in the first stack frame.
[0111] Specifically, the first, second, and third functions described above can have the following two calling relationships:
[0112] (a) The first function calls the second function, and the second function calls the third function.
[0113] The first return address is the return address contained in the third stack frame corresponding to the third function. The return address contained in the third stack frame points to the second function, and the return address contained in the second stack frame points to the first function.
[0114] In this case, the code libraries to which the first and third functions belong can include two situations: (1) The first and third functions can both be functions in the first program, that is, in the first program, the first function calls the third function, and the high-level programming languages corresponding to the first and third functions are different. For example, the first function can be written in Java and the third function can be written in C++. (2) The first function is a function in the first program, and the third function is a function in the runtime library. During the execution of the first program, the first function will call the third function in the runtime library. Since the high-level programming languages corresponding to the first function and the third function are different, a second function can be added between the first function and the third function, so that the first function calls the third function through the second function; at this time, the first function is the last function in the multi-level function call relationship in the first program.
[0115] (ii) The third function calls the second function, and the second function calls the first function.
[0116] The first return address is the return address contained in the first stack frame corresponding to the first function. The return address contained in the first stack frame points to the second function, and the return address contained in the second stack frame points to the third function.
[0117] In this case, the code libraries to which the first and third functions belong can include two situations: (1) The first and third functions can both be functions in the first program, that is, in the first program, the third function calls the first function, and the high-level programming languages corresponding to the first and third functions are different. For example, the first function can be written in Java and the third function can be written in C++. (2) The first function is a function in the first program, and the third function is a function in the runtime library. During the execution of the first program, the third function in the runtime library will call the first function in the first program. Since the high-level programming languages corresponding to the first function and the third function are different, a second function can be added between the first and third functions, so that the third function calls the first function through the second function; at this time, the first function is the first-level function in the multi-level function call relationship in the first program.
[0118] The high-level programming language corresponding to the first function refers to the high-level programming language used to write the first function. The same applies to the high-level programming languages corresponding to other functions in this embodiment, which will not be described again here.
[0119] From a technical perspective, the first, second, and third functions can have the two calling relationships mentioned above. However, the first return address is the return address in the stack frame corresponding to the function executed later. Therefore, in the subsequent stack backtracking process, since the return address in the stack frame corresponding to the function executed later is obtained first, the stack frame type of the stack frame corresponding to the function executed earlier can be determined based on the label corresponding to the return address, effectively shortening the stack backtracking time and improving the stack backtracking performance.
[0120] In one feasible implementation, the second function is generated by a compiler or by hand-written assembly and stored in a compiled high-level language code library or a compiled runtime code library.
[0121] The second function can also be called a boundary function. The second function can be assembly instructions generated by a compiler and hand-written assembler (i.e., represented by assembly language), which can then be translated into a binary executable file (or machine code) by an assembler. The second function is unrelated to the code logic in the first program or the code logic in the runtime library; that is, the second function does not exist in the code library or runtime library written in a high-level language.
[0122] From a technical perspective, using a compiler or hand-written assembly to generate the corresponding added boundary functions (i.e., the second functions), instead of implementing them in high-level language code (i.e., the first program), can improve the reusability of the boundary functions.
[0123] Step S330: Save the first return address and the tag corresponding to the first return address. The first return address is the return address contained in the stack frame of the function that is executed later among the first function and the third function. The tag corresponding to the first return address indicates the stack frame type of the first stack frame, the stack frame type of the second stack frame, and the stack frame type of the third stack frame.
[0124] The aforementioned first return address refers to the return address contained in the stack frame corresponding to the function executed later in the first and third functions. Specifically, it refers to: (1) When the first function calls the second function, and the second function calls the third function, the function executed later is the third function. Specifically, during program execution, the first function is executed first. During the execution of the first function, the second function is called to execute the second function. During the execution of the second function, the third function is called to execute the third function. In this scenario, from a timing perspective, the third function is the function executed later; or, (2) When the third function calls the second function, and the second function calls the first function, the function executed later is the first function. During program execution, the third function is executed first. During the execution of the third function, the second function is called to execute the second function. During the execution of the second function, the first function is called to execute the first function. From a timing perspective, the first function is the function executed later.
[0125] Specifically, during the execution of the first program and the creation of the call stack, the first return address and the label corresponding to the first return address are saved. The label corresponding to the first return address indicates the type of the first stack frame, the type of the second stack frame, and the type of the third stack frame.
[0126] Specifically, the type of a stack frame is matched with the high-level programming language of the function that corresponds to it. For example, if the function corresponding to a stack frame is written in Java, then the stack frame type is a Java stack frame.
[0127] The following two examples illustrate the possible data message formats for the tags corresponding to the returned addresses:
[0128] Example 1: When the third function calls the second function, and the second function calls the first function, the first return address is located in the first stack frame. The label corresponding to the first return address can be unwindPCforN2CStub. The character "N" in the label can be used to indicate that the third function is Native, that is, a function in the runtime library. At this time, the type of the third stack frame matches the high-level programming language used to write the runtime library. The character "C" in the label can be used to indicate that the first function is a function in the first program written in a high-level language. Therefore, the type of the first stack frame matches the type of high-level programming language corresponding to the first program. The character "stub" in the label can be used to indicate that the second function is a boundary function (or a stub function). The type of the second stack frame is a boundary stack frame (or a stub stack frame).
[0129] Example 2: When the first function calls the second function, and the second function calls the third function, the first return address is located in the third stack frame. The label corresponding to the first return address can be unwindPCforC2NStub. The character "C" in this label can be used to indicate that the first function is Native, that is, a function in the runtime library. At this time, the type of the first stack frame matches the high-level programming language used to write the runtime library. The character "N" in the label can be used to indicate that the third function is a function in the first program written in a high-level language. Therefore, the type of the third stack frame matches the type of high-level programming language corresponding to the first program. The character "stub" in the label can be used to indicate that the second function is a boundary function (or a stub function), and the type of the second stack frame is a boundary stack frame (or a stub stack frame).
[0130] It should be understood that the above examples only list two feasible data message formats for the labels corresponding to the return addresses. Those skilled in the art can use other data message formats to characterize the labels corresponding to each return address in the set of return addresses, or use different characters to indicate the stack frame type of the corresponding stack frame. This application does not limit this.
[0131] In one feasible implementation, the first return address is stored in a set of return addresses, where each return address points to a function that is a boundary function. The calling function of the boundary function and the called function of the boundary function correspond to different types of high-level programming languages. The label corresponding to each return address indicates the stack frame type of the stack frame corresponding to the function pointed to by each return address, as well as the stack frame type of the stack frame corresponding to the calling function of the function pointed to by each return address.
[0132] Boundary functions are functions added during the creation of the call stack. They are unrelated to the code logic in the first program or the runtime library; that is, boundary functions do not exist in the code library or runtime library written in a high-level language. Boundary functions include the second function in the above embodiments.
[0133] Among them, code logic refers to the functions implemented by programs written by program developers using high-level programming languages.
[0134] In this context, the calling function of the boundary function refers to the function that calls the boundary function, and the called function of the boundary function refers to the function that is called by the boundary function.
[0135] The label corresponding to each return address indicates the stack frame type of the stack frame corresponding to the function pointed to by each return address, and also indicates the stack frame type of the stack frame corresponding to the calling function of the function pointed to by each return address. For details, please refer to the specific description of the label corresponding to the first return address, which will not be repeated here.
[0136] From a technical perspective, using a return address set to store the return addresses of all boundary functions (i.e., functions added during program execution) allows for the determination of the stack frame type by directly judging whether the return address in the current stack frame is located in the return address set during subsequent stack backtracking. This effectively shortens the stack backtracking time and improves stack backtracking performance compared to existing technologies.
[0137] For business scenarios requiring stack backtracking, the call stack created during the first program execution can be backtracked. The stack backtracking process starts from the top of the call stack and ends at the bottom.
[0138] The following section will describe in detail the stack backtracking process in the call stack using the fourth stack frame as an example.
[0139] In one feasible implementation, the call stack includes a fourth stack frame corresponding to a fourth function, the stack frame type of the fourth stack frame is a first type, and the stack frame type of the fourth stack frame is related to the type of high-level programming language corresponding to the fourth function; the method further includes: performing stack backtracking on the fourth stack frame, including: obtaining a second return address in the fourth stack frame, the second return address pointing to a fifth function, the fifth function being the function that calls the fourth function; when the second return address belongs to the set of return addresses, setting the stack frame type of the fifth stack frame to the second type, the fifth stack frame being the stack frame corresponding to the fifth function, the second type indicating that the fifth function is the boundary function; and setting the stack frame type of the sixth stack frame to the third type, the sixth stack frame being the stack frame corresponding to the sixth function, the sixth function being the function that calls the fifth function, the stack frame type of the sixth stack frame being different from the stack frame type of the fourth stack frame; when the return address in the fourth stack frame does not belong to the set of return addresses, setting the stack frame type of the fifth stack frame to the first type, the fifth stack frame being the stack frame corresponding to the fifth function.
[0140] The stack frame type of the fourth stack frame mentioned above is related to the type of high-level programming language corresponding to the fourth function. Specifically, the fourth stack frame can be the stack frame corresponding to any non-boundary function in the call stack. This is because the boundary functions added during the creation of the call stack are implemented by the compiler or hand-written assembly, and therefore do not have a corresponding high-level programming language. Furthermore, the stack frame type of the boundary function's corresponding stack frame has already been identified during the stack backtracking process of the non-boundary function's corresponding stack frame.
[0141] Specifically, first, the second return address contained in the fourth stack frame is obtained, and then it is determined whether the second return address is located in the return address set. This mainly involves two cases:
[0142] (1) If the second return address does not belong to the return address set, it means that the fifth function pointed to by the second return address is not a boundary function. That is, the high-level programming language used to write the fourth function is the same as the high-level programming language used to write the fifth function. Therefore, the stack frame type of the fourth stack frame is the same as the stack frame type of the fifth stack frame. The stack frame type of the fifth stack frame can be set to the first type, which is the same as the type of the first stack frame. In addition, the fourth function and the fifth function belong to the same function in the first program or belong to the same function in the runtime library.
[0143] (2) If the second return address belongs to the return address set, it indicates that the fifth function pointed to by the second return address is a boundary function. The stack frame type of the fifth stack frame corresponding to the fifth function is set to the second type, where the second type is determined by the label corresponding to the second return address. The high-level programming language corresponding to the sixth function that calls the fifth function is different from the high-level programming language corresponding to the fourth function. Therefore, the sixth stack frame can be set to the third type based on the label corresponding to the second return address. The third type is different from the first type. The specific process of determining the second and third types through the label corresponding to the second return address can be found in the corresponding process of the label corresponding to the first return address in the aforementioned embodiments, and will not be repeated here. At this time, the fourth and sixth functions can both be functions belonging to the first program, or the fourth function is a function in the first program and the sixth function is a function in the runtime library, or the fourth function is a function in the runtime library and the sixth function is a function in the first program. From a technical perspective, since the return address pointing to the boundary function is added to the return address set during the creation of the call stack, the stack frame type of the relevant stack frame can be determined directly based on whether the return address belongs to the return address set and the label corresponding to the return address in the return address set during stack backtracking. Compared with the existing technology of maintaining a large return address data table or data structure in the runtime library and determining the stack frame type by looking up the table, the embodiments of this application can eliminate the maintenance overhead of the return address data table or data structure in the runtime library and the required table lookup time, thereby effectively shortening the stack backtracking time. It can significantly improve the stack backtracking performance without affecting non-stack backtracking scenarios.
[0144] Specifically, when the first function calls the second function, and the second function calls the third function, during the stack backtracking process, the fourth, fifth, and sixth stack frames can be the third stack frame, the second stack frame, and the first stack frame, respectively; when the third function calls the second function, and the second function calls the first function, during the stack backtracking process, the fourth, fifth, and sixth stack frames can be the first, second, and third stack frames, respectively.
[0145] In one possible implementation, the second type and the third type are two stack frame types indicated by the label corresponding to the second return address.
[0146] Specifically, the second and third types are determined based on the characters contained in the tag corresponding to the second return address. For details, please refer to the description of the data packet format corresponding to the tag of the first return address, which will not be repeated here.
[0147] In one feasible implementation, the method further includes: obtaining the frame pointer FP in the fourth stack frame; if the FP points to the bottom of the call stack, ending the stack backtracking process of the fourth stack frame.
[0148] Specifically, before determining whether the second return address contained in the fourth stack frame belongs to the return address set, the frame pointer (FP) contained in the fourth stack frame is obtained, and the FP in the fourth stack frame is used to determine whether the fourth stack frame is located at the bottom of the call stack. This involves two cases:
[0149] (1) When FP indicates that the fourth stack frame is at the bottom of the call stack, the stack backtracking process of the fourth stack frame ends.
[0150] (2) When FP indicates that the fourth stack frame is not the bottom of the call stack, it starts to determine whether the second return address contained in the fourth stack frame belongs to the return address set, and determines the stack frame type of the fifth and sixth stack frames accordingly. For the specific determination process, please refer to the above embodiment, which will not be repeated here.
[0151] Optionally, the above method of determining whether the fourth stack frame is located at the bottom of the call stack based on FP includes: determining whether FP is equal to a preset value to determine whether the fourth stack frame is located at the bottom of the call stack. Further, the preset value can be equal to 0; when FP is equal to 0, it indicates that the fourth stack frame is located at the bottom of the call stack; when FP is not equal to 0, it indicates that the fourth stack frame is not located at the bottom of the call stack.
[0152] From a technical perspective, before determining the type of the corresponding stack frame based on the return address, FP can be used to determine whether the current stack frame is located at the bottom of the call stack, thereby ensuring that the stack backtracking process ends smoothly when it reaches the bottom of the call stack.
[0153] In one possible implementation, when the fourth stack frame is the stack frame at the top of the call stack, the first type is related to the type of high-level programming language of the runtime library.
[0154] Specifically, when the call stack begins to backtrack, the runtime library interface is invoked. That is, the function corresponding to the stack frame at the top of the call stack is the function in the compiled runtime library. Therefore, for the stack frame that is backtracked for the first time, its stack frame type matches the type of the high-level programming language of the runtime library. For example, if the high-level programming language of the runtime library is C++, then the first type is a C++ stack frame.
[0155] It should be understood that the stack backtracking process of the stack frames corresponding to other non-boundary functions in the call stack is the same as the stack backtracking process of the fourth stack frame mentioned above, and will not be repeated here.
[0156] From a technical perspective, when initiating the stack backtracking process, since the function corresponding to the stack frame at the top of the call stack is a function in the runtime library, by setting the type of the stack frame at the top of the call stack to the same type as the high-level programming language corresponding to the function in the runtime library, it is ensured that when no boundary function is encountered subsequently, the type of the stack frame corresponding to the calling function of the current stack frame is directly set to the first type, thereby ensuring that the stack frame type of each stack frame is accurately identified during the stack backtracking process.
[0157] From a technical perspective, in this embodiment, for functions with two calling relationships in high-level language code, namely the first function and the third function, when their corresponding high-level programming languages are different, during the creation of the call stack for the first program containing the first function, a function (i.e., the second function) is added between the first function and the third function, and a first return address pointing to the second function and a label corresponding to the first return address are saved. Since the label corresponding to the first return address indicates the stack frame type of the first stack frame, the second stack frame, and the third stack frame, for subsequent stack backtracking scenarios, the stack frame types of the first, second, and third stack frames can be identified based on the label corresponding to the first return address. Compared to the prior art, which requires maintaining a large return address data table or data structure in the runtime library during stack backtracking and determining the stack frame type by looking up the table, this embodiment can eliminate the maintenance overhead of the return address data table or data structure in the runtime library and the required table lookup time, thereby effectively shortening the stack backtracking time and significantly improving stack backtracking performance without affecting non-stack backtracking scenarios.
[0158] Please see Figure 4 , Figure 4 This is a flowchart illustrating stack backtracking of a program call stack in an embodiment of this application. Figure 4 As shown, the stack backtracking process of the program call stack can be used... Figure 4 The process is described as shown, including steps S410, S420, S430, S440, S450 and step S460.
[0159] Step S410: Initiate the stack backtracking process of the program call stack.
[0160] Step S420: Obtain the return address and frame pointer of the function corresponding to the stack frame at the top of the call stack.
[0161] Specifically, the runtime library interface is used to obtain the return address and frame pointer of the function corresponding to the stack frame at the top of the call stack, which are not stored in the call stack. In the following steps, the function pointed to by the return address is referred to as the current function, and the stack frame corresponding to the function pointed to by the return address is referred to as the current stack frame.
[0162] Step S430: Determine whether the function pointed to by the return address is a boundary function.
[0163] Specifically, whether the function pointed to by the return address is a boundary function is determined by whether the return address belongs to the set of return addresses.
[0164] If yes, then execute step S431: set the stack frame type of the current stack frame and the stack frame type of the stack frame corresponding to the calling function of the current function based on the label corresponding to the return address; if no, then execute step S432: set the stack frame type of the current stack frame to be the same as the stack frame containing the return address; it should be understood that during the first stack backtracking process, the return address is stored in the call stack at this time, and the stack frame type of the current stack frame matches the high-level programming language of the runtime library.
[0165] Step S440: Obtain the return address and frame pointer of the called function.
[0166] Specifically, if step S440 follows step S431, then the return address and frame pointer of the called function are located in the stack frame corresponding to the calling function. If step S440 follows step S432, then the return address and frame pointer of the called function are located in the current stack frame, that is, the return address and frame pointer of the called function are obtained from the current stack frame.
[0167] Step S450: Determine whether it is the bottom of the call stack based on the frame pointer pointing to the called function.
[0168] If yes, proceed to step S460: end the stack backtracking process of the call stack. If no, return to step S430.
[0169] Specifically, the detailed execution process of the above steps can be found in the description of the foregoing embodiments, and will not be repeated here.
[0170] Please see Figure 5 , Figure 5 This is a schematic diagram of a stack backtracking process in an embodiment of this application. Figure 5 As shown, Figure 5 The CCP consists of three columns: from left to right, they are the first column, the second column, and the third column. The second column is the call stack created during the execution of the program (which may be the first program in the above embodiment), the first column is the function corresponding to each stack frame in the call stack in the second column, and the third column is the stack frame type of each stack frame in the call stack identified during the stack backtracking process.
[0171] Among them, high-level language function 1 and high-level language function 2 can be functions in the first program, runtime function 1 to runtime function 4 are functions in the runtime library, and boundary function 1 to boundary function 3 are functions added during the creation of the call stack.
[0172] It should be understood that, as described in the foregoing embodiments, during the creation of the call stack, the return addresses pointing to boundary function 1, boundary function 2, and boundary function 3 (the return addresses contained in stack frames 4, 6, and 8) are added to the return address set; while the return addresses pointing to other functions are not added to the return address set. Furthermore, the frame pointer contained in stack frame 1 indicates that stack frame 1 is the bottom of the call stack.
[0173] The following is combined with Figure 5 To describe the stack backtracking process of the call stack:
[0174] First, the runtime library interface is called to obtain the return address and frame pointer corresponding to runtime function 4. This pointer does not exist on the call stack. Figure 5 In the function call direction shown, the return address and frame pointer of the last-level function are not saved in the call stack. Since the return address pointing to runtime function 4 does not belong to the return address set, it can be assumed that the function pointed to by this return address is not a boundary function. In this case, the type of stack frame 9 corresponding to runtime function 4 is set to runtime stack frame (that is, matching the high-level programming language corresponding to the runtime library; for example, when the high-level programming language for writing the runtime library is C++, then the runtime stack frame is a C++ stack frame).
[0175] Obtain the return address and frame pointer of the calling function (i.e., runtime function 3) that points to runtime function 4, specifically the return address and frame pointer contained in stack frame 9. Since the frame pointer in stack frame 9 indicates that stack frame 9 is not the bottom of the call stack, and the return address in stack frame 9 does not belong to the return address set, it means that the function pointed to by that return address is not a boundary function. This implies that the high-level programming language used to write runtime function 3 is the same as the high-level programming language used to write runtime function 4. Therefore, set the type of stack frame 8 to the same runtime stack frame as stack frame 9.
[0176] Obtain the return address and frame pointer of the calling function that points to runtime function 3, i.e., the return address and frame pointer contained in stack frame 8. Since the frame pointer contained in stack frame 8 indicates that stack frame 8 is not the bottom of the call stack, and the return address contained in stack frame 8 belongs to the return address set, it means that the function corresponding to the return address is a boundary function (i.e., boundary function 3). Based on the label corresponding to the return address, identify the stack frame type of stack frame 7 and stack frame 6. Specifically, at this time, it can be identified that the type of stack frame 7 is the second type (which can be indicated by a specific character in the label corresponding to the return address), and the stack frame type of stack frame 6 is high-level language stack frame 2 (i.e., high-level language stack frame 2 matches the high-level programming language corresponding to high-level language function 2), and the high-level programming language used to write high-level language function 2 is different from the high-level programming language used to write runtime function 3.
[0177] Obtain the return address and frame pointer of the calling function pointing to high-level language function 2, i.e., the return address and frame pointer contained in stack frame 6. Since the frame pointer contained in stack frame 6 indicates that stack frame 6 is not the bottom of the call stack, and the return address in stack frame 6 belongs to the return address set, it means that the function pointed to by the return address is a boundary function (i.e., boundary function 2). Based on the label corresponding to the return address, identify the stack frame type of stack frame 5 and stack frame 4. Specifically, at this time, it can be identified that the type of stack frame 5 is the second type, and the stack frame type of stack frame 4 is high-level language stack 1 (i.e., high-level language stack frame 1 matches the high-level programming language corresponding to high-level language function 1), and the high-level programming language used to write high-level language function 1 is different from the high-level programming language used to write high-level language function 2.
[0178] Obtain the return address and frame pointer of the calling function that points to high-level language function 1, i.e., the return address and frame pointer contained in stack frame 4. Since the frame pointer contained in stack frame 4 indicates that stack frame 4 is not the bottom of the call stack, and the return address in stack frame 4 belongs to the return address set, it means that the function pointed to by the return address is a boundary function (i.e., boundary function 1). Based on the label corresponding to the return address, identify the stack frame type of stack frame 3 and stack frame 2. Specifically, at this time, it can be identified that the type of stack frame 3 is the second type, the stack frame type of stack frame 2 is a runtime stack frame, and the high-level programming language used to write runtime function 2 is different from the high-level programming language used to write high-level language function 1.
[0179] Obtain the return address and frame pointer of the calling function that points to runtime function 2, i.e., the return address and frame pointer contained in stack frame 2. Since the frame pointer in stack frame 2 indicates that stack frame 2 is not the bottom of the call stack, and the return address in stack frame 2 does not belong to the return address set, it means that the function pointed to by this return address is not a boundary function. This indicates that the high-level programming language used to write runtime function 1 is the same as the high-level programming language used to write runtime function 2. Set the stack frame type of stack frame 1 to the same stack frame type as stack frame 2, i.e., a runtime stack frame.
[0180] Obtain the return address and frame pointer of the calling function that points to runtime function 1, i.e., the return address and frame pointer contained in stack frame 1. Since the frame pointer contained in stack frame 1 indicates that stack frame 1 is the bottom of the call stack, the stack backtracking process of the call stack ends.
[0181] Please see Figure 6 , Figure 6 This is a flowchart illustrating a stack backtracking method according to an embodiment of this application. The call stack includes a fourth stack frame corresponding to the fourth function. The stack frame type of the fourth stack frame is a first type, and the stack frame type of the fourth stack frame is related to the type of high-level programming language corresponding to the fourth function. For example... Figure 6 As shown, the stack backtracking method 600 includes:
[0182] Obtain the second return address in the fourth stack frame, where the second return address points to the fifth function, which is the function that calls the fourth function. When the second return address belongs to the return address set, set the stack frame type of the fifth stack frame to the second type. Each return address in the return address set points to a boundary function, and the calling function and the called function of the boundary function correspond to different types of high-level programming languages. The fifth stack frame is the stack frame corresponding to the fifth function, and the second type indicates that the fifth function is the boundary function. Also, set the stack frame type of the sixth stack frame to the third type. The sixth stack frame is the stack frame corresponding to the sixth function, which is the function that calls the fifth function. The stack frame type of the sixth stack frame is different from that of the fourth stack frame. When the second return address does not belong to the return address set, set the stack frame type of the fifth stack frame to the first type, where the fifth stack frame is the stack frame corresponding to the fifth function.
[0183] In one feasible implementation, the second type and the third type are two stack frame types indicated by the label corresponding to the second return address; the label corresponding to each return address indicates the stack frame type of the stack frame corresponding to the function pointed to by each return address, and indicates the stack frame type of the stack frame corresponding to the caller function of the function pointed to by each return address.
[0184] In one feasible implementation, the frame pointer FP in the fourth stack frame is obtained; if the FP points to the bottom of the call stack, the stack backtracking process is terminated.
[0185] In one feasible implementation, when the fourth stack frame is the stack frame at the top of the call stack, the first type is related to the type of high-level programming language of the runtime library.
[0186] In one feasible implementation, the call stack is created during the execution of a first program by calling the runtime library. The first program includes a first function. The call stack includes a first stack frame corresponding to the first function, a second stack frame corresponding to a second function, and a third stack frame corresponding to a third function. The second function is a function added during the creation of the call stack. There is a call relationship between the first function and the second function, and a call relationship between the second function and the third function. The third function is either a function in the first program or a function in the runtime library, and the high-level programming language corresponding to the first function is different from the high-level programming language corresponding to the third function. A first return address and a label corresponding to the first return address are stored. The first return address is the return address contained in the stack frame corresponding to the function executed later among the first function and the third function. The label corresponding to the first return address indicates the stack frame type of the first stack frame, the stack frame type of the second stack frame, and the stack frame type of the third stack frame. The first return address belongs to the set of return addresses.
[0187] In one feasible implementation, the first function calls the second function, the second function calls the third function, and the first return address is the return address contained in the third stack frame; or, the third function calls the second function, the second function calls the first function, and the first return address is the return address in the first stack frame.
[0188] In one feasible implementation, the second function is generated by a compiler or by hand-written assembly and stored in a compiled high-level language code library or a compiled runtime code library.
[0189] Specifically, the detailed process of method 600 is described in the foregoing embodiments and will not be repeated here.
[0190] Please see Figure 7 , Figure 7 This is a schematic diagram of the structure of a program call stack creation device according to an embodiment of this application. Figure 7 As shown, the device 700 includes:
[0191] The acquisition unit 710 is used to acquire a first program, which includes a first function; the processing unit 720 is used to add a second function and create a call stack during the process of running the first program by calling the runtime library, wherein the call stack includes a first stack frame corresponding to the first function, a second stack frame corresponding to the second function, and a third stack frame corresponding to the third function, wherein there is a call relationship between the first function and the second function, and there is a call relationship between the second function and the third function, wherein the third function is a function in the runtime library or a function in the first program, and the high-level programming language corresponding to the first function and the high-level programming language corresponding to the third function are different; the storage unit 730 is used to store a first return address and a tag corresponding to the first return address, wherein the first return address is the return address contained in the stack frame corresponding to the function executed later among the first function and the third function, and the tag corresponding to the first return address indicates the stack frame type of the first stack frame, the stack frame type of the second stack frame, and the stack frame type of the third stack frame.
[0192] In one feasible implementation, the first function calls the second function, the second function calls the third function, and the first return address is the return address contained in the third stack frame; or, the third function calls the second function, the second function calls the first function, and the first return address is the return address contained in the first stack frame.
[0193] In one feasible implementation, the second function is generated by a compiler or by hand-written assembly and stored in a compiled high-level language code library or a compiled runtime code library.
[0194] In one feasible implementation, the first return address is stored in a set of return addresses, where each return address points to a function that is a boundary function. The calling function of the boundary function and the called function of the boundary function correspond to different types of high-level programming languages. The label corresponding to each return address indicates the stack frame type of the stack frame corresponding to the function pointed to by each return address, as well as the stack frame type of the stack frame corresponding to the calling function of the function pointed to by each return address.
[0195] In one feasible implementation, the call stack includes a fourth stack frame corresponding to the fourth function. The stack frame type of the fourth stack frame is a first type, and the stack frame type of the fourth stack frame is related to the type of high-level programming language corresponding to the fourth function. The processing unit 720 is further configured to perform stack backtracking on the fourth stack frame. During the stack backtracking process, the processing unit 720 is specifically configured to: instruct the acquisition unit 710 to acquire the second return address in the fourth stack frame, the second return address pointing to the fifth function, the fifth function being the function that calls the fourth function; and when the second return address belongs to the return function... When the address set is selected, the stack frame type of the fifth stack frame is set to the second type, where the fifth stack frame is the stack frame corresponding to the fifth function, and the second type indicates that the fifth function is the boundary function. The stack frame type of the sixth stack frame is set to the third type, where the sixth stack frame is the stack frame corresponding to the sixth function, which is the function that calls the fifth function, and the stack frame type of the sixth stack frame is different from the stack frame type of the fourth stack frame. When the return address in the fourth stack frame does not belong to the return address set, the stack frame type of the fifth stack frame is set to the first type, where the fifth stack frame is the stack frame corresponding to the fifth function.
[0196] In one possible implementation, the second type and the third type are two stack frame types indicated by the label corresponding to the second return address.
[0197] In one feasible implementation, the acquisition unit 710 is further configured to acquire the frame pointer FP in the fourth stack frame; the processing unit 720 is further configured to terminate the stack backtracking process of the fourth stack frame when the FP points to the bottom of the call stack.
[0198] In one possible implementation, when the fourth stack frame is the stack frame at the top of the call stack, the first type is related to the type of high-level programming language of the runtime library.
[0199] Specifically, the specific execution process of the aforementioned device 700 can be found in the description corresponding to the aforementioned method 300, and will not be repeated here.
[0200] Please see Figure 8 , Figure 8 This is a schematic diagram of a stack backtracking device according to an embodiment of this application. The call stack includes a fourth stack frame corresponding to a fourth function. The stack frame type of the fourth stack frame is a first type, and the stack frame type of the fourth stack frame is related to the type of high-level programming language corresponding to the fourth function. For example... Figure 8 As shown, the device 800 includes:
[0201] The acquisition unit 810 is used to acquire the second return address in the fourth stack frame, the second return address pointing to the fifth function, the fifth function being the function that calls the fourth function. The processing unit 820 is used to, when the second return address belongs to the return address set, set the stack frame type of the fifth stack frame to a second type, where each return address in the return address set points to a boundary function, the calling function of the boundary function and the called function of the boundary function correspond to different types of high-level programming languages, the fifth stack frame is the stack frame corresponding to the fifth function, the second type indicates that the fifth function is the boundary function, and set the stack frame type of the sixth stack frame to a third type, the sixth stack frame is the stack frame corresponding to the sixth function, the sixth function is the function that calls the fifth function, and the stack frame type of the sixth stack frame is different from the stack frame type of the fourth stack frame; or when the second return address does not belong to the return address set, the processing unit sets the stack frame type of the fifth stack frame to the first type, the fifth stack frame being the stack frame corresponding to the fifth function.
[0202] In one feasible implementation, the second type and the third type are two stack frame types indicated by the label corresponding to the second return address; the label corresponding to each return address indicates the stack frame type of the stack frame corresponding to the function pointed to by each return address, and indicates the stack frame type of the stack frame corresponding to the caller function of the function pointed to by each return address.
[0203] In one feasible implementation, the acquisition unit 810 is further configured to acquire the frame pointer FP in the fourth stack frame; the processing unit is further configured to terminate the stack backtracking process of the fourth stack frame when the FP points to the bottom of the call stack.
[0204] In one feasible implementation, when the fourth stack frame is the stack frame at the top of the call stack, the first type is related to the type of high-level programming language of the runtime library.
[0205] In one feasible implementation, the call stack is created during the execution of a first program by calling the runtime library. The first program includes a first function. The call stack includes a first stack frame corresponding to the first function, a second stack frame corresponding to a second function, and a third stack frame corresponding to a third function. The second function is a function added during the creation of the call stack. There is a call relationship between the first function and the second function, and a call relationship between the second function and the third function. The third function is either a function in the first program or a function in the runtime library, and the high-level programming language corresponding to the first function is different from the high-level programming language corresponding to the third function. The device further includes a storage unit 830, which is used to: store a first return address and a tag corresponding to the first return address. The first return address is the return address contained in the stack frame corresponding to the function executed later among the first function and the third function. The tag corresponding to the first return address indicates the stack frame type of the first stack frame, the stack frame type of the second stack frame, and the stack frame type of the third stack frame. The first return address belongs to the set of return addresses.
[0206] In one feasible implementation, the first function calls the second function, the second function calls the third function, and the first return address is the return address contained in the third stack frame; or, the third function calls the second function, the second function calls the first function, and the first return address is the return address in the first stack frame.
[0207] In one feasible implementation, the second function is generated by a compiler or by hand-written assembly and stored in a compiled high-level language code library or a compiled runtime code library.
[0208] Specifically, the specific execution process of the aforementioned device 800 can be found in the description corresponding to the aforementioned method 600, and will not be repeated here.
[0209] Please see Figure 9 , Figure 9 This is a schematic diagram of the hardware structure of a computer device according to an embodiment of this application. Figure 9 As shown, device 900 includes processor 901, memory 902, interface circuit 903, and bus 904. The processor 901, memory 902, and interface circuit 903 transmit data via bus 904.
[0210] Processor 901 is used to acquire a first program, which includes a first function; during the execution of the first program by calling the runtime library, a second function is added and a call stack is created, wherein the call stack includes a first stack frame corresponding to the first function, a second stack frame corresponding to the second function, and a third stack frame corresponding to the third function, wherein there is a call relationship between the first function and the second function, and a call relationship between the second function and the third function, wherein the third function is a function in the runtime library or a function in the first program, and the high-level programming language corresponding to the first function and the high-level programming language corresponding to the third function are different. Memory 902 is used to store a first return address and a tag corresponding to the first return address, wherein the first return address is the return address contained in the stack frame corresponding to the later-executed function among the first function and the third function, and the tag corresponding to the first return address indicates the stack frame type of the first stack frame, the stack frame type of the second stack frame, and the stack frame type of the third stack frame.
[0211] The processor 901 is further configured to perform stack backtracking on the fourth stack frame, including: obtaining a second return address in the fourth stack frame, the second return address pointing to a fifth function, the fifth function being a function that calls the fourth function; when the second return address belongs to the set of return addresses, setting the stack frame type of the fifth stack frame to a second type, the fifth stack frame being the stack frame corresponding to the fifth function, the second type indicating that the fifth function is the boundary function; and setting the stack frame type of the sixth stack frame to a third type, the sixth stack frame being the stack frame corresponding to the sixth function, the sixth function being a function that calls the fifth function, the stack frame type of the sixth stack frame being different from the stack frame type of the fourth stack frame; when the return address in the fourth stack frame does not belong to the set of return addresses, setting the stack frame type of the fifth stack frame to the first type, the fifth stack frame being the stack frame corresponding to the fifth function.
[0212] Specifically, the specific operation process of the processor and memory on the computer device 900 in this embodiment can be found in the corresponding processes in the aforementioned method embodiments 300 and 600, and will not be repeated here.
[0213] This application provides a computer-readable storage medium storing program instructions that, when executed on one or more processors, implement the above-described... Figure 3 and / or Figure 6 Some or all of the steps in the method embodiments.
[0214] This application provides a computer program product that, when run on a computer device, performs the above-mentioned... Figure 3 and / or Figure 6 Some or all of the steps in the method are implemented.
[0215] In the above embodiments, the descriptions of each embodiment have their own emphasis. Parts not described in detail in a particular embodiment can be found in the relevant descriptions of other embodiments. It should be noted that, for the sake of simplicity, the foregoing method embodiments are all described as a series of actions. However, those skilled in the art should understand that this application is not limited to the described order of actions, as some steps may be performed in other orders or simultaneously according to this application. Furthermore, those skilled in the art should also understand that the embodiments described in the specification are preferred embodiments, and the actions and modules involved are not necessarily essential to this application.
[0216] In the several embodiments provided in this application, it should be understood that the disclosed apparatus can be implemented in other ways. For example, the apparatus embodiments described above are merely illustrative; for instance, the division of the units described above is only a logical functional division, and in actual implementation, there may be other division methods. For example, multiple units or components may be combined or integrated into another system, or some features may be ignored or not executed. Furthermore, the coupling or direct coupling or communication connection shown or discussed may be through some interfaces; the indirect coupling or communication connection between devices or units may be electrical or other forms.
[0217] The units described above as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the units can be selected to achieve the purpose of this embodiment according to actual needs.
[0218] The above-described embodiments are only used to illustrate the technical solutions of this application, and are not intended to limit them. Although this application has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some of the technical features. Such modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of this application.
Claims
1. A method for creating a program call stack, characterized in that, The method includes: Obtain a first program, which includes a first function; During the process of calling the runtime library to run the first program, a second function is added and a call stack is created. The call stack includes a first stack frame corresponding to the first function, a second stack frame corresponding to the second function, and a third stack frame corresponding to the third function. There is a call relationship between the first function and the second function, and there is a call relationship between the second function and the third function. The third function is a function in the runtime library or a function in the first program. The high-level programming language corresponding to the first function is different from the high-level programming language corresponding to the third function. Save the first return address and the label corresponding to the first return address. The first return address is the return address contained in the stack frame of the function that is executed later among the first function and the third function. The label corresponding to the first return address indicates the stack frame type of the first stack frame, the stack frame type of the second stack frame, and the stack frame type of the third stack frame.
2. The method according to claim 1, characterized in that, The first function calls the second function, the second function calls the third function, and the first return address is the return address contained in the third stack frame. or, The third function calls the second function, the second function calls the first function, and the first return address is the return address contained in the first stack frame.
3. The method according to claim 1 or 2, characterized in that, The second function is generated by a compiler or by hand-written assembly and stored in the compiled high-level language code library or the compiled runtime code library.
4. The method according to claim 1, characterized in that, The first return address is stored in a set of return addresses. Each return address in the set points to a function that is a boundary function. The calling function of the boundary function and the called function of the boundary function correspond to different types of high-level programming languages. The label corresponding to each return address indicates the stack frame type of the stack frame corresponding to the function pointed to by each return address, and also indicates the stack frame type of the stack frame corresponding to the calling function of the function pointed to by each return address.
5. The method according to claim 4, characterized in that, The call stack includes a fourth stack frame corresponding to the fourth function. The stack frame type of the fourth stack frame is of the first type, and the stack frame type of the fourth stack frame is related to the type of high-level programming language corresponding to the fourth function. The method further includes: Performing stack backtracking on the fourth stack frame includes: Obtain the second return address in the fourth stack frame, the second return address points to the fifth function, and the fifth function is the function that calls the fourth function; When the second return address belongs to the set of return addresses, the stack frame type of the fifth stack frame is set to the second type, the fifth stack frame is the stack frame corresponding to the fifth function, the second type indicates that the fifth function is the boundary function, and the stack frame type of the sixth stack frame is set to the third type, the sixth stack frame is the stack frame corresponding to the sixth function, the sixth function is the function that calls the fifth function, and the stack frame type of the sixth stack frame is different from the stack frame type of the fourth stack frame; When the return address in the fourth stack frame does not belong to the set of return addresses, the stack frame type of the fifth stack frame is set to the first type, and the fifth stack frame is the stack frame corresponding to the fifth function.
6. The method according to claim 5, characterized in that, The second type and the third type are the two stack frame types indicated by the label corresponding to the second return address.
7. The method according to claim 5 or 6, characterized in that, The method further includes: Obtain the frame pointer FP from the fourth stack frame; If the FP points to the bottom of the call stack, the stack backtracking process of the fourth stack frame ends.
8. The method according to claim 5, characterized in that, When the fourth stack frame is the stack frame at the top of the call stack, the first type is related to the type of high-level programming language of the runtime library.
9. A method for stack backtracking of a call stack, characterized in that, The call stack includes a fourth stack frame corresponding to the fourth function. The stack frame type of the fourth stack frame is of type 1. The stack frame type of the fourth stack frame is related to the type of high-level programming language corresponding to the fourth function. The method includes: Obtain the second return address in the fourth stack frame, the second return address points to the fifth function, and the fifth function is the function that calls the fourth function; When the second return address belongs to the return address set, the stack frame type of the fifth stack frame is set to the second type. Each return address in the return address set points to a function that is a boundary function. The calling function and the called function of the boundary function correspond to different types of high-level programming languages. The fifth stack frame is the stack frame corresponding to the fifth function. The second type indicates that the fifth function is the boundary function. The stack frame type of the sixth stack frame is set to the third type. The sixth stack frame is the stack frame corresponding to the sixth function. The sixth function is the function that calls the fifth function. The stack frame type of the sixth stack frame is different from the stack frame type of the fourth stack frame. When the second return address does not belong to the set of return addresses, the stack frame type of the fifth stack frame is set to the first type, and the fifth stack frame is the stack frame corresponding to the fifth function.
10. The method according to claim 9, characterized in that, The second type and the third type are the two stack frame types indicated by the label corresponding to the second return address; The label corresponding to each return address indicates the stack frame type of the stack frame corresponding to the function pointed to by each return address, and also indicates the stack frame type of the caller function of the function pointed to by each return address.
11. The method according to claim 9 or 10, characterized in that, The method further includes: Obtain the frame pointer FP from the fourth stack frame; If the FP points to the bottom of the call stack, the stack backtracking process ends.
12. The method according to claim 9, characterized in that, When the fourth stack frame is the stack frame at the top of the call stack, the first type is related to the type of high-level programming language of the runtime library.
13. The method according to claim 9, characterized in that, The call stack is created during the execution of the first program by calling the runtime library. The first program includes a first function. The call stack includes a first stack frame corresponding to the first function, a second stack frame corresponding to the second function, and a third stack frame corresponding to the third function. The second function is a function added during the creation of the call stack. There is a call relationship between the first function and the second function, and there is a call relationship between the second function and the third function. The third function is a function in the first program or a function in the runtime library, and the high-level programming language corresponding to the first function is different from the high-level programming language corresponding to the third function. Save the first return address and the tag corresponding to the first return address. The first return address is the return address contained in the stack frame of the function that is executed later in the first function and the third function. The tag corresponding to the first return address indicates the stack frame type of the first stack frame, the stack frame type of the second stack frame and the stack frame type of the third stack frame. The first return address belongs to the set of return addresses.
14. The method according to claim 13, characterized in that, The first function calls the second function, the second function calls the third function, and the first return address is the return address contained in the third stack frame. or, The third function calls the second function, the second function calls the first function, and the first return address is the return address in the first stack frame.
15. The method according to claim 13 or 14, characterized in that, The second function is generated by a compiler or by hand-written assembly and stored in the compiled high-level language code library or the compiled runtime code library.
16. A program call stack creation device, characterized in that, The device includes: An acquisition unit is used to acquire a first program, wherein the first program includes a first function; The processing unit is configured to add a second function and create a call stack during the process of running the first program by calling the runtime library. The call stack includes a first stack frame corresponding to the first function, a second stack frame corresponding to the second function, and a third stack frame corresponding to the third function. There is a call relationship between the first function and the second function, and a call relationship between the second function and the third function. The third function is a function in the runtime library or a function in the first program. The high-level programming language corresponding to the first function is different from the high-level programming language corresponding to the third function. The storage unit is used to store a first return address and a tag corresponding to the first return address. The first return address is the return address contained in the stack frame of the function that is executed later among the first function and the third function. The tag corresponding to the first return address indicates the stack frame type of the first stack frame, the stack frame type of the second stack frame, and the stack frame type of the third stack frame.
17. The apparatus according to claim 16, characterized in that, The first function calls the second function, the second function calls the third function, and the first return address is the return address contained in the third stack frame. or, The third function calls the second function, the second function calls the first function, and the first return address is the return address contained in the first stack frame.
18. The apparatus according to claim 16 or 17, characterized in that, The second function is generated by a compiler or by hand-written assembly and stored in the compiled high-level language code library or the compiled runtime code library.
19. The apparatus according to claim 16, characterized in that, The first return address is stored in a set of return addresses. Each return address in the set points to a function that is a boundary function. The calling function of the boundary function and the called function of the boundary function correspond to different types of high-level programming languages. The label corresponding to each return address indicates the stack frame type of the stack frame corresponding to the function pointed to by each return address, and also indicates the stack frame type of the stack frame corresponding to the calling function of the function pointed to by each return address.
20. The apparatus according to claim 19, characterized in that, The call stack includes a fourth stack frame corresponding to the fourth function. The stack frame type of the fourth stack frame is of the first type, and the stack frame type of the fourth stack frame is related to the type of high-level programming language corresponding to the fourth function. The processing unit is also used to perform stack backtracking on the fourth stack frame; During the stack backtracking process, the processing unit is specifically used for: The acquisition unit is instructed to acquire the second return address in the fourth stack frame, the second return address pointing to the fifth function, the fifth function being the function that calls the fourth function; as well as When the second return address belongs to the set of return addresses, the stack frame type of the fifth stack frame is set to the second type, the fifth stack frame is the stack frame corresponding to the fifth function, the second type indicates that the fifth function is the boundary function, and the stack frame type of the sixth stack frame is set to the third type, the sixth stack frame is the stack frame corresponding to the sixth function, the sixth function is the function that calls the fifth function, and the stack frame type of the sixth stack frame is different from the stack frame type of the fourth stack frame; When the return address in the fourth stack frame does not belong to the set of return addresses, the stack frame type of the fifth stack frame is set to the first type, and the fifth stack frame is the stack frame corresponding to the fifth function.
21. The apparatus according to claim 20, characterized in that, The second type and the third type are the two stack frame types indicated by the label corresponding to the second return address.
22. The apparatus according to claim 20 or 21, characterized in that, The acquisition unit is also used to acquire the frame pointer FP in the fourth stack frame; The processing unit is further configured to terminate the stack backtracking process of the fourth stack frame when the FP points to the bottom of the call stack.
23. The apparatus according to claim 20, characterized in that, When the fourth stack frame is the stack frame at the top of the call stack, the first type is related to the type of high-level programming language of the runtime library.
24. A stack backtracking device, characterized in that, The call stack includes a fourth stack frame corresponding to the fourth function. The stack frame type of the fourth stack frame is of the first type, and the stack frame type of the fourth stack frame is related to the type of high-level programming language corresponding to the fourth function. The device includes: The acquisition unit is used to acquire the second return address in the fourth stack frame, the second return address pointing to the fifth function, the fifth function being the function that calls the fourth function; The processing unit is configured to, when the second return address belongs to the return address set, set the stack frame type of the fifth stack frame to a second type, wherein each return address in the return address set points to a boundary function, the calling function of the boundary function and the called function of the boundary function correspond to different types of high-level programming languages, the fifth stack frame is the stack frame corresponding to the fifth function, the second type indicates that the fifth function is the boundary function, and set the stack frame type of the sixth stack frame to a third type, wherein the sixth stack frame is the stack frame corresponding to the sixth function, the sixth function is the function that calls the fifth function, and the stack frame type of the sixth stack frame is different from the stack frame type of the fourth stack frame; or When the second return address does not belong to the set of return addresses, the processing unit sets the stack frame type of the fifth stack frame to the first type, and the fifth stack frame is the stack frame corresponding to the fifth function.
25. The apparatus according to claim 24, characterized in that, The second type and the third type are the two stack frame types indicated by the label corresponding to the second return address; The label corresponding to each return address indicates the stack frame type of the stack frame corresponding to the function pointed to by each return address, and also indicates the stack frame type of the caller function of the function pointed to by each return address.
26. The apparatus according to claim 24 or 25, characterized in that, The acquisition unit is also used to acquire the frame pointer FP in the fourth stack frame; The processing unit is further configured to terminate the stack backtracking process of the fourth stack frame when the FP points to the bottom of the call stack.
27. The apparatus according to claim 24, characterized in that, When the fourth stack frame is the stack frame at the top of the call stack, the first type is related to the type of high-level programming language of the runtime library.
28. The apparatus according to claim 24, characterized in that, The call stack is created during the execution of the first program by calling the runtime library. The first program includes a first function. The call stack includes a first stack frame corresponding to the first function, a second stack frame corresponding to the second function, and a third stack frame corresponding to the third function. The second function is a function added during the creation of the call stack. There is a call relationship between the first function and the second function, and there is a call relationship between the second function and the third function. The third function is a function in the first program or a function in the runtime library, and the high-level programming language corresponding to the first function is different from the high-level programming language corresponding to the third function. The device further includes a storage unit, the storage unit being used for: Save the first return address and the tag corresponding to the first return address. The first return address is the return address contained in the stack frame of the function that is executed later in the first function and the third function. The tag corresponding to the first return address indicates the stack frame type of the first stack frame, the stack frame type of the second stack frame and the stack frame type of the third stack frame. The first return address belongs to the set of return addresses.
29. The apparatus according to claim 28, characterized in that, The first function calls the second function, the second function calls the third function, and the first return address is the return address contained in the third stack frame. or, The third function calls the second function, the second function calls the first function, and the first return address is the return address in the first stack frame.
30. The apparatus according to claim 28 or 29, characterized in that, The second function is generated by a compiler or by hand-written assembly and stored in the compiled high-level language code library or the compiled runtime code library.
31. A chip system, characterized in that, The chip system includes at least one processor, a memory, and an interface circuit. The memory, the interface circuit, and the at least one processor are interconnected via circuits. The at least one memory stores instructions. When the instructions are executed by the processor, the method described in any one of claims 1-15 is implemented.
32. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores program instructions that, when executed on one or more processors, implement the method of any one of claims 1-15.
33. A computer program product, characterized in that, When the computer program product is run on a computer device, the method of any one of claims 1-15 is implemented.