tinydbg Configuration System Design and Implementation
tinydbg provides a flexible configuration system that allows users to customize the debugger's behavior according to their preferences. This article details the design and implementation of the configuration system.
Configuration Command Usage
tinydbg provides the following configuration commands:
config -list
: List all available configuration items and their current valuesconfig -save
: Save current configuration to the config fileconfig <name> <value>
: Set the value of a specified configuration item
Supported Configuration Items
tinydbg supports the following configuration items:
Command Aliases (aliases)
- Allows creating aliases for commands
- Example:
config alias print p
setsp
as an alias for theprint
command
Source Path Substitution Rules (substitute-path)
- Used to rewrite source code paths stored in program debug information
- Particularly useful when source code is moved between compilation and debugging
- Supports the following operations:
config substitute-path <from> <to>
: Add a substitution ruleconfig substitute-path <from>
: Delete a specified ruleconfig substitute-path -clear
: Clear all rulesconfig substitute-path -guess
: Automatically guess substitution rules
String Length Limit (max-string-len)
- Controls the maximum string length read when using print, locals, args, and vars commands
- Default value: 64
Array Value Limit (max-array-values)
- Controls the maximum number of array items read when using print, locals, args, and vars commands
- Default value: 64
Variable Recursion Depth (max-variable-recurse)
- Controls the output evaluation depth for nested struct members, array and slice items, and dereferenced pointers
- Default value: 1
Disassembly Style (disassemble-flavor)
- Allows users to specify the syntax style for assembly output
- Available values: "intel"(default), "gnu", "go"
Location Expression Display (show-location-expr)
- Controls whether the whatis command prints DWARF location expressions for its arguments
Source Code List Color Settings
source-list-line-color
: Source code line number colorsource-list-arrow-color
: Source code arrow colorsource-list-keyword-color
: Source code keyword colorsource-list-string-color
: Source code string colorsource-list-number-color
: Source code number colorsource-list-comment-color
: Source code comment colorsource-list-tab-color
: Source code tab color
Other Display Settings
prompt-color
: Prompt line colorstacktrace-function-color
: Function name color in stack tracestacktrace-basename-color
: Path basename color in stack tracesource-list-line-count
: Number of lines to display above and below cursor when calling printfile()position
: Controls how program current position is displayed (source/disassembly/default)tab
: Controls what is printed when encountering '\t' in source code
Configuration File Storage
Configuration files are stored in the following locations:
If
XDG_CONFIG_HOME
environment variable is set:$XDG_CONFIG_HOME/tinydbg/config.yml
On Linux systems:
$HOME/.config/tinydbg/config.yml
Other systems:
$HOME/.tinydbg/config.yml
Configuration Implementation Details
Configuration Loading
The configuration system loads configurations through the LoadConfig()
function in pkg/config/config.go
:
- First checks and creates the configuration directory
- Checks for existence of old version configuration files and migrates to new location if found
- Opens the configuration file, creates default configuration if it doesn't exist
- Uses YAML parser to parse configuration file content into the
Config
struct
Configuration Application
Main application points of configuration in the debugger:
Command Aliases
- Merged into the command system during
DebugSession
initialization viacmds.Merge(conf.Aliases)
- Allows users to use custom short commands
- Merged into the command system during
Path Substitution
- Applied through the
substitutePath()
method - Used when finding source code locations to ensure the debugger can find the correct source files
- Applied through the
Variable Loading Configuration
- Converts configuration to
api.LoadConfig
through theloadConfig()
method - Affects behavior of variable viewing commands (such as print, locals, args, etc.)
- Controls limits for string length, array size, and recursion depth
- Converts configuration to
Display Settings
- Affects debugger output format and colors
- Applied through terminal output functions
- Controls display method for source code listing and stack traces
Configuration Saving
Configuration is saved through the SaveConfig()
function:
- Serializes the
Config
struct to YAML format - Writes to the configuration file
- Maintains persistence of user custom settings
Usage Examples
Setting command aliases:
config alias print p config alias next n
Configuring source path substitution:
config substitute-path /original/path /new/path
Adjusting variable display limits:
config max-string-len 128 config max-array-values 100 config max-variable-recurse 2
Customizing display settings:
config source-list-line-count 10 config disassemble-flavor gnu
These configurations help users optimize their debugging experience and improve debugging efficiency according to their needs.