ddbug 0.5.0

Display debugging information
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
use std::io::Write;

use marksman_escape::Escape;

use super::{DiffPrefix, Printer, ValuePrinter};
use crate::{Options, Result};

const HEADER: &str = r##"<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<style>
html, body {
    margin: 0;
    height: 100%;
}
.layoutrow {
    display: flex;
    height: 100%;
}
.layoutcol {
    flex: 1;
    overflow-y: scroll;
}
#detailcol {
    display: none;
}
ul.treeroot {
    list-style: none;
    padding: 0;
    margin: 0;
    display: inline-block;
}
ul.treeroot ul {
    list-style: none;
}
ul.treeroot li {
    white-space: nowrap;
    cursor: default;
}
ul.treeroot li.treebranch {
    cursor: pointer;
    border: 1px solid black;
}
ul.treeroot li.detail {
    cursor: pointer;
}
ul.treeroot span {
    white-space: nowrap;
}
ul.treeroot .del {
    background-color: #ffdce0;
}
ul.treeroot .add {
    background-color: #cdffd8;
}
ul.treeroot .mod {
    background-color: #ffffa0;
}
ul.treeroot a {
    color: black;
}
span.field {
    display: inline-block;
    vertical-align: top;
}
</style>
<script language="javascript">
window.onload = function () {
    document.addEventListener("click", function (e) {
        var node = e.target;
        while (node.nodeName !== "LI") {
            if (node.classList.contains("link")) {
                link(node);
                e.preventDefault();
                return;
            }

            node = node.parentNode;
            if (node == null) {
                return;
            }
        }
    });
    [].forEach.call(document.querySelectorAll(".treeroot"), function(node) {
        node.addEventListener("click", function (e) {
            var node = e.target;
            while (node.nodeName !== "LI") {
                node = node.parentNode;
                if (node == null) {
                    return;
                }
            }
            if (node.classList.contains("treebranch")) {
                toggle(node);
            }
            if (node.classList.contains("detail")) {
                detail(node);
            }
        });
    });

    async function toggle (node) {
        var uls = node.getElementsByTagName("ul");
        var ul = [].find.call(uls, function(ul) {
            var li = ul;
            while (li != null && li.nodeName !== "LI") {
                li = li.parentNode;
            }
            return li === node;
        });
        if (ul == null) {
            return;
        }
        if (node.classList.contains("lazy")) {
            node.classList.remove("lazy");
            var response = await fetch("/id/" + node.id);
            var html = await response.text();
            ul.innerHTML = html;
            ul.style.display = "block";
        } else {
            if (ul.style.display == "none") {
                ul.style.display = "block";
            } else {
                ul.style.display = "none";
            }
        }
    }

    async function detail (node) {
        var parent = node;
        while (parent != null && parent.id === "") {
            parent = parent.parentNode;
        }
        if (parent == null) {
            return;
        }
        var response = await fetch("/id/" + parent.id + "/" + node.dataset.detail);
        var html = await response.text();
        document.getElementById("detail").innerHTML = html;
        document.getElementById("detailcol").style.display = "block";
    }

    async function link(anchor) {
        var id = anchor.dataset.link;
        node = document.getElementById(id);
        if (node == null) {
            // Need to load the unit first
            var response = await fetch("/id/" + id + "/parent");
            var parent_id = await response.text();
            if (parent_id === "") {
                return;
            }
            var parent = document.getElementById(parent_id);
            await toggle(parent);
            node = document.getElementById(id);
            if (node == null) {
                return;
            }
        }
        // Find UL
        var uls = node.getElementsByTagName("ul");
        var ul = [].find.call(uls, function(ul) {
            var li = ul;
            while (li != null && li.nodeName !== "LI") {
                li = li.parentNode;
            }
            return li === node;
        });
        if (ul == null) {
            return;
        }
        // Expand node
        if (node.classList.contains("lazy")) {
            node.classList.remove("lazy");
            var response = await fetch("/id/" + node.id);
            var html = await response.text();
            ul.innerHTML = html;
        }
        ul.style.display = "block";

        // Display node and parents
        while (node && node.nodeName !== "BODY") {
            if (node.style.display == "none") {
                node.style.display = "block";
            }
            node = node.parentNode;
        }
        // So that history works nicer
        node = document.getElementById(id);
        if (node != null) {
            while (anchor != null) {
                if (anchor.id !== "") {
                    history.replaceState({ id: anchor.id }, "");
                    break;
                }
                anchor = anchor.parentNode;
            }
            history.pushState({ id: id }, "", "#" + id);
            document.getElementById('treecol').scroll({
                top: node.offsetTop, behavior: 'smooth'
            });
        }
    }

    window.addEventListener("popstate", (e) => {
        if (e.state != null) {
            document.getElementById('treecol').scroll({
                top: document.getElementById(e.state.id).offsetTop, behavior: 'smooth'
            });
        }
    });
}
</script>
</head>
<body>
<div class="layoutrow">
<div id="treecol" class="layoutcol">
<ul class="treeroot">"##;

