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
// Fun times ahead!
//
// Apparently, proc-macros don't play well with `cfg_attr` yet, and their
// combination is buggy. So we can't use cfg_attr to choose between
// `wasm-bindgen` and `structopt` depending on if we're building the CLI or the
// wasm API respectively. Instead, we have `build.rs` remove unwanted attributes
// for us by invoking `grep`.
//
// It's terrible! But it works for now.

use structopt::StructOpt;

/// Options for configuring `twiggy`.
#[derive(Clone, Debug)]
#[derive(StructOpt)]
#[structopt(about = "\n`twiggy` is a code size profiler.\n\nIt analyzes a binary's call graph to answer questions like:\n\n* Why was this function included in the binary in the first place?\n\n* What is the retained size of this function? I.e. how much space\n  would be saved if I removed it and all the functions that become\n  dead code after its removal.\n\nUse `twiggy` to make your binaries slim!")]
pub enum Options {
    /// List the top code size offenders in a binary.
    #[structopt(name = "top")]
    Top(Top),

    /// Compute and display the dominator tree for a binary's call graph.
    #[structopt(name = "dominators")]
    Dominators(Dominators),

    /// Find and display the call paths to a function in the given binary's call
    /// graph.
    #[structopt(name = "paths")]
    Paths(Paths),

    /// List the generic function monomorphizations that are contributing to
    /// code bloat.
    #[structopt(name = "monos")]
    Monos(Monos),

    /// Diff the old and new versions of a binary to see what sizes changed.
    #[structopt(name = "diff")]
    Diff(Diff),

    /// Find and display code and data that is not transitively referenced by
    /// any exports or public functions.
    #[structopt(name = "garbage")]
    Garbage(Garbage),
}

/// List the top code size offenders in a binary.
#[derive(Clone, Debug)]
#[derive(StructOpt)]
pub struct Top {
    /// The path to the input binary to size profile.
    #[cfg(feature = "cli")]
    #[structopt(parse(from_os_str))]
    input: path::PathBuf,

    /// The parse mode for the input binary data.
    #[cfg(feature = "cli")]
    #[structopt(long = "mode", default_value = "auto")]
    parse_mode: traits::ParseMode,

    /// The destination to write the output to. Defaults to `stdout`.
    #[cfg(feature = "cli")]
    #[structopt(short = "o", default_value = "-")]
    output_destination: OutputDestination,

    /// The format the output should be written in.
    #[cfg(feature = "cli")]
    #[structopt(short = "f", long = "format", default_value = "text")]
    output_format: traits::OutputFormat,

    /// The maximum number of items to display.
    #[structopt(short = "n", default_value = "4294967295")]
    max_items: u32,

    /// Display retaining paths.
    #[structopt(short = "r", long = "retaining-paths")]
    retaining_paths: bool,

    /// Sort list by retained size, rather than shallow size.
    #[structopt(long = "retained")]
    retained: bool,
}

impl Default for Top {
    fn default() -> Top {
        Top {
            #[cfg(feature = "cli")]
            input: Default::default(),
            #[cfg(feature = "cli")]
            parse_mode: Default::default(),
            #[cfg(feature = "cli")]
            output_destination: Default::default(),
            #[cfg(feature = "cli")]
            output_format: Default::default(),

            max_items: 4_294_967_295,
            retaining_paths: false,
            retained: false,
        }
    }
}

impl Top {
    /// Construct a new, default `Top`.
    pub fn new() -> Top {
        Top::default()
    }

    /// The maximum number of items to display.
    pub fn max_items(&self) -> u32 {
        self.max_items
    }

    /// Display retaining paths.
    pub fn retaining_paths(&self) -> bool {
        self.retaining_paths
    }

    /// Sort list by retained size, rather than shallow size.
    pub fn retained(&self) -> bool {
        self.retained
    }

    /// Set the maximum number of items to display.
    pub fn set_max_items(&mut self, n: u32) {
        self.max_items = n;
    }

    /// Set whether to display and compute retaining paths.
    pub fn set_retaining_paths(&mut self, do_it: bool) {
        self.retaining_paths = do_it;
    }

