A method and apparatus for recording a function call order
By creating a global variable array in the embedded system to record the function call order, the problem of inaccurate recording of function call order in existing technologies is solved, achieving complete recording of historical call order and improving the efficiency of software logic analysis and problem troubleshooting.
Patent Information
- Application Number
- CN202411861824.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-17
- Publication Date
- 2026-06-19
Smart Images

Figure CN122240363A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of computer technology, specifically to a method and apparatus for recording the order of function calls. Background Technology
[0002] In the design and development of embedded systems, accurately recording the order of function calls will help with software logic analysis and troubleshooting.
[0003] Typically, analyzing the function call order during software runtime relies on the contents of the function stack, which only contains information about the currently running functions. Therefore, analyzing the function call order through the stack cannot obtain information about the call order of functions that have already finished running, leading to an inability to accurately determine the actual running status of the software. Summary of the Invention
[0004] This application provides a method and apparatus for recording function call order, capable of recording historical function call order information. The technical solution is as follows.
[0005] Firstly, a method for recording the order of function calls is provided, the method comprising:
[0006] Create a global variable array, which is used to record the function call order;
[0007] In response to a call instruction for a first function, the code block of the first function is executed, the code block of the first function includes a first identifier recording function, and the code block of the first identifier recording function includes the identifier of the first function;
[0008] The code block that executes the first identifier recording function passes the identifier of the first function to the order recording function;
[0009] Run the sequence recording function to add the identifier of the first function to the first position of the global variable array.
[0010] In some implementations, adding the identifier of the first function to the first position of the global variable array includes:
[0011] Record the index of the data currently in use in the global variable array;
[0012] In response to the index pointing to the first position, the identifier of the first function is added to the first position of the global variable array.
[0013] In some implementations, after adding the identifier of the first function to the first position of the global variable array, the method further includes:
[0014] If the first position is the end of the global variable array, the index is set to the beginning position in the global variable array;
[0015] If the first position is not the end of the global variable array, the index is set to the second position in the global variable array, where the second position is the position following the first position.
[0016] In some implementations, after adding the identifier of the first function to the first position of the global variable array, the method further includes:
[0017] In response to a call instruction for a second function, the code block of the second function is executed, the code block of the second function includes a second identifier recording function, and the code block of the second identifier recording function includes the identifier of the second function;
[0018] The code block that executes the second identifier recording function passes the identifier of the second function to the order recording function;
[0019] The sequence recording function is executed. If the first position is the end of the global variable array, the identifier of the second function is added to the beginning of the global variable array. If the first position is not the end of the global variable array, the identifier of the second function is added to the second position of the global variable array, where the second position is the position following the first position.
[0020] In some implementations, after adding the identifier of the first function to the global variable array, the method further includes:
[0021] Set the data at the second position in the global variable array as the target identifier. The target identifier is used to indicate the identifier of the most recently called function in the global variable array. The second position is the position after the first position.
[0022] In some implementations, the first identifier recording function is located at the beginning line of the code block of the first function.
[0023] In some implementations, before the code block that executes the first function, the method further includes:
[0024] During the execution of the script tool, a corresponding identifier record function is added to the beginning of the code block of each function in the source files that are being compiled.
[0025] Secondly, a device for recording the order of function calls is provided, the device comprising:
[0026] A creation unit is used to create a global variable array, which is used to record the function call order;
[0027] The execution unit is configured to, in response to a call instruction for a first function, execute the code block of the first function, wherein the code block of the first function includes a first identifier recording function, and the code block of the first identifier recording function includes the identifier of the first function; execute the code block of the first identifier recording function, and pass the identifier of the first function to a sequence recording function; execute the sequence recording function, and add the identifier of the first function to the first position of the global variable array.
[0028] In some implementations, the execution unit is configured to record the index of the data currently in use in the global variable array; in response to the index pointing to the first position, to add the identifier of the first function to the first position of the global variable array.
[0029] In some embodiments, the apparatus further includes:
[0030] The setting unit is configured to, if the first position is the end of the global variable array, set the index to the beginning position in the global variable array; if the first position is not the end of the global variable array, set the index to the second position in the global variable array, where the second position is the position following the first position.
[0031] In some embodiments, the execution unit is further configured to, in response to a call instruction for the second function, execute a code block of the second function, the code block of the second function including a second identifier recording function, the code block of the second identifier recording function including an identifier of the second function; execute the code block of the second identifier recording function, passing the identifier of the second function to a sequence recording function; execute the sequence recording function, if the first position is the end of the global variable array, adding the identifier of the second function to the beginning of the global variable array, and if the first position is not the end of the global variable array, adding the identifier of the second function to a second position of the global variable array, the second position being the position following the first position.
[0032] In some embodiments, the apparatus further includes:
[0033] The setting unit is used to set the data located at the second position in the global variable array as a target identifier. The target identifier is used to indicate the identifier of the most recently called function in the global variable array, and the second position is the position after the first position.
[0034] In some implementations, the first identifier recording function is located at the beginning line of the code block of the first function.
[0035] In some implementations, the execution unit is used to add a corresponding identifier record function to the beginning position of the code block of each function in the source file that is being compiled during the execution of the script tool.
[0036] Thirdly, a computer device is provided, comprising a processor coupled to a memory storing at least one computer program instruction, the at least one computer program instruction being loaded and executed by the processor to enable the computer device to implement the method provided by the first aspect or any alternative embodiment of the first aspect. Specific details of the computer device provided in the third aspect can be found in the first aspect or any alternative embodiment of the first aspect, and will not be repeated here.
[0037] Fourthly, a computer-readable storage medium is provided, which stores at least one instruction that, when executed on a computer, causes the computer to perform the method provided in the first aspect or any alternative method of the first aspect.
[0038] Fifthly, a computer program product is provided, the computer program product comprising one or more computer program instructions, which, when loaded and run by a computer, cause the computer to perform the method provided in the first aspect or any alternative method of the first aspect.
[0039] The method provided in this application creates a global variable array to record the function call order. When the first function is called, its code block is executed. This code block includes a first identifier recording function, which contains the identifier of the first function. The code block of the first identifier recording function is executed, and the identifier of the first function is passed to the order recording function. The order recording function is then executed, adding the identifier of the first function to the first position of the global variable array. Based on this, the identifier of the called first function is recorded in the global variable array, thus realizing the function call order recording function and avoiding the inability to obtain information about functions that have finished running by relying on the function stack. This will be beneficial for software logic analysis and problem troubleshooting. Attached Figure Description
[0040] Figure 1 This is a flowchart illustrating a method for recording the function call order provided in an embodiment of this application;
[0041] Figure 2This is a schematic diagram of the structure of a function call sequence recording device provided in an embodiment of this application;
[0042] Figure 3 This is a schematic diagram of the structure of a computer device provided in an embodiment of this application. Detailed Implementation
[0043] To make the objectives, technical solutions, and advantages of this application clearer, the embodiments of this application will be described in further detail below with reference to the accompanying drawings.
[0044] Appendix Figure 1 This is a flowchart illustrating a method for recording the function call order provided in an embodiment of this application. (Attached) Figure 1 The method shown can optionally be applied to embedded systems. (See attached...) Figure 1 The method shown enables the recording of historical function call order information. The function call order, for example, is the sequence in which different functions are called. (Appendix) Figure 1 The method shown involves multiple function calls. To distinguish between the different functions, "Function A (also called the first function)" and "Function B (the second function)" are used to describe them. (Appendix) Figure 1 The method shown includes the following steps S110 to S140.
[0045] Step S110: Create a global variable array, which is used to record the order of function calls.
[0046] For example, in an embedded system, a global variable array `unsignedint Function_IDs[RECORD_LENGTH]` can be added to record the order of function calls. The dimension `RECORD_LENGTH` of the array can be determined according to the recording requirements. The `RECORD_LENGTH` dimension can record the order of the most recent (`RECORD_LENGTH-1`) function calls, and all initial values of this array are set to 0.
[0047] Step S120: In response to the call instruction for function A, the code block of function A is executed. The code block of function A includes the ID recording function 1.
[0048] The ID recording function for function A is used to pass the identifier of function A to the order recording function when function A is called. The code block of ID recording function 1 includes the identifier of function A. The identifier (ID) of function A is used to identify function A. The identifier of function A is, for example, a number. As an example, the identifier of function A is 1. In some implementations, the function identifiers in the code blocks of different ID recording functions are different. For example, the code block of function B includes ID recording function 2, and the code block of ID recording function 2 includes the identifier of function B, which is 2.
[0049] In some implementations, the ID recording function 1 is located at the beginning line of the code block of function A. For example, a function ID recording function is added at the beginning of each function in the source file being compiled. However, if the ID recording function 1 is added in the middle or at the end of the code block of function A, it's possible that during the execution of function A, the code block might terminate due to a bug before the ID recording function 1 is reached. In this case, the ID recording function 1 corresponding to function A wouldn't be executed, and therefore the identifier of function A wouldn't be recorded in the global variable array. This results in the problem that although function A is called, its ID is missed. Therefore, by adding the ID recording function 1 at the beginning line of the code block of function A, if there is a problem in the code block of function A, the ID recording function 1 will be executed at the beginning of the code block of function A, thus recording the identifier of function A. In other words, when function A is called, the action of recording the identifier of function A is performed first, and then the original function of function A is executed. This reduces the risk of missing the ID recording due to a bug in function A and facilitates troubleshooting.
[0050] For example, the ID record function 1 is located in the code block of function A, on the first line after the definition of function A: void Function_A(void){.
[0051] The starting line of the code block for each function in the source file being compiled includes the ID record function. Taking C language function definition as an example, for instance, an embedded system contains three functions: function A, function B, and function C. The code blocks for these three functions are shown below.
[0052]
[0053] Then, three ID recording functions are added: ID recording function 1 (referred to as RecordFunction_1 in the following code) for recording the ID of function A when function A is called, ID recording function 2 (referred to as RecordFunction_2 in the following code) for recording the ID of function B when function B is called, and ID recording function 3 (referred to as RecordFunction_3 in the following code) for recording the ID of function C when function C is called. The code blocks for ID recording functions 1, 2, and 3 are shown below.
[0054]
[0055] Then, add the new ID record function to the code blocks of functions A, B, and C respectively.
[0056]
[0057]
[0058] Step S130: Run the code block of ID recording function 1 and pass the identifier of function A to the sequence recording function.
[0059] For example, each ID recording function passes a unique ID to the sequence recording function.
[0060] Step S140: Run the sequence recording function and add the identifier of function A to position A of the global variable array.
[0061] In some implementations, the index of the data currently in use in the global variable array is recorded. This index identifies which position in the global variable array is currently in use, and the identifier of the called function A is added to the position pointed to by the index. For example, in response to the index pointing to position A, the identifier of function A is added to position A in the global variable array.
[0062] In some implementations, after adding the identifier of function A to position A of the global variable array, the method further includes: if position A is the end of the global variable array, setting the index to the beginning position in the global variable array; if position A is not the end of the global variable array, setting the index to position B in the global variable array, where position B is the next position after position A.
[0063] The sequence record function has one parameter: the identifier of the called function. The ID record function is responsible for passing the function identifier to the sequence record function. The C code for the sequence record function is shown below. `Current_Index` is a specific example of the index of the currently used data in the global variable array.
[0064]
[0065] After adding the above code snippet, it is possible to record the order of the most recent (RECORD_LENGTH-1) function calls in the Function_IDs[] array during software operation.
[0066] In some implementations, the identifiers of called functions are recorded in a circular manner in the global variable array. When the function identifier has been recorded to the end of the global variable array, the recording starts again from the beginning of the global variable array. For example, after adding the identifier of function A to position A of the global variable array, in response to a call instruction for function B, the code block of function B is executed. The code block of function B includes ID recording function 2, and the code block of ID recording function 2 includes the identifier of function B. The code block of ID recording function 2 is executed, and the identifier of function B is passed to the sequence recording function. The sequence recording function is executed. If position A is at the end of the global variable array, it indicates that the global variable array has been recorded once, so the identifier of function B is added to the beginning of the global variable array. If position A is not at the end of the global variable array, the identifier of function B is added to position B of the global variable array, which is the next position after position A.
[0067] As a concrete example, the global variable array has three dimensions. Initially, the global variable array is empty, and the index (Current_Index) of the currently used data is 0. When the first function is called, the identifier of the first called function is stored in dimension 0 (position 1) of the global variable array. Then, when the second function is called, the identifier of the second called function is stored in dimension 1 (position 2). Similarly, when the third function is called, the identifier of the third called function is stored in dimension 2 (position 3). Since the global variable array has already completed one cycle, when another function is called and its identifier is added to the global variable array, the identifier of the fourth called function is again stored in dimension 0 (position 1).
[0068] Considering the potentially large number of function calls, unrestricted recording of function call identifiers would consume excessive memory. Therefore, a circular recording method is used to record the order of most recent function calls while minimizing memory usage for recording the function call order itself.
[0069] In some implementations, after adding the identifier of function A to the global variable array, the data at position B in the global variable array is set as the target identifier. Position B is the next position after position A. The target identifier occupies one position in the global variable array. The target identifier is used to indicate the identifier of the most recently called function in the global variable array. For example, the function identifier to the left of the target identifier in the global variable array is the identifier of the most recently called function (i.e., the identifier of the latest called function). When it is necessary to know the function call order, the target identifier is determined from the data recorded in the global variable array, and the function identifier to the left of the target identifier is determined as the identifier of the most recently called function. The target identifier is, for example, a string. For example, the target identifier is 0xFFFFFFFF. For example, in the data recorded in the global variable array, the ID to the left of the value 0xFFFFFFFF is the ID of the most recently called function. Taking a 4-dimensional global variable array Function_IDs[] as an example, the data recorded in the global variable array is, for example: [2,3,0xFFFFFFFF,1]. In the above data, the function identifier to the left of 0xFFFFFFFF is 3, therefore we can know that the identifier of the most recently called function is 3.
[0070] The function call order from oldest to newest is 1->2->3. Of course, 0xFFFFFFFF can also be replaced with other pre-defined strings.
[0071] Considering the use of a circular recording method, since the recording starts from the beginning of the array again when the end of the array is reached, it is difficult to know which function identifier in the global variable array is the identifier of the most recently called function if no identifier is set to identify the most recently called function. By adding the identifier of the most recently called function A to the global variable array, and then adding a target identifier to the next position after the newly added position, the identifier of the most recently called function can be obtained by finding one position to the left of the target identifier, making the function call order more explicit.
[0072] In some implementations, during the execution of the script tool, a corresponding identifier record function is added to the beginning of the code block of each function in the source files involved in the compilation, and then the append function is executed. Figure 1 The method is illustrated. Specifically, since programming languages have standardized forms for function definitions, in addition to manually adding code segments, a script tool can be used to add an identifier to the beginning of each function in the source files involved in compilation.
[0073] The method provided in this embodiment realizes the function of recording the order of historical function calls, avoiding the inability to obtain information about functions that have finished running by relying on the function stack. This will have a beneficial effect on software logic analysis and problem troubleshooting.
[0074] Furthermore, compared to stack-based function call order recording, this method can record function execution history more completely. In practical use, the length of the global variable array can be set according to recording needs.
[0075] Figure 2 This is a schematic diagram of a function call sequence recording device 200 provided in an embodiment of this application. The device 200 includes:
[0076] Creation unit 210 is used to create a global variable array, which is used to record the function call order;
[0077] The execution unit 220 is configured to, in response to a call instruction for a first function, execute the code block of the first function, the code block of the first function including a first identifier recording function, the code block of the first identifier recording function including an identifier of the first function; execute the code block of the first identifier recording function, passing the identifier of the first function to a sequence recording function; execute the sequence recording function, adding the identifier of the first function to the first position of the global variable array.
[0078] In some implementations, the execution unit 220 is configured to record the index of the data currently in use in the global variable array; in response to the index pointing to the first position, to add the identifier of the first function to the first position of the global variable array.
[0079] In some embodiments, the device 200 further includes:
[0080] The setting unit is configured to, if the first position is the end of the global variable array, set the index to the beginning position in the global variable array; if the first position is not the end of the global variable array, set the index to the second position in the global variable array, where the second position is the position following the first position.
[0081] In some embodiments, the execution unit 220 is further configured to, in response to a call instruction for the second function, execute a code block of the second function, the code block of the second function including a second identifier recording function, the code block of the second identifier recording function including an identifier of the second function; execute the code block of the second identifier recording function, passing the identifier of the second function to a sequence recording function; execute the sequence recording function, if the first position is the end of the global variable array, adding the identifier of the second function to the beginning position of the global variable array, and if the first position is not the end of the global variable array, adding the identifier of the second function to a second position of the global variable array, the second position being the position following the first position.
[0082] In some embodiments, the device 200 further includes:
[0083] The setting unit is used to set the data located at the second position in the global variable array as a target identifier. The target identifier is used to indicate the identifier of the most recently called function in the global variable array, and the second position is the position after the first position.
[0084] In some implementations, the first identifier recording function is located at the beginning line of the code block of the first function.
[0085] In some implementations, the execution unit 220 is used to add a corresponding identifier record function to the beginning position of the code block of each function in the source file that is being compiled during the execution of the script tool.
[0086] Figure 3 This is a schematic diagram of the structure of a computer device provided in an embodiment of this application. The computer device 600 includes: a processor 601, which is coupled to a memory 602. The memory 602 stores at least one computer program instruction, which is loaded and executed by the processor 601 to enable the computer device 600 to perform... Figure 1 The method provided in the embodiments.
[0087] In some embodiments, a computer-readable storage medium is also provided, storing at least one instruction that, when executed on a computer, causes the computer to perform the above-described... Figure 1 The method provided in the embodiments.
[0088] In some embodiments, a computer program product is also provided, comprising one or more computer program instructions that, when loaded and executed by a computer, cause the computer to perform the aforementioned... Figure 1 The method provided in the embodiments.
[0089] The various embodiments in this specification are described in a progressive manner. The same or similar parts between the various embodiments can be referred to each other. Each embodiment focuses on describing the differences from other embodiments.
[0090] A references B, which means that A is the same as B or A is a simple variation of B.
[0091] The terms "first" and "second," etc., used in the specification and claims of this application are used to distinguish different objects, not to describe a specific order of objects, and should not be construed as indicating or implying relative importance. For example, "first function" and "second function" are used to distinguish different functions, not to describe a specific order of functions, and should not be construed as the first function being more important than the second function.
[0092] In this application, unless otherwise stated, "at least one" means one or more, and "multiple" means two or more. For example, multiple functions refer to two or more functions.
[0093] The above embodiments can be implemented, in whole or in part, by software, hardware, firmware, or any combination thereof. When implemented in software, they can be implemented, in whole or in part, as a computer program product. The computer program product includes one or more computer instructions. When the computer program instructions are loaded and executed on a computer, all or part of the processes or functions described in the embodiments of this application are generated. The computer can be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device. The computer instructions can be stored in a computer-readable storage medium or transmitted from one computer-readable storage medium to another. For example, the computer instructions can be transmitted from one website, computer, server, or data center to another website, computer, server, or data center via wired (e.g., coaxial cable, fiber optic, digital subscriber line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.) means. The computer-readable storage medium can be any available medium that a computer can access or a data storage device such as a server or data center that integrates one or more available media. The available medium can be a magnetic medium (e.g., floppy disk, hard disk, magnetic tape), an optical medium (e.g., DVD), or a semiconductor medium (e.g., a solid-state drive (SSD)).
[0094] The above embodiments are only used to illustrate the technical solutions of this application, and are not intended to limit it. 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 scope of the technical solutions of the embodiments of this application.
Claims
1. A method for recording the order of function calls, characterized in that, The method includes: Create a global variable array, which is used to record the function call order; In response to a call instruction for a first function, the code block of the first function is executed, the code block of the first function includes a first identifier recording function, and the code block of the first identifier recording function includes the identifier of the first function; The code block that executes the first identifier recording function passes the identifier of the first function to the order recording function; Run the sequence recording function to add the identifier of the first function to the first position of the global variable array.
2. The method according to claim 1, characterized in that, Adding the identifier of the first function to the first position of the global variable array includes: Record the index of the data currently in use in the global variable array; In response to the index pointing to the first position, the identifier of the first function is added to the first position of the global variable array.
3. The method according to claim 2, characterized in that, After adding the identifier of the first function to the first position of the global variable array, the method further includes: If the first position is the end of the global variable array, the index is set to the beginning position in the global variable array; If the first position is not the end of the global variable array, the index is set to the second position in the global variable array, where the second position is the position following the first position.
4. The method according to claim 1, characterized in that, After adding the identifier of the first function to the first position of the global variable array, the method further includes: In response to a call instruction for a second function, the code block of the second function is executed, the code block of the second function includes a second identifier recording function, and the code block of the second identifier recording function includes the identifier of the second function; The code block that executes the second identifier recording function passes the identifier of the second function to the order recording function; The sequence recording function is executed. If the first position is the end of the global variable array, the identifier of the second function is added to the beginning of the global variable array. If the first position is not the end of the global variable array, the identifier of the second function is added to the second position of the global variable array, where the second position is the position following the first position.
5. The method according to claim 1, characterized in that, After adding the identifier of the first function to the global variable array, the method further includes: Set the data at the second position in the global variable array as the target identifier. The target identifier is used to indicate the identifier of the most recently called function in the global variable array. The second position is the position after the first position.
6. The method according to claim 1, characterized in that, The first identifier recording function is located at the beginning line of the code block of the first function.
7. The method according to claim 1, characterized in that, Before the code block that executes the first function, the method further includes: During the execution of the script tool, a corresponding identifier record function is added to the beginning of the code block of each function in the source files that are being compiled.
8. A device for recording the order of function calls, characterized in that, The device includes: A creation unit is used to create a global variable array, which is used to record the function call order; The execution unit is configured to, in response to a call instruction for a first function, execute the code block of the first function, wherein the code block of the first function includes a first identifier recording function, and the code block of the first identifier recording function includes the identifier of the first function; execute the code block of the first identifier recording function, and pass the identifier of the first function to a sequence recording function; execute the sequence recording function, and add the identifier of the first function to the first position of the global variable array.
9. A computer device, characterized in that, The computer device includes: a processor coupled to a memory, the memory storing at least one computer program instruction, the at least one computer program instruction being loaded and executed by the processor to enable the computer device to implement the method of any one of claims 1-7.
10. A computer-readable storage medium, characterized in that, The storage medium stores at least one instruction, which, when executed on a computer, causes the computer to perform the method as described in any one of claims 1-7.