Skip to content

Commit 4d1cf1c

Browse files
authored
feat: add srcpath command for source path resolution (#11)
1 parent 566ddb8 commit 4d1cf1c

21 files changed

Lines changed: 1982 additions & 154 deletions

config.toml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,45 @@ perf_page_count = 32
137137
# Set to true to force PerfEventArray even on kernels that support RingBuf.
138138
# Default: false
139139
force_perf_event_array = false
140+
141+
# Source code path configuration
142+
# When DWARF debug info contains compilation-time paths that differ from runtime paths,
143+
# use these settings to help ghostscope locate the actual source files.
144+
145+
[source]
146+
# Path substitution rules (applied first, highest priority)
147+
# Replaces compilation-time path prefixes with runtime path prefixes.
148+
# Useful when source code was compiled on a different machine or moved to a new location.
149+
#
150+
# Example scenarios:
151+
# - Compiled on CI server: /home/build/project -> /home/user/work/project
152+
# - Kernel sources moved: /usr/src/linux-5.15 -> /home/user/kernel/linux-5.15
153+
# - Cross-compilation: /buildroot/arm/src -> /local/embedded/src
154+
#
155+
# Format: array of { from = "compile_path_prefix", to = "runtime_path_prefix" }
156+
substitutions = [
157+
# { from = "/home/build/myproject", to = "/home/user/work/myproject" },
158+
# { from = "/usr/src/linux", to = "/home/user/kernel/linux" },
159+
]
160+
161+
# Additional search directories (fallback when substitution fails)
162+
# When a source file cannot be found via substitution, ghostscope will search
163+
# these directories by filename (basename matching).
164+
# Similar to GDB's "directory" command.
165+
#
166+
# Format: array of directory paths
167+
search_dirs = [
168+
# "/home/user/sources",
169+
# "/opt/local/src",
170+
]
171+
172+
# Runtime configuration:
173+
# You can also configure source paths interactively using the 'srcpath' command:
174+
# srcpath - Show current configuration
175+
# srcpath map <from> <to> - Add path substitution rule
176+
# srcpath add <directory> - Add search directory
177+
# srcpath remove <path> - Remove a rule
178+
# srcpath clear - Clear all runtime rules
179+
# srcpath reset - Reset to config file rules
180+
#
181+
# Runtime rules take precedence over config file rules and are not persisted.

docs/input-commands.md

Lines changed: 300 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,216 @@ i a <addr> # Short form
322322

323323
---
324324

325+
## 🗂️ Source Path Commands
326+
327+
Commands for managing source code path mappings when DWARF debug information contains compilation-time paths that differ from runtime paths.
328+
329+
### srcpath - Show Path Mappings
330+
331+
**Syntax:**
332+
```
333+
srcpath
334+
```
335+
336+
**Description:**
337+
Display current path substitution rules and search directories. Shows both runtime-added rules and config file rules.
338+
339+
### srcpath add - Add Search Directory
340+
341+
**Syntax:**
342+
```
343+
srcpath add <directory>
344+
```
345+
346+
**Parameters:**
347+
- `<directory>`: Directory path to search for source files
348+
349+
**Examples:**
350+
```
351+
srcpath add /usr/local/src # Add search directory
352+
srcpath add /home/user/sources # Add user sources directory
353+
```
354+
355+
**Description:**
356+
When a source file cannot be found via exact path or substitution, GhostScope will search in the **root** of these directories by filename (basename matching, non-recursive). For example, after adding `/usr/local/src`, it can find `/usr/local/src/foo.c` but not `/usr/local/src/subdir/bar.c`.
357+
358+
### srcpath map - Map Compilation Path to Runtime Path
359+
360+
**Syntax:**
361+
```
362+
srcpath map <from> <to>
363+
```
364+
365+
**Parameters:**
366+
- `<from>`: Compilation-time path prefix (from DWARF debug info)
367+
- `<to>`: Runtime path prefix (actual location on this machine)
368+
369+
**Examples:**
370+
```
371+
srcpath map /build/project /home/user/project # CI build path to local
372+
srcpath map /usr/src/linux-5.15 /home/user/kernel # Kernel sources moved
373+
srcpath map /buildroot/arm /local/embedded # Cross-compilation paths
374+
```
375+
376+
**Description:**
377+
Replaces path prefixes from compilation time with runtime paths. If you run the same mapping twice with different `<to>` paths, the second one will update the existing mapping.
378+
379+
### srcpath remove - Remove Mapping or Directory
380+
381+
**Syntax:**
382+
```
383+
srcpath remove <path>
384+
```
385+
386+
**Parameters:**
387+
- `<path>`: Path prefix of a mapping or search directory to remove
388+
389+
**Examples:**
390+
```
391+
srcpath remove /build/project # Remove mapping with this 'from' prefix
392+
srcpath remove /usr/local/src # Remove search directory
393+
```
394+
395+
**Description:**
396+
Removes a runtime-added rule (mapping or search directory). Config file rules cannot be removed via this command.
397+
398+
### srcpath clear - Clear All Runtime Rules
399+
400+
**Syntax:**
401+
```
402+
srcpath clear
403+
```
404+
405+
**Description:**
406+
Clears all runtime-added rules (both mappings and search directories). Config file rules are preserved.
407+
408+
### srcpath reset - Reset to Config Rules
409+
410+
**Syntax:**
411+
```
412+
srcpath reset
413+
```
414+
415+
**Description:**
416+
Removes all runtime-added rules and resets to config file rules only. Same as `srcpath clear`.
417+
418+
---
419+
420+
### Path Resolution Mechanism
421+
422+
GhostScope uses a **relative path + directory prefix** approach to locate source files:
423+
424+
1. **DWARF Information Contains**:
425+
- Compilation directory (comp_dir): e.g., `/home/build/nginx-1.27.1`
426+
- Source file relative path: e.g., `src/core/nginx.c`
427+
428+
2. **Path Composition**:
429+
- Full path = Compilation directory + Relative path
430+
- Example: `/home/build/nginx-1.27.1/src/core/nginx.c`
431+
432+
3. **Resolution Order**:
433+
- **First**: Try original full path
434+
- **Then**: Apply `map` substitution rules (recommended)
435+
- **Last**: Search in `add` directories by filename
436+
437+
### Recommended Usage
438+
439+
#### 🌟 Recommended: Use `srcpath map` to Map DWARF Directory
440+
441+
Map the compilation directory from DWARF to your local source directory. This makes **all relative path files automatically resolve**:
442+
443+
```bash
444+
# Check file loading error message for DWARF directory
445+
# Error displays:
446+
# DWARF Directory: /home/build/nginx-1.27.1
447+
# Relative Path: src/core/nginx.c
448+
449+
# Map DWARF directory to local path
450+
srcpath map /home/build/nginx-1.27.1 /home/user/nginx-1.27.1
451+
452+
# Now all files can be found:
453+
# /home/build/nginx-1.27.1/src/core/nginx.c → /home/user/nginx-1.27.1/src/core/nginx.c
454+
# /home/build/nginx-1.27.1/src/http/ngx_http.c → /home/user/nginx-1.27.1/src/http/ngx_http.c
455+
```
456+
457+
**Advantages**:
458+
- ✅ One-time setup, works for all files
459+
- ✅ Preserves directory structure, easy to understand
460+
- ✅ Supports multi-level directories and complex projects
461+
- ✅ File search (`o` key) automatically updates paths
462+
463+
#### Auxiliary: Use `srcpath add` for Search Directories
464+
465+
Only use when `map` cannot solve the problem (e.g., header files scattered across multiple locations):
466+
467+
```bash
468+
# Add additional search directories
469+
srcpath add /usr/local/include
470+
srcpath add /opt/vendor/include
471+
```
472+
473+
**Note**: `add` searches by **filename** (basename) only in directory root (non-recursive), cannot handle subdirectories, and may not find the correct file when names conflict.
474+
475+
### Configuration File Support
476+
477+
Path mappings can be persisted in `config.toml` to avoid manual configuration each time:
478+
479+
```toml
480+
[source]
481+
# Recommended: DWARF directory mappings
482+
substitutions = [
483+
{ from = "/home/build/myproject", to = "/home/user/work/myproject" },
484+
{ from = "/usr/src/linux-5.15", to = "/home/user/kernel/linux-5.15" },
485+
]
486+
487+
# Auxiliary: Additional search directories (searches by filename)
488+
search_dirs = [
489+
"/usr/local/include",
490+
"/opt/local/src",
491+
]
492+
```
493+
494+
Runtime rules (via commands) take priority over config file rules.
495+
496+
### Common Use Cases
497+
498+
**Scenario 1: Source compiled on CI server** ⭐ Recommended
499+
```bash
500+
# Check DWARF Directory in error message
501+
# Then map it to your local source root
502+
srcpath map /home/jenkins/workspace/myproject /home/user/myproject
503+
```
504+
505+
**Scenario 2: Compiled in container, debugging locally** ⭐ Recommended
506+
```bash
507+
# Docker container compilation directory: /build/app
508+
# Local source at: /home/user/app
509+
srcpath map /build/app /home/user/app
510+
```
511+
512+
**Scenario 3: Multiple independent header directories**
513+
```bash
514+
# System headers and third-party library headers
515+
srcpath add /usr/local/include
516+
srcpath add /opt/project/vendor
517+
```
518+
519+
**Scenario 4: Correcting a wrong mapping**
520+
```bash
521+
srcpath map /build /wrong/path # First attempt (wrong)
522+
srcpath map /build /correct/path # Second attempt (updates existing mapping)
523+
```
524+
525+
### Best Practices
526+
527+
1. **Prefer `map`**: Map DWARF compilation directory, not individual files
528+
2. **Check error messages**: File loading failures display DWARF Directory, map it directly
529+
3. **Preserve directory structure**: Keep same relative path structure as during compilation
530+
4. **Save to config**: Save common mappings to `config.toml` to avoid repeated configuration
531+
5. **Use `add` cautiously**: Only use when `map` cannot solve the problem, as it only searches by filename in directory root (non-recursive), cannot handle subdirectories, and may find wrong files with same names
532+
533+
---
534+
325535
## ⚙️ Control Commands
326536

327537
General control and utility commands.
@@ -376,16 +586,98 @@ Exit GhostScope. You can also use `Ctrl+C` twice to quit.
376586
| `info address` | `i a` | View address info |
377587
| `save traces` | `s t` | Save trace points |
378588
| `source` | `s` | Load script (except "s t") |
589+
| `srcpath` | - | Manage source path mappings |
379590

380591
---
381592

382-
## Command Completion
593+
## Command Completion & History
594+
595+
### Command Completion
596+
597+
GhostScope provides a multi-layered intelligent completion system:
598+
599+
#### 1. Tab Command Completion
600+
601+
Press `Tab` to trigger command and parameter completion:
602+
603+
- **Command Completion**: Auto-complete GhostScope commands (trace, enable, info, etc.)
604+
- **File Path Completion**: Auto-complete file paths for source command
605+
- **Parameter Completion**: Context-aware parameter suggestions
606+
607+
**Examples**:
608+
```
609+
gs > t<Tab> → trace
610+
gs > info s<Tab> → info source
611+
gs > source /pa<Tab> → source /path/to/script.gs
612+
```
613+
614+
#### 2. Auto-suggestion (Gray Hints)
615+
616+
As you type, GhostScope displays gray suggestion text based on command history:
617+
618+
- **Trigger Condition**: After typing 3+ characters
619+
- **Source**: Matches prefix from command history
620+
- **Accept Suggestion**: Press `` (Right arrow) or `Ctrl+E`
621+
- **Ignore Suggestion**: Continue typing or move cursor
622+
623+
**Example**:
624+
```
625+
gs > trace main<typing>
626+
trace main -s "print(a, b, c)" ← Gray suggestion
627+
628+
Press → to accept the entire suggestion
629+
```
630+
631+
#### 3. File Name Completion
383632

384-
GhostScope provides intelligent command completion in Input Mode:
633+
For commands requiring file paths, Tab completion supports:
385634

386-
1. **Tab Completion**: Press `Tab` to auto-complete commands and filenames
387-
2. **Auto-suggestion**: Gray suggestion text appears as you type, press `` or `Ctrl+E` to accept
388-
3. **Smart Matching**: Completion works for both full commands and shortcuts
635+
- **Relative Paths**: From current directory
636+
- **Absolute Paths**: From root directory
637+
- **Multi-level Navigation**: Navigate through directory hierarchies
638+
- **Smart Filtering**: Only show relevant file types (.gs script files)
639+
640+
### Command History
641+
642+
GhostScope automatically saves command history for quick reuse and search:
643+
644+
#### History Navigation
645+
646+
| Shortcut | Function |
647+
|----------|----------|
648+
| `` or `Ctrl+P` | Previous history command |
649+
| `` or `Ctrl+N` | Next history command |
650+
651+
#### History Persistence
652+
653+
- **Auto-save**: Commands automatically saved to `.ghostscope_history` file
654+
- **Cross-session**: History shared across different GhostScope sessions
655+
- **Deduplication**: Consecutive duplicate commands not recorded
656+
- **Capacity Limit**: Stores up to 1000 recent commands by default
657+
658+
#### History Management
659+
660+
Use `clear` command to clear history:
661+
662+
```bash
663+
gs > clear # Clear command history
664+
```
665+
666+
**Note**: Clearing history deletes all records in `.ghostscope_history` file.
667+
668+
### Completion Configuration
669+
670+
History and completion behavior can be adjusted in config file (see [Configuration](configuration.md)):
671+
672+
```toml
673+
[history]
674+
enabled = true # Enable history recording
675+
max_entries = 1000 # Maximum history entries
676+
677+
[auto_suggestion]
678+
enabled = true # Enable auto-suggestion
679+
min_chars = 3 # Minimum chars to trigger suggestion
680+
```
389681

390682
---
391683

@@ -394,10 +686,9 @@ GhostScope provides intelligent command completion in Input Mode:
394686
1. **Use Shortcuts**: Master command shortcuts (`t`, `en`, `dis`) for faster workflow
395687
2. **Tab Completion**: Use `Tab` extensively for completion and command discovery
396688
3. **History Navigation**: Use `Ctrl+P`/`Ctrl+N` or ``/`` to quickly reuse commands
397-
4. **History Search**: Press `Ctrl+R` to search through command history
398-
5. **Batch Operations**: Use `all` parameter to operate on all traces at once
399-
6. **Save Sessions**: Regularly save your trace configuration with `save traces`
400-
7. **Script Reuse**: Save common trace patterns in files and load with `source`
689+
4. **Batch Operations**: Use `all` parameter to operate on all traces at once
690+
5. **Save Sessions**: Regularly save your trace configuration with `save traces`
691+
6. **Script Reuse**: Save common trace patterns in files and load with `source`
401692

402693
---
403694

0 commit comments

Comments
 (0)