    /// Set whether to sort list by retained size, rather than shallow size.
    pub fn set_retained(&mut self, do_it: bool) {
        self.retained = do_it;
    }
}

/// Compute and display the dominator tree for a binary's call graph.
#[derive(Clone, Debug, Default)]
#[derive(StructOpt)]
pub struct Dominators {
    /// The path to the input binary to size profile.
    #[cfg(feature = "cli")]
    #[structopt(parse(from_os_str))]
    input: path::PathBuf,

    /// The parse mode for the input binary data.
    #[cfg(feature = "cli")]
    #[structopt(long = "mode", default_value = "auto")]
    parse_mode: traits::ParseMode,

    /// The destination to write the output to. Defaults to `stdout`.
    #[cfg(feature = "cli")]
    #[structopt(short = "o", default_value = "-")]
    output_destination: OutputDestination,

    /// The format the output should be written in.
    #[cfg(feature = "cli")]
    #[structopt(short = "f", long = "format", default_value = "text")]
    output_format: traits::OutputFormat,

    /// The name of the function whose dominator subtree should be printed.
    items: Vec<String>,

    /// The maximum depth to print the dominators tree.
    #[structopt(short = "d")]
    max_depth: Option<u32>,

    /// The maximum number of rows, regardless of depth in the tree, to display.
    #[structopt(short = "r")]
    max_rows: Option<u32>,

    /// Whether or not `items` should be treated as regular expressions.
    #[structopt(long = "regex")]
    using_regexps: bool,
}

impl Dominators {
    // TODO: wasm-bindgen does not support sending Vec<String> across
    // the wasm ABI boundary yet.

    /// The items whose dominators subtree should be printed.
    pub fn items(&self) -> &[String] {
        &self.items
    }
}

impl Dominators {
    /// Construct a new, default `Dominators`.
    pub fn new() -> Dominators {
        Dominators::default()
    }

    /// The maximum depth to print the dominators tree.
    pub fn max_depth(&self) -> u32 {
        self.max_depth.unwrap_or(u32::MAX)
    }

    /// The maximum number of rows, regardless of depth in the tree, to display.
    pub fn max_rows(&self) -> u32 {
        self.max_rows.unwrap_or(u32::MAX)
    }

    /// Whether or not `items` should be treated as regular expressions.
    pub fn using_regexps(&self) -> bool {
        self.using_regexps
    }

    /// Set the maximum depth to print the dominators tree.
    pub fn set_max_depth(&mut self, max_depth: u32) {
        self.max_depth = Some(max_depth);
    }

    /// Set the maximum number of rows, regardless of depth in the tree, to display.
    pub fn set_max_rows(&mut self, max_rows: u32) {
        self.max_rows = Some(max_rows);
    }

    /// Set whether or not `items` should be treated as regular expressions.
    pub fn set_using_regexps(&mut self, using_regexps: bool) {
        self.using_regexps = using_regexps;
    }
}

/// Find and display the call paths to a function in the given binary's call
/// graph.
#[derive(Clone, Debug)]
#[derive(StructOpt)]
pub struct Paths {
    /// The path to the input binary to size profile.
    #[cfg(feature = "cli")]
    #[structopt(parse(from_os_str))]
    input: path::PathBuf,

    /// The parse mode for the input binary data.
    #[cfg(feature = "cli")]
    #[structopt(long = "mode", default_value = "auto")]
    parse_mode: traits::ParseMode,

    /// The destination to write the output to. Defaults to `stdout`.
    #[cfg(feature = "cli")]
    #[structopt(short = "o", default_value = "-")]
    output_destination: OutputDestination,

    /// The format the output should be written in.
    #[cfg(feature = "cli")]
    #[structopt(short = "f", long = "format", default_value = "text")]
    output_format: traits::OutputFormat,

    /// The functions to find call paths to.
    functions: Vec<String>,

    /// The maximum depth to print the paths.
    #[structopt(short = "d", default_value = "10")]
    max_depth: u32,

    /// The maximum number of paths, regardless of depth in the tree, to display.
    #[structopt(short = "r", default_value = "10")]
    max_paths: u32,

