tinydbg Logging System Design
Multi-level Debugger and Logging Challenges
Modern debuggers are complex systems, typically containing multiple layers and components such as core debugging engines, RPC communication layers, function call handling, stack traces, etc. In such complex systems, without a well-designed logging system, the following problems can arise:
- Difficulty in Problem Location: When issues occur in the debugger, it's challenging to quickly identify which layer or component is affected
- Logging Chaos: Logs from different layers are mixed together, lacking clear classification and identification
- Incomplete Information: Missing critical context information, making it difficult to understand the specific scenarios where logs are generated
- Performance Impact: Improper logging can affect the debugger's performance
Therefore, a well-designed logging system is crucial for the development and maintenance of debuggers.
tinydbg's Logging System Design
tinydbg's logging system is implemented based on Go 1.21's slog
package with customized design. Its core design features are as follows:
Hierarchical Log Classification
tinydbg divides logs into multiple layers, each corresponding to different components of the debugger:
debugger
: Core debugger layer logsdebuglineerr
: DWARF line number information processing related error logsrpc
: RPC communication layer logsfncall
: Function call related logsstack
: Stack trace related logs
This classification gives logs a clear hierarchical structure, facilitating problem location and analysis.
Flexible Log Configuration
The logging system provides flexible configuration options:
Log Switch Control:
- Can globally enable/disable logging
- Can individually control logging switches for each layer
Log Output Targets:
- Supports output to file descriptors
- Supports output to file paths
- Default output to standard error
Log Levels:
- Supports Debug, Info, Warn, Error levels
- Each layer can independently set its log level
Structured Log Format
The logging system adopts a structured log format, with each log entry containing:
- Timestamp: Using RFC3339 format
- Log Level: In lowercase form (debug/info/warn/error)
- Context Attributes: Displayed in key=value format
- Log Message: Specific log content
Example log format:
2024-03-21T10:30:45Z debug layer=debugger,kind=fncall message content
Custom Handler Implementation
tinydbg implements a custom textHandler
that:
- Overrides the
slog.Handler
interface - Optimizes the log formatting process
- Supports attribute pre-formatting for better performance
- Implements flexible log level control
Convenient Logging Interfaces
Provides two sets of convenient logging interfaces:
Formatting Interfaces:
Debugf/Infof/Warnf/Errorf
: Supports formatted strings
Direct Interfaces:
Debug/Info/Warn/Error
: Direct parameter output
Each layer provides corresponding Logger retrieval functions, such as:
LogDebuggerLogger()
LogDebugLineLogger()
RPCLogger()
FnCallLogger()
StackLogger()
Summary
tinydbg's logging system design fully considers the characteristics and requirements of debuggers:
- Hierarchical Design: Through clear layer division, logs have better readability and maintainability
- Flexibility: Provides rich configuration options to meet different scenario requirements
- Performance Optimization: Ensures logging doesn't affect debugger performance through mechanisms like pre-formatting
- Usability: Provides simple and intuitive interfaces for developers
This design not only improves the maintainability of the debugger but also provides strong support for problem diagnosis and performance analysis. In practical use, developers can quickly locate issues, understand system behavior, and improve development efficiency.