Implementation method of serial port-based firmware on-the-fly debugger
By embedding the debugger into the UEFI firmware and utilizing the serial port to implement debugger functionality, a unified debugging command is provided, which solves the problem of time-consuming and labor-intensive debugging methods in the UEFI firmware development process, and realizes the rapid and efficient setup of the debugging environment and cross-platform debugging.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-11-08
- Publication Date
- 2026-03-17
AI Technical Summary
In the current UEFI firmware development process, debugging methods are time-consuming and labor-intensive. Hardware debuggers are expensive and have high requirements for the target motherboard, while software debugger environments are difficult to set up, and different compilers require different debugging software and environments.
The debugger function is embedded in the UEFI firmware, and the serial port is used as the debugging channel to provide unified debugging commands, enabling the rapid and efficient setup of the debugging environment and supporting firmware and code debugging for different architectures and compilers.
It reduces the time and learning cost for developers to set up debugging environments, supports unified debugging commands for different systems and compilers, and improves debugging efficiency.
Smart Images

Figure CN114036047B_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the technical field of UEFI firmware development tools, specifically relating to a method for implementing a serial port-based firmware real-time debugger. Background Technology
[0002] A debugger is a computer program and tool used to debug other programs. It allows code to be examined and selectively executed in an instruction set simulator (ISS) for troubleshooting and debugging.
[0003] Developers currently have three debugging methods during UEFI firmware development:
[0004] The first method is using a system debug library, which prints debug information by calling the debug library's API in the code. The drawback of this method is that it requires modifying the code and recompiling and flashing the firmware each time before the results can be seen, which is time-consuming and labor-intensive.
[0005] The second type is a hardware debugger (JTAG tool). This tool connects to the JTAG interface of the target motherboard and the USB interface of the host computer. Developers can use the JTAG debugger software on the host computer to debug the target motherboard's system settings and code. The disadvantages of this method are that JTAG tools are generally expensive and not every developer has them. Also, it has relatively high requirements for the target motherboard; if there is no JTAG interface, the JTAG tool cannot be used.
[0006] The third type is a software debugger (Debug Agent). The Debug Agent module is embedded in the target motherboard firmware. The target motherboard is connected to the debugging host via a serial port or USB debugging cable. Debugging software (WinDBG or GDB) installed on the debugging host provides the necessary debugging tools for the motherboard. This method is purely software-based, requiring no additional hardware besides the serial port or USB debugging cable. However, its drawback is the complexity of setting up the debugging environment. Different compilers require different host system environments and different debugging software, and developers need to be familiar with the commands of different debugging software.
[0007] Currently, serial communication is an essential tool for firmware debugging on both x86 and ARM platforms. It allows direct connection between the debugging host and the target board via serial port or USB-to-serial adapter. Summary of the Invention
[0008] The technical problem solved by this invention is to provide a method for implementing a serial port-based firmware real-time debugger that embeds debugger functionality into UEFI firmware and can debug firmware and code generated by different architectures and compilers.
[0009] Technical Solution: To solve the above-mentioned technical problems, the present invention adopts the following technical solution:
[0010] A method for implementing a serial port-based firmware real-time debugger includes using the serial port of the UEFI firmware as a channel for system debugging and code debugging, using serial terminal software as an input / output interface for debugging commands, and embedding the debugger function into the UEFI firmware to achieve rapid and efficient setup of the debugging environment. In addition, a unified set of debugging commands is used to debug firmware and code generated by different architectures and different compilers.
[0011] The specific implementation steps are as follows:
[0012] (1) The UEFI firmware initializes the serial port to ensure that the serial port can perform read and write operations;
[0013] (2) Provide a proprietary exception trap handler for the real-time debugger in the UEFI firmware. In the exception trap handler, the real-time debugger defines a unified command format that can eliminate differences between different architectures and different compilers.
[0014] (3) The debugger’s exception trap handler parses and executes user commands through serial port read operations and outputs command-related results through serial port write operations.
[0015] Furthermore, the real-time debugger achieves system debugging capabilities through calls to system libraries, and utilizes the CPU's debugging functions to achieve code debugging capabilities.
[0016] Furthermore, system debugging includes reading and writing operations of CPU internal registers; reading and writing operations of CPU status registers; reading and writing operations of memory contents; reading and writing operations of PCI device configuration space; reading and writing operations of I / O device ports; and UEFI system debugging methods.
[0017] Code debugging includes pausing and running code; setting and canceling software breakpoints; setting and canceling hardware breakpoints; displaying the status of code breakpoints; displaying code call stack information; and displaying and single-stepping through assembly code.
[0018] Furthermore, when the CPU suspends the execution of the current code and enters the exception trap handler, the real-time debugger records the CPU's current running parameters. When the exception trap handler is in the exception trap handler, the exception trap handler can be exited by entering the run command through the serial port. The real-time debugger will restore the CPU's running parameters, allowing the CPU to continue execution.
[0019] Furthermore, by adding disassembly modules for different architectures to the PEI / DXE / SMM stage of the UEFI firmware, when the CPU suspends the currently running code and enters the exception trap handler, the real-time debugger will use the serial port to disassemble the address of the running code at the time the CPU is suspended, the name of the module where the running code is located, and the running code itself, and then display the final assembly code, thereby displaying the current code information of the UEFI firmware and the state of the CPU.
[0020] Furthermore, by embedding the C language function table generated during the compilation process into the UEFI firmware, when displaying assembly code, the real-time debugger can find the corresponding C language function name at the address in the UEFI firmware's FV; when displaying call stack commands and assembly code, the corresponding function name is displayed simultaneously; thus achieving rapid location of the CPU's running code in the C file.
[0021] Beneficial effects: Compared with the prior art, the present invention has the following advantages:
[0022] The present invention provides a novel implementation method for a serial port-based firmware real-time debugger, which innovatively embeds debugger functionality into UEFI firmware, reducing the requirements and time required for ordinary developers to set up debugging environments. Simultaneously, it defines unified debugging commands, eliminating the differences in debugging commands across different systems (Windows / Linux). This allows developers to debug firmware and code generated by different architectures and compilers by familiarizing themselves with a single set of debugging commands, thereby saving developers' learning time. Attached Figure Description
[0023] Figure 1 This is a schematic diagram illustrating the implementation structure of a serial port-based firmware real-time debugger.
[0024] Figure 2 This is a function call stack diagram showing the implementation method of a serial port-based firmware real-time debugger. Detailed Implementation
[0025] The present invention will be further illustrated below with reference to specific embodiments. These embodiments are implemented based on the technical solutions of the present invention, and it should be understood that these embodiments are only used to illustrate the present invention and are not intended to limit the scope of the present invention.
[0026] The serial port-based firmware real-time debugger implementation method of this application utilizes the serial port of UEFI firmware as a channel for system debugging and code debugging, uses serial terminal software as the input and output interface for debugging commands, and embeds the debugger function into UEFI firmware to achieve rapid and efficient setup of the debugging environment. In addition, a unified set of debugging commands is used to debug firmware and code generated by different architectures and different compilers.
[0027] like Figure 1 As shown, the specific implementation steps are as follows:
[0028] (1) The UEFI firmware initializes the serial port to ensure that the serial port can perform read and write operations;
[0029] (2) Provide a proprietary exception trap handler for the just-in-time debugger in the UEFI firmware, replacing the exception trap handler in the original firmware. In the exception trap handler, the just-in-time debugger defines a unified command format, which can eliminate differences between different architectures and different compilers.
[0030] (3) The debugger’s exception trap handler parses and executes user commands through serial port read operations and outputs command-related results through serial port write operations.
[0031] The real-time debugger can achieve the following system debugging capabilities through calls to system libraries:
[0032] (1) Read and write operations of CPU internal registers; (2) Read and write operations of CPU status register (MSR); (3) Read and write operations of memory contents; (4) Read and write operations of PCI device configuration space; (5) Read and write operations of I / O (input / output) device ports; (6) UEFI-specific system debugging methods.
[0033] A real-time debugger utilizes the CPU's debugging capabilities to achieve the following code debugging abilities:
[0034] (1) Pause and run code; (2) Set and cancel software breakpoints; (3) Set and cancel hardware breakpoints (code execution breakpoints, memory read / write breakpoints, I / O port read / write breakpoints); (4) Display the status of code breakpoints; (5) Display information of the code call stack; (6) Display assembly code and single-step trace.
[0035] Regarding hardware breakpoints: In exception trap handlers, hardware breakpoints are set by the real-time debugger through the CPU's hardware debug register via serial port input commands. Hardware breakpoints are triggered under the following conditions:
[0036] (1) When the code runs to the specified address;
[0037] (2) When reading or writing a specified memory address;
[0038] (3) When reading or writing the specified I / O port.
[0039] Regarding software breakpoints: In the exception trap handler, the real-time debugger can set software breakpoints by inputting commands through the serial port, either by a specific address or by adding an embedded debugging instruction to the code. The software breakpoint will be triggered when the embedded debugging instruction is executed.
[0040] When the CPU suspends execution of the current code and enters an exception trap handler, the real-time debugger records the CPU's current operating parameters. Within the exception trap handler, entering a run command via the serial port exits the handler, restoring the CPU's operating parameters and allowing the CPU to continue execution.
[0041] This invention allows developers to understand the current code information and CPU status of the UEFI firmware: such as Figure 2 As shown, by adding disassembly modules of different architectures to the PEI / DXE / SMM stage of the UEFI firmware, when the CPU suspends the currently running code and enters the exception trap handler, the real-time debugger will use the serial port to disassemble the address of the running code at the time the CPU is suspended, the name of the module where the running code is located, and the running code, and then display the final assembly code.
[0042] During UEFI firmware operation, the CPU can be paused from running the current code and enter the exception trap handler in the following ways:
[0043] (1) Type the ctr1+b key combination in the serial port software;
[0044] (2) When the UEFI firmware encounters a CPU exception;
[0045] (3) When the UEFI firmware encounters a pre-set software or hardware breakpoint;
[0046] (4) When the UEFI firmware encounters other pre-set debug breakpoints.
[0047] When the CPU enters an exception trap handler, the real-time debugger saves the CPU's current runtime parameters and displays the relevant status and information of the code execution, as follows, to help users locate and debug problems:
[0048] (1) Display the current CPU number;
[0049] (2) Display the reason for the current CPU pause, including but not limited to user interrupt, software interrupt, hardware interrupt, conditional interrupt, abnormal interrupt, etc.;
[0050] (3) Display the address currently being executed by the CPU;
[0051] (4) Display the name of the function and module currently being executed by the CPU;
[0052] (5) Display the disassembled code currently being executed by the CPU.
[0053] This invention can help developers locate the position of CPU-running code in C files, facilitating problem identification and code debugging: such as Figure 2 As shown, by embedding the C language function table (function names and their offset addresses within the module) generated during compilation into the UEFI firmware, the real-time debugger can locate the corresponding C language function name at the specified address in the UEFI firmware's Function Values (FV) when displaying assembly code. The corresponding function names are displayed simultaneously when displaying call stack commands and assembly code. This helps developers locate the position of CPU-running code within the C file, facilitating problem identification and debugging. For example, embedding the C language function table allows for the display of C function names in the code call stack.
[0054] Furthermore, in the exception trap handling program, this invention allows the real-time debugger to perform single-step debugging functions such as step-in / step-out / step-over in assembly language via serial port input commands. Firmware condition breakpoints (specifically for UEFI firmware based on software breakpoints) can be preset, such as module loading breakpoints and SMM entry breakpoints.
[0055] The real-time debugger of this invention is compatible with various serial port software terminal types, eliminating the need to select the serial port software terminal type based on the UEFI firmware. Furthermore, it supports debugging code generated by different systems and compilers through a unified set of debugging commands.
[0056] In the specific execution of this invention, when the CPU enters the exception trap handler, it performs different system debugging operations according to the received debugging commands, as shown in Table 1:
[0057]
[0058]
[0059] Once the CPU enters the exception trap handler, if it receives debug commands such as S, SF, and SO, the just-in-time debugger can perform single-step tracing and other debugging on the assembly code. After each debug command is executed, the just-in-time debugger displays the latest runtime status and information of the code, facilitating continued tracing and debugging.
[0060] When the CPU enters the exception trap handler, if it receives the CS command, the real-time debugger will display the function call stack information of the UEFI firmware running code, including the address of the called function, the C language function name, and the name of the module it belongs to. This can help locate the position of the code execution.
[0061] Once the CPU enters the exception trap handler, if it receives MEM, IO, or PCI commands, the real-time debugger will display or modify the system and peripherals according to the specified size and format.
[0062] When the CPU enters the exception trap handler, if it receives the G, S, SF, and SO commands, the real-time debugger will exit the exception trap handler and restore the saved CPU operating parameters, allowing the CPU to continue running the firmware.
[0063] When the CPU enters the exception trap handler, if it receives the B and BH commands, the real-time debugger will create software and hardware breakpoints according to the specified addresses and methods. When the CPU executes to the specified breakpoint, it will enter the exception trap handler.
[0064] When the CPU enters the exception trap handler, if it receives the M command, the real-time debugger will display the module information that the firmware has already loaded. This information includes the module's loading sequence number, module name, module's loading address in memory, and size.
[0065] When the CPU enters the exception trap handler, if it receives the M command, the real-time debugger will display the module information that the firmware has already loaded. The information includes the module loading sequence number, module name, module loading address in memory, and size.
[0066] The above description is only a preferred embodiment of the present invention. It should be noted that for those skilled in the art, several improvements and modifications can be made without departing from the principle of the present invention, and these improvements and modifications should also be considered within the scope of protection of the present invention.
Claims
1. An implementation method of a serial port based firmware on-the-fly debugger, characterized in that: The serial port of the UEFI firmware is used as a channel for system debugging and code debugging, a serial terminal software is used as an input / output interface for debugging commands, and a debugger function is embedded in the UEFI firmware, so that a debugging environment is quickly and efficiently built, and a unified debugging command is used to debug firmware and code generated by different architectures and different compilers; the specific implementation steps are shown as steps 1-3: Step 1, the UEFI firmware initializes the serial port to ensure that the serial port can read and write; Step 2, a special exception trap processing program of the instant debugger is provided in the UEFI firmware, in which the instant debugger defines a unified command format to eliminate differences between different architectures and different compilers, and the specific implementation content is as follows: 2.1 The instant debugger realizes system debugging capability through the call of a system library: The content of system debugging includes reading and writing operations of CPU internal registers, reading and writing operations of CPU state registers, reading and writing operations of memory content, reading and writing operations of PCI device configuration space, reading and writing operations of I / O device ports, and a system debugging method of UEFI; 2.2 The instant debugger realizes code debugging capability by using the debugging function of the CPU: The content of code debugging includes pausing and running code, setting and canceling software breakpoints, setting and canceling hardware breakpoints, displaying the state of code breakpoints, displaying the information of code call stack, and displaying and single-step tracing of assembly code; Step 3, the exception trap processing program of the debugger realizes the analysis and execution of user commands through the reading operation of the serial port, and realizes the output of command-related results through the writing operation of the serial port; 3.1 In the exception trap processing program, the instant debugger sets a hardware breakpoint through the hardware debugging register of the CPU by inputting a command through the serial port; 3.2 In the exception trap processing program, the instant debugger sets a software breakpoint through a specific address or a module name and a function name by inputting a command through the serial port; 3.3 When the CPU pauses the current code to enter the exception trap processing program, the instant debugger records the current running parameters of the CPU, so that the developer can understand the current code information and the state of the CPU of the UEFI firmware, and help the user to locate the problem and debug, and the specific implementation content is as follows: By adding the disassembly modules of different architectures into the PEI / DXE / SMM stage of the UEFI firmware, when the CPU pauses the current running code to enter the exception trap processing program, the instant debugger displays the address of the running code at the time of pausing, the module name of the running code, and the disassembled code of the running code through the serial port, so as to realize the display of the current code information and the state of the CPU of the UEFI firmware; By embedding the C language function table generated in the compiling process into the UEFI firmware, when the assembly code is displayed, the instant debugger finds the C language function name corresponding to the address in the FV of the UEFI firmware; when the call stack command and the assembly code are displayed, the corresponding function name is also displayed; The position of the running code of the CPU in the C file is quickly located; 3.4 When in exception trap handler, the run command typed through serial port exits the exception trap handler, the on-the-fly debugger resumes the CPU run parameters so that the CPU continues execution; In the exception trap handler, the input command typed through serial port, the on-the-fly debugger completes the assembly language step-in / step-out / step-over, single-step trace debugging function, preset firmware conditional breakpoint, module loading breakpoint and SMM entry breakpoint function.
Citation Information
Patent Citations
Debugging method for linux operation system
CN106294147A
Symbol support in embedded firmware images
US20040054945A1