    /// This direction of the path traversal.
    #[structopt(long = "descending")]
    descending: bool,

    /// Whether or not `functions` should be treated as regular expressions.
    #[structopt(long = "regex")]
    using_regexps: bool,
}

impl Default for Paths {
    fn default() -> Paths {
        Paths {
            #[cfg(feature = "cli")]
            input: Default::default(),
            #[cfg(feature = "cli")]
            parse_mode: Default::default(),
            #[cfg(feature = "cli")]
            output_destination: Default::default(),
            #[cfg(feature = "cli")]
            output_format: Default::default(),

            functions: Default::default(),
            max_depth: 10,
            max_paths: 10,
            descending: false,
            using_regexps: false,
        }
    }
}

impl Paths {
    // TODO: wasm-bindgen doesn't support sending Vec<String> across the wasm
    // ABI boundary yet.

    /// The functions to find call paths to.
    pub fn functions(&self) -> &[String] {
        &self.functions
    }
}

impl Paths {
    /// Construct a new, default `Paths`.
    pub fn new() -> Paths {
        Paths::default()
    }

    /// Add a function to find call paths for.
    pub fn add_function(&mut self, function: String) {
        self.functions.push(function);
    }

    /// The maximum depth to print the paths.
    pub fn max_depth(&self) -> u32 {
        self.max_depth
    }

    /// The maximum number of paths, regardless of depth in the tree, to display.
    pub fn max_paths(&self) -> u32 {
        self.max_paths
    }

    /// The direction in which the call paths are traversed.
    pub fn descending(&self) -> bool {
        self.descending
    }

    /// Whether or not `functions` should be treated as regular expressions.
    pub fn using_regexps(&self) -> bool {
        self.using_regexps
    }

    /// Set the maximum depth to print the paths.
    pub fn set_max_depth(&mut self, max_depth: u32) {
        self.max_depth = max_depth;
    }

    /// Set the maximum number of paths, regardless of depth in the tree, to display.
    pub fn set_max_paths(&mut self, max_paths: u32) {
        self.max_paths = max_paths;
    }

    /// Set the call path traversal direction.
    pub fn set_descending(&mut self, descending: bool) {
        self.descending = descending;
    }

    /// Set Whether or not `functions` should be treated as regular expressions.
    pub fn set_using_regexps(&mut self, using_regexps: bool) {
        self.using_regexps = using_regexps;
    }
}

/// List the generic function monomorphizations that are contributing to
/// code bloat.
#[derive(Clone, Debug)]
#[derive(StructOpt)]
pub struct Monos {
    /// The path to the input binary to size profile.
    #[cfg(feature = "cli")]
    #[structopt(parse(from_os_str))]
    input: path::PathBuf,

    /// The names of the generic functions whose monomorphizations
    /// should be printed.
    functions: Vec<String>,

    /// The parse mode for the input binary data.
    #[cfg(feature = "cli")]
    #[structopt(short = "d", long = "mode", default_value = "auto")]
    parse_mode: traits::ParseMode,

    /// The destination to write the output to. Defaults to `stdout`.
    #[cfg(feature = "cli")]
    #[structopt(short = "o", default_value = "-")]
    output_destination: OutputDestination,

    /// The format the output should be written in.
    #[cfg(feature = "cli")]
    #[structopt(short = "f", long = "format", default_value = "text")]
    output_format: traits::OutputFormat,

    /// Hide individual monomorphizations and only show the generic functions.
    #[structopt(short = "g", long = "only-generics")]
    only_generics: bool,

    /// The maximum number of generics to list.
    #[structopt(short = "m", long = "max-generics", default_value = "10")]
    max_generics: u32,

    /// The maximum number of individual monomorphizations to list for each
    /// listed generic function.
    #[structopt(short = "n", long = "max-monos", default_value = "10")]
    max_monos: u32,

    /// List all generics and all of their individual monomorphizations.
    /// If combined with -g then monomorphizations are hidden.
    /// Overrides -m <max_generics> and -n <max_monos>
    #[structopt(short = "a", long = "all")]
    all_generics_and_monos: bool,

