ghostscope 0.1.1

Command-line entrypoint that drives GhostScope compiler, loader, and UI end-to-end.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
use crate::config::settings::SourceConfig;
use ghostscope_ui::events::{PathSubstitution, SourcePathInfo};
use std::path::{Path, PathBuf};
use tracing::{debug, warn};

/// Source path resolver for mapping DWARF compilation paths to runtime filesystem paths
#[derive(Debug)]
pub struct SourcePathResolver {
    // Config file rules (immutable baseline)
    config_substitutions: Vec<PathSubstitution>,
    config_search_dirs: Vec<String>,

    // Runtime-added rules (mutable)
    runtime_substitutions: Vec<PathSubstitution>,
    runtime_search_dirs: Vec<String>,
}

impl SourcePathResolver {
    /// Create new source path resolver from config
    pub fn new(config: &SourceConfig) -> Self {
        // Convert settings::PathSubstitution to events::PathSubstitution
        // This conversion is necessary to avoid circular dependencies between crates
        let config_substitutions = config
            .substitutions
            .iter()
            .map(Self::convert_substitution)
            .collect();

        Self {
            config_substitutions,
            config_search_dirs: config.search_dirs.clone(),
            runtime_substitutions: Vec::new(),
            runtime_search_dirs: Vec::new(),
        }
    }

    /// Convert config PathSubstitution to events PathSubstitution
    #[inline]
    fn convert_substitution(sub: &crate::config::settings::PathSubstitution) -> PathSubstitution {
        PathSubstitution {
            from: sub.from.clone(),
            to: sub.to.clone(),
        }
    }

    /// Resolve DWARF path to actual filesystem path
    ///
    /// Resolution strategy:
    /// 1. Try original path if it exists
    /// 2. Apply substitution rules (runtime first, then config)
    /// 3. Search in additional directories by basename
    pub fn resolve(&self, dwarf_path: &str) -> Option<PathBuf> {
        // Strategy 1: Try exact path
        let path = Path::new(dwarf_path);
        if path.exists() {
            debug!("Source path resolved (exact): {}", dwarf_path);
            return Some(path.to_path_buf());
        }

        // Strategy 2: Apply substitution rules (runtime > config) with boundary checking
        if let Some(substituted) = self.try_substitute_path(dwarf_path) {
            let new_path = PathBuf::from(&substituted);
            if new_path.exists() {
                debug!(
                    "Source path resolved (substitution): {} -> {}",
                    dwarf_path,
                    new_path.display()
                );
                return Some(new_path);
            }
        }

        // Strategy 3: Search in additional directories by basename
        // Note: Searches only in the root of each directory (non-recursive)
        // For example, /usr/local/src will find /usr/local/src/foo.c but not /usr/local/src/subdir/bar.c
        if let Some(basename) = path.file_name() {
            for search_dir in self
                .runtime_search_dirs
                .iter()
                .chain(self.config_search_dirs.iter())
            {
                let candidate = PathBuf::from(search_dir).join(basename);
                if candidate.exists() {
                    debug!(
                        "Source path resolved (search dir): {} -> {} (dir: {})",
                        dwarf_path,
                        candidate.display(),
                        search_dir
                    );
                    return Some(candidate);
                }
            }
        }

        warn!("Failed to resolve source path: {}", dwarf_path);
        None
    }

    /// Add search directory at runtime
    pub fn add_search_dir(&mut self, dir: String) {
        if !self.runtime_search_dirs.contains(&dir) {
            self.runtime_search_dirs.push(dir);
        }
    }

    /// Add path substitution at runtime
    /// If a mapping for the same 'from' prefix already exists, it will be updated with the new 'to' path
    pub fn add_substitution(&mut self, from: String, to: String) {
        // Check if a mapping for this 'from' prefix already exists
        if let Some(existing) = self
            .runtime_substitutions
            .iter_mut()
            .find(|s| s.from == from)
        {
            // Update existing mapping
            existing.to = to;
        } else {
            // Add new mapping
            self.runtime_substitutions
                .push(PathSubstitution { from, to });
        }
    }

    /// Remove rule from runtime (by pattern matching)
    /// Returns true if something was removed
    pub fn remove(&mut self, pattern: &str) -> bool {
        let mut removed = false;

        // Try to remove as search directory
        if let Some(pos) = self.runtime_search_dirs.iter().position(|d| d == pattern) {
            self.runtime_search_dirs.remove(pos);
            removed = true;
        }

        // Try to remove as substitution rule (match 'from' field)
        if let Some(pos) = self
            .runtime_substitutions
            .iter()
            .position(|s| s.from == pattern)
        {
            self.runtime_substitutions.remove(pos);
            removed = true;
        }

        removed
    }

    /// Clear all runtime rules
    pub fn clear_runtime(&mut self) {
        self.runtime_substitutions.clear();
        self.runtime_search_dirs.clear();
    }

    /// Reset to config-only rules
    pub fn reset(&mut self) {
        self.clear_runtime();
    }

