cargo-mate 1.0.45

Rust development companion that enhances cargo with intelligent workflows, state management, performance optimization, and comprehensive project monitoring
Documentation
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
# 🚀 **Cargo Mate Performance Tools - Complete Command Reference**

## **Overview**

Cargo Mate now includes three powerful performance analysis tools to help you optimize your Rust applications:

- **`bloat-check`** - Binary size analysis and optimization
- **`cache-analyzer`** - CPU cache usage and performance monitoring
- **`async-lint`** - Async programming pattern detection and improvement

---

## **🎯 Performance Tools Quick Start**

### **Get Help**
```bash
cm tool list                    # See all available tools
cm tool help bloat-check       # Help for bloat-check
cm tool help cache-analyzer    # Help for cache-analyzer
cm tool help async-lint        # Help for async-lint
```

### **Quick Performance Check**
```bash
# Check binary size
cm tool bloat-check --binary target/release/myapp

# Analyze cache usage
cm tool cache-analyzer --target target/release/myapp --functions process_data,handle_request

# Lint async code
cm tool async-lint --input src/ --blocking --await --deadlock
```

---

## **📊 1. bloat-check - Binary Size Analysis**

### **What it does:**
Analyzes your compiled binary size, tracks changes between builds, identifies bloat sources, and suggests optimizations.

### **Perfect for:**
- Reducing application size
- Tracking size regressions
- Optimizing for embedded systems
- Understanding what's taking up space

### **Basic Usage:**
```bash
# Analyze current binary
cm tool bloat-check --binary target/release/myapp

# Compare with previous version
cm tool bloat-check --binary target/release/myapp --baseline previous-build/myapp

# Generate optimization report
cm tool bloat-check --binary target/release/myapp --optimize --report
```

### **Command Options:**

| Flag | Description | Example |
|------|-------------|---------|
| `--binary` | Path to binary to analyze | `--binary target/release/app` |
| `--baseline` | Baseline binary for comparison | `--baseline old-build/app` |
| `--threshold` | Size change threshold (%) | `--threshold 5.0` |
| `--symbols` | Show largest symbols | `--symbols` |
| `--debug-compare` | Compare debug vs release | `--debug-compare` |
| `--optimize` | Generate optimization suggestions | `--optimize` |
| `--report` | Generate detailed size report | `--report` |

### **Example Output:**
```
📊 Binary Size Analysis Report
═══════════════════════════════════════════════

Binary: target/release/myapp
Size: 2.4 MB
Symbols: 1,247

📈 Size Changes:
• Total size: +156 KB (+6.9%)
• Text section: +89 KB (+12.3%) ⚠️
• Data section: +45 KB (+3.2%)
• BSS section: +22 KB (+15.1%) ⚠️

🔍 Largest Symbols:
1. function_a (45.2 KB) - Consider inlining or splitting
2. static_data_b (32.8 KB) - Large static data structure

💡 Optimization Suggestions:
• Use #[inline] for frequently called small functions
• Replace large static arrays with dynamic allocation
```

### **Pro Tips:**
- Run after each major feature addition
- Use `--threshold 1.0` for strict monitoring
- Combine `--symbols --optimize` for detailed analysis
- Compare debug vs release to understand build impact

---

## **🔍 2. cache-analyzer - CPU Cache Analysis**

### **What it does:**
Monitors CPU cache usage, detects cache-unfriendly patterns, analyzes data structures, and suggests cache optimizations.

### **Perfect for:**
- Performance-critical applications
- High-throughput systems
- Memory-bound workloads
- Game development

### **Basic Usage:**
```bash
# Analyze specific functions
cm tool cache-analyzer --target target/release/myapp --functions process_data,handle_request

# Analyze data structures in source
cm tool cache-analyzer --target src/main.rs --data-structures --false-sharing

# Use Linux perf for profiling
cm tool cache-analyzer --target target/release/myapp --perf --threshold 10.0
```

### **Command Options:**

