You signed in with another tab or window. Reload to refresh your session.You signed out in an
8000
other tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Ever wondered what's making your binary big? Bloaty
McBloatface will show you a size profile of the binary so
you can understand what's taking up space inside.
Bloaty works on binaries, shared objects, object files, and
static libraries (.a files). The following file formats
are supported:
ELF
Mach-O
WebAssembly (experimental)
These formats are NOT supported, but I am very interested
in adding support for them (I may implement these myself but
would also be happy to get contributions!)
PE/COFF (not supported)
Android APK (not supported, might be tricky due to compression)
This is not an official Google product.
Building Bloaty
Building Bloaty requires CMake and protoc, the protobuf compiler. On Ubuntu, install them with:
$ sudo apt install cmake protobuf-compiler
Bloaty bundles libprotobuf, re2, capstone, and pkg-config as Git submodules, but it will prefer the system's versions of those dependencies if available. All other dependencies are included as Git submodules. To build, run:
$ cmake .
$ make -j6
To run tests (Git only, these are not included in the release tarball), type:
$ make test
All the normal CMake features are available, like out-of-source builds:
$ mkdir build
$ cd build
$ cmake ..
$ make -j6
Running Bloaty
Run it directly on a binary target. For example, run it on itself.
The "VM SIZE" column tells you how much space the binary
will take when it is loaded into memory. The "FILE SIZE"
column tells you about how much space the binary is taking
on disk. These two can be very different from each other:
Some data lives in the file but isn't loaded into memory,
like debug information.
Some data is mapped into memory but doesn't exist in the
file. This mainly applies to the .bss section
(zero-initialized data).
The default breakdown in Bloaty is by sections, but many
other ways of slicing the binary are supported such as
symbols and segments. If you compiled with debug info, you
can even break down by compile units and inlines!
Run Bloaty with --help to see a list of available options:
$ ./bloaty --help
Bloaty McBloatface: a size profiler for binaries.
USAGE: bloaty [OPTION]... FILE... [-- BASE_FILE...]
Options:
--csv Output in CSV format instead of human-readable.
--tsv Output in TSV format instead of human-readable.
-c FILE Load configuration from <file>.
-d SOURCE,SOURCE Comma-separated list of sources to scan.
--debug-file=FILE Use this file for debug symbols and/or symbol table.
-C MODE How to demangle symbols. Possible values are:
--demangle=MODE --demangle=none no demangling, print raw symbols
--demangle=short demangle, but omit arg/return types
--demangle=full print full demangled type
The default is --demangle=short.
--disassemble=FUNCTION
Disassemble this function (EXPERIMENTAL)
--domain=DOMAIN Which domains to show. Possible values are:
--domain=vm
--domain=file
--domain=both (the default)
-n NUM How many rows to show per level before collapsing
other keys into '[Other]'. Set to '0' for unlimited.
Defaults to 20.
-s SORTBY Whether to sort by VM or File size. Possible values
are:
-s vm
-s file
-s both (the default: sorts by max(vm, file)).
-w Wide output; don't truncate long labels.
--help Display this message and exit.
--list-sources Show a list of available sources and exit.
--source-filter=PATTERN
Only show keys with names matching this pattern.
Options for debugging Bloaty:
--debug-vmaddr=ADDR
--debug-fileoff=OFF
Print extended debugging information for the given
VM address and/or file offset.
-v Verbose output. Dumps warnings encountered during
processing and full VM/file maps at the end.
Add more v's (-vv, -vvv) for even more.
Size Diffs
You can use Bloaty to see how the size of a binary changed.
On the command-line, pass -- followed by the files you
want to use as the diff base.
For example, here is a size diff between a couple different versions
of Bloaty, showing how it grew when I added some features.
Each line shows the how much each part changed compared to
its previous size. Most sections grew, but one section at
the bottom (.debug_str) shrank. The "TOTAL" line shows
how much the size changed overall.
Hierarchical Profiles
Bloaty supports breaking the binary down in lots of
different ways. You can combine multiple data sources into
a single hierarchical profile. For example, we can use the
segments and sections data sources in a single report:
Bloaty displays a maximum of 20 lines for each level; other
values are grouped into an [Other] bin. Use -n <num>
to override this setting. If you pass -n 0, all data
will be output without collapsing anything into [Other].
Debugging Stripped Binaries
Bloaty supports reading debuginfo/symbols from separate
binaries. This lets you profile a stripped binary, even for
data sources like "compileunits" or "symbols" that require
this extra information.
Bloaty uses build IDs to verify that the binary and the
debug file match. Otherwise the results would be nonsense
(this kind of mismatch might sound unlikely but it's a very
easy mistake to make, and one that I made several times even
as Bloaty's author!).
If your binary has a build ID, then using separate debug
files is as simple as:
For ELF, make sure you are compiling with build IDs enabled.
With gcc this happens automatically, but Clang decided not
to make this the default, since it makes the link
slower.
For Clang add -Wl,--build-id to your link line. (If you
want a slightly faster link and don't care about
reproducibility, you can use -Wl,--build-id=uuid instead).
Bloaty does not currently support the GNU debuglink or
looking up debug files by build ID, which are the methods
GDB uses to find debug
files.
If there are use cases where Bloaty's --debug-file option
won't work, we can reconsider implementing these.
Mach-O
Mach-O files always have build IDs (as far as I can tell),
so no special configuration is needed to make sure you get
them.
Mach-O puts debug information in separate files which you
can create with dsymutil:
Any options that you can specify on the command-line, you
can put into a configuration file instead. Then use can use
-c FILE to load those options from the config file. Also,
a few features are only available with configuration files
and cannot be specify on the command-line.
The configuration file is a in Protocol Buffers text format.
The schema is the Options message in
src/bloaty.proto.
The two most useful cases for configuration files are:
You have too many input files to put on the command-line.
At Google we sometimes run Bloaty over thousands of input
files. This can cause the overall command-line to exceed
OS limits. With a config file, we can avoid this:
filename: "path/to/long_filename_a.o"
filename: "path/to/long_filename_b.o"
filename: "path/to/long_filename_c.o"
# ...repeat for thousands of files.
For custom data sources, it can be very useful to put
them in a config file, for greater reusability. For
example, see the custom data sources defined in
custom_sources.bloaty.
Also read more about custom data sources below.
Data Sources
Bloaty has many data sources built in. These all provide
different ways of looking at the binary. You can also
create your own data sources by applying regexes to the
built-in data sources (see "Custom Data Sources" below).
While Bloaty works on binaries, shared objects, object
files, and static libraries (.a files), some of the data
sources don't work on object files. This applies especially
to data sources that read debug info.
Segments
Segments are what the run-time loader uses to determine what
parts of the binary need to be loaded/mapped into memory.
There are usually just a few segments: one for each set of
mmap() permissions required:
Here we see one segment mapped [RX] (read/execute) and
one segment mapped [RW] (read/write). A large part of
the binary is not loaded into memory, which we see as
[Unmapped].
Object files and static libraries don't have segments.
However we fake it by grouping sections by their flags.
This gives us a break-down sort of like real segments.
Sections give us a bit more granular look into the binary.
If we want to find the symbol table, the unwind information,
or the debug information, each kind of information lives in
its own section. Bloaty's default output is sections.
You can control how symbols are demangled with the -C MODE
or --demangle=MODE flag. You can also specify the
demangling mode explicitly in the -d switch. We have
three different demangling modes:
-C none or -d rawsymbols: no, demangling.
-C short or -d shortsymbols: short demangling: return
types, template parameters, and function parameter types
are omitted. For example:
bloaty::dwarf::FormReader<>::GetFunctionForForm<>().
This is the default.
-C full or -d fullsymbols: full demangling.
One very handy thing about -C short (the default) is that
it groups all template instantiations together, regardless
of their parameters. You can use this to determine how much
code size you are paying by doing multiple instantiations of
templates. Try bloaty -d shortsymbols,fullsymbols.
Input Files
When you pass multiple files to Bloaty, the inputfiles
source will let you break it down by input file:
You are free to use this data source even for non-.a
files, but it won't be very useful since it will always just
resolve to the input file (the .a file).
Compile Units
Using debug information, we can tell what compile unit (and
corresponding source file) each bit of the binary came from.
The DWARF debugging information also contains "line info"
information that understands inlining. So within a
function, it will know which instructions came from an
inlined function from a header file. This is the
information the debugger uses to point at a specific source
line as you're tracing through a program.
Sometimes you want to munge the labels from an existing data
source. For example, when we use "compileunits" on Bloaty
itself, we see files from all our dependencies mixed
together:
If we want to bucket all of these by which library they came
from, we can write a custom data source. It specifies the
base data source and a set of regexes to apply to it. The
regexes are tried in order, and the first matching regex
will cause the entire label to be rewritten to the
replacement text. Regexes follow RE2
syntax and the
replacement can refer to capture groups.
Sometimes, you are only interested in parts of the binary
instead of the whole package. This is common in embedded
programming, where ELF files are used only as a container
format, and only a few sections are actually loaded onto
the device.
For this, Bloaty provides a --source-filter option which
allows filtering out irrelevant data. It takes a regex
which is applied to each of the symbol names in a data
source. Only symbols which match the regex are displayed
in the output. This is especially powerful when combined
with custom data sources, as the rewriting occurs before
the filtering.
In the case of hierarchical data source profiles, the regex
is applied to all symbol names in the hierarchy. If any
name matches, all of its parents will be displayed as well.
For example, given the above scenario, maybe we are only
interested in how large the first-party Bloaty code is.
This can be displayed using a source filter on the src
directory.