    /// List all generics. Overrides -m <max_generics>
    #[structopt(long = "all-generics")]
    all_generics: bool,

    /// List all individual monomorphizations for each listed generic
    /// function. Overrides -n <max_monos>
    #[structopt(long = "all-monos")]
    all_monos: bool,

    /// Whether or not `names` should be treated as regular expressions.
    #[structopt(long = "regex")]
    using_regexps: bool,
}

impl Default for Monos {
    fn default() -> Monos {
        Monos {
            #[cfg(feature = "cli")]
            input: Default::default(),
            #[cfg(feature = "cli")]
            parse_mode: Default::default(),
            #[cfg(feature = "cli")]
            output_destination: Default::default(),
            #[cfg(feature = "cli")]
            output_format: Default::default(),

            functions: Default::default(),

            only_generics: false,
            max_generics: 10,
            max_monos: 10,

            all_generics_and_monos: false,
            all_generics: false,
            all_monos: false,

            using_regexps: false,
        }
    }
}

impl Monos {
    // TODO: wasm-bindgen doesn't support sending Vec<String> across the wasm
    // ABI boundary yet.

    /// The functions to find call paths to.
    pub fn functions(&self) -> &[String] {
        &self.functions
    }
}

impl Monos {
    /// Construct a new, default `Monos`.
    pub fn new() -> Monos {
        Monos::default()
    }

    /// Hide individual monomorphizations and only show the generic functions.
    pub fn only_generics(&self) -> bool {
        self.only_generics
    }

    /// The maximum number of generics to list.
    pub fn max_generics(&self) -> u32 {
        if self.all_generics_and_monos || self.all_generics {
            u32::MAX
        } else {
            self.max_generics
        }
    }

    /// The maximum number of individual monomorphizations to list for each
    /// generic function.
    pub fn max_monos(&self) -> u32 {
        if self.all_generics_and_monos || self.all_monos {
            u32::MAX
        } else {
            self.max_monos
        }
    }

    /// Whether or not `functions` should be treated as regular expressions.
    pub fn using_regexps(&self) -> bool {
        self.using_regexps
    }

    /// Set whether to hide individual monomorphizations and only show the
    /// generic functions.
    pub fn set_only_generics(&mut self, do_it: bool) {
        self.only_generics = do_it;
    }

    /// Set the maximum number of generics to list.
    pub fn set_max_generics(&mut self, max: u32) {
        self.max_generics = max;
        self.all_generics = false;
        if self.all_generics_and_monos {
            self.all_generics_and_monos = false;
            self.all_monos = true;
        }
    }

    /// Set the maximum number of individual monomorphizations to list for each
    /// generic function.
    pub fn set_max_monos(&mut self, max: u32) {
        self.max_monos = max;
        self.all_monos = false;
        if self.all_generics_and_monos {
            self.all_generics_and_monos = false;
            self.all_generics = true;
        }
    }
}

/// Diff the old and new versions of a binary to see what sizes changed.
#[derive(Clone, Debug)]
#[derive(StructOpt)]
pub struct Diff {
    /// The path to the old version of the input binary.
    #[cfg(feature = "cli")]
    #[structopt(parse(from_os_str))]
    old_input: path::PathBuf,

    /// The parse mode for the input binary data.
    #[cfg(feature = "cli")]
    #[structopt(long = "mode", default_value = "auto")]
    parse_mode: traits::ParseMode,

    /// The path to the new version of the input binary.
    #[cfg(feature = "cli")]
    #[structopt(parse(from_os_str))]
    new_input: path::PathBuf,

    /// The destination to write the output to. Defaults to `stdout`.
    #[cfg(feature = "cli")]
    #[structopt(short = "o", default_value = "-")]
    output_destination: OutputDestination,

    /// The format the output should be written in.
    #[cfg(feature = "cli")]
    #[structopt(short = "f", long = "format", default_value = "text")]
    output_format: traits::OutputFormat,

    /// The name of the item(s) whose diff should be printed.
    items: Vec<String>,

