1 //===-- sancov.cpp --------------------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 // This file is a command-line tool for reading and analyzing sanitizer
10 // coverage.
11 //===----------------------------------------------------------------------===//
12 #include "llvm/ADT/STLExtras.h"
13 #include "llvm/ADT/StringExtras.h"
14 #include "llvm/ADT/Twine.h"
15 #include "llvm/DebugInfo/Symbolize/Symbolize.h"
16 #include "llvm/MC/MCAsmInfo.h"
17 #include "llvm/MC/MCContext.h"
18 #include "llvm/MC/MCDisassembler/MCDisassembler.h"
19 #include "llvm/MC/MCInst.h"
20 #include "llvm/MC/MCInstrAnalysis.h"
21 #include "llvm/MC/MCInstrInfo.h"
22 #include "llvm/MC/MCObjectFileInfo.h"
23 #include "llvm/MC/MCRegisterInfo.h"
24 #include "llvm/MC/MCSubtargetInfo.h"
25 #include "llvm/Object/Archive.h"
26 #include "llvm/Object/Binary.h"
27 #include "llvm/Object/COFF.h"
28 #include "llvm/Object/MachO.h"
29 #include "llvm/Object/ObjectFile.h"
30 #include "llvm/Support/Casting.h"
31 #include "llvm/Support/CommandLine.h"
32 #include "llvm/Support/Errc.h"
33 #include "llvm/Support/ErrorOr.h"
34 #include "llvm/Support/FileSystem.h"
35 #include "llvm/Support/MD5.h"
36 #include "llvm/Support/ManagedStatic.h"
37 #include "llvm/Support/MemoryBuffer.h"
38 #include "llvm/Support/Path.h"
39 #include "llvm/Support/PrettyStackTrace.h"
40 #include "llvm/Support/Regex.h"
41 #include "llvm/Support/SHA1.h"
42 #include "llvm/Support/Signals.h"
43 #include "llvm/Support/SourceMgr.h"
44 #include "llvm/Support/SpecialCaseList.h"
45 #include "llvm/Support/TargetRegistry.h"
46 #include "llvm/Support/TargetSelect.h"
47 #include "llvm/Support/YAMLParser.h"
48 #include "llvm/Support/raw_ostream.h"
49
50 #include <set>
51 #include <vector>
52
53 using namespace llvm;
54
55 namespace {
56
57 // --------- COMMAND LINE FLAGS ---------
58
59 enum ActionType {
60 CoveredFunctionsAction,
61 HtmlReportAction,
62 MergeAction,
63 NotCoveredFunctionsAction,
64 PrintAction,
65 PrintCovPointsAction,
66 StatsAction,
67 SymbolizeAction
68 };
69
70 cl::opt<ActionType> Action(
71 cl::desc("Action (required)"), cl::Required,
72 cl::values(
73 clEnumValN(PrintAction, "print", "Print coverage addresses"),
74 clEnumValN(PrintCovPointsAction, "print-coverage-pcs",
75 "Print coverage instrumentation points addresses."),
76 clEnumValN(CoveredFunctionsAction, "covered-functions",
77 "Print all covered funcions."),
78 clEnumValN(NotCoveredFunctionsAction, "not-covered-functions",
79 "Print all not covered funcions."),
80 clEnumValN(StatsAction, "print-coverage-stats",
81 "Print coverage statistics."),
82 clEnumValN(HtmlReportAction, "html-report",
83 "REMOVED. Use -symbolize & coverage-report-server.py."),
84 clEnumValN(SymbolizeAction, "symbolize",
85 "Produces a symbolized JSON report from binary report."),
86 clEnumValN(MergeAction, "merge", "Merges reports.")));
87
88 static cl::list<std::string>
89 ClInputFiles(cl::Positional, cl::OneOrMore,
90 cl::desc("<action> <binary files...> <.sancov files...> "
91 "<.symcov files...>"));
92
93 static cl::opt<bool> ClDemangle("demangle", cl::init(true),
94 cl::desc("Print demangled function name."));
95
96 static cl::opt<bool>
97 ClSkipDeadFiles("skip-dead-files", cl::init(true),
98 cl::desc("Do not list dead source files in reports."));
99
100 static cl::opt<std::string> ClStripPathPrefix(
101 "strip_path_prefix", cl::init(""),
102 cl::desc("Strip this prefix from file paths in reports."));
103
104 static cl::opt<std::string>
105 ClBlacklist("blacklist", cl::init(""),
106 cl::desc("Blacklist file (sanitizer blacklist format)."));
107
108 static cl::opt<bool> ClUseDefaultBlacklist(
109 "use_default_blacklist", cl::init(true), cl::Hidden,
110 cl::desc("Controls if default blacklist should be used."));
111
112 static const char *const DefaultBlacklistStr = "fun:__sanitizer_.*\n"
113 "src:/usr/include/.*\n"
114 "src:.*/libc\\+\\+/.*\n";
115
116 // --------- FORMAT SPECIFICATION ---------
117
118 struct FileHeader {
119 uint32_t Bitness;
120 uint32_t Magic;
121 };
122
123 static const uint32_t BinCoverageMagic = 0xC0BFFFFF;
124 static const uint32_t Bitness32 = 0xFFFFFF32;
125 static const uint32_t Bitness64 = 0xFFFFFF64;
126
127 static Regex SancovFileRegex("(.*)\\.[0-9]+\\.sancov");
128 static Regex SymcovFileRegex(".*\\.symcov");
129
130 // --------- MAIN DATASTRUCTURES ----------
131
132 // Contents of .sancov file: list of coverage point addresses that were
133 // executed.
134 struct RawCoverage {
RawCoverage__anonb1aff5e70111::RawCoverage135 explicit RawCoverage(std::unique_ptr<std::set<uint64_t>> Addrs)
136 : Addrs(std::move(Addrs)) {}
137
138 // Read binary .sancov file.
139 static ErrorOr<std::unique_ptr<RawCoverage>>
140 read(const std::string &FileName);
141
142 std::unique_ptr<std::set<uint64_t>> Addrs;
143 };
144
145 // Coverage point has an opaque Id and corresponds to multiple source locations.
146 struct CoveragePoint {
CoveragePoint__anonb1aff5e70111::CoveragePoint147 explicit CoveragePoint(const std::string &Id) : Id(Id) {}
148
149 std::string Id;
150 SmallVector<DILineInfo, 1> Locs;
151 };
152
153 // Symcov file content: set of covered Ids plus information about all available
154 // coverage points.
155 struct SymbolizedCoverage {
156 // Read json .symcov file.
157 static std::unique_ptr<SymbolizedCoverage> read(const std::string &InputFile);
158
159 std::set<std::string> CoveredIds;
160 std::string BinaryHash;
161 std::vector<CoveragePoint> Points;
162 };
163
164 struct CoverageStats {
165 size_t AllPoints;
166 size_t CovPoints;
167 size_t AllFns;
168 size_t CovFns;
169 };
170
171 // --------- ERROR HANDLING ---------
172
fail(const llvm::Twine & E)173 static void fail(const llvm::Twine &E) {
174 errs() << "ERROR: " << E << "\n";
175 exit(1);
176 }
177
failIf(bool B,const llvm::Twine & E)178 static void failIf(bool B, const llvm::Twine &E) {
179 if (B)
180 fail(E);
181 }
182
failIfError(std::error_code Error)183 static void failIfError(std::error_code Error) {
184 if (!Error)
185 return;
186 errs() << "ERROR: " << Error.message() << "(" << Error.value() << ")\n";
187 exit(1);
188 }
189
failIfError(const ErrorOr<T> & E)190 template <typename T> static void failIfError(const ErrorOr<T> &E) {
191 failIfError(E.getError());
192 }
193
failIfError(Error Err)194 static void failIfError(Error Err) {
195 if (Err) {
196 logAllUnhandledErrors(std::move(Err), errs(), "ERROR: ");
197 exit(1);
198 }
199 }
200
failIfError(Expected<T> & E)201 template <typename T> static void failIfError(Expected<T> &E) {
202 failIfError(E.takeError());
203 }
204
failIfNotEmpty(const llvm::Twine & E)205 static void failIfNotEmpty(const llvm::Twine &E) {
206 if (E.str().empty())
207 return;
208 fail(E);
209 }
210
211 template <typename T>
failIfEmpty(const std::unique_ptr<T> & Ptr,const std::string & Message)212 static void failIfEmpty(const std::unique_ptr<T> &Ptr,
213 const std::string &Message) {
214 if (Ptr.get())
215 return;
216 fail(Message);
217 }
218
219 // ----------- Coverage I/O ----------
220 template <typename T>
readInts(const char * Start,const char * End,std::set<uint64_t> * Ints)221 static void readInts(const char *Start, const char *End,
222 std::set<uint64_t> *Ints) {
223 const T *S = reinterpret_cast<const T *>(Start);
224 const T *E = reinterpret_cast<const T *>(End);
225 std::copy(S, E, std::inserter(*Ints, Ints->end()));
226 }
227
228 ErrorOr<std::unique_ptr<RawCoverage>>
read(const std::string & FileName)229 RawCoverage::read(const std::string &FileName) {
230 ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
231 MemoryBuffer::getFile(FileName);
232 if (!BufOrErr)
233 return BufOrErr.getError();
234 std::unique_ptr<MemoryBuffer> Buf = std::move(BufOrErr.get());
235 if (Buf->getBufferSize() < 8) {
236 errs() << "File too small (<8): " << Buf->getBufferSize() << '\n';
237 return make_error_code(errc::illegal_byte_sequence);
238 }
239 const FileHeader *Header =
240 reinterpret_cast<const FileHeader *>(Buf->getBufferStart());
241
242 if (Header->Magic != BinCoverageMagic) {
243 errs() << "Wrong magic: " << Header->Magic << '\n';
244 return make_error_code(errc::illegal_byte_sequence);
245 }
246
247 auto Addrs = llvm::make_unique<std::set<uint64_t>>();
248
249 switch (Header->Bitness) {
250 case Bitness64:
251 readInts<uint64_t>(Buf->getBufferStart() + 8, Buf->getBufferEnd(),
252 Addrs.get());
253 break;
254 case Bitness32:
255 readInts<uint32_t>(Buf->getBufferStart() + 8, Buf->getBufferEnd(),
256 Addrs.get());
257 break;
258 default:
259 errs() << "Unsupported bitness: " << Header->Bitness << '\n';
260 return make_error_code(errc::illegal_byte_sequence);
261 }
262
263 return std::unique_ptr<RawCoverage>(new RawCoverage(std::move(Addrs)));
264 }
265
266 // Print coverage addresses.
operator <<(raw_ostream & OS,const RawCoverage & CoverageData)267 raw_ostream &operator<<(raw_ostream &OS, const RawCoverage &CoverageData) {
268 for (auto Addr : *CoverageData.Addrs) {
269 OS << "0x";
270 OS.write_hex(Addr);
271 OS << "\n";
272 }
273 return OS;
274 }
275
operator <<(raw_ostream & OS,const CoverageStats & Stats)276 static raw_ostream &operator<<(raw_ostream &OS, const CoverageStats &Stats) {
277 OS << "all-edges: " << Stats.AllPoints << "\n";
278 OS << "cov-edges: " << Stats.CovPoints << "\n";
279 OS << "all-functions: " << Stats.AllFns << "\n";
280 OS << "cov-functions: " << Stats.CovFns << "\n";
281 return OS;
282 }
283
284 // Helper for writing out JSON. Handles indents and commas using
285 // scope variables for objects and arrays.
286 class JSONWriter {
287 public:
JSONWriter(raw_ostream & Out)288 JSONWriter(raw_ostream &Out) : OS(Out) {}
289 JSONWriter(const JSONWriter &) = delete;
~JSONWriter()290 ~JSONWriter() { OS << "\n"; }
291
operator <<(StringRef S)292 void operator<<(StringRef S) { printJSONStringLiteral(S, OS); }
293
294 // Helper RAII class to output JSON objects.
295 class Object {
296 public:
Object(JSONWriter * W,raw_ostream & OS)297 Object(JSONWriter *W, raw_ostream &OS) : W(W), OS(OS) {
298 OS << "{";
299 W->Indent++;
300 }
301 Object(const Object &) = delete;
~Object()302 ~Object() {
303 W->Indent--;
304 OS << "\n";
305 W->indent();
306 OS << "}";
307 }
308
key(StringRef Key)309 void key(StringRef Key) {
310 Index++;
311 if (Index > 0)
312 OS << ",";
313 OS << "\n";
314 W->indent();
315 printJSONStringLiteral(Key, OS);
316 OS << " : ";
317 }
318
319 private:
320 JSONWriter *W;
321 raw_ostream &OS;
322 int Index = -1;
323 };
324
object()325 std::unique_ptr<Object> object() { return make_unique<Object>(this, OS); }
326
327 // Helper RAII class to output JSON arrays.
328 class Array {
329 public:
Array(raw_ostream & OS)330 Array(raw_ostream &OS) : OS(OS) { OS << "["; }
331 Array(const Array &) = delete;
~Array()332 ~Array() { OS << "]"; }
next()333 void next() {
334 Index++;
335 if (Index > 0)
336 OS << ", ";
337 }
338
339 private:
340 raw_ostream &OS;
341 int Index = -1;
342 };
343
array()344 std::unique_ptr<Array> array() { return make_unique<Array>(OS); }
345
346 private:
indent()347 void indent() { OS.indent(Indent * 2); }
348
printJSONStringLiteral(StringRef S,raw_ostream & OS)349 static void printJSONStringLiteral(StringRef S, raw_ostream &OS) {
350 if (S.find('"') == std::string::npos) {
351 OS << "\"" << S << "\"";
352 return;
353 }
354 OS << "\"";
355 for (char Ch : S.bytes()) {
356 if (Ch == '"')
357 OS << "\\";
358 OS << Ch;
359 }
360 OS << "\"";
361 }
362
363 raw_ostream &OS;
364 int Indent = 0;
365 };
366
367 // Output symbolized information for coverage points in JSON.
368 // Format:
369 // {
370 // '<file_name>' : {
371 // '<function_name>' : {
372 // '<point_id'> : '<line_number>:'<column_number'.
373 // ....
374 // }
375 // }
376 // }
operator <<(JSONWriter & W,const std::vector<CoveragePoint> & Points)377 static void operator<<(JSONWriter &W,
378 const std::vector<CoveragePoint> &Points) {
379 // Group points by file.
380 auto ByFile(W.object());
381 std::map<std::string, std::vector<const CoveragePoint *>> PointsByFile;
382 for (const auto &Point : Points) {
383 for (const DILineInfo &Loc : Point.Locs) {
384 PointsByFile[Loc.FileName].push_back(&Point);
385 }
386 }
387
388 for (const auto &P : PointsByFile) {
389 std::string FileName = P.first;
390 ByFile->key(FileName);
391
392 // Group points by function.
393 auto ByFn(W.object());
394 std::map<std::string, std::vector<const CoveragePoint *>> PointsByFn;
395 for (auto PointPtr : P.second) {
396 for (const DILineInfo &Loc : PointPtr->Locs) {
397 PointsByFn[Loc.FunctionName].push_back(PointPtr);
398 }
399 }
400
401 for (const auto &P : PointsByFn) {
402 std::string FunctionName = P.first;
403 std::set<std::string> WrittenIds;
404
405 ByFn->key(FunctionName);
406
407 // Output <point_id> : "<line>:<col>".
408 auto ById(W.object());
409 for (const CoveragePoint *Point : P.second) {
410 for (const auto &Loc : Point->Locs) {
411 if (Loc.FileName != FileName || Loc.FunctionName != FunctionName)
412 continue;
413 if (WrittenIds.find(Point->Id) != WrittenIds.end())
414 continue;
415
416 WrittenIds.insert(Point->Id);
417 ById->key(Point->Id);
418 W << (utostr(Loc.Line) + ":" + utostr(Loc.Column));
419 }
420 }
421 }
422 }
423 }
424
operator <<(JSONWriter & W,const SymbolizedCoverage & C)425 static void operator<<(JSONWriter &W, const SymbolizedCoverage &C) {
426 auto O(W.object());
427
428 {
429 O->key("covered-points");
430 auto PointsArray(W.array());
431
432 for (const auto &P : C.CoveredIds) {
433 PointsArray->next();
434 W << P;
435 }
436 }
437
438 {
439 if (!C.BinaryHash.empty()) {
440 O->key("binary-hash");
441 W << C.BinaryHash;
442 }
443 }
444
445 {
446 O->key("point-symbol-info");
447 W << C.Points;
448 }
449 }
450
parseScalarString(yaml::Node * N)451 static std::string parseScalarString(yaml::Node *N) {
452 SmallString<64> StringStorage;
453 yaml::ScalarNode *S = dyn_cast<yaml::ScalarNode>(N);
454 failIf(!S, "expected string");
455 return S->getValue(StringStorage);
456 }
457
458 std::unique_ptr<SymbolizedCoverage>
read(const std::string & InputFile)459 SymbolizedCoverage::read(const std::string &InputFile) {
460 auto Coverage(make_unique<SymbolizedCoverage>());
461
462 std::map<std::string, CoveragePoint> Points;
463 ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
464 MemoryBuffer::getFile(InputFile);
465 failIfError(BufOrErr);
466
467 SourceMgr SM;
468 yaml::Stream S(**BufOrErr, SM);
469
470 yaml::document_iterator DI = S.begin();
471 failIf(DI == S.end(), "empty document: " + InputFile);
472 yaml::Node *Root = DI->getRoot();
473 failIf(!Root, "expecting root node: " + InputFile);
474 yaml::MappingNode *Top = dyn_cast<yaml::MappingNode>(Root);
475 failIf(!Top, "expecting mapping node: " + InputFile);
476
477 for (auto &KVNode : *Top) {
478 auto Key = parseScalarString(KVNode.getKey());
479
480 if (Key == "covered-points") {
481 yaml::SequenceNode *Points =
482 dyn_cast<yaml::SequenceNode>(KVNode.getValue());
483 failIf(!Points, "expected array: " + InputFile);
484
485 for (auto I = Points->begin(), E = Points->end(); I != E; ++I) {
486 Coverage->CoveredIds.insert(parseScalarString(&*I));
487 }
488 } else if (Key == "binary-hash") {
489 Coverage->BinaryHash = parseScalarString(KVNode.getValue());
490 } else if (Key == "point-symbol-info") {
491 yaml::MappingNode *PointSymbolInfo =
492 dyn_cast<yaml::MappingNode>(KVNode.getValue());
493 failIf(!PointSymbolInfo, "expected mapping node: " + InputFile);
494
495 for (auto &FileKVNode : *PointSymbolInfo) {
496 auto Filename = parseScalarString(FileKVNode.getKey());
497
498 yaml::MappingNode *FileInfo =
499 dyn_cast<yaml::MappingNode>(FileKVNode.getValue());
500 failIf(!FileInfo, "expected mapping node: " + InputFile);
501
502 for (auto &FunctionKVNode : *FileInfo) {
503 auto FunctionName = parseScalarString(FunctionKVNode.getKey());
504
505 yaml::MappingNode *FunctionInfo =
506 dyn_cast<yaml::MappingNode>(FunctionKVNode.getValue());
507 failIf(!FunctionInfo, "expected mapping node: " + InputFile);
508
509 for (auto &PointKVNode : *FunctionInfo) {
510 auto PointId = parseScalarString(PointKVNode.getKey());
511 auto Loc = parseScalarString(PointKVNode.getValue());
512
513 size_t ColonPos = Loc.find(':');
514 failIf(ColonPos == std::string::npos, "expected ':': " + InputFile);
515
516 auto LineStr = Loc.substr(0, ColonPos);
517 auto ColStr = Loc.substr(ColonPos + 1, Loc.size());
518
519 if (Points.find(PointId) == Points.end())
520 Points.insert(std::make_pair(PointId, CoveragePoint(PointId)));
521
522 DILineInfo LineInfo;
523 LineInfo.FileName = Filename;
524 LineInfo.FunctionName = FunctionName;
525 char *End;
526 LineInfo.Line = std::strtoul(LineStr.c_str(), &End, 10);
527 LineInfo.Column = std::strtoul(ColStr.c_str(), &End, 10);
528
529 CoveragePoint *CoveragePoint = &Points.find(PointId)->second;
530 CoveragePoint->Locs.push_back(LineInfo);
531 }
532 }
533 }
534 } else {
535 errs() << "Ignoring unknown key: " << Key << "\n";
536 }
537 }
538
539 for (auto &KV : Points) {
540 Coverage->Points.push_back(KV.second);
541 }
542
543 return Coverage;
544 }
545
546 // ---------- MAIN FUNCTIONALITY ----------
547
stripPathPrefix(std::string Path)548 std::string stripPathPrefix(std::string Path) {
549 if (ClStripPathPrefix.empty())
550 return Path;
551 size_t Pos = Path.find(ClStripPathPrefix);
552 if (Pos == std::string::npos)
553 return Path;
554 return Path.substr(Pos + ClStripPathPrefix.size());
555 }
556
createSymbolizer()557 static std::unique_ptr<symbolize::LLVMSymbolizer> createSymbolizer() {
558 symbolize::LLVMSymbolizer::Options SymbolizerOptions;
559 SymbolizerOptions.Demangle = ClDemangle;
560 SymbolizerOptions.UseSymbolTable = true;
561 return std::unique_ptr<symbolize::LLVMSymbolizer>(
562 new symbolize::LLVMSymbolizer(SymbolizerOptions));
563 }
564
normalizeFilename(const std::string & FileName)565 static std::string normalizeFilename(const std::string &FileName) {
566 SmallString<256> S(FileName);
567 sys::path::remove_dots(S, /* remove_dot_dot */ true);
568 return stripPathPrefix(S.str().str());
569 }
570
571 class Blacklists {
572 public:
Blacklists()573 Blacklists()
574 : DefaultBlacklist(createDefaultBlacklist()),
575 UserBlacklist(createUserBlacklist()) {}
576
isBlacklisted(const DILineInfo & I)577 bool isBlacklisted(const DILineInfo &I) {
578 if (DefaultBlacklist &&
579 DefaultBlacklist->inSection("sancov", "fun", I.FunctionName))
580 return true;
581 if (DefaultBlacklist &&
582 DefaultBlacklist->inSection("sancov", "src", I.FileName))
583 return true;
584 if (UserBlacklist &&
585 UserBlacklist->inSection("sancov", "fun", I.FunctionName))
586 return true;
587 if (UserBlacklist && UserBlacklist->inSection("sancov", "src", I.FileName))
588 return true;
589 return false;
590 }
591
592 private:
createDefaultBlacklist()593 static std::unique_ptr<SpecialCaseList> createDefaultBlacklist() {
594 if (!ClUseDefaultBlacklist)
595 return std::unique_ptr<SpecialCaseList>();
596 std::unique_ptr<MemoryBuffer> MB =
597 MemoryBuffer::getMemBuffer(DefaultBlacklistStr);
598 std::string Error;
599 auto Blacklist = SpecialCaseList::create(MB.get(), Error);
600 failIfNotEmpty(Error);
601 return Blacklist;
602 }
603
createUserBlacklist()604 static std::unique_ptr<SpecialCaseList> createUserBlacklist() {
605 if (ClBlacklist.empty())
606 return std::unique_ptr<SpecialCaseList>();
607
608 return SpecialCaseList::createOrDie({{ClBlacklist}});
609 }
610 std::unique_ptr<SpecialCaseList> DefaultBlacklist;
611 std::unique_ptr<SpecialCaseList> UserBlacklist;
612 };
613
614 static std::vector<CoveragePoint>
getCoveragePoints(const std::string & ObjectFile,const std::set<uint64_t> & Addrs,const std::set<uint64_t> & CoveredAddrs)615 getCoveragePoints(const std::string &ObjectFile,
616 const std::set<uint64_t> &Addrs,
617 const std::set<uint64_t> &CoveredAddrs) {
618 std::vector<CoveragePoint> Result;
619 auto Symbolizer(createSymbolizer());
620 Blacklists B;
621
622 std::set<std::string> CoveredFiles;
623 if (ClSkipDeadFiles) {
624 for (auto Addr : CoveredAddrs) {
625 auto LineInfo = Symbolizer->symbolizeCode(ObjectFile, Addr);
626 failIfError(LineInfo);
627 CoveredFiles.insert(LineInfo->FileName);
628 auto InliningInfo = Symbolizer->symbolizeInlinedCode(ObjectFile, Addr);
629 failIfError(InliningInfo);
630 for (uint32_t I = 0; I < InliningInfo->getNumberOfFrames(); ++I) {
631 auto FrameInfo = InliningInfo->getFrame(I);
632 CoveredFiles.insert(FrameInfo.FileName);
633 }
634 }
635 }
636
637 for (auto Addr : Addrs) {
638 std::set<DILineInfo> Infos; // deduplicate debug info.
639
640 auto LineInfo = Symbolizer->symbolizeCode(ObjectFile, Addr);
641 failIfError(LineInfo);
642 if (ClSkipDeadFiles &&
643 CoveredFiles.find(LineInfo->FileName) == CoveredFiles.end())
644 continue;
645 LineInfo->FileName = normalizeFilename(LineInfo->FileName);
646 if (B.isBlacklisted(*LineInfo))
647 continue;
648
649 auto Id = utohexstr(Addr, true);
650 auto Point = CoveragePoint(Id);
651 Infos.insert(*LineInfo);
652 Point.Locs.push_back(*LineInfo);
653
654 auto InliningInfo = Symbolizer->symbolizeInlinedCode(ObjectFile, Addr);
655 failIfError(InliningInfo);
656 for (uint32_t I = 0; I < InliningInfo->getNumberOfFrames(); ++I) {
657 auto FrameInfo = InliningInfo->getFrame(I);
658 if (ClSkipDeadFiles &&
659 CoveredFiles.find(FrameInfo.FileName) == CoveredFiles.end())
660 continue;
661 FrameInfo.FileName = normalizeFilename(FrameInfo.FileName);
662 if (B.isBlacklisted(FrameInfo))
663 continue;
664 if (Infos.find(FrameInfo) == Infos.end()) {
665 Infos.insert(FrameInfo);
666 Point.Locs.push_back(FrameInfo);
667 }
668 }
669
670 Result.push_back(Point);
671 }
672
673 return Result;
674 }
675
isCoveragePointSymbol(StringRef Name)676 static bool isCoveragePointSymbol(StringRef Name) {
677 return Name == "__sanitizer_cov" || Name == "__sanitizer_cov_with_check" ||
678 Name == "__sanitizer_cov_trace_func_enter" ||
679 Name == "__sanitizer_cov_trace_pc_guard" ||
680 // Mac has '___' prefix
681 Name == "___sanitizer_cov" || Name == "___sanitizer_cov_with_check" ||
682 Name == "___sanitizer_cov_trace_func_enter" ||
683 Name == "___sanitizer_cov_trace_pc_guard";
684 }
685
686 // Locate __sanitizer_cov* function addresses inside the stubs table on MachO.
findMachOIndirectCovFunctions(const object::MachOObjectFile & O,std::set<uint64_t> * Result)687 static void findMachOIndirectCovFunctions(const object::MachOObjectFile &O,
688 std::set<uint64_t> *Result) {
689 MachO::dysymtab_command Dysymtab = O.getDysymtabLoadCommand();
690 MachO::symtab_command Symtab = O.getSymtabLoadCommand();
691
692 for (const auto &Load : O.load_commands()) {
693 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
694 MachO::segment_command_64 Seg = O.getSegment64LoadCommand(Load);
695 for (unsigned J = 0; J < Seg.nsects; ++J) {
696 MachO::section_64 Sec = O.getSection64(Load, J);
697
698 uint32_t SectionType = Sec.flags & MachO::SECTION_TYPE;
699 if (SectionType == MachO::S_SYMBOL_STUBS) {
700 uint32_t Stride = Sec.reserved2;
701 uint32_t Cnt = Sec.size / Stride;
702 uint32_t N = Sec.reserved1;
703 for (uint32_t J = 0; J < Cnt && N + J < Dysymtab.nindirectsyms; J++) {
704 uint32_t IndirectSymbol =
705 O.getIndirectSymbolTableEntry(Dysymtab, N + J);
706 uint64_t Addr = Sec.addr + J * Stride;
707 if (IndirectSymbol < Symtab.nsyms) {
708 object::SymbolRef Symbol = *(O.getSymbolByIndex(IndirectSymbol));
709 Expected<StringRef> Name = Symbol.getName();
710 failIfError(Name);
711 if (isCoveragePointSymbol(Name.get())) {
712 Result->insert(Addr);
713 }
714 }
715 }
716 }
717 }
718 }
719 if (Load.C.cmd == MachO::LC_SEGMENT) {
720 errs() << "ERROR: 32 bit MachO binaries not supported\n";
721 }
722 }
723 }
724
725 // Locate __sanitizer_cov* function addresses that are used for coverage
726 // reporting.
727 static std::set<uint64_t>
findSanitizerCovFunctions(const object::ObjectFile & O)728 findSanitizerCovFunctions(const object::ObjectFile &O) {
729 std::set<uint64_t> Result;
730
731 for (const object::SymbolRef &Symbol : O.symbols()) {
732 Expected<uint64_t> AddressOrErr = Symbol.getAddress();
733 failIfError(AddressOrErr);
734 uint64_t Address = AddressOrErr.get();
735
736 Expected<StringRef> NameOrErr = Symbol.getName();
737 failIfError(NameOrErr);
738 StringRef Name = NameOrErr.get();
739
740 if (!(Symbol.getFlags() & object::BasicSymbolRef::SF_Undefined) &&
741 isCoveragePointSymbol(Name)) {
742 Result.insert(Address);
743 }
744 }
745
746 if (const auto *CO = dyn_cast<object::COFFObjectFile>(&O)) {
747 for (const object::ExportDirectoryEntryRef &Export :
748 CO->export_directories()) {
749 uint32_t RVA;
750 std::error_code EC = Export.getExportRVA(RVA);
751 failIfError(EC);
752
753 StringRef Name;
754 EC = Export.getSymbolName(Name);
755 failIfError(EC);
756
757 if (isCoveragePointSymbol(Name))
758 Result.insert(CO->getImageBase() + RVA);
759 }
760 }
761
762 if (const auto *MO = dyn_cast<object::MachOObjectFile>(&O)) {
763 findMachOIndirectCovFunctions(*MO, &Result);
764 }
765
766 return Result;
767 }
768
769 // Locate addresses of all coverage points in a file. Coverage point
770 // is defined as the 'address of instruction following __sanitizer_cov
771 // call - 1'.
getObjectCoveragePoints(const object::ObjectFile & O,std::set<uint64_t> * Addrs)772 static void getObjectCoveragePoints(const object::ObjectFile &O,
773 std::set<uint64_t> *Addrs) {
774 Triple TheTriple("unknown-unknown-unknown");
775 TheTriple.setArch(Triple::ArchType(O.getArch()));
776 auto TripleName = TheTriple.getTriple();
777
778 std::string Error;
779 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
780 failIfNotEmpty(Error);
781
782 std::unique_ptr<const MCSubtargetInfo> STI(
783 TheTarget->createMCSubtargetInfo(TripleName, "", ""));
784 failIfEmpty(STI, "no subtarget info for target " + TripleName);
785
786 std::unique_ptr<const MCRegisterInfo> MRI(
787 TheTarget->createMCRegInfo(TripleName));
788 failIfEmpty(MRI, "no register info for target " + TripleName);
789
790 std::unique_ptr<const MCAsmInfo> AsmInfo(
791 TheTarget->createMCAsmInfo(*MRI, TripleName));
792 failIfEmpty(AsmInfo, "no asm info for target " + TripleName);
793
794 std::unique_ptr<const MCObjectFileInfo> MOFI(new MCObjectFileInfo);
795 MCContext Ctx(AsmInfo.get(), MRI.get(), MOFI.get());
796 std::unique_ptr<MCDisassembler> DisAsm(
797 TheTarget->createMCDisassembler(*STI, Ctx));
798 failIfEmpty(DisAsm, "no disassembler info for target " + TripleName);
799
800 std::unique_ptr<const MCInstrInfo> MII(TheTarget->createMCInstrInfo());
801 failIfEmpty(MII, "no instruction info for target " + TripleName);
802
803 std::unique_ptr<const MCInstrAnalysis> MIA(
804 TheTarget->createMCInstrAnalysis(MII.get()));
805 failIfEmpty(MIA, "no instruction analysis info for target " + TripleName);
806
807 auto SanCovAddrs = findSanitizerCovFunctions(O);
808 if (SanCovAddrs.empty())
809 fail("__sanitizer_cov* functions not found");
810
811 for (object::SectionRef Section : O.sections()) {
812 if (Section.isVirtual() || !Section.isText()) // llvm-objdump does the same.
813 continue;
814 uint64_t SectionAddr = Section.getAddress();
815 uint64_t SectSize = Section.getSize();
816 if (!SectSize)
817 continue;
818
819 StringRef BytesStr;
820 failIfError(Section.getContents(BytesStr));
821 ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(BytesStr.data()),
822 BytesStr.size());
823
824 for (uint64_t Index = 0, Size = 0; Index < Section.getSize();
825 Index += Size) {
826 MCInst Inst;
827 if (!DisAsm->getInstruction(Inst, Size, Bytes.slice(Index),
828 SectionAddr + Index, nulls(), nulls())) {
829 if (Size == 0)
830 Size = 1;
831 continue;
832 }
833 uint64_t Addr = Index + SectionAddr;
834 // Sanitizer coverage uses the address of the next instruction - 1.
835 uint64_t CovPoint = Addr + Size - 1;
836 uint64_t Target;
837 if (MIA->isCall(Inst) &&
838 MIA->evaluateBranch(Inst, SectionAddr + Index, Size, Target) &&
839 SanCovAddrs.find(Target) != SanCovAddrs.end())
840 Addrs->insert(CovPoint);
841 }
842 }
843 }
844
845 static void
visitObjectFiles(const object::Archive & A,function_ref<void (const object::ObjectFile &)> Fn)846 visitObjectFiles(const object::Archive &A,
847 function_ref<void(const object::ObjectFile &)> Fn) {
848 Error Err = Error::success();
849 for (auto &C : A.children(Err)) {
850 Expected<std::unique_ptr<object::Binary>> ChildOrErr = C.getAsBinary();
851 failIfError(ChildOrErr);
852 if (auto *O = dyn_cast<object::ObjectFile>(&*ChildOrErr.get()))
853 Fn(*O);
854 else
855 failIfError(object::object_error::invalid_file_type);
856 }
857 failIfError(std::move(Err));
858 }
859
860 static void
visitObjectFiles(const std::string & FileName,function_ref<void (const object::ObjectFile &)> Fn)861 visitObjectFiles(const std::string &FileName,
862 function_ref<void(const object::ObjectFile &)> Fn) {
863 Expected<object::OwningBinary<object::Binary>> BinaryOrErr =
864 object::createBinary(FileName);
865 if (!BinaryOrErr)
866 failIfError(BinaryOrErr);
867
868 object::Binary &Binary = *BinaryOrErr.get().getBinary();
869 if (object::Archive *A = dyn_cast<object::Archive>(&Binary))
870 visitObjectFiles(*A, Fn);
871 else if (object::ObjectFile *O = dyn_cast<object::ObjectFile>(&Binary))
872 Fn(*O);
873 else
874 failIfError(object::object_error::invalid_file_type);
875 }
876
877 static std::set<uint64_t>
findSanitizerCovFunctions(const std::string & FileName)878 findSanitizerCovFunctions(const std::string &FileName) {
879 std::set<uint64_t> Result;
880 visitObjectFiles(FileName, [&](const object::ObjectFile &O) {
881 auto Addrs = findSanitizerCovFunctions(O);
882 Result.insert(Addrs.begin(), Addrs.end());
883 });
884 return Result;
885 }
886
887 // Locate addresses of all coverage points in a file. Coverage point
888 // is defined as the 'address of instruction following __sanitizer_cov
889 // call - 1'.
findCoveragePointAddrs(const std::string & FileName)890 static std::set<uint64_t> findCoveragePointAddrs(const std::string &FileName) {
891 std::set<uint64_t> Result;
892 visitObjectFiles(FileName, [&](const object::ObjectFile &O) {
893 getObjectCoveragePoints(O, &Result);
894 });
895 return Result;
896 }
897
printCovPoints(const std::string & ObjFile,raw_ostream & OS)898 static void printCovPoints(const std::string &ObjFile, raw_ostream &OS) {
899 for (uint64_t Addr : findCoveragePointAddrs(ObjFile)) {
900 OS << "0x";
901 OS.write_hex(Addr);
902 OS << "\n";
903 }
904 }
905
isCoverageFile(const std::string & FileName)906 static ErrorOr<bool> isCoverageFile(const std::string &FileName) {
907 auto ShortFileName = llvm::sys::path::filename(FileName);
908 if (!SancovFileRegex.match(ShortFileName))
909 return false;
910
911 ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
912 MemoryBuffer::getFile(FileName);
913 if (!BufOrErr) {
914 errs() << "Warning: " << BufOrErr.getError().message() << "("
915 << BufOrErr.getError().value()
916 << "), filename: " << llvm::sys::path::filename(FileName) << "\n";
917 return BufOrErr.getError();
918 }
919 std::unique_ptr<MemoryBuffer> Buf = std::move(BufOrErr.get());
920 if (Buf->getBufferSize() < 8) {
921 return false;
922 }
923 const FileHeader *Header =
924 reinterpret_cast<const FileHeader *>(Buf->getBufferStart());
925 return Header->Magic == BinCoverageMagic;
926 }
927
isSymbolizedCoverageFile(const std::string & FileName)928 static bool isSymbolizedCoverageFile(const std::string &FileName) {
929 auto ShortFileName = llvm::sys::path::filename(FileName);
930 return SymcovFileRegex.match(ShortFileName);
931 }
932
933 static std::unique_ptr<SymbolizedCoverage>
symbolize(const RawCoverage & Data,const std::string ObjectFile)934 symbolize(const RawCoverage &Data, const std::string ObjectFile) {
935 auto Coverage = make_unique<SymbolizedCoverage>();
936
937 ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
938 MemoryBuffer::getFile(ObjectFile);
939 failIfError(BufOrErr);
940 SHA1 Hasher;
941 Hasher.update((*BufOrErr)->getBuffer());
942 Coverage->BinaryHash = toHex(Hasher.final());
943
944 Blacklists B;
945 auto Symbolizer(createSymbolizer());
946
947 for (uint64_t Addr : *Data.Addrs) {
948 auto LineInfo = Symbolizer->symbolizeCode(ObjectFile, Addr);
949 failIfError(LineInfo);
950 if (B.isBlacklisted(*LineInfo))
951 continue;
952
953 Coverage->CoveredIds.insert(utohexstr(Addr, true));
954 }
955
956 std::set<uint64_t> AllAddrs = findCoveragePointAddrs(ObjectFile);
957 if (!std::includes(AllAddrs.begin(), AllAddrs.end(), Data.Addrs->begin(),
958 Data.Addrs->end())) {
959 fail("Coverage points in binary and .sancov file do not match.");
960 }
961 Coverage->Points = getCoveragePoints(ObjectFile, AllAddrs, *Data.Addrs);
962 return Coverage;
963 }
964
965 struct FileFn {
operator <__anonb1aff5e70111::FileFn966 bool operator<(const FileFn &RHS) const {
967 return std::tie(FileName, FunctionName) <
968 std::tie(RHS.FileName, RHS.FunctionName);
969 }
970
971 std::string FileName;
972 std::string FunctionName;
973 };
974
975 static std::set<FileFn>
computeFunctions(const std::vector<CoveragePoint> & Points)976 computeFunctions(const std::vector<CoveragePoint> &Points) {
977 std::set<FileFn> Fns;
978 for (const auto &Point : Points) {
979 for (const auto &Loc : Point.Locs) {
980 Fns.insert(FileFn{Loc.FileName, Loc.FunctionName});
981 }
982 }
983 return Fns;
984 }
985
986 static std::set<FileFn>
computeNotCoveredFunctions(const SymbolizedCoverage & Coverage)987 computeNotCoveredFunctions(const SymbolizedCoverage &Coverage) {
988 auto Fns = computeFunctions(Coverage.Points);
989
990 for (const auto &Point : Coverage.Points) {
991 if (Coverage.CoveredIds.find(Point.Id) == Coverage.CoveredIds.end())
992 continue;
993
994 for (const auto &Loc : Point.Locs) {
995 Fns.erase(FileFn{Loc.FileName, Loc.FunctionName});
996 }
997 }
998
999 return Fns;
1000 }
1001
1002 static std::set<FileFn>
computeCoveredFunctions(const SymbolizedCoverage & Coverage)1003 computeCoveredFunctions(const SymbolizedCoverage &Coverage) {
1004 auto AllFns = computeFunctions(Coverage.Points);
1005 std::set<FileFn> Result;
1006
1007 for (const auto &Point : Coverage.Points) {
1008 if (Coverage.CoveredIds.find(Point.Id) == Coverage.CoveredIds.end())
1009 continue;
1010
1011 for (const auto &Loc : Point.Locs) {
1012 Result.insert(FileFn{Loc.FileName, Loc.FunctionName});
1013 }
1014 }
1015
1016 return Result;
1017 }
1018
1019 typedef std::map<FileFn, std::pair<uint32_t, uint32_t>> FunctionLocs;
1020 // finds first location in a file for each function.
resolveFunctions(const SymbolizedCoverage & Coverage,const std::set<FileFn> & Fns)1021 static FunctionLocs resolveFunctions(const SymbolizedCoverage &Coverage,
1022 const std::set<FileFn> &Fns) {
1023 FunctionLocs Result;
1024 for (const auto &Point : Coverage.Points) {
1025 for (const auto &Loc : Point.Locs) {
1026 FileFn Fn = FileFn{Loc.FileName, Loc.FunctionName};
1027 if (Fns.find(Fn) == Fns.end())
1028 continue;
1029
1030 auto P = std::make_pair(Loc.Line, Loc.Column);
1031 auto I = Result.find(Fn);
1032 if (I == Result.end() || I->second > P) {
1033 Result[Fn] = P;
1034 }
1035 }
1036 }
1037 return Result;
1038 }
1039
printFunctionLocs(const FunctionLocs & FnLocs,raw_ostream & OS)1040 static void printFunctionLocs(const FunctionLocs &FnLocs, raw_ostream &OS) {
1041 for (const auto &P : FnLocs) {
1042 OS << stripPathPrefix(P.first.FileName) << ":" << P.second.first << " "
1043 << P.first.FunctionName << "\n";
1044 }
1045 }
computeStats(const SymbolizedCoverage & Coverage)1046 CoverageStats computeStats(const SymbolizedCoverage &Coverage) {
1047 CoverageStats Stats = {Coverage.Points.size(), Coverage.CoveredIds.size(),
1048 computeFunctions(Coverage.Points).size(),
1049 computeCoveredFunctions(Coverage).size()};
1050 return Stats;
1051 }
1052
1053 // Print list of covered functions.
1054 // Line format: <file_name>:<line> <function_name>
printCoveredFunctions(const SymbolizedCoverage & CovData,raw_ostream & OS)1055 static void printCoveredFunctions(const SymbolizedCoverage &CovData,
1056 raw_ostream &OS) {
1057 auto CoveredFns = computeCoveredFunctions(CovData);
1058 printFunctionLocs(resolveFunctions(CovData, CoveredFns), OS);
1059 }
1060
1061 // Print list of not covered functions.
1062 // Line format: <file_name>:<line> <function_name>
printNotCoveredFunctions(const SymbolizedCoverage & CovData,raw_ostream & OS)1063 static void printNotCoveredFunctions(const SymbolizedCoverage &CovData,
1064 raw_ostream &OS) {
1065 auto NotCoveredFns = computeNotCoveredFunctions(CovData);
1066 printFunctionLocs(resolveFunctions(CovData, NotCoveredFns), OS);
1067 }
1068
1069 // Read list of files and merges their coverage info.
readAndPrintRawCoverage(const std::vector<std::string> & FileNames,raw_ostream & OS)1070 static void readAndPrintRawCoverage(const std::vector<std::string> &FileNames,
1071 raw_ostream &OS) {
1072 std::vector<std::unique_ptr<RawCoverage>> Covs;
1073 for (const auto &FileName : FileNames) {
1074 auto Cov = RawCoverage::read(FileName);
1075 if (!Cov)
1076 continue;
1077 OS << *Cov.get();
1078 }
1079 }
1080
1081 static std::unique_ptr<SymbolizedCoverage>
merge(const std::vector<std::unique_ptr<SymbolizedCoverage>> & Coverages)1082 merge(const std::vector<std::unique_ptr<SymbolizedCoverage>> &Coverages) {
1083 if (Coverages.empty())
1084 return nullptr;
1085
1086 auto Result = make_unique<SymbolizedCoverage>();
1087
1088 for (size_t I = 0; I < Coverages.size(); ++I) {
1089 const SymbolizedCoverage &Coverage = *Coverages[I];
1090 std::string Prefix;
1091 if (Coverages.size() > 1) {
1092 // prefix is not needed when there's only one file.
1093 Prefix = utostr(I);
1094 }
1095
1096 for (const auto &Id : Coverage.CoveredIds) {
1097 Result->CoveredIds.insert(Prefix + Id);
1098 }
1099
1100 for (const auto &CovPoint : Coverage.Points) {
1101 CoveragePoint NewPoint(CovPoint);
1102 NewPoint.Id = Prefix + CovPoint.Id;
1103 Result->Points.push_back(NewPoint);
1104 }
1105 }
1106
1107 if (Coverages.size() == 1) {
1108 Result->BinaryHash = Coverages[0]->BinaryHash;
1109 }
1110
1111 return Result;
1112 }
1113
1114 static std::unique_ptr<SymbolizedCoverage>
readSymbolizeAndMergeCmdArguments(std::vector<std::string> FileNames)1115 readSymbolizeAndMergeCmdArguments(std::vector<std::string> FileNames) {
1116 std::vector<std::unique_ptr<SymbolizedCoverage>> Coverages;
1117
1118 {
1119 // Short name => file name.
1120 std::map<std::string, std::string> ObjFiles;
1121 std::string FirstObjFile;
1122 std::set<std::string> CovFiles;
1123
1124 // Partition input values into coverage/object files.
1125 for (const auto &FileName : FileNames) {
1126 if (isSymbolizedCoverageFile(FileName)) {
1127 Coverages.push_back(SymbolizedCoverage::read(FileName));
1128 }
1129
1130 auto ErrorOrIsCoverage = isCoverageFile(FileName);
1131 if (!ErrorOrIsCoverage)
1132 continue;
1133 if (ErrorOrIsCoverage.get()) {
1134 CovFiles.insert(FileName);
1135 } else {
1136 auto ShortFileName = llvm::sys::path::filename(FileName);
1137 if (ObjFiles.find(ShortFileName) != ObjFiles.end()) {
1138 fail("Duplicate binary file with a short name: " + ShortFileName);
1139 }
1140
1141 ObjFiles[ShortFileName] = FileName;
1142 if (FirstObjFile.empty())
1143 FirstObjFile = FileName;
1144 }
1145 }
1146
1147 SmallVector<StringRef, 2> Components;
1148
1149 // Object file => list of corresponding coverage file names.
1150 std::map<std::string, std::vector<std::string>> CoverageByObjFile;
1151 for (const auto &FileName : CovFiles) {
1152 auto ShortFileName = llvm::sys::path::filename(FileName);
1153 auto Ok = SancovFileRegex.match(ShortFileName, &Components);
1154 if (!Ok) {
1155 fail("Can't match coverage file name against "
1156 "<module_name>.<pid>.sancov pattern: " +
1157 FileName);
1158 }
1159
1160 auto Iter = ObjFiles.find(Components[1]);
1161 if (Iter == ObjFiles.end()) {
1162 fail("Object file for coverage not found: " + FileName);
1163 }
1164
1165 CoverageByObjFile[Iter->second].push_back(FileName);
1166 };
1167
1168 for (const auto &Pair : ObjFiles) {
1169 auto FileName = Pair.second;
1170 if (CoverageByObjFile.find(FileName) == CoverageByObjFile.end())
1171 errs() << "WARNING: No coverage file for " << FileName << "\n";
1172 }
1173
1174 // Read raw coverage and symbolize it.
1175 for (const auto &Pair : CoverageByObjFile) {
1176 if (findSanitizerCovFunctions(Pair.first).empty()) {
1177 errs()
1178 << "WARNING: Ignoring " << Pair.first
1179 << " and its coverage because __sanitizer_cov* functions were not "
1180 "found.\n";
1181 continue;
1182 }
1183
1184 for (const std::string &CoverageFile : Pair.second) {
1185 auto DataOrError = RawCoverage::read(CoverageFile);
1186 failIfError(DataOrError);
1187 Coverages.push_back(symbolize(*DataOrError.get(), Pair.first));
1188 }
1189 }
1190 }
1191
1192 return merge(Coverages);
1193 }
1194
1195 } // namespace
1196
main(int Argc,char ** Argv)1197 int main(int Argc, char **Argv) {
1198 // Print stack trace if we signal out.
1199 sys::PrintStackTraceOnErrorSignal(Argv[0]);
1200 PrettyStackTraceProgram X(Argc, Argv);
1201 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
1202
1203 llvm::InitializeAllTargetInfos();
1204 llvm::InitializeAllTargetMCs();
1205 llvm::InitializeAllDisassemblers();
1206
1207 cl::ParseCommandLineOptions(Argc, Argv,
1208 "Sanitizer Coverage Processing Tool (sancov)\n\n"
1209 " This tool can extract various coverage-related information from: \n"
1210 " coverage-instrumented binary files, raw .sancov files and their "
1211 "symbolized .symcov version.\n"
1212 " Depending on chosen action the tool expects different input files:\n"
1213 " -print-coverage-pcs - coverage-instrumented binary files\n"
1214 " -print-coverage - .sancov files\n"
1215 " <other actions> - .sancov files & corresponding binary "
1216 "files, .symcov files\n"
1217 );
1218
1219 // -print doesn't need object files.
1220 if (Action == PrintAction) {
1221 readAndPrintRawCoverage(ClInputFiles, outs());
1222 return 0;
1223 } else if (Action == PrintCovPointsAction) {
1224 // -print-coverage-points doesn't need coverage files.
1225 for (const std::string &ObjFile : ClInputFiles) {
1226 printCovPoints(ObjFile, outs());
1227 }
1228 return 0;
1229 }
1230
1231 auto Coverage = readSymbolizeAndMergeCmdArguments(ClInputFiles);
1232 failIf(!Coverage, "No valid coverage files given.");
1233
1234 switch (Action) {
1235 case CoveredFunctionsAction: {
1236 printCoveredFunctions(*Coverage, outs());
1237 return 0;
1238 }
1239 case NotCoveredFunctionsAction: {
1240 printNotCoveredFunctions(*Coverage, outs());
1241 return 0;
1242 }
1243 case StatsAction: {
1244 outs() << computeStats(*Coverage);
1245 return 0;
1246 }
1247 case MergeAction:
1248 case SymbolizeAction: { // merge & symbolize are synonims.
1249 JSONWriter W(outs());
1250 W << *Coverage;
1251 return 0;
1252 }
1253 case HtmlReportAction:
1254 errs() << "-html-report option is removed: "
1255 "use -symbolize & coverage-report-server.py instead\n";
1256 return 1;
1257 case PrintAction:
1258 case PrintCovPointsAction:
1259 llvm_unreachable("unsupported action");
1260 }
1261 }
1262