| Flag | Description | Example |
|------|-------------|---------|
| `--target` | Binary or source file to analyze | `--target target/release/app` |
| `--functions` | Functions to analyze (comma-separated) | `--functions func1,func2` |
| `--perf` | Use Linux perf for profiling | `--perf` |
| `--cachegrind` | Use cachegrind for analysis | `--cachegrind` |
| `--data-structures` | Analyze struct layouts | `--data-structures` |
| `--false-sharing` | Detect false sharing issues | `--false-sharing` |
| `--prefetch` | Analyze prefetching efficiency | `--prefetch` |
| `--threshold` | Cache miss rate threshold (%) | `--threshold 5.0` |

### **Example Output:**
```
🔍 CPU Cache Analysis Report
═══════════════════════════════════════════════

Function: process_large_dataset
Cache Miss Rate: 12.3% (High ⚠️)
L1 Cache Hits: 87.7%
L2 Cache Hits: 95.2%

🚨 Issues Detected:
1. High cache misses in loop at line 142
   - Array access pattern causes cache thrashing
   - Consider changing iteration order

📊 Data Structure Analysis:
Struct: LargeDataStructure (size: 128 bytes)
• Cache line alignment: Poor (spans 2 cache lines)
• Hot fields: data[0], metadata.status
• Cold fields: debug_info, timestamps

💡 Optimization Suggestions:
• Reorder struct fields by access frequency
• Use cache-aligned allocations for hot data
```

### **Pro Tips:**
- Focus on hot functions first
- Use `--data-structures` on your main structs
- Look for cache miss rates > 5%
- Consider SOA (Struct of Arrays) for cache optimization

---

## **⚡ 3. async-lint - Async Pattern Detection**

### **What it does:**
Detects common async programming pitfalls, finds blocking operations in async contexts, and suggests improvements.

### **Perfect for:**
- Async web servers
- Concurrent applications
- Network services
- Real-time systems

### **Basic Usage:**
```bash
# Analyze entire source directory
cm tool async-lint --input src/ --blocking --await --deadlock

# Focus on specific issues
cm tool async-lint --input src/main.rs --blocking --fix

# Strict analysis with custom rules
cm tool async-lint --input src/ --strict --ignore async-move,unnecessary-await
```

### **Command Options:**

| Flag | Description | Example |
|------|-------------|---------|
| `--input` | Source file or directory | `--input src/` |
| `--blocking` | Detect blocking operations | `--blocking` |
| `--await` | Analyze await patterns | `--await` |
| `--deadlock` | Detect deadlock risks | `--deadlock` |
| `--concurrency` | Analyze concurrent operations | `--concurrency` |
| `--fix` | Generate fix suggestions | `--fix` |
| `--strict` | Enable strict linting | `--strict` |
| `--ignore` | Rules to ignore (comma-separated) | `--ignore rule1,rule2` |

### **Example Output:**
```
⚡ Async Pattern Analysis Report
═══════════════════════════════════════════════

File: src/handlers.rs
Issues Found: 5

🚨 Blocking Operations in Async Context:
1. Line 45: std::fs::read() in async function
   • Use tokio::fs::read() instead
   • Impact: Blocks async runtime

🔄 Async/Await Issues:
2. Line 123: Unnecessary await on immediate value
   • Code: let x = async { 42 }.await;
   • Suggestion: let x = 42;

🔒 Deadlock Risks:
3. Line 201: Potential deadlock in select! macro
   • Multiple futures competing for same resource
   • Suggestion: Use try_select! or restructure

💡 Improvement Suggestions:
• Replace std::fs with tokio::fs for async file operations
• Use tokio::time::sleep for async delays
```

### **Pro Tips:**
- Run on all async code
- Use `--fix` to get actionable suggestions
- Pay special attention to blocking operations
- Use `--strict` for production code

---

## **🎨 Output Formats**

All tools support multiple output formats:

### **Human Readable (Default)**
```bash
cm tool bloat-check --binary myapp
```

### **JSON Output**
```bash
cm tool bloat-check --binary myapp --output json
```

### **Table Format**
```bash
cm tool bloat-check --binary myapp --output table
```

### **Verbose Mode**
```bash
cm tool bloat-check --binary myapp --verbose
```

---

## **🔧 Common Workflows**

