aprender-profile 0.29.0

Pure Rust system call tracer with source-aware correlation for Rust binaries
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
//! HTML output format for syscall trace reports
//!
//! Sprint 22: Rich visual reports with styled tables and embedded CSS

use crate::stats::StatsTracker;

/// HTML record for a single syscall event
#[derive(Debug, Clone)]
pub struct HtmlSyscall {
    pub name: String,
    pub arguments: String,
    pub result: i64,
    pub duration_us: Option<u64>,
    pub source_location: Option<String>,
}

/// HTML output formatter
#[derive(Debug)]
pub struct HtmlOutput {
    syscalls: Vec<HtmlSyscall>,
    include_timing: bool,
    include_source: bool,
}

impl HtmlOutput {
    /// Create a new HTML output formatter
    pub fn new(include_timing: bool, include_source: bool) -> Self {
        Self { syscalls: Vec::new(), include_timing, include_source }
    }

    /// Add a syscall to the output
    pub fn add_syscall(&mut self, syscall: HtmlSyscall) {
        self.syscalls.push(syscall);
    }

    /// Escape HTML special characters to prevent XSS
    fn escape_html(text: &str) -> String {
        text.replace('&', "&amp;")
            .replace('<', "&lt;")
            .replace('>', "&gt;")
            .replace('"', "&quot;")
            .replace('\'', "&#39;")
    }

