Code tracking method based on tool chain
By automatically inserting the tracking function using compiler options in the toolchain, and combining the operating system interface to obtain CPU cores and timestamps, the problem of time-consuming and complexity of code tracking is solved, and the dynamic, accuracy and performance of code tracking is achieved. Optimized for large-scale code projects.
Patent Information
- Application Number
- CN202411121609.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-15
- Publication Date
- 2024-11-12
AI Technical Summary
Existing code tracking methods are time-consuming, complex and incomplete, especially in projects with larger code scales, which are difficult to effectively track the execution of each function.
Using a toolchain-based code tracking method, the compiler's special options automatically insert custom hook functions at the entrance and exit of each function, record function call information, and use the operating system interface to obtain CPU cores and timestamps, combining low priority Level tasks periodically write data to files, and the analysis program organizes data to display function call flow and time consumption.
It realizes dynamic and accurate code tracking, automatically adds tracking functions, reduces performance losses, is suitable for large-scale code projects, and simplifies the workflow of technicians.
Smart Images

Figure CN118939464A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of operating systems, and in particular to a code tracing method based on a tool chain. Background Art
[0002] As code complexity increases, when a program has problems, technicians often find it difficult to understand the complete program operation process due to the complexity of the code, and spend a lot of energy on analyzing the program call relationship and operation process. At this time, code tracking tools are needed to help technicians track the call relationship within the program during actual runtime.
[0003] There are two main solutions for implementing code tracing. One is to complete it through static code scanning. These tools often analyze the calling relationship of each function in the code by scanning the code content to form relevant process information. Another method is to manually add tracing code at the entry and exit positions of each function in the code to determine the calling relationship between functions when the program is actually running.
[0004] Although static code scanning can comprehensively analyze the calling relationships at all levels of the code, due to the inevitable existence of branches in the code, the static scanning method cannot specifically reflect the execution flow of the code when the program is actually running. When debugging, technicians still need to manually analyze the code to understand the actual calling flow of the program. In addition, static code analysis tools are often complex and difficult to implement.
[0005] Manually adding tracking code at the function entry and exit can record the actual running process of the program, providing technicians with a better code tracking function. However, this method requires manually adding tracking code to each function to record information such as the function name called by the program. If the code is large, it is difficult to fully track the execution of each function in the code. Summary of the invention
[0006] Aiming at the current problems of time-consuming, complex and incomplete code tracing, a tool chain-based code tracing method was proposed.
[0007] The technical solution of the present invention is:
[0008] A code tracing method based on toolchain is used to perform code tracing by inserting tracing code at the entry and exit of a function. By using the special option of the compiler in the toolchain, after setting the option, the compiler will automatically add a user-defined hook function at the entry and exit of each function during compilation. The user can freely define the specific function of the function. This method implements the specific function of the tracing function in these two functions. This method includes two parts: tracing function and analysis program, as follows:
[0009] The tracing function is recorded in tasks. When a task calls a function, the tracing function at the function entry will first determine whether the current task is the one that needs to be recorded, and then obtain the running CPU core, timestamp, the called function address and the level of the current call. The CPU core and timestamp can be quickly obtained through the corresponding interface of the operating system, and the called function address is obtained from the parameters passed by the system. The tasks that need to be recorded each maintain a variable to record the level of the current call. The variable is incremented by one at the function entry and decremented by one at the function exit.
[0010] In order to reduce the performance loss of the original program caused by adding the tracking function, the tracking function directly writes the data into the corresponding memory after obtaining it, and the operating system has another low-priority task that periodically writes the data in the memory into the file;
[0011] At this time, only the original data recorded is saved in the file, and the analysis program needs to be used to organize the original data. At this time, each line of data in the original data contains the following data: Data 1. Indicates whether the line of data is recorded at the function entry or exit, the entry is i, and the exit is o; Data 2, the address of the function called in or out; Data 3, which CPU core the task runs on when the function is called; Data 4, the timestamp of the call; Data 5, the level of the current call;
[0012] The analysis program will organize the raw data according to certain rules so that technicians can clearly understand the specific operation process of the task; first, the analysis program will translate the function address into the corresponding function name according to the symbol table, and then add several spaces before each line according to the call level n, and finally calculate the total time consumed by this function call based on the timestamps recorded when calling in and out.
[0013] Preferably, in the analysis program, 4*n spaces are added before each line according to the calling level n.
[0014] The beneficial effects of the present invention are:
[0015] As a dynamic code tracking method, this method can accurately track the running process of the task and the time consumed by each function. At the same time, compared with manually added tracking functions, this method can automatically add tracking functions to the code, which is suitable for projects with large code sizes. Finally, the internal implementation of the tracking function in this method is very simple, involving only a small amount of memory reading and writing, which minimizes the performance loss of the program itself due to the addition of the tracking function, and the technical staff will not spend too much time on the specific implementation.
[0016] This method first uses the special options of the compiler in the tool chain to realize the automatic insertion of tracing functions, and then combines this option to realize a set of simple and efficient tracing functions and corresponding analysis programs, so that technicians can realize a set of applicable code analysis tools by only adding a small amount of code. BRIEF DESCRIPTION OF THE DRAWINGS
[0017] Figure 1 It is a schematic diagram of the operation flow of the tracking function of the present invention. DETAILED DESCRIPTION
[0018] The present invention is described in detail below in conjunction with the accompanying drawings and specific embodiments. This embodiment is implemented based on the technical solution of the present invention, and provides a detailed implementation method and specific operation process, but the protection scope of the present invention is not limited to the following embodiments.
[0019] This method also uses the method of inserting tracking code at the function entry and exit to perform code tracking, but it does not do it manually. Instead, it uses the special options of the compiler in the tool chain. After setting this option, the compiler will automatically add a user-defined hook function at the entry and exit of each function during compilation. The user can freely define the specific functions of the function. This method implements the specific functions of the tracking function in these two functions.
[0020] The method includes two parts: the tracking function and the analysis program. The tracking function is recorded in tasks. When a task calls a function, the tracking function at the function entry will first determine whether the current task is the task that needs to be recorded, and then obtain the running CPU core, timestamp, the address of the called function and the level of the current call. The CPU core and timestamp can be quickly obtained through the corresponding interface of the operating system, and the address of the called function is obtained from the parameters passed by the system. The tasks that need to be recorded each maintain a variable to record the level of the current call. The variable is incremented by one at the function entry and decremented by one at the function exit. The running process of the tracking function is as follows: Figure 1 As shown, the figure shows the operation process of the entry tracking function, and the one in brackets is the operation process of the exit tracking function.
[0021] In order to reduce the performance loss of the original program caused by adding the tracking function, the tracking function directly writes the data into the corresponding memory after obtaining it. The operating system also has a low-priority task that periodically writes the data in the memory into the file.
[0022] At this time, only the raw data recorded is saved in the file, and the analysis program needs to be used to organize the raw data. At this time, each line of data in the raw data contains the following data: 1. Indicates whether the line of data is recorded at the function entry or exit, the entry is i, and the exit is o; 2. The address of the function called in or out; 3. Which CPU core the task runs on when the function is called; 4. The timestamp of the call; 5. The level of the current call. The following is an example of a raw record: i 0x80105563 0 312350. This record indicates that function 0x80105563 was called by the task running on CPU0 at time 31235, and the call level is 0.
[0023] The analysis program will organize the raw data according to certain rules, so that the technicians can clearly understand the specific operation process of the task. First, the analysis program will translate the function address into the corresponding function name according to the symbol table, and then add 4*n spaces before each line according to the call level n, and finally calculate the total time consumed by this function call according to the timestamp recorded when calling in and out. After data processing, the data format is similar to the following format.
[0024] i test 0 31235spendtime:300
[0025] i test1 0 31245spendtime:200
[0026] o test1 0 31445
[0027] o test 0 31535
[0028] After the above processing, the technician can more intuitively understand that function test calls function test1 in CPU0, and can understand the execution time of the two functions respectively.
[0029] The above-mentioned embodiment only expresses one implementation mode of the present invention, and its description is relatively specific and detailed, but it cannot be understood as limiting the scope of the invention patent. It should be pointed out that for ordinary technicians in this field, several modifications and improvements can be made without departing from the concept of the present invention, which all belong to the protection scope of the present invention. Therefore, the protection scope of the patent of the present invention shall be based on the attached claims.
Claims
1. A code tracing method based on a tool chain, characterized in that: Code tracing is performed by inserting tracing code at the function entry and exit. The special option of the compiler in the tool chain is used. After setting this option, the compiler will automatically add a user-defined hook function at the entry and exit of each function during compilation. The user can freely define the specific function of the function. This method implements the specific function of the tracing function in these two functions. This method includes two parts: tracing function and analysis program, as follows: The tracing function is recorded in tasks. When a task calls a function, the tracing function at the function entry will first determine whether the current task is the one that needs to be recorded, and then obtain the running CPU core, timestamp, the called function address and the level of the current call. The CPU core and timestamp can be quickly obtained through the corresponding interface of the operating system, and the called function address is obtained from the parameters passed by the system. The tasks that need to be recorded each maintain a variable to record the level of the current call. The variable is incremented by one at the function entry and decremented by one at the function exit. In order to reduce the performance loss of the original program caused by adding the tracking function, the tracking function directly writes the data into the corresponding memory after obtaining it, and the operating system has another low-priority task that periodically writes the data in the memory into the file; At this time, only the original data recorded is saved in the file, and the analysis program is required to organize the original data; at this time, each line of data in the original data contains the following data: Data 1, indicating whether the line of data is recorded at the function entry or exit, the entry is i, and the exit is o; Data 2: the address of the function being called in or out; Data 3: the CPU core on which the task is running when the function is called; Data 4: the timestamp of the call; Data 5: the level of the current call; The analysis program will organize the raw data according to certain rules so that technicians can clearly understand the specific operation process of the task; first, the analysis program will translate the function address into the corresponding function name according to the symbol table, and then add several spaces before each line according to the call level n, and finally calculate the total time consumed by this function call based on the timestamps recorded when calling in and out.
2. The toolchain-based code tracing method according to claim 1, characterized in that: In the analysis program, 4*n spaces are added before each line according to the calling level n.