### **Performance Investigation Workflow:**
```bash
# 1. Check binary size first
cm tool bloat-check --binary target/release/app --optimize

# 2. If size is okay, check cache usage
cm tool cache-analyzer --target target/release/app --perf --functions hot_function

# 3. If async, check for patterns
cm tool async-lint --input src/ --blocking --deadlock --fix
```

### **CI/CD Integration:**
```bash
# In your CI pipeline
cm tool bloat-check --binary target/release/app --threshold 2.0
cm tool async-lint --input src/ --strict --blocking
```

### **Development Workflow:**
```bash
# After major changes
cm tool bloat-check --binary target/release/app --baseline main-build/app
cm tool cache-analyzer --target src/ --data-structures --false-sharing
```

---

## **⚠️ Requirements & Dependencies**

### **System Requirements:**
- **Linux**: `perf` tool for cache analysis (`apt install linux-tools-common`)
- **macOS**: Limited cache analysis (perf not available)
- **Windows**: Limited functionality (external tools may not work)

### **Rust Dependencies:**
All tools use existing dependencies:
- `syn` - For AST parsing
- `quote` - For code generation
- `clap` - For CLI parsing
- `colored` - For colored output
- `serde_json` - For JSON output

### **External Tools:**
- `size` - For binary section analysis
- `nm` - For symbol analysis
- `perf` - For cache profiling (optional)

---

## **🚀 Advanced Usage Examples**

### **Comprehensive Performance Audit:**
```bash
# Run all performance tools
cm tool bloat-check --binary target/release/app --symbols --optimize --report
cm tool cache-analyzer --target target/release/app --perf --data-structures --prefetch
cm tool async-lint --input src/ --blocking --await --deadlock --concurrency --fix
```

### **Monitoring Performance Regression:**
```bash
# Before changes
cp target/release/app baseline-app
cm tool bloat-check --binary target/release/app --report > baseline-report.txt

# After changes
cm tool bloat-check --binary target/release/app --baseline baseline-app --threshold 1.0
```

### **Async Code Review:**
```bash
# Review async patterns before merge
cm tool async-lint --input src/ --strict --blocking --deadlock --concurrency --output json | jq '.issues'
```

### **Cache Optimization:**
```bash
# Find cache issues in hot path
cm tool cache-analyzer --target target/release/app --functions hot_function1,hot_function2 --perf --threshold 2.0
```

---

## **📈 Performance Impact**

### **Expected Improvements:**
- **Binary Size**: 10-30% reduction with optimizations
- **Cache Performance**: 20-50% improvement in cache hit rates
- **Async Performance**: 2-10x throughput improvements

### **Typical Use Cases:**
- **Web Services**: 15-25% performance improvement
- **Data Processing**: 30-40% cache efficiency gains
- **Embedded Systems**: 20-35% binary size reduction
- **Games**: 25-45% cache-related performance boosts

---

## **🔍 Troubleshooting**

### **Common Issues:**

**"Tool not available" errors:**
```bash
# Install required tools
sudo apt install linux-tools-common  # Linux
# macOS: Limited functionality expected
```

**"Binary not found" errors:**
```bash
# Build your project first
cargo build --release
cm tool bloat-check --binary target/release/your-app
```

**"Failed to parse Rust file" errors:**
```bash
# Check syntax
cargo check
# Ensure file is valid Rust code
```

**"Permission denied" errors:**
```bash
# Make binary executable
chmod +x target/release/your-app
```

### **Performance Tips:**
- Run tools on release builds for accurate results
- Use `--verbose` for detailed debugging information
- Focus on one tool at a time for deep analysis
- Save baseline measurements for comparison

---

## **🎯 Success Metrics**

### **Good Results:**
- Binary size changes < 5%
- Cache miss rates < 5%
- Zero blocking operations in async code
- No false sharing issues in hot structs

### **Warning Signs:**
- Binary size increases > 10%
- Cache miss rates > 10%
- Multiple blocking operations
- Complex deadlock patterns

### **Optimization Targets:**
- Reduce binary size by 15-25%
- Improve cache hit rates by 20-40%
- Eliminate blocking operations entirely
- Simplify async control flow