    /// Generate embedded CSS styles
    fn generate_styles() -> &'static str {
        r"
        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
            margin: 20px;
            background-color: #f5f5f5;
        }
        h1, h2 {
            color: #333;
        }
        table {
            border-collapse: collapse;
            width: 100%;
            background-color: white;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
            margin-bottom: 20px;
        }
        th, td {
            border: 1px solid #ddd;
            padding: 8px;
            text-align: left;
        }
        th {
            background-color: #4a90d9;
            color: white;
            font-weight: bold;
        }
        tr:nth-child(even) {
            background-color: #f9f9f9;
        }
        tr:hover {
            background-color: #f0f0f0;
        }
        .syscall {
            color: #0066cc;
            font-weight: bold;
            font-family: monospace;
        }
        .args {
            font-family: monospace;
            font-size: 0.9em;
            color: #555;
        }
        .result {
            font-family: monospace;
        }
        .result-error {
            color: #cc0000;
        }
        .duration {
            font-family: monospace;
            color: #666;
        }
        .source {
            font-size: 0.85em;
            color: #888;
        }
        .stats-table {
            margin-top: 20px;
        }
        .stats-table th {
            background-color: #5cb85c;
        }
        .footer {
            margin-top: 20px;
            font-size: 0.8em;
            color: #888;
            text-align: center;
        }
        "
    }

    /// Generate HTML table header
    fn generate_header(&self) -> String {
        let mut headers = vec!["Syscall", "Arguments", "Result"];

        if self.include_timing {
            headers.push("Duration");
        }

        if self.include_source {
            headers.push("Source");
        }

        let header_cells: Vec<String> = headers.iter().map(|h| format!("<th>{h}</th>")).collect();

        format!("<tr>{}</tr>", header_cells.join(""))
    }

    /// Format a syscall as HTML table row
    fn format_syscall_row(&self, syscall: &HtmlSyscall) -> String {
        let result_class = if syscall.result < 0 { "result result-error" } else { "result" };

        let mut cells = vec![
            format!(r#"<td class="syscall">{}</td>"#, Self::escape_html(&syscall.name)),
            format!(r#"<td class="args">{}</td>"#, Self::escape_html(&syscall.arguments)),
            format!(r#"<td class="{}">{}</td>"#, result_class, syscall.result),
        ];

        if self.include_timing {
            let duration_text = match syscall.duration_us {
                Some(d) => format!("{d} us"),
                None => String::new(),
            };
            cells.push(format!(
                r#"<td class="duration">{}</td>"#,
                Self::escape_html(&duration_text)
            ));
        }

        if self.include_source {
            let source_text = syscall.source_location.as_deref().unwrap_or("");
            cells.push(format!(r#"<td class="source">{}</td>"#, Self::escape_html(source_text)));
        }

        format!("<tr>{}</tr>", cells.join(""))
    }

    /// Generate complete HTML document
    pub fn to_html(&self, stats: Option<&StatsTracker>) -> String {
        let mut html = String::new();

        // DOCTYPE and HTML start
        html.push_str("<!DOCTYPE html>\n");
        html.push_str("<html lang=\"en\">\n");

        // Head section
        html.push_str("<head>\n");
        html.push_str("    <meta charset=\"UTF-8\">\n");
        html.push_str(
            "    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n",
        );
        html.push_str("    <title>Renacer Trace Report</title>\n");
        html.push_str("    <style>");
        html.push_str(Self::generate_styles());
        html.push_str("</style>\n");
        html.push_str("</head>\n");

        // Body section
        html.push_str("<body>\n");
        html.push_str("    <h1>Syscall Trace Report</h1>\n");

        // Syscall trace table
        html.push_str("    <table>\n");
        html.push_str("        ");
        html.push_str(&self.generate_header());
        html.push('\n');

        for syscall in &self.syscalls {
            html.push_str("        ");
            html.push_str(&self.format_syscall_row(syscall));
            html.push('\n');
        }

        html.push_str("    </table>\n");

        // Statistics section (if provided)
        if let Some(tracker) = stats {
            html.push_str(&self.render_statistics(tracker));
        }

        // Footer
        html.push_str("    <div class=\"footer\">\n");
        html.push_str("        Generated by Renacer - System Call Tracer\n");
        html.push_str("    </div>\n");

        html.push_str("</body>\n");
        html.push_str("</html>\n");

        html
    }

    /// Render statistics as HTML table
    fn render_statistics(&self, tracker: &StatsTracker) -> String {
        let mut html = String::new();

        html.push_str("    <h2>Statistics Summary</h2>\n");
        html.push_str("    <table class=\"stats-table\">\n");
        html.push_str("        <tr><th>% time</th><th>seconds</th><th>usecs/call</th><th>calls</th><th>errors</th><th>syscall</th></tr>\n");

        let stats = tracker.stats_map();
        let total_time: u64 = stats.values().map(|s| s.total_time_us).sum();

        // Sort by total time (descending)
        let mut sorted_stats: Vec<_> = stats.iter().collect();
        sorted_stats.sort_by(|a, b| b.1.total_time_us.cmp(&a.1.total_time_us));

        for (name, stat) in sorted_stats {
            let pct = if total_time > 0 {
                (stat.total_time_us as f64 / total_time as f64) * 100.0
            } else {
                0.0
            };

            let usecs_per_call = if stat.count > 0 { stat.total_time_us / stat.count } else { 0 };

            let seconds = stat.total_time_us as f64 / 1_000_000.0;

            html.push_str(&format!(
                "        <tr><td>{:.2}</td><td>{:.6}</td><td>{}</td><td>{}</td><td>{}</td><td class=\"syscall\">{}</td></tr>\n",
                pct,
                seconds,
                usecs_per_call,
                stat.count,
                stat.errors,
                Self::escape_html(name)
            ));
        }

        html.push_str("    </table>\n");

        html
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_html_escape() {
        assert_eq!(HtmlOutput::escape_html("<script>"), "&lt;script&gt;");
        assert_eq!(HtmlOutput::escape_html("a&b"), "a&amp;b");
        assert_eq!(HtmlOutput::escape_html("\"test\""), "&quot;test&quot;");
        assert_eq!(HtmlOutput::escape_html("'test'"), "&#39;test&#39;");
    }

    #[test]
    fn test_html_output_new() {
        let output = HtmlOutput::new(true, true);
        assert!(output.include_timing);
        assert!(output.include_source);
        assert!(output.syscalls.is_empty());
    }

    #[test]
    fn test_html_output_add_syscall() {
        let mut output = HtmlOutput::new(false, false);
        output.add_syscall(HtmlSyscall {
            name: "write".to_string(),
            arguments: "1, \"test\", 4".to_string(),
            result: 4,
            duration_us: None,
            source_location: None,
        });
        assert_eq!(output.syscalls.len(), 1);
        assert_eq!(output.syscalls[0].name, "write");
    }

    #[test]
    fn test_html_output_basic_structure() {
        let output = HtmlOutput::new(false, false);
        let html = output.to_html(None);

        assert!(html.contains("<!DOCTYPE html>"));
        assert!(html.contains("<html"));
        assert!(html.contains("</html>"));
        assert!(html.contains("<head>"));
        assert!(html.contains("<body>"));
        assert!(html.contains("<style>"));
        assert!(html.contains("<table"));
    }

    #[test]
    fn test_html_output_with_syscall() {
        let mut output = HtmlOutput::new(false, false);
        output.add_syscall(HtmlSyscall {
            name: "write".to_string(),
            arguments: "1, \"hello\", 5".to_string(),
            result: 5,
            duration_us: None,
            source_location: None,
        });

        let html = output.to_html(None);
        assert!(html.contains("write"));
        assert!(html.contains("<tr"));
        assert!(html.contains("<td"));
    }

    #[test]
    fn test_html_output_escape_xss() {
        let mut output = HtmlOutput::new(false, false);
        output.add_syscall(HtmlSyscall {
            name: "write".to_string(),
            arguments: "<script>alert('xss')</script>".to_string(),
            result: 0,
            duration_us: None,
            source_location: None,
        });

        let html = output.to_html(None);
        assert!(!html.contains("<script>alert"));
        assert!(html.contains("&lt;script&gt;"));
    }

    #[test]
    fn test_html_output_with_timing() {
        let mut output = HtmlOutput::new(true, false);
        output.add_syscall(HtmlSyscall {
            name: "write".to_string(),
            arguments: "1, \"test\", 4".to_string(),
            result: 4,
            duration_us: Some(1234),
            source_location: None,
        });

        let html = output.to_html(None);
        assert!(html.contains("Duration"));
        assert!(html.contains("1234 us"));
    }

    #[test]
    fn test_html_output_with_source() {
        let mut output = HtmlOutput::new(false, true);
        output.add_syscall(HtmlSyscall {
            name: "write".to_string(),
            arguments: "1, \"test\", 4".to_string(),
            result: 4,
            duration_us: None,
            source_location: Some("src/main.rs:42".to_string()),
        });

        let html = output.to_html(None);
        assert!(html.contains("Source"));
        assert!(html.contains("src/main.rs:42"));
    }

    #[test]
    fn test_html_output_error_result() {
        let mut output = HtmlOutput::new(false, false);
        output.add_syscall(HtmlSyscall {
            name: "open".to_string(),
            arguments: "\"/nonexistent\"".to_string(),
            result: -2,
            duration_us: None,
            source_location: None,
        });

        let html = output.to_html(None);
        assert!(html.contains("result-error"));
        assert!(html.contains("-2"));
    }

    #[test]
    fn test_html_output_header_columns() {
        let output = HtmlOutput::new(true, true);
        let header = output.generate_header();

        assert!(header.contains("Syscall"));
        assert!(header.contains("Arguments"));
        assert!(header.contains("Result"));
        assert!(header.contains("Duration"));
        assert!(header.contains("Source"));
    }

    #[test]
    fn test_html_output_with_stats() {
        let mut output = HtmlOutput::new(false, false);
        output.add_syscall(HtmlSyscall {
            name: "write".to_string(),
            arguments: "1, \"test\", 4".to_string(),
            result: 4,
            duration_us: None,
            source_location: None,
        });

        let mut tracker = StatsTracker::new();
        tracker.record("write", 0, 1000);
        tracker.record("write", 0, 2000);
        tracker.record("read", 0, 500);
        tracker.record("read", -1, 100); // Error

        let html = output.to_html(Some(&tracker));
        assert!(html.contains("Statistics Summary"));
        assert!(html.contains("stats-table"));
    }

    #[test]
    fn test_html_output_with_timing_no_duration() {
        let mut output = HtmlOutput::new(true, false);
        output.add_syscall(HtmlSyscall {
            name: "write".to_string(),
            arguments: "1, \"test\", 4".to_string(),
            result: 4,
            duration_us: None, // No timing
            source_location: None,
        });

        let html = output.to_html(None);
        assert!(html.contains("Duration"));
        // Empty duration cell
    }

    #[test]
    fn test_html_output_with_source_none() {
        let mut output = HtmlOutput::new(false, true);
        output.add_syscall(HtmlSyscall {
            name: "write".to_string(),
            arguments: "1, \"test\", 4".to_string(),
            result: 4,
            duration_us: None,
            source_location: None, // No source location
        });

        let html = output.to_html(None);
        assert!(html.contains("Source"));
    }

    #[test]
    fn test_html_syscall_clone() {
        let syscall = HtmlSyscall {
            name: "write".to_string(),
            arguments: "fd=1".to_string(),
            result: 10,
            duration_us: Some(100),
            source_location: Some("test.rs:42".to_string()),
        };

        let cloned = syscall.clone();
        assert_eq!(cloned.name, "write");
        assert_eq!(cloned.result, 10);
        assert_eq!(cloned.duration_us, Some(100));
    }

    #[test]
    fn test_html_syscall_debug() {
        let syscall = HtmlSyscall {
            name: "read".to_string(),
            arguments: "fd=0".to_string(),
            result: 5,
            duration_us: None,
            source_location: None,
        };

        let debug_str = format!("{:?}", syscall);
        assert!(debug_str.contains("read"));
    }

    #[test]
    fn test_html_output_debug() {
        let output = HtmlOutput::new(true, true);
        let debug_str = format!("{:?}", output);
        assert!(debug_str.contains("HtmlOutput"));
    }

    #[test]
    fn test_html_output_multiple_syscalls() {
        let mut output = HtmlOutput::new(true, true);

        for i in 0..5 {
            output.add_syscall(HtmlSyscall {
                name: format!("syscall_{}", i),
                arguments: format!("arg_{}", i),
                result: i as i64,
                duration_us: Some(i as u64 * 100),
                source_location: Some(format!("file.rs:{}", i)),
            });
        }

        let html = output.to_html(None);
        assert!(html.contains("syscall_0"));
        assert!(html.contains("syscall_4"));
    }

    #[test]
    fn test_render_statistics_zero_count() {
        let output = HtmlOutput::new(false, false);
        let mut tracker = StatsTracker::new();

        // Record with zero total time
        tracker.record("empty", 0, 0);

        let html = output.to_html(Some(&tracker));
        assert!(html.contains("Statistics Summary"));
    }

    #[test]
    fn test_generate_styles() {
        let styles = HtmlOutput::generate_styles();
        assert!(styles.contains("body"));
        assert!(styles.contains("table"));
        assert!(styles.contains(".syscall"));
        assert!(styles.contains(".result-error"));
    }
}