    /// The maximum number of items to display.
    #[structopt(short = "n", default_value = "20")]
    max_items: u32,

    /// Whether or not `items` should be treated as regular expressions.
    #[structopt(long = "regex")]
    using_regexps: bool,

    /// Displays all items. Overrides -n <max_items>
    #[structopt(short = "a", long = "all")]
    all_items: bool,
}

impl Default for Diff {
    fn default() -> Diff {
        Diff {
            #[cfg(feature = "cli")]
            old_input: Default::default(),
            #[cfg(feature = "cli")]
            parse_mode: Default::default(),
            #[cfg(feature = "cli")]
            new_input: Default::default(),
            #[cfg(feature = "cli")]
            output_destination: Default::default(),
            #[cfg(feature = "cli")]
            output_format: Default::default(),

            items: Default::default(),
            max_items: 20,
            using_regexps: false,
            all_items: false,
        }
    }
}

impl Diff {
    // TODO: wasm-bindgen does not support sending Vec<String> across
    // the wasm ABI boundary yet.

    /// The items whose dominators subtree should be printed.
    pub fn items(&self) -> &[String] {
        &self.items
    }
}

impl Diff {
    /// The maximum number of items to display.
    pub fn max_items(&self) -> u32 {
        if self.all_items {
            u32::MAX
        } else {
            self.max_items
        }
    }

    /// Whether or not `items` should be treated as regular expressions.
    pub fn using_regexps(&self) -> bool {
        self.using_regexps
    }

    /// Set the maximum number of items to display.
    pub fn set_max_items(&mut self, n: u32) {
        self.max_items = n;
        self.all_items = false;
    }

    /// Set whether or not `items` should be treated as regular expressions.
    pub fn set_using_regexps(&mut self, using_regexps: bool) {
        self.using_regexps = using_regexps;
    }
}

/// Find and display code and data that is not transitively referenced by any
/// exports or public functions.
#[derive(Clone, Debug)]
#[derive(StructOpt)]
pub struct Garbage {
    /// The path to the input binary to size profile.
    #[cfg(feature = "cli")]
    #[structopt(parse(from_os_str))]
    input: path::PathBuf,

    /// The parse mode for the input binary data.
    #[cfg(feature = "cli")]
    #[structopt(long = "mode", default_value = "auto")]
    parse_mode: traits::ParseMode,

    /// The destination to write the output to. Defaults to `stdout`.
    #[cfg(feature = "cli")]
    #[structopt(short = "o", default_value = "-")]
    output_destination: OutputDestination,

    /// The format the output should be written in.
    #[cfg(feature = "cli")]
    #[structopt(short = "f", long = "format", default_value = "text")]
    output_format: traits::OutputFormat,

    /// The maximum number of items to display.
    #[structopt(short = "n", default_value = "10")]
    max_items: u32,

    /// Display all items. Overrides -n <max_items>
    #[structopt(short = "a", long = "all")]
    all_items: bool,

    /// Show data segments rather than summarizing them in a single line.
    #[structopt(long = "show-data-segments")]
    show_data_segments: bool,
}

impl Default for Garbage {
    fn default() -> Garbage {
        Garbage {
            #[cfg(feature = "cli")]
            input: Default::default(),
            #[cfg(feature = "cli")]
            parse_mode: Default::default(),
            #[cfg(feature = "cli")]
            output_destination: Default::default(),
            #[cfg(feature = "cli")]
            output_format: Default::default(),

            max_items: 10,
            all_items: false,
            show_data_segments: false,
        }
    }
}

impl Garbage {
    /// Construct a new, default `Garbage`
    pub fn new() -> Garbage {
        Garbage::default()
    }

    /// The maximum number of items to display.
    pub fn max_items(&self) -> u32 {
        if self.all_items {
            u32::MAX
        } else {
            self.max_items
        }
    }

    /// Set the maximum number of items to display.
    pub fn set_max_items(&mut self, max: u32) {
        self.max_items = max;
        self.all_items = false;
    }

    /// Should data segments be shown normally or summarized in a single line?
    pub fn show_data_segments(&self) -> bool {
        self.show_data_segments
    }
}