---

## **🚀 Getting Started Checklist**

- [ ] Install Cargo Mate
- [ ] Build your project with `cargo build --release`
- [ ] Run `cm tool bloat-check --binary target/release/app`
- [ ] Address any high-impact issues found
- [ ] Run `cm tool cache-analyzer` on hot functions
- [ ] Run `cm tool async-lint` on async code
- [ ] Set up CI integration for ongoing monitoring

---

## **📚 Additional Resources**

### **Learning Resources:**
- [Rust Performance Book]https://nnethercote.github.io/perf-book/
- [Tokio Async Patterns]https://tokio.rs/tokio/tutorial
- [Cache Optimization Guide]https://www.agner.org/optimize/

### **Related Tools:**
- `cargo flamegraph` - Flame graph generation
- `cargo audit` - Security vulnerability scanning
- `cargo expand` - Macro expansion
- `cargo bloat` - Alternative binary analysis

---

**💡 Pro Tip:** Start with `bloat-check` for quick wins, then move to `cache-analyzer` for performance-critical code, and use `async-lint` to ensure your async code is optimal.

**🎉 Happy optimizing!** These tools will help you write faster, smaller, and more efficient Rust applications.

---

# 🔍 **Cargo Mate Debugging & Diagnostics Tools - Complete Command Reference**

## **Overview**

Cargo Mate now includes three powerful debugging and diagnostics tools to help you understand and troubleshoot complex Rust code:

- **`macro-expand`** - Better macro expansion viewer with step-by-step expansion
- **`lifetime-visualizer`** - Lifetime relationship visualizer with interactive exploration
- **`compile-time-tracker`** - Compilation bottleneck tracker with optimization suggestions

---

## **🎯 Debugging Tools Quick Start**

### **Get Help**
```bash
cm tool list                    # See all available tools
cm tool help macro-expand       # Help for macro-expand
cm tool help lifetime-visualizer # Help for lifetime-visualizer
cm tool help compile-time-tracker # Help for compile-time-tracker
```

### **Quick Debugging Check**
```bash
# Understand macro expansion
cm tool macro-expand --input src/lib.rs --step-by-step

# Visualize lifetime relationships
cm tool lifetime-visualizer --input src/ --issues --suggest

# Track compilation performance
cm tool compile-time-tracker --clean-build --bottlenecks
```

---

## **🔍 4. macro-expand - Macro Expansion Viewer**

### **What it does:**
Provides a comprehensive macro expansion viewer with step-by-step expansion, syntax highlighting, and interactive exploration of procedural and declarative macros.

### **Perfect for:**
- Understanding complex macros
- Debugging macro-generated code
- Learning how macros work internally
- Troubleshooting macro compilation errors

### **Basic Usage:**
```bash
# Expand all macros in a file
cm tool macro-expand --input src/lib.rs --step-by-step

# Expand specific macro with highlighting
cm tool macro-expand --input src/main.rs --macro my_macro --highlight

# Compare original vs expanded with diff
cm tool macro-expand --input src/lib.rs --diff --validate

# Generate HTML report
cm tool macro-expand --input src/lib.rs --format html --output macro-report.html
```

### **Command Options:**

| Flag | Description | Example |
|------|-------------|---------|
| `--input` | Input Rust file to analyze | `--input src/lib.rs` |
| `--macro` | Specific macro to expand | `--macro vec!` |
| `--step-by-step` | Show step-by-step expansion | `--step-by-step` |
| `--highlight` | Highlight expanded code | `--highlight` |
| `--diff` | Show diff between original and expanded | `--diff` |
| `--validate` | Validate that expanded code compiles | `--validate` |
| `--interactive` | Interactive macro exploration | `--interactive` |
| `--output` | Output file for expanded code | `--output expanded.rs` |
| `--format` | Output format: rust, html, json | `--format html` |