const FOOTER: &str = r#"</ul>
</div>
<div id="detailcol" class="layoutcol">
<table id="detail"></table>
</div>
</div>
</body>
</html>"#;

pub struct HtmlPrinter<'w> {
    w: &'w mut dyn Write,
    indent: usize,
    prefix: DiffPrefix,
    inline_depth: usize,
    // Hack to allow indented <ul> to be included within parent <li>.
    line_started: bool,
    http: bool,
}

impl<'w> HtmlPrinter<'w> {
    pub fn new(w: &'w mut dyn Write, options: &Options) -> Self {
        HtmlPrinter {
            w,
            indent: 0,
            prefix: DiffPrefix::None,
            inline_depth: options.inline_depth,
            line_started: false,
            http: options.http,
        }
    }

    pub fn begin(&mut self) -> Result<()> {
        self.w.write_all(HEADER.as_bytes())?;
        Ok(())
    }

    pub fn end(&mut self) -> Result<()> {
        self.w.write_all(FOOTER.as_bytes())?;
        Ok(())
    }
}

impl<'w> Printer for HtmlPrinter<'w> {
    fn value(
        &mut self,
        buf: &mut Vec<u8>,
        f: &mut dyn FnMut(&mut dyn ValuePrinter) -> Result<()>,
    ) -> Result<()> {
        let mut p = HtmlValuePrinter { w: buf };
        f(&mut p)
    }

    /// Calls `f` to write to a temporary buffer.
    fn buffer(
        &mut self,
        buf: &mut Vec<u8>,
        f: &mut dyn FnMut(&mut dyn Printer) -> Result<()>,
    ) -> Result<()> {
        let mut p = HtmlPrinter {
            w: buf,
            // TODO: need to ensure these are all unchanged?
            indent: self.indent,
            prefix: self.prefix,
            inline_depth: self.inline_depth,
            line_started: self.line_started,
            http: self.http,
        };
        f(&mut p)?;
        Ok(())
    }

    fn write_buf(&mut self, buf: &[u8]) -> Result<()> {
        self.w.write_all(buf)?;
        Ok(())
    }

    fn line_break(&mut self) -> Result<()> {
        Ok(())
    }

    fn line(&mut self, label: &str, buf: &[u8]) -> Result<()> {
        if !self.line_started {
            write!(self.w, "<li>")?;
        }
        if buf.is_empty() {
            write!(self.w, "<span")?;
            if self.prefix == DiffPrefix::Modify {
                write!(self.w, " class=\"field mod\"")?;
            }
            write!(self.w, ">{}:</span>", label)?;
        } else {
            if !label.is_empty() {
                write!(self.w, "<span class=\"field\">{}:</span> ", label)?;
            }
            write!(self.w, "<span")?;
            match self.prefix {
                DiffPrefix::None | DiffPrefix::Equal => write!(self.w, " class=\"field\"")?,
                DiffPrefix::Modify => {
                    write!(self.w, " class=\"field mod\"")?;
                }
                DiffPrefix::Delete => {
                    write!(self.w, " class=\"field del\"")?;
                }
                DiffPrefix::Add => {
                    write!(self.w, " class=\"field add\"")?;
                }
            }
            write!(self.w, ">")?;
            self.w.write_all(buf)?;
            write!(self.w, "</span>")?;
        }
        if !self.line_started {
            writeln!(self.w, "</li>")?;
        }
        Ok(())
    }

