1//===-- gsymutil.cpp - GSYM dumping and creation utility for llvm ---------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "llvm/ADT/STLExtras.h"
10#include "llvm/DebugInfo/DWARF/DWARFContext.h"
11#include "llvm/Object/ELFObjectFile.h"
12#include "llvm/Object/MachOUniversal.h"
13#include "llvm/Object/ObjectFile.h"
14#include "llvm/Option/ArgList.h"
15#include "llvm/Option/Option.h"
16#include "llvm/Support/CommandLine.h"
17#include "llvm/Support/Debug.h"
18#include "llvm/Support/Driver.h"
19#include "llvm/Support/Error.h"
20#include "llvm/Support/FileSystem.h"
21#include "llvm/Support/JSON.h"
22#include "llvm/Support/ManagedStatic.h"
23#include "llvm/Support/MemoryBuffer.h"
24#include "llvm/Support/PrettyStackTrace.h"
25#include "llvm/Support/Regex.h"
26#include "llvm/Support/Signals.h"
27#include "llvm/Support/TargetSelect.h"
28#include "llvm/Support/raw_ostream.h"
29#include "llvm/TargetParser/Triple.h"
30#include <cstring>
31#include <inttypes.h>
32#include <iostream>
33#include <optional>
34#include <string>
35#include <system_error>
36#include <vector>
37
38#include "llvm/DebugInfo/GSYM/CallSiteInfo.h"
39#include "llvm/DebugInfo/GSYM/DwarfTransformer.h"
40#include "llvm/DebugInfo/GSYM/FunctionInfo.h"
41#include "llvm/DebugInfo/GSYM/GsymCreator.h"
42#include "llvm/DebugInfo/GSYM/GsymCreatorV1.h"
43#include "llvm/DebugInfo/GSYM/GsymCreatorV2.h"
44#include "llvm/DebugInfo/GSYM/GsymReader.h"
45#include "llvm/DebugInfo/GSYM/Header.h"
46#include "llvm/DebugInfo/GSYM/HeaderV2.h"
47#include "llvm/DebugInfo/GSYM/InlineInfo.h"
48#include "llvm/DebugInfo/GSYM/LookupResult.h"
49#include "llvm/DebugInfo/GSYM/ObjectFileTransformer.h"
50#include "llvm/DebugInfo/GSYM/OutputAggregator.h"
51
52using namespace llvm;
53using namespace gsym;
54using namespace object;
55
56/// @}
57/// Command line options.
58/// @{
59
60using namespace llvm::opt;
61enum ID {
62 OPT_INVALID = 0, // This is not an option ID.
63#define OPTION(...) LLVM_MAKE_OPT_ID(__VA_ARGS__),
64#include "Opts.inc"
65#undef OPTION
66};
67
68#define OPTTABLE_CODE
69#include "Opts.inc"
70
71class GSYMUtilOptTable : public llvm::opt::OptTable {
72public:
73 GSYMUtilOptTable() : OptTable(optionTables()) {
74 setGroupedShortOptions(true);
75 }
76};
77
78static bool Verbose;
79static std::vector<std::string> InputFilenames;
80static std::string ConvertFilename;
81static std::string SymtabFilename;
82static std::vector<std::string> ArchFilters;
83static std::string OutputFilename;
84static std::string JsonSummaryFile;
85static bool Verify;
86static bool BenchmarkReader;
87static uint32_t BenchmarkStart;
88static uint32_t BenchmarkStride;
89static unsigned NumThreads;
90static uint64_t SegmentSize;
91static bool Quiet;
92static std::vector<uint64_t> LookupAddresses;
93static bool LookupAddressesFromStdin;
94static bool UseMergedFunctions = false;
95static bool LoadDwarfCallSites = false;
96static std::string CallSiteYamlPath;
97static std::vector<std::string> MergedFunctionsFilters;
98// Default output version. Can be overridden by --output-version.
99static uint32_t OutputVersion = Header::getVersion();
100static bool ShowStatistics;
101static GsymReader::StatisticsFormat StatisticsFormat;
102
103static void parseArgs(int argc, char **argv) {
104 GSYMUtilOptTable Tbl;
105 llvm::StringRef ToolName = argv[0];
106 llvm::BumpPtrAllocator A;
107 llvm::StringSaver Saver{A};
108 llvm::opt::InputArgList Args =
109 Tbl.parseArgs(Argc: argc, Argv: argv, Unknown: OPT_UNKNOWN, Saver, ErrorFn: [&](StringRef Msg) {
110 llvm::errs() << Msg << '\n';
111 std::exit(status: 1);
112 });
113 if (Args.hasArg(Ids: OPT_help)) {
114 const char *Overview =
115 "A tool for dumping, searching and creating GSYM files.\n\n"
116 "Specify one or more GSYM paths as arguments to dump all of the "
117 "information in each GSYM file.\n"
118 "Specify a single GSYM file along with one or more --lookup options to "
119 "lookup addresses within that GSYM file.\n"
120 "Use the --convert option to specify a file with option --out-file "
121 "option to convert to GSYM format.\n";
122
123 Tbl.printHelp(OS&: llvm::outs(), Usage: "llvm-gsymutil [options] <input GSYM files>",
124 Title: Overview);
125 std::exit(status: 0);
126 }
127 if (Args.hasArg(Ids: OPT_version)) {
128 llvm::outs() << ToolName << '\n';
129 cl::PrintVersionMessage();
130 std::exit(status: 0);
131 }
132
133 Verbose = Args.hasArg(Ids: OPT_verbose);
134
135 for (const llvm::opt::Arg *A : Args.filtered(Ids: OPT_INPUT))
136 InputFilenames.emplace_back(args: A->getValue());
137
138 if (const llvm::opt::Arg *A = Args.getLastArg(Ids: OPT_convert_EQ))
139 ConvertFilename = A->getValue();
140
141 if (const llvm::opt::Arg *A = Args.getLastArg(Ids: OPT_symtab_file_EQ))
142 SymtabFilename = A->getValue();
143
144 for (const llvm::opt::Arg *A : Args.filtered(Ids: OPT_arch_EQ))
145 ArchFilters.emplace_back(args: A->getValue());
146
147 if (const llvm::opt::Arg *A = Args.getLastArg(Ids: OPT_out_file_EQ))
148 OutputFilename = A->getValue();
149
150 if (const llvm::opt::Arg *A = Args.getLastArg(Ids: OPT_json_summary_file_EQ))
151 JsonSummaryFile = A->getValue();
152
153 Verify = Args.hasArg(Ids: OPT_verify);
154 BenchmarkStart = 0;
155 BenchmarkStride = 1;
156 if (Args.hasArg(Ids: OPT_benchmark_reader_all)) {
157 BenchmarkReader = true;
158 } else if (const llvm::opt::Arg *A = Args.getLastArg(Ids: OPT_benchmark_reader)) {
159 BenchmarkReader = true;
160 StringRef S{A->getValue()};
161 if (!S.empty()) {
162 auto [StartStr, StrideStr] = S.split(Separator: ',');
163 if (!llvm::to_integer(S: StartStr, Num&: BenchmarkStart, Base: 0)) {
164 llvm::errs() << ToolName
165 << ": for the --benchmark-reader option: invalid start '"
166 << StartStr << "'\n";
167 std::exit(status: 1);
168 }
169 if (!StrideStr.empty() &&
170 !llvm::to_integer(S: StrideStr, Num&: BenchmarkStride, Base: 0)) {
171 llvm::errs() << ToolName
172 << ": for the --benchmark-reader option: invalid stride '"
173 << StrideStr << "'\n";
174 std::exit(status: 1);
175 }
176 if (BenchmarkStride == 0) {
177 llvm::errs() << ToolName
178 << ": for the --benchmark-reader option: stride must be "
179 "positive\n";
180 std::exit(status: 1);
181 }
182 }
183 }
184
185 if (const llvm::opt::Arg *A = Args.getLastArg(Ids: OPT_num_threads_EQ)) {
186 StringRef S{A->getValue()};
187 if (!llvm::to_integer(S, Num&: NumThreads, Base: 0)) {
188 llvm::errs() << ToolName << ": for the --num-threads option: '" << S
189 << "' value invalid for uint argument!\n";
190 std::exit(status: 1);
191 }
192 }
193
194 if (const llvm::opt::Arg *A = Args.getLastArg(Ids: OPT_segment_size_EQ)) {
195 StringRef S{A->getValue()};
196 if (!llvm::to_integer(S, Num&: SegmentSize, Base: 0)) {
197 llvm::errs() << ToolName << ": for the --segment-size option: '" << S
198 << "' value invalid for uint argument!\n";
199 std::exit(status: 1);
200 }
201 }
202
203 Quiet = Args.hasArg(Ids: OPT_quiet);
204
205 for (const llvm::opt::Arg *A : Args.filtered(Ids: OPT_address_EQ)) {
206 StringRef S{A->getValue()};
207 if (!llvm::to_integer(S, Num&: LookupAddresses.emplace_back(), Base: 0)) {
208 llvm::errs() << ToolName << ": for the --address option: '" << S
209 << "' value invalid for uint argument!\n";
210 std::exit(status: 1);
211 }
212 }
213
214 LookupAddressesFromStdin = Args.hasArg(Ids: OPT_addresses_from_stdin);
215 UseMergedFunctions = Args.hasArg(Ids: OPT_merged_functions);
216
217 if (Args.hasArg(Ids: OPT_callsites_yaml_file_EQ)) {
218 CallSiteYamlPath = Args.getLastArgValue(Id: OPT_callsites_yaml_file_EQ);
219 if (CallSiteYamlPath.empty()) {
220 llvm::errs()
221 << ToolName
222 << ": --callsites-yaml-file option requires a non-empty argument.\n";
223 std::exit(status: 1);
224 }
225 }
226
227 LoadDwarfCallSites = Args.hasArg(Ids: OPT_dwarf_callsites);
228
229 ShowStatistics = Args.hasArg(Ids: OPT_statistics_EQ);
230 if (const llvm::opt::Arg *A = Args.getLastArg(Ids: OPT_statistics_EQ)) {
231 StringRef Val = A->getValue();
232 if (Val == "" || Val == "text")
233 StatisticsFormat = GsymReader::StatisticsFormat::Text;
234 else if (Val == "json")
235 StatisticsFormat = GsymReader::StatisticsFormat::JSON;
236 else if (Val == "pretty-json")
237 StatisticsFormat = GsymReader::StatisticsFormat::PrettyJSON;
238 else {
239 errs() << "error: unknown statistics format '" << Val
240 << "'. Supported formats: text, json, pretty-json\n";
241 std::exit(status: 1);
242 }
243 }
244
245 for (const llvm::opt::Arg *A :
246 Args.filtered(Ids: OPT_merged_functions_filter_EQ)) {
247 MergedFunctionsFilters.push_back(x: A->getValue());
248 // Validate the filter is only used with correct flags
249 if (LookupAddresses.empty() && !LookupAddressesFromStdin) {
250 llvm::errs() << ToolName
251 << ": --merged-functions-filter can only be used with "
252 "--address/--addresses-from-stdin\n";
253 std::exit(status: 1);
254 }
255 if (!UseMergedFunctions) {
256 llvm::errs()
257 << ToolName
258 << ": --merged-functions-filter requires --merged-functions\n";
259 std::exit(status: 1);
260 }
261 }
262
263 if (const llvm::opt::Arg *A = Args.getLastArg(Ids: OPT_output_version_EQ)) {
264 StringRef Val = A->getValue();
265 uint32_t Version;
266 if (Val.getAsInteger(Radix: 10, Result&: Version) || (Version != Header::getVersion() &&
267 Version != HeaderV2::getVersion())) {
268 llvm::errs() << ToolName << ": for the --output-version option: '" << Val
269 << "' is invalid. Use '1' or '2'.\n";
270 std::exit(status: 1);
271 }
272 OutputVersion = Version;
273 }
274}
275
276/// @}
277//===----------------------------------------------------------------------===//
278
279static void error(Error Err) {
280 if (!Err)
281 return;
282 WithColor::error() << toString(E: std::move(Err)) << "\n";
283 exit(status: 1);
284}
285
286static void error(StringRef Prefix, llvm::Error Err) {
287 if (!Err)
288 return;
289 errs() << Prefix << ": " << Err << "\n";
290 consumeError(Err: std::move(Err));
291 exit(status: 1);
292}
293
294static void error(StringRef Prefix, std::error_code EC) {
295 if (!EC)
296 return;
297 errs() << Prefix << ": " << EC.message() << "\n";
298 exit(status: 1);
299}
300
301static uint32_t getCPUType(MachOObjectFile &MachO) {
302 if (MachO.is64Bit())
303 return MachO.getHeader64().cputype;
304 else
305 return MachO.getHeader().cputype;
306}
307
308static std::string getArchitectureName(const ObjectFile &Obj) {
309 if (const auto *MachO = dyn_cast<object::MachOObjectFile>(Val: &Obj)) {
310 Triple ObjTriple(MachO->getArchTriple());
311 return ObjTriple.getArchName().str();
312 }
313
314 Triple ObjTriple(Obj.makeTriple());
315 return ObjTriple.getArchName().str();
316}
317
318/// Return true if the object file has not been filtered by an --arch option.
319static bool filterArch(MachOObjectFile &Obj) {
320 if (ArchFilters.empty())
321 return true;
322
323 Triple ObjTriple(Obj.getArchTriple());
324 StringRef ObjArch = ObjTriple.getArchName();
325
326 for (StringRef Arch : ArchFilters) {
327 // Match name.
328 if (Arch == ObjArch)
329 return true;
330
331 // Match architecture number.
332 unsigned Value;
333 if (!Arch.getAsInteger(Radix: 0, Result&: Value))
334 if (Value == getCPUType(MachO&: Obj))
335 return true;
336 }
337 return false;
338}
339
340/// Determine the virtual address that is considered the base address of an ELF
341/// object file.
342///
343/// The base address of an ELF file is the "p_vaddr" of the first program
344/// header whose "p_type" is PT_LOAD.
345///
346/// \param ELFFile An ELF object file we will search.
347///
348/// \returns A valid image base address if we are able to extract one.
349template <class ELFT>
350static std::optional<uint64_t>
351getImageBaseAddress(const object::ELFFile<ELFT> &ELFFile) {
352 auto PhdrRangeOrErr = ELFFile.program_headers();
353 if (!PhdrRangeOrErr) {
354 consumeError(PhdrRangeOrErr.takeError());
355 return std::nullopt;
356 }
357 for (const typename ELFT::Phdr &Phdr : *PhdrRangeOrErr)
358 if (Phdr.p_type == ELF::PT_LOAD)
359 return (uint64_t)Phdr.p_vaddr;
360 return std::nullopt;
361}
362
363/// Determine the virtual address that is considered the base address of mach-o
364/// object file.
365///
366/// The base address of a mach-o file is the vmaddr of the "__TEXT" segment.
367///
368/// \param MachO A mach-o object file we will search.
369///
370/// \returns A valid image base address if we are able to extract one.
371static std::optional<uint64_t>
372getImageBaseAddress(const object::MachOObjectFile *MachO) {
373 for (const auto &Command : MachO->load_commands()) {
374 if (Command.C.cmd == MachO::LC_SEGMENT) {
375 MachO::segment_command SLC = MachO->getSegmentLoadCommand(L: Command);
376 StringRef SegName = SLC.segname;
377 if (SegName == "__TEXT")
378 return SLC.vmaddr;
379 } else if (Command.C.cmd == MachO::LC_SEGMENT_64) {
380 MachO::segment_command_64 SLC = MachO->getSegment64LoadCommand(L: Command);
381 StringRef SegName = SLC.segname;
382 if (SegName == "__TEXT")
383 return SLC.vmaddr;
384 }
385 }
386 return std::nullopt;
387}
388
389/// Determine the virtual address that is considered the base address of an
390/// object file.
391///
392/// Since GSYM files are used for symbolication, many clients will need to
393/// easily adjust addresses they find in stack traces so the lookups happen
394/// on unslid addresses from the original object file. If the base address of
395/// a GSYM file is set to the base address of the image, then this address
396/// adjusting is much easier.
397///
398/// \param Obj An object file we will search.
399///
400/// \returns A valid image base address if we are able to extract one.
401static std::optional<uint64_t> getImageBaseAddress(object::ObjectFile &Obj) {
402 if (const auto *MachO = dyn_cast<object::MachOObjectFile>(Val: &Obj))
403 return getImageBaseAddress(MachO);
404 else if (const auto *ELFObj = dyn_cast<object::ELF32LEObjectFile>(Val: &Obj))
405 return getImageBaseAddress(ELFFile: ELFObj->getELFFile());
406 else if (const auto *ELFObj = dyn_cast<object::ELF32BEObjectFile>(Val: &Obj))
407 return getImageBaseAddress(ELFFile: ELFObj->getELFFile());
408 else if (const auto *ELFObj = dyn_cast<object::ELF64LEObjectFile>(Val: &Obj))
409 return getImageBaseAddress(ELFFile: ELFObj->getELFFile());
410 else if (const auto *ELFObj = dyn_cast<object::ELF64BEObjectFile>(Val: &Obj))
411 return getImageBaseAddress(ELFFile: ELFObj->getELFFile());
412 return std::nullopt;
413}
414
415static Expected<ObjectFile *>
416resolveSymtabObject(StringRef ArchName, Binary *SymtabBinary,
417 StringRef SymtabPath,
418 std::unique_ptr<ObjectFile> &OwnedSymtabObj) {
419 if (!SymtabBinary)
420 return nullptr;
421
422 if (auto *SymtabObj = dyn_cast<ObjectFile>(Val: SymtabBinary)) {
423 std::string SymtabArchName = getArchitectureName(Obj: *SymtabObj);
424 if (SymtabArchName != ArchName)
425 return createStringError(EC: std::errc::invalid_argument,
426 Fmt: "architecture mismatch: input file is %s but "
427 "symbol table file '%s' is %s",
428 Vals: ArchName.str().c_str(), Vals: SymtabPath.str().c_str(),
429 Vals: SymtabArchName.c_str());
430
431 return SymtabObj;
432 }
433
434 if (auto *SymtabFat = dyn_cast<MachOUniversalBinary>(Val: SymtabBinary)) {
435 auto SymtabObjOrErr = SymtabFat->getMachOObjectForArch(ArchName);
436 if (!SymtabObjOrErr) {
437 consumeError(Err: SymtabObjOrErr.takeError());
438 return createStringError(
439 EC: std::errc::invalid_argument,
440 Fmt: "symbol table file '%s' does not contain architecture '%s'",
441 Vals: SymtabPath.str().c_str(), Vals: ArchName.str().c_str());
442 }
443
444 OwnedSymtabObj = std::move(*SymtabObjOrErr);
445 return OwnedSymtabObj.get();
446 }
447
448 return createStringError(EC: std::errc::invalid_argument,
449 Fmt: "symbol table file '%s' is not a valid object file",
450 Vals: SymtabPath.str().c_str());
451}
452
453static llvm::Error handleObjectFile(ObjectFile &Obj, ObjectFile *SymtabObj,
454 StringRef SymtabPath,
455 const std::string &OutFile,
456 OutputAggregator &Out) {
457 auto ThreadCount =
458 NumThreads > 0 ? NumThreads : std::thread::hardware_concurrency();
459
460 std::unique_ptr<GsymCreator> GsymPtr;
461 switch (OutputVersion) {
462 case Header::getVersion():
463 GsymPtr = std::make_unique<GsymCreatorV1>();
464 break;
465 case HeaderV2::getVersion():
466 GsymPtr = std::make_unique<GsymCreatorV2>();
467 break;
468 default:
469 return createStringError(EC: std::errc::invalid_argument,
470 Fmt: "invalid --output-version option");
471 }
472 GsymCreator &Gsym = *GsymPtr;
473
474 // See if we can figure out the base address for a given object file, and if
475 // we can, then set the base address to use to this value. This will ease
476 // symbolication since clients can slide the GSYM lookup addresses by using
477 // the load bias of the shared library.
478 if (auto ImageBaseAddr = getImageBaseAddress(Obj))
479 Gsym.setBaseAddress(*ImageBaseAddr);
480
481 // We need to know where the valid sections are that contain instructions.
482 // See header documentation for DWARFTransformer::SetValidTextRanges() for
483 // defails.
484 AddressRanges TextRanges;
485 for (const object::SectionRef &Sect : Obj.sections()) {
486 if (!Sect.isText())
487 continue;
488 const uint64_t Size = Sect.getSize();
489 if (Size == 0)
490 continue;
491 const uint64_t StartAddr = Sect.getAddress();
492 TextRanges.insert(Range: AddressRange(StartAddr, StartAddr + Size));
493 }
494
495 // Make sure there is DWARF to convert first.
496 std::unique_ptr<DWARFContext> DICtx = DWARFContext::create(
497 Obj,
498 /*RelocAction=*/DWARFContext::ProcessDebugRelocations::Process,
499 L: nullptr,
500 /*DWPName=*/"",
501 /*RecoverableErrorHandler=*/WithColor::defaultErrorHandler,
502 /*WarningHandler=*/WithColor::defaultWarningHandler,
503 /*ThreadSafe*/true);
504 if (!DICtx)
505 return createStringError(EC: std::errc::invalid_argument,
506 Fmt: "unable to create DWARF context");
507
508 // Make a DWARF transformer object and populate the ranges of the code
509 // so we don't end up adding invalid functions to GSYM data.
510 bool IsMachO = dyn_cast<object::MachOObjectFile>(Val: &Obj) != nullptr;
511
512 DwarfTransformer DT(*DICtx, Gsym, LoadDwarfCallSites, IsMachO);
513 if (!TextRanges.empty())
514 Gsym.SetValidTextRanges(TextRanges);
515
516 // Convert all DWARF to GSYM.
517 if (auto Err = DT.convert(NumThreads: ThreadCount, OS&: Out))
518 return Err;
519
520 // If enabled, merge functions with identical address ranges as merged
521 // functions in the first FunctionInfo with that address range. Do this right
522 // after loading the DWARF data so we don't have to deal with functions from
523 // the symbol table.
524 if (UseMergedFunctions)
525 Gsym.prepareMergedFunctions(Out);
526
527 // Get the UUID and convert symbol table to GSYM.
528 if (SymtabObj) {
529 Out << "Using symbol table file: " << SymtabPath << "\n";
530 if (auto Err = ObjectFileTransformer::convert(Obj: *SymtabObj, Output&: Out, Gsym))
531 return Err;
532 } else if (auto Err = ObjectFileTransformer::convert(Obj, Output&: Out, Gsym)) {
533 return Err;
534 }
535
536 // If any call site YAML files were specified, load them now.
537 if (!CallSiteYamlPath.empty())
538 if (auto Err = Gsym.loadCallSitesFromYAML(YAMLFile: CallSiteYamlPath))
539 return Err;
540
541 // Finalize the GSYM to make it ready to save to disk. This will remove
542 // duplicate FunctionInfo entries where we might have found an entry from
543 // debug info and also a symbol table entry from the object file. Pass along
544 // the object file that the symbols came from so the size of a trailing
545 // symbol that has no size doesn't extend past the end of its section.
546 if (auto Err = Gsym.finalize(OS&: Out, Obj: SymtabObj ? SymtabObj : &Obj))
547 return Err;
548
549 // Save the GSYM file to disk.
550 llvm::endianness Endian = Obj.makeTriple().isLittleEndian()
551 ? llvm::endianness::little
552 : llvm::endianness::big;
553
554 std::optional<uint64_t> OptSegmentSize;
555 if (SegmentSize > 0)
556 OptSegmentSize = SegmentSize;
557 if (auto Err = Gsym.save(Path: OutFile, ByteOrder: Endian, SegmentSize: OptSegmentSize))
558 return Err;
559
560 // Verify the DWARF if requested. This will ensure all the info in the DWARF
561 // can be looked up in the GSYM and that all lookups get matching data.
562 if (Verify) {
563 if (auto Err = DT.verify(GsymPath: OutFile, OS&: Out))
564 return Err;
565 }
566
567 return Error::success();
568}
569
570static llvm::Error handleBuffer(StringRef Filename, MemoryBufferRef Buffer,
571 Binary *SymtabBinary, StringRef SymtabPath,
572 const std::string &OutFile,
573 OutputAggregator &Out) {
574 Expected<std::unique_ptr<Binary>> BinOrErr = object::createBinary(Source: Buffer);
575 error(Prefix: Filename, EC: errorToErrorCode(Err: BinOrErr.takeError()));
576
577 if (auto *Obj = dyn_cast<ObjectFile>(Val: BinOrErr->get())) {
578 std::string ArchName = getArchitectureName(Obj: *Obj);
579 std::unique_ptr<ObjectFile> OwnedSymtabObj;
580 auto SymtabObjOrErr =
581 resolveSymtabObject(ArchName, SymtabBinary, SymtabPath, OwnedSymtabObj);
582 if (!SymtabObjOrErr)
583 return SymtabObjOrErr.takeError();
584
585 outs() << "Output file (" << ArchName << "): " << OutFile << "\n";
586 if (auto Err =
587 handleObjectFile(Obj&: *Obj, SymtabObj: *SymtabObjOrErr, SymtabPath, OutFile, Out))
588 return Err;
589 } else if (auto *Fat = dyn_cast<MachOUniversalBinary>(Val: BinOrErr->get())) {
590 // Iterate over all contained architectures and filter out any that were
591 // not specified with the "--arch <arch>" option. If the --arch option was
592 // not specified on the command line, we will process all architectures.
593 std::vector<std::unique_ptr<MachOObjectFile>> FilterObjs;
594 for (auto &ObjForArch : Fat->objects()) {
595 auto MachOOrErr = ObjForArch.getAsObjectFile();
596 if (!MachOOrErr) {
597 error(Prefix: Filename, Err: MachOOrErr.takeError());
598 continue;
599 }
600
601 std::unique_ptr<MachOObjectFile> Obj = std::move(*MachOOrErr);
602 if (filterArch(Obj&: *Obj))
603 FilterObjs.emplace_back(args: std::move(Obj));
604 }
605 if (FilterObjs.empty())
606 error(Prefix: Filename, Err: createStringError(EC: std::errc::invalid_argument,
607 Fmt: "no matching architectures found"));
608
609 // Now handle each architecture we need to convert.
610 bool MultipleArchitecturesSelected = FilterObjs.size() > 1;
611 if (MultipleArchitecturesSelected && SymtabBinary &&
612 isa<ObjectFile>(Val: SymtabBinary))
613 return createStringError(
614 EC: std::errc::invalid_argument,
615 Fmt: "symbol table file '%s' is not a universal binary, but the input "
616 "contains multiple architectures; use --arch to select a single "
617 "architecture",
618 Vals: SymtabPath.str().c_str());
619
620 for (auto &Obj : FilterObjs) {
621 std::string ArchName = getArchitectureName(Obj: *Obj);
622 std::unique_ptr<ObjectFile> OwnedSymtabObj;
623 auto SymtabObjOrErr = resolveSymtabObject(ArchName, SymtabBinary,
624 SymtabPath, OwnedSymtabObj);
625 if (!SymtabObjOrErr)
626 return SymtabObjOrErr.takeError();
627
628 std::string ArchOutFile(OutFile);
629 // If we are only handling a single architecture, then we will use the
630 // normal output file. If we are handling multiple architectures append
631 // the architecture name to the end of the out file path so that we
632 // don't overwrite the previous architecture's gsym file.
633 if (MultipleArchitecturesSelected) {
634 ArchOutFile.append(n: 1, c: '.');
635 ArchOutFile.append(str: ArchName);
636 }
637 outs() << "Output file (" << ArchName << "): " << ArchOutFile << "\n";
638 if (auto Err = handleObjectFile(Obj&: *Obj, SymtabObj: *SymtabObjOrErr, SymtabPath,
639 OutFile: ArchOutFile, Out))
640 return Err;
641 }
642 }
643 return Error::success();
644}
645
646static llvm::Error handleFileConversionToGSYM(StringRef Filename,
647 const std::string &OutFile,
648 OutputAggregator &Out) {
649 ErrorOr<std::unique_ptr<MemoryBuffer>> BuffOrErr =
650 MemoryBuffer::getFileOrSTDIN(Filename, /*IsText=*/true);
651 error(Prefix: Filename, EC: BuffOrErr.getError());
652 std::unique_ptr<MemoryBuffer> Buffer = std::move(BuffOrErr.get());
653
654 std::unique_ptr<MemoryBuffer> SymtabBuffer;
655 std::unique_ptr<Binary> SymtabBinary;
656 if (!SymtabFilename.empty()) {
657 auto SymtabBufOrErr =
658 MemoryBuffer::getFile(Filename: SymtabFilename, /*IsText=*/true);
659 if (!SymtabBufOrErr)
660 return createStringError(EC: SymtabBufOrErr.getError(),
661 Fmt: "failed to open symbol table file '%s'",
662 Vals: SymtabFilename.c_str());
663
664 SymtabBuffer = std::move(*SymtabBufOrErr);
665 auto SymtabBinOrErr = object::createBinary(Source: *SymtabBuffer);
666 if (!SymtabBinOrErr)
667 return SymtabBinOrErr.takeError();
668 SymtabBinary = std::move(*SymtabBinOrErr);
669 }
670
671 return handleBuffer(Filename, Buffer: *Buffer, SymtabBinary: SymtabBinary.get(), SymtabPath: SymtabFilename,
672 OutFile, Out);
673}
674
675static llvm::Error convertFileToGSYM(OutputAggregator &Out) {
676 // Expand any .dSYM bundles to the individual object files contained therein.
677 std::vector<std::string> Objects;
678 std::string OutFile = OutputFilename;
679 if (OutFile.empty()) {
680 OutFile = ConvertFilename;
681 OutFile += ".gsym";
682 }
683
684 Out << "Input file: " << ConvertFilename << "\n";
685
686 if (auto DsymObjectsOrErr =
687 MachOObjectFile::findDsymObjectMembers(Path: ConvertFilename)) {
688 if (DsymObjectsOrErr->empty())
689 Objects.push_back(x: ConvertFilename);
690 else
691 llvm::append_range(C&: Objects, R&: *DsymObjectsOrErr);
692 } else {
693 error(Err: DsymObjectsOrErr.takeError());
694 }
695
696 for (StringRef Object : Objects)
697 if (Error Err = handleFileConversionToGSYM(Filename: Object, OutFile, Out))
698 return Err;
699 return Error::success();
700}
701
702static void doLookup(GsymReader &Gsym, uint64_t Addr, raw_ostream &OS) {
703 if (UseMergedFunctions) {
704 if (auto Results = Gsym.lookupAll(Addr)) {
705 // If we have filters, count matching results first
706 size_t NumMatching = Results->size();
707 if (!MergedFunctionsFilters.empty()) {
708 NumMatching = 0;
709 for (const auto &Result : *Results) {
710 bool Matches = false;
711 for (const auto &Filter : MergedFunctionsFilters) {
712 Regex Pattern(Filter);
713 if (Pattern.match(String: Result.FuncName)) {
714 Matches = true;
715 break;
716 }
717 }
718 if (Matches)
719 NumMatching++;
720 }
721 }
722
723 OS << "Found " << NumMatching << " function"
724 << (NumMatching != 1 ? "s" : "") << " at address " << HEX64(Addr)
725 << ":\n";
726
727 for (size_t i = 0; i < Results->size(); ++i) {
728 // Skip if doesn't match any filter
729 if (!MergedFunctionsFilters.empty()) {
730 bool Matches = false;
731 for (const auto &Filter : MergedFunctionsFilters) {
732 Regex Pattern(Filter);
733 if (Pattern.match(String: Results->at(n: i).FuncName)) {
734 Matches = true;
735 break;
736 }
737 }
738 if (!Matches)
739 continue;
740 }
741
742 OS << " " << Results->at(n: i);
743
744 if (i != Results->size() - 1)
745 OS << "\n";
746 }
747 } else {
748 if (Verbose)
749 OS << "\nLookupResult for " << HEX64(Addr) << ":\n";
750 OS << HEX64(Addr) << ": ";
751 logAllUnhandledErrors(E: Results.takeError(), OS, ErrorBanner: "error: ");
752 }
753 } else { /* UseMergedFunctions == false */
754 if (auto Result = Gsym.lookup(Addr)) {
755 // If verbose is enabled dump the full function info for the address.
756 if (Verbose) {
757 if (auto FI = Gsym.getFunctionInfo(Addr)) {
758 OS << "FunctionInfo for " << HEX64(Addr) << ":\n";
759 Gsym.dump(OS, FI: *FI);
760 OS << "\nLookupResult for " << HEX64(Addr) << ":\n";
761 }
762 }
763 // Don't print call site info if --merged-functions is not specified.
764 Result->CallSiteFuncRegex.clear();
765 OS << Result.get();
766 } else {
767 if (Verbose)
768 OS << "\nLookupResult for " << HEX64(Addr) << ":\n";
769 OS << HEX64(Addr) << ": ";
770 logAllUnhandledErrors(E: Result.takeError(), OS, ErrorBanner: "error: ");
771 }
772 if (Verbose)
773 OS << "\n";
774 }
775}
776
777static llvm::Error benchmarkReader(StringRef GSYMPath, uint32_t Start,
778 uint32_t Stride) {
779 auto Gsym = GsymReader::openFile(Path: GSYMPath);
780 if (!Gsym)
781 return Gsym.takeError();
782 uint32_t N = (*Gsym)->getNumAddresses();
783 uint32_t NumLookups = 0;
784 for (uint32_t I = Start; I < N; I += Stride) {
785 auto Addr = (*Gsym)->getAddress(Index: I);
786 if (!Addr)
787 return createStringError(EC: std::errc::invalid_argument,
788 Fmt: "failed to extract address[%u]", Vals: I);
789 auto LR = (*Gsym)->lookup(Addr: *Addr);
790 if (!LR)
791 return LR.takeError();
792 ++NumLookups;
793 }
794 outs() << "Benchmarked " << NumLookups << " lookups (out of " << N
795 << " addresses) in \"" << GSYMPath << "\"\n";
796 return Error::success();
797}
798
799int llvm_gsymutil_main(int argc, char **argv, const llvm::ToolContext &) {
800 // Print a stack trace if we signal out.
801 sys::PrintStackTraceOnErrorSignal(Argv0: argv[0]);
802 PrettyStackTraceProgram X(argc, argv);
803 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
804
805 llvm::InitializeAllTargets();
806
807 parseArgs(argc, argv);
808
809 raw_ostream &OS = outs();
810
811 if (BenchmarkReader) {
812 for (const auto &GSYMPath : InputFilenames)
813 if (auto Err = benchmarkReader(GSYMPath, Start: BenchmarkStart, Stride: BenchmarkStride))
814 error(Prefix: "Benchmark failed: ", Err: std::move(Err));
815 return EXIT_SUCCESS;
816 }
817
818 OutputAggregator Aggregation(&OS, Quiet);
819 if (!ConvertFilename.empty()) {
820 // Convert DWARF to GSYM
821 if (!InputFilenames.empty()) {
822 OS << "error: no input files can be specified when using the --convert "
823 "option.\n";
824 return 1;
825 }
826 // Call error() if we have an error and it will exit with a status of 1
827 if (auto Err = convertFileToGSYM(Out&: Aggregation))
828 error(Prefix: "DWARF conversion failed: ", Err: std::move(Err));
829
830 // Report the errors from aggregator:
831 Aggregation.EnumerateResults(handleCounts: [&](StringRef category, unsigned count) {
832 OS << category << " occurred " << count << " time(s)\n";
833 });
834 if (!JsonSummaryFile.empty()) {
835 std::error_code EC;
836 raw_fd_ostream JsonStream(JsonSummaryFile, EC, sys::fs::OF_Text);
837 if (EC) {
838 OS << "error opening aggregate error json file '" << JsonSummaryFile
839 << "' for writing: " << EC.message() << '\n';
840 return 1;
841 }
842
843 llvm::json::Object Categories;
844 uint64_t ErrorCount = 0;
845 Aggregation.EnumerateResults(handleCounts: [&](StringRef Category, unsigned Count) {
846 llvm::json::Object Val;
847 Val.try_emplace(K: "count", Args&: Count);
848 Categories.try_emplace(K: Category, Args: std::move(Val));
849 ErrorCount += Count;
850 });
851 llvm::json::Object RootNode;
852 RootNode.try_emplace(K: "error-categories", Args: std::move(Categories));
853 RootNode.try_emplace(K: "error-count", Args&: ErrorCount);
854
855 JsonStream << llvm::json::Value(std::move(RootNode));
856 }
857 return 0;
858 }
859
860 if (LookupAddressesFromStdin) {
861 if (!LookupAddresses.empty() || !InputFilenames.empty()) {
862 OS << "error: no input files or addresses can be specified when using "
863 "the --addresses-from-stdin "
864 "option.\n";
865 return 1;
866 }
867
868 std::string InputLine;
869 std::string CurrentGSYMPath;
870 std::unique_ptr<GsymReader> CurrentGsym;
871
872 while (std::getline(is&: std::cin, str&: InputLine)) {
873 // Strip newline characters.
874 std::string StrippedInputLine(InputLine);
875 llvm::erase_if(C&: StrippedInputLine,
876 P: [](char c) { return c == '\r' || c == '\n'; });
877
878 StringRef AddrStr, GSYMPath;
879 std::tie(args&: AddrStr, args&: GSYMPath) =
880 llvm::StringRef{StrippedInputLine}.split(Separator: ' ');
881
882 if (GSYMPath != CurrentGSYMPath) {
883 auto GsymOrErr = GsymReader::openFile(Path: GSYMPath);
884 if (!GsymOrErr)
885 error(Prefix: GSYMPath, Err: GsymOrErr.takeError());
886 CurrentGsym = std::move(*GsymOrErr);
887 CurrentGSYMPath = GSYMPath;
888 }
889
890 uint64_t Addr;
891 if (AddrStr.getAsInteger(Radix: 0, Result&: Addr)) {
892 OS << "error: invalid address " << AddrStr
893 << ", expected: Address GsymFile.\n";
894 return 1;
895 }
896
897 doLookup(Gsym&: *CurrentGsym, Addr, OS);
898
899 OS << "\n";
900 OS.flush();
901 }
902
903 return EXIT_SUCCESS;
904 }
905
906 // Dump or access data inside GSYM files
907 for (const auto &GSYMPath : InputFilenames) {
908 auto Gsym = GsymReader::openFile(Path: GSYMPath);
909 if (!Gsym)
910 error(Prefix: GSYMPath, Err: Gsym.takeError());
911
912 if (ShowStatistics) {
913 (*Gsym)->dumpStatistics(OS, Format: StatisticsFormat, GSYMPath);
914 continue;
915 }
916
917 if (LookupAddresses.empty()) {
918 (*Gsym)->dump(OS&: outs());
919 continue;
920 }
921
922 // Lookup an address in a GSYM file and print any matches.
923 OS << "Looking up addresses in \"" << GSYMPath << "\":\n";
924 for (auto Addr : LookupAddresses) {
925 doLookup(Gsym&: **Gsym, Addr, OS);
926 }
927 }
928 return EXIT_SUCCESS;
929}
930