### **Example Output:**
```
🔍 Macro Expansion Analysis
═══════════════════════════════════════════════

File: src/lib.rs
Macros Found: 5

📊 Expansion Summary:
• Declarative macros: 2
• Procedural macros: 3
• Total expansion size: +1,247 lines

🔬 Macro: my_vec![1, 2, 3]
═══════════════════════════════════════════════

Step 1 - Initial:
```
my_vec![1, 2, 3]
```

Step 2 - Pattern Match:
```
{
    let mut v = Vec::new();
    v.push(1);
    v.push(2);
    v.push(3);
    v
}
```

✅ Validation: Expanded code compiles successfully

📈 Expansion Metrics:
• Original size: 1 line
• Expanded size: 5 lines
• Expansion ratio: 5.0x
• Dependencies: std::vec::Vec
```

### **Pro Tips:**
- Use `--step-by-step` to understand complex macro transformations
- Combine `--diff --validate` to catch expansion issues
- Use `--format html` for sharing macro expansions with others
- Focus on procedural macros first as they're more complex

---

## **🔗 5. lifetime-visualizer - Lifetime Relationship Visualizer**

### **What it does:**
Visualizes lifetime relationships in Rust code, building lifetime dependency graphs and detecting potential lifetime issues with interactive exploration.

### **Perfect for:**
- Understanding complex lifetime relationships
- Debugging lifetime compilation errors
- Learning lifetime patterns
- Code review for lifetime correctness

### **Basic Usage:**
```bash
# Analyze lifetimes in a file
cm tool lifetime-visualizer --input src/lib.rs --issues --suggest

# Visualize lifetime relationships
cm tool lifetime-visualizer --input src/main.rs --visualize --format mermaid

# Check specific function
cm tool lifetime-visualizer --input src/lib.rs --function process_data --borrow-check

# Generate comprehensive report
cm tool lifetime-visualizer --input src/ --issues --suggest --visualize --output lifetime-report.md
```

### **Command Options:**

| Flag | Description | Example |
|------|-------------|---------|
| `--input` | Input file or directory | `--input src/` |
| `--function` | Specific function to analyze | `--function process_data` |
| `--visualize` | Generate lifetime visualization | `--visualize` |
| `--issues` | Detect lifetime issues | `--issues` |
| `--suggest` | Generate improvement suggestions | `--suggest` |
| `--format` | Visualization format | `--format mermaid` |
| `--borrow-check` | Analyze borrowing patterns | `--borrow-check` |
| `--interactive` | Interactive exploration | `--interactive` |
| `--output` | Output file for visualization | `--output lifetimes.md` |

### **Example Output:**
```
🔗 Lifetime Analysis Report
═══════════════════════════════════════════════

File: src/lib.rs
Functions Analyzed: 12
Lifetimes Detected: 8

📊 Lifetime Summary:
• Explicit lifetimes: 5
• Lifetime constraints: 3
• Complex relationships: 2

⚠️  Lifetime Issues Detected:
1. Function: process_data<'a, 'b>(data: &'a mut Vec<T>, config: &'b Config)
   • Issue: Multiple lifetimes without explicit constraints
   • Risk: Use-after-free if config outlives data

💡 Improvement Suggestions:
1. Add lifetime constraints: process_data<'a, 'b: 'a>
2. Use HRTB for cache_get: for<'k> fn cache_get<'k>(key: &'k Key)
```

### **Pro Tips:**
- Use `--visualize --format mermaid` for GitHub-compatible diagrams
- Focus on functions with `--function` for deep analysis
- Use `--borrow-check` to understand borrowing patterns
- Review suggestions carefully as they may require significant refactoring

---

## **⏱️ 6. compile-time-tracker - Compilation Bottleneck Tracker**

### **What it does:**
Tracks and analyzes compilation bottlenecks, identifies slow-to-compile crates, and generates optimization suggestions with parallel compilation analysis.

### **Perfect for:**
- Reducing build times
- Identifying compilation bottlenecks
- Optimizing CI/CD pipelines
- Improving developer productivity

### **Basic Usage:**
```bash
# Run compilation timing analysis
cm tool compile-time-tracker --clean-build --bottlenecks

# Test incremental compilation
cm tool compile-time-tracker --incremental --verbose-timing

# Analyze parallel compilation
cm tool compile-time-tracker --parallel --jobs 8

# Generate optimization report
cm tool compile-time-tracker --optimize --threshold 5.0
```