    fn line_diff(&mut self, label: &str, a: &[u8], b: &[u8]) -> Result<()> {
        if !self.line_started {
            write!(self.w, "<li>")?;
        }
        if !label.is_empty() {
            write!(self.w, "<span class=\"field\">{}:</span> ", label)?;
        }
        write!(self.w, "<span class=\"field\"><span class=\"del\">")?;
        self.w.write_all(a)?;
        write!(self.w, "</span><br>\n<span class=\"add\">")?;
        self.w.write_all(b)?;
        write!(self.w, "</span></span>")?;
        if !self.line_started {
            writeln!(self.w, "</li>")?;
        }
        Ok(())
    }

    fn indent_body(
        &mut self,
        buf: &mut Vec<u8>,
        body: &mut dyn FnMut(&mut dyn Printer) -> Result<()>,
    ) -> Result<()> {
        let mut printer = HtmlPrinter {
            w: buf,
            // TODO: need to ensure these are all unchanged?
            indent: self.indent + 1,
            prefix: self.prefix,
            inline_depth: self.inline_depth,
            line_started: false,
            http: self.http,
        };
        body(&mut printer)?;
        Ok(())
    }

    fn indent_header(
        &mut self,
        collapsed: bool,
        body: &[u8],
        header: &mut dyn FnMut(&mut dyn Printer) -> Result<()>,
    ) -> Result<()> {
        debug_assert!(!self.line_started);
        write!(self.w, "<li class=\"treebranch\">")?;
        self.line_started = true;
        header(self)?;
        if collapsed {
            writeln!(self.w, "<ul style=\"display:none\">")?;
        } else {
            writeln!(self.w, "<ul>")?;
        }
        self.write_buf(body)?;
        writeln!(self.w, "</ul></li>")?;
        self.line_started = false;
        Ok(())
    }

    fn indent_id(
        &mut self,
        id: usize,
        header: &mut dyn FnMut(&mut dyn Printer) -> Result<()>,
        body: &mut dyn FnMut(&mut dyn Printer) -> Result<()>,
    ) -> Result<()> {
        debug_assert!(!self.line_started);
        write!(
            self.w,
            "<li class=\"treebranch{}\" id=\"{}\">",
            if self.http { " lazy" } else { "" },
            id,
        )?;
        self.line_started = true;
        header(self)?;
        writeln!(self.w, "<ul style=\"display:none\">")?;
        self.line_started = false;
        if !self.http {
            body(self)?;
        }
        writeln!(self.w, "</ul></li>")?;
        Ok(())
    }

    fn indent_detail(&mut self, id: &str, label: &str) -> Result<()> {
        debug_assert!(self.http);
        debug_assert!(!self.line_started);
        write!(self.w, "<li class=\"detail\" data-detail=\"{}\">", id,)?;
        self.line_started = true;
        self.line(label, &[])?;
        writeln!(self.w, "</li>")?;
        self.line_started = false;
        Ok(())
    }

    fn prefix(&mut self, prefix: DiffPrefix) {
        self.prefix = prefix;
    }

    fn get_prefix(&self) -> DiffPrefix {
        self.prefix
    }

    fn inline_begin(&mut self) -> bool {
        if self.inline_depth == 0 {
            false
        } else {
            self.inline_depth -= 1;
            true
        }
    }

    fn inline_end(&mut self) {
        self.inline_depth += 1;
    }

