Skip to content

Commit

Permalink
Add an option to exclude based on source names
Browse files Browse the repository at this point in the history
  • Loading branch information
learn-more committed Aug 15, 2021
1 parent 60209eb commit d59d47d
Show file tree
Hide file tree
Showing 9 changed files with 619 additions and 14 deletions.
56 changes: 43 additions & 13 deletions src/bloaty.cc
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,9 @@ 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;
}

// Subtract the values in "other" from this.
Expand Down Expand Up @@ -325,7 +326,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 @@ -344,20 +346,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 @@ -1724,13 +1739,17 @@ void Bloaty::ScanAndRollupFiles(
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) {
try {
Expand Down Expand Up @@ -1875,6 +1894,8 @@ 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.
Options for debugging Bloaty:
Expand Down Expand Up @@ -2076,6 +2097,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 @@ -2184,6 +2207,13 @@ void BloatyDoMain(const Options& options, const InputFileFactory& file_factory,
}
}

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

verbose_level = options.verbose_level();

if (options.data_source_size() > 0) {
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

0 comments on commit d59d47d

Please sign in to comment.