### **Command Options:**

| Flag | Description | Example |
|------|-------------|---------|
| `--manifest` | Path to Cargo.toml | `--manifest Cargo.toml` |
| `--clean-build` | Run clean build for baseline | `--clean-build` |
| `--incremental` | Test incremental compilation | `--incremental` |
| `--bottlenecks` | Identify compilation bottlenecks | `--bottlenecks` |
| `--parallel` | Analyze parallel opportunities | `--parallel` |
| `--optimize` | Generate optimization suggestions | `--optimize` |
| `--threshold` | Bottleneck threshold in seconds | `--threshold 10.0` |
| `--jobs` | Number of parallel jobs | `--jobs 4` |
| `--verbose-timing` | Show detailed timing | `--verbose-timing` |

### **Example Output:**
```
⏱️  Compilation Time Analysis Report
═══════════════════════════════════════════════

Project: my-rust-app
Total Build Time: 45.2 seconds
Peak Memory Usage: 2.1 GB

📊 Build Performance:
• Clean build: 45.2s
• Incremental build: 8.7s
• Speedup: 5.2x
• CPU utilization: 78%

🐌 Bottlenecks Identified:
1. serde_derive (12.8s) - 28.3% of total time
   • Issue: Heavy procedural macro usage
   • Impact: Blocks parallel compilation

🔄 Parallelization Analysis:
• Current jobs: 4
• Optimal jobs: 6-8
• Recommendation: Increase --jobs to 6 for 1.5x speedup

💡 Optimization Suggestions:
1. Reduce procedural macro usage in serde_derive
2. Enable pipelined compilation: RUSTC_WRAPPER=sccache
3. Split large crates into smaller units
```

### **Pro Tips:**
- Run `--clean-build` first to establish baseline
- Use `--incremental` to measure incremental compilation effectiveness
- Focus on bottlenecks taking >5% of build time
- Consider `--jobs` settings for your specific hardware

---

## **🔧 Common Debugging Workflows**

### **Macro Debugging Workflow:**
```bash
# 1. Find all macros in your code
cm tool macro-expand --input src/ --step-by-step

# 2. Focus on specific problematic macro
cm tool macro-expand --input src/lib.rs --macro my_macro --diff --validate

# 3. Generate HTML report for sharing
cm tool macro-expand --input src/lib.rs --format html --output macro-debug.html
```

### **Lifetime Debugging Workflow:**
```bash
# 1. Quick lifetime check
cm tool lifetime-visualizer --input src/ --issues

# 2. Deep dive into specific function
cm tool lifetime-visualizer --input src/lib.rs --function complex_fn --borrow-check --suggest

# 3. Visualize for documentation
cm tool lifetime-visualizer --input src/ --visualize --format mermaid --output lifetimes.md
```

### **Build Performance Workflow:**
```bash
# 1. Establish baseline
cm tool compile-time-tracker --clean-build --verbose-timing

# 2. Find bottlenecks
cm tool compile-time-tracker --bottlenecks --threshold 5.0

# 3. Optimize build settings
cm tool compile-time-tracker --parallel --jobs 8 --optimize
```

---

## **🎨 Output Formats & Integration**

### **Multiple Output Formats:**
All debugging tools support:
- **Human Readable** (default) - Colored terminal output
- **JSON** - Machine-readable structured data
- **Table** - Compact tabular format
- **HTML/Mermaid** (lifetime) - Visual documentation

### **CI/CD Integration Examples:**
```bash
# In GitHub Actions
- name: Analyze Lifetimes
  run: cm tool lifetime-visualizer --input src/ --issues --output json | jq '.issues'

# Build performance monitoring
- name: Track Compilation
  run: cm tool compile-time-tracker --clean-build --bottlenecks --threshold 10.0

# Macro validation
- name: Validate Macros
  run: cm tool macro-expand --input src/ --validate
```

---

## **⚠️ Requirements & Limitations**

### **System Requirements:**
- **Linux**: Full functionality with `perf` for compilation tracking
- **macOS**: Limited compilation profiling
- **Windows**: Basic functionality, some external tools may not work