    fn instruction(&mut self, address: Option<u64>, mnemonic: &str, buf: &[u8]) -> Result<()> {
        write!(self.w, "<tr><td>")?;
        if let Some(address) = address {
            write!(self.w, "{:x}", address)?;
        }
        write!(self.w, "</td><td>{}</td><td>", mnemonic)?;
        self.w.write_all(buf)?;
        write!(self.w, "</td></tr>")?;
        Ok(())
    }
}

fn escaped(bytes: &[u8]) -> Vec<u8> {
    Escape::new(bytes.iter().cloned()).collect()
}

fn escaped_str(s: &str) -> String {
    String::from_utf8(Escape::new(s.as_bytes().iter().cloned()).collect()).unwrap()
}

struct HtmlValuePrinter<'w> {
    w: &'w mut Vec<u8>,
}

impl<'w> Write for HtmlValuePrinter<'w> {
    fn write(&mut self, buf: &[u8]) -> std::result::Result<usize, std::io::Error> {
        self.w.write_all(&escaped(buf))?;
        Ok(buf.len())
    }

    fn flush(&mut self) -> std::result::Result<(), std::io::Error> {
        self.w.flush()
    }
}

impl<'w> ValuePrinter for HtmlValuePrinter<'w> {
    fn link(
        &mut self,
        id: usize,
        f: &mut dyn FnMut(&mut dyn ValuePrinter) -> Result<()>,
    ) -> Result<()> {
        if id == 0 {
            f(self)
        } else {
            write!(
                self.w,
                "<a class=\"link\" data-link=\"{}\" href=\"#{}\">",
                id, id,
            )?;
            f(self)?;
            write!(self.w, "</a>")?;
            Ok(())
        }
    }

    fn name(&mut self, name: &str) -> Result<()> {
        let mut generic = 0;
        let mut colon = 0;
        let mut brace = 0;
        let mut result = String::new();
        let mut s = String::new();
        // Strip leading namespaces from generics in Rust.
        for c in name.chars() {
            if c == '{' {
                // Start of brace. Treat like part of an identifier.
                brace += 1;
                s.push(c);
            } else if c == '}' {
                // End of brace. Treat like part of an identifier.
                if brace > 0 {
                    brace -= 1;
                }
                s.push(c);
            } else if brace > 0 {
                // Inside brace. Treat like part of an identifier.
                s.push(c);
            } else if c == '<' {
                // Start of a generic. Previous string is identifier, not namespace.
                result.push_str(&s);
                s.clear();
                generic += 1;
                if generic <= 2 {
                    result.push(c);
                }
                if generic == 2 {
                    // Deeply nested generic. Skip everything.
                    result.push_str("...");
                }
            } else if c == '>' {
                // End of a generic. Previous string is identifier, not namespace.
                if generic <= 2 {
                    result.push_str(&s);
                    s.clear();
                    result.push(c);
                }
                if generic > 0 {
                    generic -= 1;
                }
            } else if generic == 0 {
                // Not in generic. Allow everything.
                result.push(c);
            } else if generic >= 2 {
                // Deeply nested generic. Skip everything.
            } else if c == ':' {
                if colon == 0 {
                    // Possible start of namespace separator.
                    colon = 1;
                    s.push(c);
                } else {
                    // Namespace separator so discard s.
                    colon = 0;
                    s.clear();
                }
            } else if c == '_' || c.is_alphanumeric() {
                // Part of an identifier.
                // TODO: probably missing some allowable characters here.
                s.push(c);
            } else {
                // Some other punctuation. Previous string is identifier, not namespace.
                result.push_str(&s);
                s.clear();
                result.push(c);
            }
        }
        result.push_str(&s);
        if name != result {
            write!(
                self.w,
                "<span title=\"{}\">{}</span>",
                &escaped_str(name),
                &escaped_str(&result),
            )?;
        } else {
            self.w.write_all(&escaped(name.as_bytes()))?;
        }
        Ok(())
    }
}