Method and device for automatically generating eBPF program
Through the method of automatically generating eBPF programs, the problem of poor compatibility of eBPF programs on different Android kernel versions is solved, the automatic generation and compatibility of programs is realized, the development complexity and workload are reduced, and the correctness and compatibility of the programs are ensured. sex.
Patent Information
- Application Number
- CN202410710876.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-06-03
- Publication Date
- 2024-08-30
AI Technical Summary
The eBPF program has poor compatibility on different Android kernel versions. The programming and debugging workload during the development process is heavy, and writing errors are prone to occur, resulting in the program being unable to run on different kernel versions.
Provides a method to automatically generate eBPF programs. The user specifies tracepoints subsystems and mount points to generate kernel-mode and user-mode programs. It uses format files to extract data structures, defines general business logic and map tables, and realizes automatic generation and execution of programs. Compatibility, reducing coding workload.
It realizes the compatible operation of eBPF programs on different Android kernel versions, reduces the programming and debugging workload, ensures that the generated program has no syntax errors, and reduces the development complexity.
Smart Images

Figure CN118567628A
Abstract
Description
Technical Field
[0001] The present invention relates to the field of Android kernel subsystem tracing, and in particular to a method and device for automatically generating an eBPF program. Background Art
[0002] In modern computer systems, Android kernel subsystem tracing and analysis are of great significance in optimizing system performance and troubleshooting. eBPF is an extension framework in the Linux kernel that allows users to inject and execute custom small programs in the kernel. These programs can achieve functional expansion without modifying the kernel source code. The design goal of eBPF is to allow users to extend the kernel's functions through the BPF (Berkeley Packet Filter) instruction set in a safe and efficient way, so that contextual information of the system can be obtained during runtime, and fine-grained monitoring, optimization and troubleshooting of system performance can be achieved. The eBPF program written on the Linux platform can be compiled into an eBPF program adapted to the Android platform through cross-compilation.
[0003] During the development of eBPF programs, it is necessary to interact with the kernel through different mount points to obtain the context of the system runtime. Common mount point types include tracepoints, kprobes, uprobes, etc. Among them, tracepoints are predefined event mount points in the kernel, which are used to track specific system events. It allows users to execute custom eBPF programs when events occur. As a kernel static embedded point, tracepoints has the lowest overhead among many mount point types, so it is the first choice for eBPF program development.
[0004] However, there are some problems in the development of eBPF programs based on tracepoints mount points. (1) The compatibility of eBPF programs is poor. Because tracepoins are predefined event mount points in the kernel, different Android kernel versions may have different tracepoints mount points, resulting in the eBPF program developed by programmers under a certain Android kernel version may not be able to run on other Android kernel versions. (2) The workload of programming and debugging eBPF programs is large. Because there are a large number of tracepoints mount points and programs are prone to writing errors, developing a large number of eBPF programs based on multiple tracepoints mount points requires a lot of repetitive coding and debugging work. Therefore, a solution that can not only solve the problem of eBPF programs running compatibly on different Android kernel versions, but also automatically generate eBPF programs based on multiple tracepoints mount points is urgently needed. Summary of the invention
[0005] The present invention provides a method and device for automatically generating an eBPF program, and provides a solution and device that can not only solve the compatible operation of the eBPF program on different Android kernel versions, but also automatically generate an eBPF program solution and device based on multiple tracepoints mounting points.
[0006] In order to solve the above problems, the present invention provides the following technical solutions:
[0007] In a first aspect, the present invention provides a method for automatically generating an eBPF program, which can be used in the field of program code in computer technology. The method comprises the following steps:
[0008] 1. The user specifies the tracepoints subsystem and mount point name to be traced. Provide a human-computer interaction interface for the user to specify the tracepoints subsystem and mount point to be traced through parameters;
[0009] 2.According to the name of the tracepoints subsystem and mount point specified by the user, obtain the corresponding path of the mount point on the Android system;
[0010] 3. Generate kernel-mode eBPF program;
[0011] 3.1 Find the format file of the mount point in the corresponding path of the mount point to be tracked on the Android system, which contains the data structure corresponding to the tracepoints mount point;
[0012] 3.2 Read the format file content, extract each field name and type, and display it to the user;
[0013] 3.3 Based on the name and type of each field extracted in 3.2, construct the data structure corresponding to the mount point that the user needs to track in the program;
[0014] 3.4 The user specifies the type and name of the key and value of the map table to be created. The map table is used to store the kernel context information obtained when the kernel state eBPF program is running. Through the map table, the kernel state and the user state can communicate;
[0015] 3.5 Define the general business logic and the business logic for updating the map. The general business logic at least includes the header files, data structures, and general codes required by the kernel-mode eBPF program;
[0016] 3.6 Splice the codes generated in 3.1 to 3.5 to generate a kernel-mode eBPF program;
[0017] 4. Generate user-mode programs;
[0018] 4.1 Contains the header files and *.skel.h files required by user-mode programs;
[0019] 4.2 Introduce the data structure generated in 3.3 into the user-mode program;
[0020] 4.3 Define the common functions and variables used by user-mode programs. At least include the function for printing log information and the exception signal processing function. The print log information function is used to control the log output in the libbpf library. The signal processing function is used to handle the exception processing when the user interrupts the user-mode program;
[0021] 4.4 Define the user-mode program entry function;
[0022] 4.4.1 Define common business logic;
[0023] 4.4.2 Open the kernel-mode eBPF program;
[0024] 4.4.3 Load the kernel-mode eBPF program;
[0025] 4.4.4 Mount the kernel-mode eBPF program to the corresponding kernel mount point. When the kernel runs to these mount points, it will trigger the execution of the kernel-mode eBPF program on the corresponding path.
[0026] 4.4.5 Read the map table content and print the map table content. The map stores the system context information when the mount point is triggered;
[0027] 4.5 Splice the codes generated in 4.1 to 4.4 to generate a user-mode program.
[0028] In the second aspect, this patent provides a device for automatically generating eBPF programs, which is used to implement the above-mentioned method for automatically generating eBPF programs. The device includes:
[0029] 1. User interaction module. This module is used to obtain the tracepoints subsystem and mount point and corresponding kernel path information specified by the user to be traced as described in the first aspect;
[0030] 2. A kernel-mode eBPF program generation module. This module is used to generate the kernel-mode eBPF program described in the first aspect;
[0031] 3. User-mode program generation module: This module is used to generate the user-mode program described in the first aspect.
[0032] In a third aspect, the present invention provides an electronic device for running the user interaction module, kernel state eBPF program generation module, and user state program generation module described in the second aspect. The electronic device includes a processor, a memory, a readable and writable storage medium, an output terminal, and an input terminal. The processor, the memory, the readable and writable storage medium, the output terminal, and the input terminal are connected via a bus, and data is exchanged between them; wherein the readable and writable storage medium is used to persistently store automatically generated kernel state eBPF programs, user state programs, and computer script programs that can automatically generate eBPF programs, the memory stores user state programs and kernel state eBPF programs at runtime, the processor is used to execute user state and kernel state eBPF programs, and the input terminal is used to receive input information during user interaction, including tracepoints mount point name, map table name, map table key value type and name, map table value type and name, and eBPF file name. The output terminal is used to display output information when interacting with the user.
[0033] In a fourth aspect, the present invention provides a computer script program, which, when executed on a computer, enables the computer to execute the method described in the first aspect.
[0034] The beneficial effect of the present invention is that, through the method and device designed by the present invention, kernel-state eBPF programs and user-state programs suitable for the current Android kernel version can be automatically generated, solving the problem that the eBPF program is heavily dependent on the kernel version. In the process of automatic program generation, the user is allowed to select the required mount point to customize the generation of the eBPF program. The eBPF program generated using the tracepoints type mount point has the advantage of lower overhead. In addition, by defining another script file and running the method proposed by the present invention in batches, kernel-state eBPF programs and user-state programs can be generated in batches, reducing the coding workload of programmers. Finally, the automatically generated eBPF program has no syntax errors, eliminating the workload of code debugging. BRIEF DESCRIPTION OF THE DRAWINGS
[0035] Figure 1 The overall flow chart of the method for automatically generating eBPF programs of the present invention;
[0036] Figure 2 A structural block diagram of a device for automatically generating eBPF programs according to the present invention;
[0037] Figure 3 A schematic diagram of the structure of electronic equipment required for automatically generating eBPF program execution according to the present invention. DETAILED DESCRIPTION
[0038] The eBPF program is automatically generated by running a script file (such as autoGen.sh). The script language is a shell script language, but other script languages are not limited to implement the script file. The script file uses a script language to implement the method described in the present invention. All logic for automatically generating eBPF programs is written in the script file. When the directory, path, mount point, file, function and data structure of the Android kernel need to be referenced, it is expressed in the form of variables or symbol placeholders. When the script file is run, the script file will dynamically obtain the directory, path, mount point, file, function and data structure of the current Android kernel to replace these variables or symbols, so as to achieve the purpose that the automatically generated eBPF program can be compatible with the current Android kernel.
[0039] See also Figure 1 The specific implementation of the method for automatically generating eBPF programs of the present invention includes the following steps:
[0040] 1. The user specifies the tracepoints subsystem and mount point name to be traced, and provides a human-computer interaction interface for the user. The user specifies the tracepoints subsystem and mount point name to be traced. The human-computer interaction interface uses a question-and-answer method to define a variable to store the tracepoints subsystem directory ( / sys / kernel / debug / tracing / events) path, traverse the tracepoints subsystem directory content, obtain all subsystem names that can be traced by the host Android kernel and display them on the screen for the user to view. The user selects a subsystem name (such as sched) according to the needs, obtains all tracepoint mount point names under the subsystem specified by the user and displays them on the screen for the user to view. The user selects a tracepoints mount point (such as sched_switch) according to the needs. The interaction method can also be set in the form of command line parameters. The naming line parameters include the tracepoints subsystem name and the mount point name. This method is used in the scenario where the user knows in advance the specific name of the tracepoints subsystem and the specific name of the mount point of the Android kernel to be traced.
[0041] 2. According to the name of the tracepoints subsystem and mount point specified by the user to be traced, obtain the corresponding path of the mount point on the Android system, and form the kernel directory where the tracepoint mount point selected by the user is located (such as / sys / kernel / debug / tracing / events / sched / sched_switch) by splicing the tracepoints subsystem directory name, subsystem name, and mount point name.
[0042] 3. Generate kernel-mode eBPF program;
[0043] 3.1 Find the format file of the mount point in the corresponding path of the mount point to be tracked on the Android system, which contains the data structure corresponding to the tracepoints mount point;
[0044] 3.2 Read the format file content, extract each field name and type, and display it to the user;
[0045] 3.3 Based on the name and type of each field extracted in 3.2, construct the data structure corresponding to the mount point that the user needs to track in the program. This implementation example expresses the data structure in C language, and other languages can also be used to express it, so that the automatically generated eBPF program corresponds to the expression form of other languages;
[0046] 3.4 The interactive interface in question-and-answer mode prompts the user to specify the type and name of the key and value of the map table to be created. The map table is used to store the kernel context information obtained when the kernel state eBPF program is running. Through the map table, the kernel state and the user state can communicate. The human-computer interaction method can also be set in the form of command line parameters. The named line parameters include the type and name of the key and value of the map table;
[0047] 3.5 Define the general business logic and the business logic for updating the map. The general business logic at least includes the header files, data structures, and general codes required by the kernel-mode eBPF program;
[0048] 3.6 The interactive interface in question-and-answer format prompts the user to specify the name of the automatically generated kernel-mode eBPF program (such as test), and automatically adds the extension .bpf.c. The automatically generated eBPF program in this implementation case uses the C language, and eBPF programs in other languages can also be automatically generated. Therefore, the automatically added extension needs to be defined as a file extension supported by other languages. The codes generated in 3.1 to 3.5 are concatenated to generate a kernel-mode eBPF program (such as test.bpf.c), and are persistently stored in the current directory of the Android system.
[0049] 4. Generate user-mode programs;
[0050] 4.1 The script file for automatically generating the eBPF program contains the header files and *.skel.h files required by the user-mode program;
[0051] 4.2 Introduce the data structure generated in 3.3 into the user-mode program;
[0052] 4.3 Define the common functions and variables used by user-mode programs. At least include the function for printing log information and the exception signal processing function. The print log information function is used to control the log output in the libbpf library. The signal processing function is used to handle the exception processing when the user interrupts the user-mode program;
[0053] 4.4 Define the user-mode program entry function;
[0054] 4.4.1 Define general business logic, which is determined based on actual user needs;
[0055] 4.4.2 Open the kernel-mode eBPF program;
[0056] 4.4.3 Load the kernel-mode eBPF program;
[0057] 4.4.4 Mount the kernel-mode eBPF program to the corresponding kernel mount point. When the kernel runs to these mount points, it will trigger the execution of the kernel-mode eBPF program on the corresponding path.
[0058] 4.4.5 Read the map table content and print the map table content. The map stores the system context information when the mount point is triggered;
[0059] 4.5 Concatenate the codes generated in 4.1 to 4.4 to generate a user-mode program. The user-mode program is named as the file name described in 3.6 plus the extension .c (such as test.c). The user-mode program automatically generated in this implementation case uses the C language, and user-mode programs in other languages can also be automatically generated. Therefore, the automatically added extension needs to be defined as a file extension supported by other languages and stored persistently in the current directory of the Android system.
[0060] See also Figure 2 The device for automatically generating eBPF programs of the present invention includes a user interaction module, a kernel-mode eBPF program generation module, and a user-mode program generation module.
[0061] User interaction module. This module is used to obtain the tracepoints subsystem and mount point and corresponding kernel path information specified by the user to be traced as described in the first aspect;
[0062] A kernel-mode eBPF program generation module. This module is used to generate the kernel-mode eBPF program described in the first aspect;
[0063] A user-mode program generation module: This module is used to generate the user-mode program described in the first aspect.
[0064] Corresponding to the embodiment of the aforementioned method for automatically generating an eBPF program, the present invention also provides an embodiment of an electronic device.
[0065] See also Figure 3 An electronic device provided by an embodiment of the present invention includes a processor, a memory, a readable and writable storage medium, an output terminal, and an input terminal. The processor, the memory, the readable and writable storage medium, the output terminal, and the input terminal are connected via a bus, and data is exchanged between them; wherein the readable and writable storage medium is used to persistently store automatically generated kernel-state eBPF programs, user-state programs, and computer script programs that can automatically generate eBPF programs, the memory stores user-state programs and kernel-state eBPF programs at runtime, the processor is used to execute user-state and kernel-state eBPF programs, and the input terminal is used to receive user input information, including tracepoints mount point name, map table name, map table key value type and name, map table value type and name, and eBPF file name. The output terminal is used to display output information when interacting with the user.
Claims
1. A method for automatically generating an eBPF program, characterized in that: The following steps are involved: S1: The user specifies the tracepoints subsystem and mount point name to be traced, and provides a human-computer interaction interface for the user to specify the tracepoints subsystem and mount point to be traced through parameters; S2: According to the name of the tracepoints subsystem and the mount point to be traced specified by the user, obtain the corresponding path of the mount point on the Android system; S3: Automatically generate kernel-mode eBPF programs; (S3.1) searching for the format file of the mount point to be traced in the corresponding path of the mount point on the Android system, which contains the data structure corresponding to the tracepoints mount point; (S3.2) Read the content of the format file, extract each field name and type, and display it to the user; (S3.3) constructing a data structure corresponding to the mount point that the user needs to track according to each field name and type extracted in (S3.2); (S3.4) The user specifies the type and name of the key and value of the map table to be created. The map table is used to store the kernel context information obtained when the kernel state eBPF program is running. Through the map table, the kernel state and the user state can communicate; (S3.5) define the general business logic and the business logic for updating the map, where the general business logic at least includes the header files, data structures, and general codes required by the kernel-mode eBPF program; (S3.6) Splicing the codes generated in S3.1 to S3.5 into a kernel-mode eBPF program; S4: Automatically generate user-mode programs; (S4.1) Contains the header files and *.skel.h files required by user-mode programs; (S4.2) Introducing the data structure generated by S3.3 into the user-mode program; (S4.3) Define common functions and variables used by user space programs. At least include a function for printing log information and an exception signal processing function. The function for printing log information is used to control the log output in the libbpf library, and the signal processing function is used for exception processing when the user interrupts the user space program; (S4.4) define the user mode program entry function; (S4.4.1) Define common business logic; (S4.4.2) Open the kernel-mode eBPF program; (S4.4.3) Loading kernel-mode eBPF program; (S4.4.4) Mounting the kernel-mode eBPF program to the corresponding kernel mount points. When the kernel runs to these mount points, the kernel-mode eBPF program on the corresponding path will be triggered to execute; (S4.4.5) Read the map table content and print the map table content. The map stores the context information of the system when the mount point is triggered; (S4.5) Splice the codes generated in S4.1 to S4.4 into a user-mode program.
2. The method for automatically generating an eBPF program according to claim 1, characterized in that: Methods for obtaining the path of the corresponding mount point on the Android system include: S1: traverse the / sys / kernel / debug / tracing / events directory, obtain all tracepoints subsystems that can be traced by the Android system kernel, and display them on the screen for users to view; S2: The user specifies a tracepoints subsystem (such as sched) according to the requirements; S3: Get all the tracepoints mount points under the specified tracepoints subsystem (such as sched) and display them on the screen for users to view; S4: The user specifies a tracepoints mount point (such as sched_switch) according to the requirements; S5: concatenate the user-specified tracepoints subsystem and tracepoints mount point names obtained by S1 to S4 to form the path of the corresponding mount point on the Android system (such as / sys / kernel / debug / tracing / events / sched / sched_switch).
3. The method according to claim 1 or 2, characterized in that: The method of constructing the data structure corresponding to the mount point that the user needs to track includes: S1: Search the format file of the specified mount point in the path of the corresponding mount point on the Android system, which contains the data structure corresponding to the tracepoints mount point; S2: Read the format file content, extract each field name and type, and display it to the user; S3: Based on the name and type of each field extracted by S2, a data structure corresponding to the mount point that the user needs to track in the kernel-mode eBPF program is constructed.
4. The method according to claim 1, 2 or 3, characterized in that: Methods for automatically generating kernel-mode eBPF programs include: S1: Contains the header files required for kernel-mode eBPF programs, including at least vmlinux.h, bpf / bpf_helpers.h, bpf / bpf_tracing.h, and bpf / bpf_core_read.h; S2: define the data structure corresponding to the mount point that the user needs to track as described in claim 3; S3: Define a map table according to the type and name of the key and value of the map table to be created as specified by the user. The map table can be a BPF_MAP_TYPE_ARRAY type map table, but is not limited to this type. S4: define the processing function when the tracepoints mount point is triggered. The entry parameter type of the processing function is the data structure corresponding to the tracepoints mount point generated by claim S3. The function body of the processing function defines the processing logic of the Android system context information and the update operation of the map table when the tracepoints mount point is triggered. S5: Contains the license information code required for kernel-mode eBPF programs.
5. The method according to claim 1, 2 or 3, characterized in that: Methods for automatically generating user-mode programs include: S1: Includes the header files required for user-mode programs, including at least *.skel.h files; S2: define the data structure corresponding to the mount point that the user needs to track as described in claim 3; S3: define common functions and variables used by user space programs, including at least a function for printing log information and an exception signal processing function. The function for printing log information is used to control the log output in the libbpf library, and the signal processing function is used to handle exceptions when a user interrupts a user space program. S4: Define the user-mode program entry function; (S4.1) Define common business logic; (S4.2) Open the kernel-mode eBPF program; (S4.3) Loading kernel-mode eBPF program; (S4.4) Mounting the kernel-mode eBPF program to the corresponding kernel mount points. When the kernel runs to these mount points, the kernel-mode eBPF program on the corresponding path will be triggered to execute. (S4.5) Read the map table content and print the map table content. The map stores the context information of the system when the mount point is triggered; S5: Splice the codes generated in S1 to S4 into a user-mode program.
6. A device for automatically generating eBPF programs, characterized in that: The device comprises: A user interaction module, the module is used to obtain the tracepoints subsystem and mount point and corresponding kernel path information specified by the user to be traced as described in claim 1; A kernel-mode eBPF program generation module, the module being used to generate the kernel-mode eBPF program described in claim 1; A user-mode program generation module, wherein the module is used to generate the user-mode program described in claim 1.
7. An electronic device comprising a memory and a processor, characterized in that: The memory is coupled to the processor. The memory is used to store program data, and the processor is used to execute the program data to implement the method for automatically generating eBPF programs as described in any one of claims 1 to 5.
8. A readable and writable storage medium storing a program, characterized in that: When the program is executed by a processor, the method for automatically generating an eBPF program as described in any one of claims 1 to 5 is implemented.
9. A computer script program, when the program is run on a computer, causes the computer to execute the method according to claim 1, 2, 3, 4 or 5.
Citation Information
Cited By
Kernel probe generation method and device, computer equipment and storage medium
CN120950340A
Kernel probe generation method, apparatus, computer equipment and storage medium
CN120950340B
Cross-kernel compatible eBPF weak sign function dynamic replacement method and system
CN121957678A
A cross-kernel compatible eBPF weak symbol function dynamic replacement method and system
CN121957678B
Visual interaction-based kernel probe configuration method and system
CN122673051A