    /// Try to substitute path prefix with proper boundary checking
    /// Returns the substituted path if a valid match is found
    ///
    /// Boundary checking ensures that:
    /// - `/build/my` does NOT match `/build/myproject/src/main.c`
    /// - `/build/myproject` DOES match `/build/myproject/src/main.c`
    /// - `/build/myproject` DOES match `/build/myproject` (exact match)
    fn try_substitute_path(&self, path: &str) -> Option<String> {
        // Try runtime substitutions first, then config substitutions
        for sub in self
            .runtime_substitutions
            .iter()
            .chain(self.config_substitutions.iter())
        {
            if let Some(suffix) = path.strip_prefix(&sub.from) {
                // Ensure boundary: suffix must be empty (exact match) or start with path separator
                // This prevents `/build/my` from matching `/build/myproject`
                if suffix.is_empty() || suffix.starts_with('/') {
                    return Some(format!("{}{}", sub.to, suffix));
                }
            }
        }
        None
    }

    /// Get all rules for display
    pub fn get_all_rules(&self) -> SourcePathInfo {
        let all_substitutions: Vec<PathSubstitution> = self
            .runtime_substitutions
            .iter()
            .chain(self.config_substitutions.iter())
            .cloned()
            .collect();

        let all_search_dirs: Vec<String> = self
            .runtime_search_dirs
            .iter()
            .chain(self.config_search_dirs.iter())
            .cloned()
            .collect();

        SourcePathInfo {
            substitutions: all_substitutions,
            search_dirs: all_search_dirs,
            runtime_substitution_count: self.runtime_substitutions.len(),
            runtime_search_dir_count: self.runtime_search_dirs.len(),
            config_substitution_count: self.config_substitutions.len(),
            config_search_dir_count: self.config_search_dirs.len(),
        }
    }

    /// Attempt to reverse-map a filesystem path back to the original DWARF path using substitutions.
    /// This helps when users provide a local path (after srcpath map) but the DWARF lookup
    /// requires the original compilation directory path. We only invert substitution rules; search
    /// directories are not invertible.
    pub fn reverse_map_to_dwarf(&self, fs_path: &str) -> Option<String> {
        // Try runtime substitutions first, then config substitutions (mirror forward priority)
        for sub in self
            .runtime_substitutions
            .iter()
            .chain(self.config_substitutions.iter())
        {
            if let Some(suffix) = fs_path.strip_prefix(&sub.to) {
                // Boundary check: suffix must be empty or start with path separator
                if suffix.is_empty() || suffix.starts_with('/') {
                    return Some(format!("{}{}", sub.from, suffix));
                }
            }
        }
        None
    }
}

