Kernel debugging system and method
Through the user interface module, compilation module and Kwasm engine of the kernel debugging system, the simplification of kernel debugging is achieved, the problem of complexity and professionalism of the existing kernel debugging methods is solved, and the debugging efficiency and safety are improved.
Patent Information
- Application Number
- CN202110240693.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-03-04
- Publication Date
- 2024-08-13
- Estimated Expiration
- 2041-03-04
AI Technical Summary
The existing kernel debugging methods are complex and highly professional, making it difficult for ordinary developers to debug the kernel and may cause system downtime.
Provides a kernel debugging system, including user interface module, compilation module and Kwasm engine. Users can edit program code through the user interface module. The compilation module compiles the code into a target file. The Kwasm engine interprets and executes the target file in the kernel state to generate execution The result is sent to the user interface module through the sending module to implement kernel debugging.
This allows users to write program code just like ordinary applications and run it in the system kernel, which simplifies the kernel debugging process, improves debugging efficiency, and avoids paying attention to system kernel details.
Smart Images

Figure CN112905472B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of software debugging, and in particular to a kernel debugging system and method. Background Art
[0002] In Linux systems, only professionals can often modify and compile kernel source code. However, if developers who are not very familiar with the kernel need to debug some functions of the kernel or read kernel data for testing or analysis when the kernel is running, they usually need to modify and compile the kernel completely, or create a new kernel module. In the case of modifying and compiling the kernel completely, the kernel of the target system also needs to be updated. In the case of creating a new kernel module, the new kernel module also needs to be matched with the kernel source code running the system. Therefore, this method relies heavily on the kernel source code and has high requirements for the compilation environment. Commonly used kernel debugging methods currently include, for example, methods for kernel debugging by modifying the kernel source code, Kgdb kernel debugging methods, Perf kernel debugging methods, kernel ftrace function debugging methods, etc. However, these debugging methods are relatively professional and complex, and may cause system downtime if used improperly.
[0003] Therefore, it is necessary to provide a simple and easy-to-use kernel debugging system and method, so that users can write program codes like writing ordinary application programs, and run them in the system kernel for execution without having to pay attention to the details of the system kernel. Summary of the invention
[0004] In view of the above technical problems, the present application provides a kernel debugging system and method, which allows users to write program codes like writing ordinary applications, and run them in the system kernel for execution without having to pay attention to the details of the system kernel, but only need to understand the kernel interface called.
[0005] In a first aspect of the present application, a kernel debugging system is provided, comprising: a user interface module, the user interface module being configured to edit program code and output execution results of the program code; a compilation module, the compilation module being configured to compile the program code into a target file; a Kwasm engine, the Kwasm engine being directly installed in a system kernel of an operating system, and being configured to interpret and execute the target file in kernel state to obtain the execution result.
[0006] In one implementation, the operating system is a Linux operating system.
[0007] In one implementation, the target file is a target file in a 64-bit wasm format.
[0008] In one implementation, the Kwasm engine includes: a receiving module, the receiving module is configured to receive an operation instruction and forward the operation instruction to a management module, wherein the operation instruction includes an operation instruction associated with the target file; a management module, the management module is configured to provide auxiliary management of the operation instruction in response to receiving the operation instruction; an interpretation and execution module, the interpretation and execution module is configured to interpret and execute the target file in kernel mode to generate the execution result; and a sending module, the sending module is configured to send the execution result to the user interface module.
[0009] In one implementation, the interpretation and execution module is implemented based on a predetermined wasm interpreter, but the underlying interface on which the interpretation and execution module depends is configured as a kernel interface.
[0010] In one implementation, the interpretation and execution module is configured to support interpretation and execution of target files in 64-bit wasm format.
[0011] In one implementation, the interpreted execution module is configured not to support floating point operation functions.
[0012] In one implementation, the Kwasm engine further includes a symbol table generation module, which is configured to generate an export function symbol table based on a system symbol table and a kernel header file.
[0013] In one implementation, the auxiliary management includes: system status maintenance, file management, memory management and message queue management.
[0014] In a second aspect of the present application, a kernel debugging method is provided, the kernel debugging method is performed using the kernel debugging system described in the first aspect of the present application, the kernel debugging method comprises: editing program code via a user interface module; compiling the program code into a target file via a compilation module; interpreting and executing the target file in kernel state via a Kwasm engine, and sending the execution result of the program code to the user interface module for display. BRIEF DESCRIPTION OF THE DRAWINGS
[0015] Figure 1 A structural block diagram of a kernel debugging system according to an embodiment of the present application is shown;
[0016] Figure 2 A schematic diagram of the structure of a Kwasm engine according to an embodiment of the present application is shown;
[0017] Figure 3 A structural block diagram of a management module according to an embodiment of the present application is shown;
[0018] Figure 4 A flowchart of a kernel debugging method according to an embodiment of the present application is shown. DETAILED DESCRIPTION
[0019] In order to make the above-mentioned purpose, features and advantages of the present application more obvious and easy to understand, the specific embodiments of the present invention are described in detail below in conjunction with the accompanying drawings. In the following description, many specific details are set forth to facilitate a full understanding of the present invention. However, the present invention can be implemented in many other ways different from those described herein, and those skilled in the art can make similar improvements without violating the connotation of the present invention, so the present invention is not limited by the specific embodiments disclosed below.
[0020] like Figure 1 As shown, in one embodiment, the present application provides a kernel debugging system 100. The kernel debugging system 100 includes a user interface module 101, a compilation module 102, and a Kwasm engine 103 (ie, a WebAssembly interpretation and execution system for embedding in the kernel).
[0021] User interface module 101 is configured to edit program code (for example, program code code written in C language), and output the execution result of this program code.In one implementation, user interface module 101 can be a user interface for displaying and editing the program code written by developers, and showing the corresponding execution result.It is worth mentioning that, in this article, editing program code can refer to program code being written or modified, including inputting new program code or modifying program code already inputted before.
[0022] Compiler module 102 is configured to compile program code into target file. At present, most central processing units (CPUs) are 64-bit, and the corresponding system kernel (e.g., Kernel) is also a 64-bit address range, so in one implementation, the target file is configured as a target file of 64-bit wasm format (WebAssembly format), so as to ensure that the operation of wasm code accessing the kernel interface can be correctly executed. In one implementation, the compiler of Clang 12.0 or above can be used to compile the target file of 64-bit wasm format. It should be understood that Clang is a lightweight compiler of C language, C++, and Objective-C language. Of course, in this application, other compilers can also be used, as long as it can compile program code into a target file of the required format.
[0023] In this application, the Kwasm engine 103 is implemented as a service directly installed in the system kernel, which is configured to interpret and execute the target file in the kernel state to obtain the execution result of the program code. Figure 2A further detailed description of the Kwasm engine 103. In this application, kernel state is actually relative to user state. Specifically, kernel state refers to the central processing unit (CPU) of the electronic device running at a higher privilege level, with higher system permissions, and can operate more system resources, such as interrupt vector table, special registers, etc., while user state refers to the central processing unit running at the lowest privilege level. For example, ordinary applications run in user state. When operating system resources are needed, user state programs need to enter kernel state through operating system calls, and the interface provided by the kernel implements the operation of specified resources.
[0024] In one implementation, Figure 1 As shown, the user interface module 101 and the compilation module 102 are both implemented in the host computer, and the kwasm engine 103 is directly installed in the system kernel (Kernal) of the operating system (for example, Linux system) on the lower computer (or target machine). In the present application, the host computer refers to an electronic device used to issue operation commands, and the lower computer refers to an electronic device used to process operation commands. The lower computer is generally installed with an operating system such as Linux. In the present application, the electronic device can represent various forms of digital computers, such as laptop computers, desktop computers, workbenches, personal digital assistants, servers, blade servers, mainframe computers, and other suitable computers. The electronic device can also represent various forms of mobile devices, such as personal digital processing, cellular phones, smart phones, wearable devices and other similar computing devices. Although in Figure 1 The user interface module 101 and the compilation module 102 are shown as being implemented in the upper computer, and the kwasm engine 103 is shown as being directly installed in the lower computer, but it should be understood that the user interface module 101, the compilation module 102 and the operating system installed with the kwasm engine 103 can also be implemented in the same electronic device without departing from the scope of protection claimed in this application.
[0025] like Figure 2 As shown, in one embodiment, the Kwasm engine 103 may include a receiving module 201, a sending module 202, a management module 203 and an interpretation and execution module 204.
[0026] The receiving module 201 is configured to receive an operation instruction and forward the operation instruction to the management module 203, wherein the operation instruction includes an operation instruction associated with a target file. Specifically, in the present application, the operation instruction may include executing a target file, querying which target files are in the Kwasm engine, transmitting a target file, and the like. In one implementation, the receiving module 201 may be connected to a host computer via a communication interface such as a serial interface, a USB interface, or a network interface to receive a target file sent by the host computer. The receiving module 201 may be configured to receive the target file from the host computer via a protobuf (Google Protocol Buffers) communication protocol with a predefined communication field. In the present application, a custom communication field can be used to distinguish different operation instructions (for example, to distinguish different communication functions between the host computer and the Kwasm engine), so that the Kwasm engine can know whether the operation instruction is, for example, to execute a certain wasm64 target file, or to query which wasm 64 target files are in the Kwasm engine, or to just transmit a certain wasm 64 target file, and the like. It should be understood that the protobuf communication protocol is a communication protocol that can realize the conversion of interface data structures across languages / machines. It is relatively mature and universal, and does not occupy high bandwidth. Of course, the receiving module 201 can also be configured to receive the target file from the host computer via other communication protocols, such as in json format.
[0027] The sending module 202 is configured to send the execution result of the program code to the user interface module 101. In one implementation, the sending module 202 may also be connected to the host computer via a communication interface such as a serial interface, a USB interface or a network interface to send the corresponding execution result to the host computer for display via the user interface module 101. The sending module 202 may be configured to send the execution result to the host computer via the protobuf communication protocol. It is worth mentioning that when the length of the data packet of the execution result to be sent is too long (for example, exceeding the transmission limit of the communication interface (for example, a serial interface, a USB interface or a network interface)), the sending module 202 may also be divided into a plurality of pieces for transmission.
[0028] It is worth mentioning that although Figure 2 The receiving module 201 and the sending module 202 are described as two separate modules, but it should be understood that the receiving module 201 and the sending module 202 can also be integrated into the same module without departing from the protection scope of the present application.
[0029] The management module 203 is configured to provide auxiliary management of the operation instruction in response to receiving the operation instruction. In one implementation, the auxiliary management may include system status maintenance, file management, memory management, and message queue management. Figure 3 As shown, in one embodiment, the management module 203 may correspondingly include a system status maintenance module 301 , a file management module 302 , a memory management module 303 and a message queue management module 304 .
[0030] The system status maintenance module 301 is configured to perform system status management functions, which may specifically include initializing the Kwasm engine when the Kwasm engine is started in response to receiving a corresponding operation instruction, releasing the resources of the Kwasm engine when the Kwasm engine exits in response to receiving a corresponding operation instruction, and maintaining the status of the Kwasm engine when the Kwasm engine is running, etc.
[0031] The file management module 302 is configured to perform file management functions, which may include, in response to corresponding operation instructions, sending the target file to the Kwasm engine, that is, sending it to the specified target of the operating system for storage (for example, as an example only, the specified directory may be, for example, / home / kwasm / ), so that the kwasm engine can read the target file from the directory and execute it. It is worth mentioning that once the target file is sent to the specified target of the operating system for storage, the target file can be repeatedly executed by the Kwasm engine without having to be re-sent before each execution. File management may also include reading, deleting, querying, etc. the sent target files in response to corresponding operation instructions.
[0032] Because the derived function symbol table is huge in number and not fixed, therefore when generating the derived function symbol table, generally need to dynamically expand memory resources, and also can apply for a lot of scattered memory, for example, to be used for storing symbol name etc., usually different symbol name lengths are all different.Hereinafter, the derived function symbol table will be described in more detail in conjunction with symbol table generation module 205.Thus, in the present application, memory management module 303 is configured to be mainly used for performing following memory management function: the function of memory pool is provided, is used for the required memory of dynamic allocation and the allocated memory is centrally managed, to ensure the effective use and the release of resource.
[0033] The message queue management module is configured to perform message queue management functions, which may specifically include: after receiving an operation instruction from the receiving module 201, the operation instruction is stored in the message queue, so that the main thread of the Kwasm engine can guard the message queue of the operation instruction. Such guarding may be, for example, processing the operation instruction differently according to the ID of the operation instruction after it arrives, such as providing the operation instruction of deleting a file to the file management for execution, providing the operation instruction of executing the target file to the interpretation and execution module 204 for execution, and so on.
[0034] The interpretation execution module 204 is configured to interpret and execute the target file in the kernel state to generate the corresponding execution result. In one implementation, the interpretation execution module is implemented on the basis of a predetermined wasm interpreter, but the underlying interface on which the interpretation execution module depends is configured as a kernel interface. In the present application, the predetermined wasm interpreter can be any existing wasm interpreter (e.g., an open source wasm interpreter) or a wasm interpreter to be developed in the future. It should be understood that the existing open source wasm interpreter is a program running in user mode and cannot be directly embedded in the system kernel of the operating system, i.e., it cannot be directly installed in the system kernel. In order to enable this open source wasm interpreter to be directly installed in the system kernel, the present application has made substantial improvements on the basis thereof, thereby realizing the interpretation execution module 204.
[0035] On the one hand, by improving the existing open source wasm interpreter, the underlying interface on which the finally implemented interpretation execution module 204 depends is configured as a kernel interface. The open source wasm interpreter is usually executed in user mode, and most of the underlying interfaces are basic libraries such as libc, but the system kernel of the operating system does not support this user-mode basic library. Therefore, the interpretation execution module 204 according to the embodiment of the present application removes the dependence of the open source wasm interpreter on this basic library, and the underlying interfaces required to be relied on (for example, all C library interfaces) are changed to the corresponding interfaces (i.e., kernel interfaces) provided by the system kernel. In one case, when the kernel system includes a corresponding kernel interface, the underlying interface can be directly replaced, for example, the malloc (memory allocation interface provided by the C standard library) function is directly changed to vmalloc (memory allocation interface provided by the kernel), and free (release) (memory release interface of the C library) is directly changed to vfree (memory release interface provided by the kernel). In another case, when the kernel system does not include the corresponding kernel interface, the underlying interface can be developed and adapted, that is, the corresponding kernel interface can be redeveloped. For example, open is the interface for opening files provided by the C library, but there is no corresponding interface in the kernel. Then, the known interfaces provided by the kernel can be combined into an interface for the open function.
[0036] On the other hand, by improving the existing open source wasm interpreter, the finally implemented interpretation execution module 204 is configured to support the interpretation and execution of the target file in the 64-bit wasm format. The existing open source interpreters only support the 32-bit wasm file format, but the system kernel generally runs in a 64-bit central processing unit (CPU), that is to say, the address space of the system kernel is 64 bits, so the existing open source wasm interpreter cannot access the kernel address of the system kernel. Therefore, it is necessary to modify this open source interpreter to support the 64-bit wasm format so that the kernel interface can be used normally. In order to modify the open source interpreter to support the 64-bit wasm format, all operations on the address can be changed to be processed according to 64 bits, for example, the length of the read address is extended to 64 bits, the storage type is stored according to the 64-bit space, and the parts fixed as 32-bit identification in the original interpreter (mainly various pointer types) are processed according to 64 bits. For example, just as an example, when encountering address processing, the calculation method of I32 can be changed to the calculation method of I64, which can be specific to reading the wasm64 bytecode length extension, instruction push address update, instruction pop address update, parameter passing and other aspects.
[0037] On the other hand, by improving the existing open source wasm interpreter, the final implemented interpretation execution module is also configured to not support floating point operation functions. The system kernel does not support floating point operations, so all floating point related functions in the existing open source wasm interpreter need to be deleted, and the corresponding floating point instructions are set to not support.
[0038] Continue to refer Figure 2In one embodiment, the Kwasm engine 103 may further include a symbol table generation module 205. The symbol table generation module 205 is configured to generate an export function symbol table based on the system symbol table and the kernel header file. The export function symbol table is a corresponding table of the interface function exported by the kernel and the actual kernel address when the system is running. Therefore, through the export function symbol table, the kernel export function (i.e., the interface function defined in the kernel code) that needs to be called can be quickly found during the execution of the target file. The kernel header file refers to the header file generated during the compilation process of the system kernel, which may contain function prototype definitions, i.e., function definitions that the kernel can provide for user calls. The system symbol table refers to the actual symbol table generated in the operating system after the system kernel is running, and the symbol table only contains the name and address of the function and the symbol type. The symbol table generation module 205 can match the system symbol table and the kernel header file to generate the final export function symbol table. In one implementation, the export function symbol table is a table of items with three elements, the three elements being: a. function name (i.e., the name of the kernel export function), b. memory mapping address of the function (i.e., the internal parameter mapping address of the kernel export function), and c. function prototype (i.e., the prototype definition of the kernel export function). When the interpretation and execution module 204 is interpreting and executing the target file, if it encounters a situation where the corresponding kernel function needs to be called, it will query the export function symbol table to map the kernel function referenced in the target file to a specific memory address (including passing the relevant actual parameters to the kernel function at the memory address), and jump to the kernel function code segment for execution, thereby realizing the call of the kernel function (i.e., the interface function exported by the kernel).
[0039] By installing the above-mentioned Kwasm engine in the system kernel, it is equivalent to opening a window in the system kernel, so that developers can debug the system kernel simply and conveniently without having to pay attention to the details of the system kernel, which helps to improve the efficiency of kernel debugging.
[0040] like Figure 4 As shown, in one embodiment, the present application also provides a kernel debugging method, which is implemented using the above kernel debugging system. Specifically, the kernel debugging method may include steps 401-403.
[0041] In step 401, program code (eg, program code input by a developer on the user interface module) is edited through the user interface module.
[0042] In step 402, the program code is compiled into a target file using a compiling module.
[0043] In step 403, the target file is interpreted and executed in kernel state via the Kwasm engine, and the execution result of the program code is sent to the user interface module for display. As described above, the Kwasm engine is installed in the system kernel, so in this method, the target file actually runs in kernel state, so it can call the interface function exported by the kernel during operation.
[0044] It can be seen from the above process that the operation process of the kernel debugging method of the present application is simple and easy to use. The developer only needs to input the test code on the user interface module to make it run in the system kernel, thereby greatly improving the efficiency of debugging the kernel code.
[0045] The above-mentioned embodiments only express several implementation methods of the present application, and the descriptions thereof are relatively specific and detailed, but they cannot be understood as limiting the scope of the invention patent. It should be pointed out that, for a person of ordinary skill in the art, several variations and improvements can be made without departing from the concept of the present application, and these all belong to the protection scope of the present application. Therefore, the protection scope of the patent of the present application shall be subject to the attached claims.
Claims
1. A kernel debugging system, characterized in that: include: A user interface module, wherein the user interface module is configured to edit the program code and output the execution result of the program code; A compiling module, wherein the compiling module is configured to compile the program code into a target file; A Kwasm engine, wherein the Kwasm engine is directly installed in the system kernel of the operating system and is configured to interpret and execute the target file in kernel state to obtain the execution result; The Kwasm engine includes: A receiving module, the receiving module is configured to receive an operation instruction and forward the operation instruction to the management module, wherein the operation instruction includes an operation instruction associated with the target file; a management module, the management module being configured to provide auxiliary management of the operation instruction in response to receiving the operation instruction; An interpretation and execution module, wherein the interpretation and execution module is configured to interpret and execute the target file in kernel mode to generate the execution result; A sending module is configured to send the execution result to the user interface module.
2. The kernel debugging system according to claim 1, characterized in that: The operating system is a Linux operating system.
3. The kernel debugging system according to claim 1, characterized in that: The target file is a 64-bit wasm format target file.
4. The kernel debugging system according to claim 1, characterized in that: The interpretation and execution module is implemented based on a predetermined wasm interpreter, but the underlying interface on which the interpretation and execution module depends is configured as a kernel interface.
5. The kernel debugging system according to claim 4, characterized in that: The interpretation and execution module is configured to support interpretation and execution of target files in 64-bit wasm format.
6. The kernel debugging system according to claim 4, characterized in that: The interpreted execution module is configured not to support floating point operation functions.
7. The kernel debugging system according to claim 1, characterized in that: The Kwasm engine also includes a symbol table generation module, which is configured to generate an export function symbol table based on a system symbol table and a kernel header file.
8. The kernel debugging system according to claim 1, characterized in that: The auxiliary management includes: system status maintenance, file management, memory management and message queue management.
9. A kernel debugging method, characterized in that: The kernel debugging method is executed by using the kernel debugging system according to any one of claims 1 to 8, and the kernel debugging method includes: Editing program code via a user interface module; Compiling the program code into an object file via a compiling module; The target file is interpreted and executed in kernel mode via the Kwasm engine, and the execution result of the program code is sent to the user interface module for display.
Citation Information
Patent Citations
Virtual machine system and implementing method thereof
CN102096598A
Method and system for debugging linux kernel in user mode
CN104050080A