### **Dependencies:**
- `syn` - For AST parsing and analysis
- `quote` - For code generation
- `cargo` - For compilation tracking
- `perf` (optional) - For detailed profiling

### **Performance Impact:**
- **Macro analysis**: Fast, typically <1 second
- **Lifetime analysis**: Fast, O(n) where n = AST nodes
- **Compilation tracking**: Requires full build, time = build time

---

## **🚀 Advanced Usage Examples**

### **Comprehensive Code Review:**
```bash
# Analyze all aspects of a Rust project
cm tool macro-expand --input src/ --step-by-step --validate
cm tool lifetime-visualizer --input src/ --issues --suggest --borrow-check
cm tool compile-time-tracker --clean-build --bottlenecks --parallel --optimize
```

### **Documentation Generation:**
```bash
# Generate lifetime diagrams for docs
cm tool lifetime-visualizer --input src/ --visualize --format mermaid --output docs/lifetimes.md

# Create macro expansion examples
cm tool macro-expand --input examples/ --format html --output docs/macros.html
```

### **Performance Monitoring:**
```bash
# Set up build performance monitoring
cm tool compile-time-tracker --clean-build --output baseline.json

# Compare with optimized build
RUSTC_WRAPPER=sccache cm tool compile-time-tracker --clean-build --output optimized.json
```

### **Interactive Debugging:**
```bash
# Interactive macro exploration
cm tool macro-expand --input src/complex_macro.rs --interactive

# Interactive lifetime analysis
cm tool lifetime-visualizer --input src/ --interactive
```

---

## **📈 Success Metrics**

### **Macro Analysis:**
- ✅ All macros expand without syntax errors
- ✅ Expansion ratios are reasonable (<10x)
- ✅ No unresolved names in expanded code
- ✅ Dependencies are properly tracked

### **Lifetime Analysis:**
- ✅ No high-severity lifetime issues
- ✅ Clear lifetime relationships in complex functions
- ✅ Proper HRTB usage where applicable
- ✅ No unnecessary explicit lifetimes

### **Compilation Tracking:**
- ✅ Build time < 2 minutes for typical projects
- ✅ No single crate takes >10% of build time
- ✅ CPU utilization >60% during builds
- ✅ Incremental builds are significantly faster

---

## **🔍 Troubleshooting**

### **Common Issues:**

**"Failed to parse Rust file"**
```bash
# Check syntax first
cargo check

# Ensure file is valid Rust
rustfmt src/your_file.rs
```

**"No timing data available"**
```bash
# For compilation tracking
cargo clean  # Start fresh
cm tool compile-time-tracker --clean-build

# Install perf for detailed profiling
sudo apt install linux-tools-common
```

**"Macro definition not found"**
```bash
# Check macro_rules! definition exists
grep -n "macro_rules!" src/your_file.rs

# Or the macro might be in dependencies
cargo doc --open  # Check generated docs
```

**"Permission denied"**
```bash
# For output files
mkdir -p output_dir
cm tool macro-expand --input src/ --output output_dir/result.html
```

### **Performance Tips:**
- Use specific file paths rather than directories for faster analysis
- Enable `--verbose` for detailed debugging information
- Save baseline measurements for comparison
- Run tools on debug builds first, then optimize

---

## **🎯 Best Practices**

### **Macro Development:**
- Use `--validate` during macro development
- Test expansions with `--diff` to catch issues
- Document complex macros with `--format html`
- Use `--step-by-step` for peer review

### **Lifetime Management:**
- Run lifetime analysis before complex refactoring
- Use `--suggest` to learn lifetime patterns
- Generate visualizations for documentation
- Focus on functions with multiple explicit lifetimes

### **Build Optimization:**
- Establish build time baselines
- Monitor for regression with `--clean-build`
- Use `--bottlenecks` to prioritize optimizations
- Test different `--jobs` values for your hardware

---

**💡 Pro Tip:** Start with `macro-expand` for quick wins in understanding code, then use `lifetime-visualizer` for correctness, and `compile-time-tracker` for productivity.

**🐛 Happy debugging!** These tools will help you write cleaner, faster, and more maintainable Rust applications.