Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an option to exclude based on source names #234

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 47 additions & 13 deletions src/bloaty.cc
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,10 @@ class Rollup {
CreateRows(row, base, options, true);
}

void SetFilterRegex(const ReImpl* regex) { filter_regex_ = regex; }
void SetFilterRegex(const ReImpl* regex_include, const ReImpl* regex_exclude) {
filter_regex_include_ = regex_include;
filter_regex_exclude_ = regex_exclude;
}

// Add the values in "other" from this.
void Add(const Rollup& other) {
Expand Down Expand Up @@ -316,7 +319,8 @@ class Rollup {
int64_t filtered_vm_total_ = 0;
int64_t filtered_file_total_ = 0;

const ReImpl* filter_regex_ = nullptr;
const ReImpl* filter_regex_include_ = nullptr;
const ReImpl* filter_regex_exclude_ = nullptr;

// Putting Rollup by value seems to work on some compilers/libs but not
// others.
Expand All @@ -335,20 +339,33 @@ class Rollup {
// If there are more entries names[i+1, i+2, etc] add them to sub-rollups.
void AddInternal(const std::vector<std::string>& names, size_t i,
uint64_t size, bool is_vmsize) {
if (filter_regex_ != nullptr) {
if (filter_regex_include_ != nullptr || filter_regex_exclude_ != nullptr) {
// filter_regex_ is only set in the root rollup, which checks the full
// label hierarchy for a match to determine whether a region should be
// considered.
bool any_matched = false;
bool exclude = false;
if (filter_regex_include_ != nullptr) {
bool any_matched = false;

for (const auto& name : names) {
if (ReImpl::PartialMatch(name, *filter_regex_include_)) {
any_matched = true;
break;
}
}
exclude = !any_matched;
}

for (const auto& name : names) {
if (ReImpl::PartialMatch(name, *filter_regex_)) {
any_matched = true;
break;
if (!exclude && filter_regex_exclude_ != nullptr) {
for (const auto& name : names) {
if (ReImpl::PartialMatch(name, *filter_regex_exclude_)) {
exclude = true;
break;
}
}
}

if (!any_matched) {
if (exclude) {
// Ignore this region in the rollup and don't visit sub-rollups.
if (is_vmsize) {
CheckedAdd(&filtered_vm_total_, size);
Expand Down Expand Up @@ -1810,13 +1827,17 @@ void Bloaty::ScanAndRollupFiles(const std::vector<std::string>& filenames,
std::vector<std::thread> threads(num_threads);
ThreadSafeIterIndex index(filenames.size());

std::unique_ptr<ReImpl> regex = nullptr;
std::unique_ptr<ReImpl> regex_include = nullptr;
std::unique_ptr<ReImpl> regex_exclude = nullptr;
if (options_.has_source_filter()) {
regex = absl::make_unique<ReImpl>(options_.source_filter());
regex_include = absl::make_unique<ReImpl>(options_.source_filter());
}
if (options_.has_exclude_source_filter()) {
regex_exclude = absl::make_unique<ReImpl>(options_.exclude_source_filter());
}

for (int i = 0; i < num_threads; i++) {
thread_data[i].rollup.SetFilterRegex(regex.get());
thread_data[i].rollup.SetFilterRegex(regex_include.get(), regex_exclude.get());

threads[i] = std::thread(
[this, &index, &filenames](PerThreadData* data) {
Expand Down Expand Up @@ -1960,6 +1981,10 @@ USAGE: bloaty [OPTION]... FILE... [-- BASE_FILE...]
--list-sources Show a list of available sources and exit.
--source-filter=PATTERN
Only show keys with names matching this pattern.
--exclude-source-filter=PATTERN
Exclude keys with names matching this pattern.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should document what happens if a name matches both --source-filter and --exclude-source-filter.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wrote a test that shows what happens, basically nothing is output other than the fact that everything is omitted.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has been added the the help text.

When both --source-filter and --exclude-source-filter
match the same data, the data will be excluded.

Options for debugging Bloaty:

Expand Down Expand Up @@ -2159,6 +2184,8 @@ bool DoParseOptions(bool skip_unknown, int* argc, char** argv[],
}
} else if (args.TryParseOption("--source-filter", &option)) {
options->set_source_filter(std::string(option));
} else if (args.TryParseOption("--exclude-source-filter", &option)) {
options->set_exclude_source_filter(std::string(option));
} else if (args.TryParseFlag("-v")) {
options->set_verbose_level(1);
} else if (args.TryParseFlag("-vv")) {
Expand Down Expand Up @@ -2264,7 +2291,14 @@ void BloatyDoMain(const Options& options, const InputFileFactory& file_factory,
if (options.has_source_filter()) {
ReImpl re(options.source_filter());
if (!re.ok()) {
THROW("invalid regex for source_filter");
THROW("invalid regex for --source-filter");
}
}

if (options.has_exclude_source_filter()) {
ReImpl re(options.exclude_source_filter());
if (!re.ok()) {
THROW("invalid regex for --exclude-source-filter");
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/bloaty.proto
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ message Options {

// Dump raw memory map instead of printing normal output.
optional bool dump_raw_map = 14;

// Regex with which to exclude names in the data sources.
optional string exclude_source_filter = 15;
}

// A custom data source allows users to create their own label space by
Expand Down
97 changes: 97 additions & 0 deletions tests/PE/filter/exclude-source-filter-text-pe32.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# RUN: %yaml2obj %s -o %t.obj
# RUN: %bloaty --exclude-source-filter text %t.obj | %FileCheck %s --dump-input fail

--- !COFF
OptionalHeader:
AddressOfEntryPoint: 4160
ImageBase: 268435456
SectionAlignment: 4096
FileAlignment: 512
MajorOperatingSystemVersion: 4
MinorOperatingSystemVersion: 0
MajorImageVersion: 0
MinorImageVersion: 0
MajorSubsystemVersion: 4
MinorSubsystemVersion: 0
Subsystem: IMAGE_SUBSYSTEM_WINDOWS_GUI
DLLCharacteristics: [ ]
SizeOfStackReserve: 1048576
SizeOfStackCommit: 4096
SizeOfHeapReserve: 1048576
SizeOfHeapCommit: 4096
ExportTable:
RelativeVirtualAddress: 8304
Size: 366
ImportTable:
RelativeVirtualAddress: 8224
Size: 40
ResourceTable:
RelativeVirtualAddress: 0
Size: 0
ExceptionTable:
RelativeVirtualAddress: 0
Size: 0
CertificateTable:
RelativeVirtualAddress: 0
Size: 0
BaseRelocationTable:
RelativeVirtualAddress: 12288
Size: 16
Debug:
RelativeVirtualAddress: 0
Size: 0
Architecture:
RelativeVirtualAddress: 0
Size: 0
GlobalPtr:
RelativeVirtualAddress: 0
Size: 0
TlsTable:
RelativeVirtualAddress: 0
Size: 0
LoadConfigTable:
RelativeVirtualAddress: 0
Size: 0
BoundImport:
RelativeVirtualAddress: 0
Size: 0
IAT:
RelativeVirtualAddress: 8264
Size: 8
DelayImportDescriptor:
RelativeVirtualAddress: 0
Size: 0
ClrRuntimeHeader:
RelativeVirtualAddress: 0
Size: 0
header:
Machine: IMAGE_FILE_MACHINE_I386
Characteristics: [ IMAGE_FILE_EXECUTABLE_IMAGE, IMAGE_FILE_LINE_NUMS_STRIPPED, IMAGE_FILE_LOCAL_SYMS_STRIPPED, IMAGE_FILE_32BIT_MACHINE, IMAGE_FILE_DEBUG_STRIPPED, IMAGE_FILE_DLL ]
sections:
- Name: .text
Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
VirtualAddress: 4096
VirtualSize: 160
SectionData: 5589E581EC0000000090B80020001050E88300000083C404C9C35589E581EC0000000090B80920001050E86900000083C404B800000000E900000000C9C210005589E581EC04000000908B4510508B450C508B450850E8250000008945FC8B45FCE900000000C9C20C00000000000000000000000000000000000000000000005589E581EC0000000090B801000000E900000000C9C20C00FF25482000100000
- Name: .data
Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ, IMAGE_SCN_MEM_WRITE ]
VirtualAddress: 8192
VirtualSize: 480
SectionData: 637572696F757321004D61726B204A616E73656E00000000000000000000000050200000000000000000000058200000482000000000000000000000000000000000000000000000632000000000000063200000000000006D73766372742E646C6C00000070757473000000000000000000000000000000000000001A210000010000000D0000000D00000098200000CC20000000210000801000001A1000004010000000300000003000000030000000300000003000000030000020200000E0210000981000000010000027210000332100003F2100004D2100005E210000712100008221000095210000A9210000BF210000C6210000CB210000D221000000000100020003000400050006000700080009000A000B000C00706533325F7374642E646C6C005F446C6C4D61696E403132005F446C6C4D61696E403136005F5F646C6C7374617274403132005F5F66696E695F61727261795F656E64005F5F66696E695F61727261795F7374617274005F5F696E69745F61727261795F656E64005F5F696E69745F61727261795F7374617274005F5F707265696E69745F61727261795F656E64005F5F707265696E69745F61727261795F7374617274005F6564617461005F656E64005F65746578740068656C6C6F5F7468657265000000
- Name: .reloc
Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_DISCARDABLE, IMAGE_SCN_MEM_READ ]
VirtualAddress: 12288
VirtualSize: 16
SectionData: 00100000100000000B3025309A300000
symbols: []
...

# CHECK: FILE SIZE VM SIZE
# CHECK: -------------- --------------
# CHECK: 33.3% 512 48.4% 480 .data
# CHECK: 33.3% 512 1.6% 16 .reloc
# CHECK: 24.5% 376 37.9% 376 [PE Headers]
# CHECK: 7.8% 120 12.1% 120 [PE Section Headers]
# CHECK: 1.0% 16 0.0% 0 [Unmapped]
# CHECK: 100.0% 1.50Ki 100.0% 992 TOTAL
# CHECK:Filtering enabled (source_filter); omitted file = 512, vm = 160 of entries
97 changes: 97 additions & 0 deletions tests/PE/filter/exclude-source-filter-text-pe64.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# RUN: %yaml2obj %s -o %t.obj
# RUN: %bloaty --exclude-source-filter text %t.obj | %FileCheck %s --dump-input fail

--- !COFF
OptionalHeader:
AddressOfEntryPoint: 4192
ImageBase: 268435456
SectionAlignment: 4096
FileAlignment: 512
MajorOperatingSystemVersion: 4
MinorOperatingSystemVersion: 0
MajorImageVersion: 0
MinorImageVersion: 0
MajorSubsystemVersion: 4
MinorSubsystemVersion: 0
Subsystem: IMAGE_SUBSYSTEM_WINDOWS_GUI
DLLCharacteristics: [ ]
SizeOfStackReserve: 1048576
SizeOfStackCommit: 4096
SizeOfHeapReserve: 1048576
SizeOfHeapCommit: 4096
ExportTable:
RelativeVirtualAddress: 8320
Size: 336
ImportTable:
RelativeVirtualAddress: 8224
Size: 40
ResourceTable:
RelativeVirtualAddress: 0
Size: 0
ExceptionTable:
RelativeVirtualAddress: 12288
Size: 36
CertificateTable:
RelativeVirtualAddress: 0
Size: 0
BaseRelocationTable:
RelativeVirtualAddress: 0
Size: 0
Debug:
RelativeVirtualAddress: 0
Size: 0
Architecture:
RelativeVirtualAddress: 0
Size: 0
GlobalPtr:
RelativeVirtualAddress: 0
Size: 0
TlsTable:
RelativeVirtualAddress: 0
Size: 0
LoadConfigTable:
RelativeVirtualAddress: 0
Size: 0
BoundImport:
RelativeVirtualAddress: 0
Size: 0
IAT:
RelativeVirtualAddress: 8264
Size: 16
DelayImportDescriptor:
RelativeVirtualAddress: 0
Size: 0
ClrRuntimeHeader:
RelativeVirtualAddress: 0
Size: 0
header:
Machine: IMAGE_FILE_MACHINE_AMD64
Characteristics: [ IMAGE_FILE_EXECUTABLE_IMAGE, IMAGE_FILE_LINE_NUMS_STRIPPED, IMAGE_FILE_LOCAL_SYMS_STRIPPED, IMAGE_FILE_LARGE_ADDRESS_AWARE, IMAGE_FILE_DEBUG_STRIPPED, IMAGE_FILE_DLL ]
sections:
- Name: .text
Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
VirtualAddress: 4096
VirtualSize: 168
SectionData: 554889E54881EC20000000488D0DEE0F0000E889000000C9C30000000104020504030150554889E54881EC2000000048894D10488955184C8945204C894D28488D0DC30F0000E855000000B800000000E900000000C9C3000000000000000000554889E54881EC3000000048894D10488955184C894520488B45204989C08B5518488B4D10E89AFFFFFF8945FC8B45FCE900000000C9C3000104020504030150FF25A20F00000000
- Name: .data
Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ, IMAGE_SCN_MEM_WRITE ]
VirtualAddress: 8192
VirtualSize: 480
SectionData: 637572696F757321004D61726B204A616E73656E0000000000000000000000005820000000000000000000006820000048200000000000000000000000000000000000000000000073200000000000000000000000000000732000000000000000000000000000006D73766372742E646C6C000000707574730000000000000000000000000000000000000020210000010000000C0000000C000000A8200000D820000008210000241000000040000000400000004000000040000000400000004000006010000020200000E0210000A0100000001000002D2100003521000046210000592100006A2100007D21000091210000A7210000B1210000B8210000BD210000C421000000000100020003000400050006000700080009000A000B00706536345F7374642E646C6C00446C6C4D61696E005F5F66696E695F61727261795F656E64005F5F66696E695F61727261795F7374617274005F5F696E69745F61727261795F656E64005F5F696E69745F61727261795F7374617274005F5F707265696E69745F61727261795F656E64005F5F707265696E69745F61727261795F7374617274005F646C6C7374617274005F6564617461005F656E64005F65746578740068656C6C6F5F74686572650000000000000000000000000000000000
- Name: .pdata
Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ ]
VirtualAddress: 12288
VirtualSize: 36
SectionData: 0B100000191000001C1000002F100000571000001C1000006B1000009710000098100000
symbols: []
...

# CHECK: FILE SIZE VM SIZE
# CHECK: -------------- --------------
# CHECK: 33.3% 512 46.7% 480 .data
# CHECK: 33.3% 512 3.5% 36 .pdata
# CHECK: 25.5% 392 38.1% 392 [PE Headers]
# CHECK: 7.8% 120 11.7% 120 [PE Section Headers]
# CHECK: 100.0% 1.50Ki 100.0% 1.00Ki TOTAL
# CHECK:Filtering enabled (source_filter); omitted file = 512, vm = 168 of entries

Loading