/// Apply substitutions to directory path only (for info source)
pub fn apply_substitutions_to_directory(resolver: &SourcePathResolver, directory: &str) -> String {
    resolver
        .try_substitute_path(directory)
        .unwrap_or_else(|| directory.to_string())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::settings::{PathSubstitution as SettingsPathSubstitution, SourceConfig};

    /// Create a test resolver with given config
    fn create_test_resolver(
        config_subs: Vec<(&str, &str)>,
        config_search: Vec<&str>,
    ) -> SourcePathResolver {
        let config = SourceConfig {
            substitutions: config_subs
                .into_iter()
                .map(|(from, to)| SettingsPathSubstitution {
                    from: from.to_string(),
                    to: to.to_string(),
                })
                .collect(),
            search_dirs: config_search.into_iter().map(|s| s.to_string()).collect(),
        };
        SourcePathResolver::new(&config)
    }

    #[test]
    fn test_boundary_matching_prevents_partial_matches() {
        let resolver = create_test_resolver(vec![("/home/user", "/local/user")], vec![]);

        // Should NOT match: "/home/user" prefix but not at boundary
        let result = resolver.try_substitute_path("/home/username");
        assert_eq!(result, None);

        let result2 = resolver.try_substitute_path("/home/user2");
        assert_eq!(result2, None);

        // Should match: exact boundary
        let result3 = resolver.try_substitute_path("/home/user");
        assert_eq!(result3, Some("/local/user".to_string()));

        // Should match: with path separator
        let result4 = resolver.try_substitute_path("/home/user/project/main.c");
        assert_eq!(result4, Some("/local/user/project/main.c".to_string()));
    }

    #[test]
    fn test_runtime_substitutions_override_config() {
        let mut resolver = create_test_resolver(vec![("/build", "/config/path")], vec![]);

        // Add runtime substitution
        resolver.add_substitution("/build".to_string(), "/runtime/path".to_string());

        // Runtime should take precedence
        let result = resolver.try_substitute_path("/build/main.c");
        assert_eq!(result, Some("/runtime/path/main.c".to_string()));
    }

    #[test]
    fn test_apply_substitutions_to_directory() {
        let resolver = create_test_resolver(vec![("/usr/src/debug", "/home/user/sources")], vec![]);

        // Should substitute
        let result = apply_substitutions_to_directory(&resolver, "/usr/src/debug/myproject");
        assert_eq!(result, "/home/user/sources/myproject");

        // Should not substitute (no boundary)
        let result2 = apply_substitutions_to_directory(&resolver, "/usr/src/debug-backup");
        assert_eq!(result2, "/usr/src/debug-backup");

        // Should not substitute (no match)
        let result3 = apply_substitutions_to_directory(&resolver, "/other/path");
        assert_eq!(result3, "/other/path");
    }

    #[test]
    fn test_search_dir_management() {
        let mut resolver = create_test_resolver(vec![], vec!["/config/search"]);

        // Add runtime search dir
        resolver.add_search_dir("/runtime/search".to_string());

        // Check it's added
        let rules = resolver.get_all_rules();
        assert_eq!(rules.runtime_search_dir_count, 1);
        assert_eq!(rules.config_search_dir_count, 1);
        assert!(rules.search_dirs.contains(&"/runtime/search".to_string()));

        // Remove runtime search dir
        let removed = resolver.remove("/runtime/search");
        assert!(removed);

        let rules2 = resolver.get_all_rules();
        assert_eq!(rules2.runtime_search_dir_count, 0);
        assert!(!rules2.search_dirs.contains(&"/runtime/search".to_string()));
    }

    #[test]
    fn test_substitution_management() {
        let mut resolver = create_test_resolver(vec![("/config", "/cfg")], vec![]);

        // Add runtime substitution
        resolver.add_substitution("/runtime".to_string(), "/rt".to_string());

        let rules = resolver.get_all_rules();
        assert_eq!(rules.runtime_substitution_count, 1);
        assert_eq!(rules.config_substitution_count, 1);

        // Remove runtime substitution by 'from' pattern
        let removed = resolver.remove("/runtime");
        assert!(removed);

        let rules2 = resolver.get_all_rules();
        assert_eq!(rules2.runtime_substitution_count, 0);

        // Config substitution should remain
        assert_eq!(rules2.config_substitution_count, 1);
    }

    #[test]
    fn test_clear_and_reset() {
        let mut resolver = create_test_resolver(vec![("/config", "/cfg")], vec!["/config/dir"]);

        // Add runtime rules
        resolver.add_substitution("/runtime".to_string(), "/rt".to_string());
        resolver.add_search_dir("/runtime/dir".to_string());

        // Clear runtime
        resolver.clear_runtime();

        let rules = resolver.get_all_rules();
        assert_eq!(rules.runtime_substitution_count, 0);
        assert_eq!(rules.runtime_search_dir_count, 0);
        assert_eq!(rules.config_substitution_count, 1);
        assert_eq!(rules.config_search_dir_count, 1);

        // Reset (same as clear_runtime)
        resolver.add_substitution("/temp".to_string(), "/tmp".to_string());
        resolver.reset();

        let rules2 = resolver.get_all_rules();
        assert_eq!(rules2.runtime_substitution_count, 0);
    }

    #[test]
    fn test_duplicate_prevention() {
        let mut resolver = create_test_resolver(vec![], vec![]);

        // Add same substitution twice (same from and to)
        resolver.add_substitution("/path".to_string(), "/new".to_string());
        resolver.add_substitution("/path".to_string(), "/new".to_string());

        let rules = resolver.get_all_rules();
        assert_eq!(rules.runtime_substitution_count, 1);

        // Add same search dir twice
        resolver.add_search_dir("/search".to_string());
        resolver.add_search_dir("/search".to_string());

        let rules2 = resolver.get_all_rules();
        assert_eq!(rules2.runtime_search_dir_count, 1);
    }

    #[test]
    fn test_update_existing_substitution() {
        let mut resolver = create_test_resolver(vec![], vec![]);

        // Add initial mapping
        resolver.add_substitution("/build".to_string(), "/wrong/path".to_string());

        // Verify initial mapping
        let result = resolver.try_substitute_path("/build/main.c");
        assert_eq!(result, Some("/wrong/path/main.c".to_string()));

        let rules = resolver.get_all_rules();
        assert_eq!(rules.runtime_substitution_count, 1);

        // Update the same 'from' prefix with a new 'to' path
        resolver.add_substitution("/build".to_string(), "/correct/path".to_string());

        // Should still have only 1 substitution (updated, not duplicated)
        let rules2 = resolver.get_all_rules();
        assert_eq!(rules2.runtime_substitution_count, 1);

        // Verify the mapping was updated to use the new path
        let result2 = resolver.try_substitute_path("/build/main.c");
        assert_eq!(result2, Some("/correct/path/main.c".to_string()));

        // Verify the old path is no longer used
        assert!(rules2
            .substitutions
            .iter()
            .any(|s| s.from == "/build" && s.to == "/correct/path"));
        assert!(!rules2
            .substitutions
            .iter()
            .any(|s| s.from == "/build" && s.to == "/wrong/path"));
    }
}