39 std::optional<bool> ColorsEnabled)
40 :
OS(
OS), Symbolizer(Symbolizer),
42 ColorsEnabled.value_or(
WithColor::defaultAutoDetectFunction()(
OS))) {}
45 Line = std::move(InputLine);
53 while (std::optional<MarkupNode> Node =
Parser.nextNode()) {
55 if (tryContextualElement(*Node, DeferredNodes))
62 endAnyModuleInfoLine();
69 while (std::optional<MarkupNode> Node =
Parser.nextNode())
71 endAnyModuleInfoLine();
85bool MarkupFilter::tryContextualElement(
87 if (tryMMap(Node, DeferredNodes))
89 if (tryReset(Node, DeferredNodes))
91 return tryModule(Node, DeferredNodes);
94bool MarkupFilter::tryMMap(
const MarkupNode &Node,
96 if (Node.Tag !=
"mmap")
98 std::optional<MMap> ParsedMMap = parseMMap(Node);
102 if (
const MMap *M = getOverlappingMMap(*ParsedMMap)) {
104 <<
formatv(
"overlapping mmap: #{0:x} [{1:x}-{2:x}]\n", M->Mod->ID,
105 M->Addr, M->Addr + M->Size - 1);
106 reportLocation(Node.Fields[0].begin());
110 auto Res = MMaps.emplace(ParsedMMap->Addr, std::move(*ParsedMMap));
111 assert(Res.second &&
"Overlap check should ensure emplace succeeds.");
112 MMap &MMap = Res.first->second;
114 if (!MIL || MIL->Mod != MMap.Mod) {
115 endAnyModuleInfoLine();
118 beginModuleInfoLine(MMap.Mod);
121 MIL->MMaps.push_back(&MMap);
127 if (
Node.Tag !=
"reset")
129 if (!checkNumFields(
Node, 0))
132 if (!Modules.
empty() || !MMaps.empty()) {
133 endAnyModuleInfoLine();
136 printRawElement(
Node);
147 if (
Node.Tag !=
"module")
149 std::optional<Module> ParsedModule = parseModule(
Node);
154 ParsedModule->ID, std::make_unique<Module>(std::move(*ParsedModule)));
157 reportLocation(
Node.Fields[0].begin());
162 endAnyModuleInfoLine();
165 beginModuleInfoLine(&
Module);
171void MarkupFilter::beginModuleInfoLine(
const Module *M) {
173 OS <<
"[[[ELF module";
174 printValue(
formatv(
" #{0:x} ",
M->ID));
178 MIL = ModuleInfoLine{
M};
181void MarkupFilter::endAnyModuleInfoLine() {
185 return A->Addr < B->Addr;
187 for (
const MMap *M : MIL->MMaps) {
188 OS << (
M == MIL->MMaps.front() ?
' ' :
',');
190 printValue(
formatv(
"{0:x}",
M->Addr));
192 printValue(
formatv(
"{0:x}",
M->Addr +
M->Size - 1));
197 OS <<
"]]]" << lineEnding();
206 if (tryPresentation(
Node))
219 if (tryBackTrace(
Node))
221 return tryData(
Node);
225 if (
Node.Tag !=
"symbol")
227 if (!checkNumFields(
Node, 1))
237 if (
Node.Tag !=
"pc")
239 if (!checkNumFieldsAtLeast(
Node, 1))
241 warnNumFieldsAtMost(
Node, 2);
243 std::optional<uint64_t>
Addr = parseAddr(
Node.Fields[0]);
249 PCType
Type = PCType::PreciseCode;
250 if (
Node.Fields.size() == 2) {
251 std::optional<PCType> ParsedType = parsePCType(
Node.Fields[1]);
258 const MMap *MMap = getContainingMMap(*
Addr);
261 reportLocation(
Node.Fields[0].begin());
262 printRawElement(
Node);
267 MMap->Mod->BuildID, {MMap->getModuleRelativeAddr(*Addr)});
270 printRawElement(
Node);
274 printRawElement(
Node);
279 printValue(LI->FunctionName);
281 printValue(LI->FileName);
283 printValue(
Twine(LI->Line));
290 if (
Node.Tag !=
"bt")
292 if (!checkNumFieldsAtLeast(
Node, 2))
294 warnNumFieldsAtMost(
Node, 3);
296 std::optional<uint64_t> FrameNumber = parseFrameNumber(
Node.Fields[0]);
300 std::optional<uint64_t>
Addr = parseAddr(
Node.Fields[1]);
305 PCType
Type = PCType::ReturnAddress;
306 if (
Node.Fields.size() == 3) {
307 std::optional<PCType> ParsedType = parsePCType(
Node.Fields[2]);
314 const MMap *MMap = getContainingMMap(*
Addr);
317 reportLocation(
Node.Fields[0].begin());
318 printRawElement(
Node);
327 printRawElement(
Node);
332 for (
unsigned I = 0, E =
II->getNumberOfFrames();
I != E; ++
I) {
333 auto Header =
formatv(
"{0, +6}",
formatv(
"#{0}", FrameNumber)).sstr<16>();
335 size_t NumberIdx = Header.find(
"#") + 1;
336 OS << Header.substr(0, NumberIdx);
337 printValue(Header.substr(NumberIdx));
342 printValue(
formatv(
"{0, -2}",
I + 1));
358 printValue(MMap->Mod->Name);
360 printValue(
formatv(
"{0:x}", MRA));
370 if (
Node.Tag !=
"data")
372 if (!checkNumFields(
Node, 1))
374 std::optional<uint64_t>
Addr = parseAddr(
Node.Fields[0]);
378 const MMap *MMap = getContainingMMap(*
Addr);
381 reportLocation(
Node.Fields[0].begin());
382 printRawElement(
Node);
387 MMap->Mod->BuildID, {MMap->getModuleRelativeAddr(*Addr)});
390 printRawElement(
Node);
401 if (
Node.Text ==
"\033[0m") {
405 if (
Node.Text ==
"\033[1m") {
433void MarkupFilter::highlight() {
442void MarkupFilter::highlightValue() {
450void MarkupFilter::restoreColor() {
463void MarkupFilter::resetColor() {
472void MarkupFilter::printRawElement(
const MarkupNode &Element) {
475 printValue(Element.
Tag);
493#define ASSIGN_OR_RETURN_NONE(TYPE, NAME, EXPR) \
494 auto NAME##Opt = (EXPR); \
496 return std::nullopt; \
497 TYPE NAME = std::move(*NAME##Opt)
499std::optional<MarkupFilter::Module>
500MarkupFilter::parseModule(
const MarkupNode &Element)
const {
501 if (!checkNumFieldsAtLeast(Element, 3))
508 reportLocation(
Type.begin());
511 if (!checkNumFields(Element, 4))
519std::optional<MarkupFilter::MMap>
520MarkupFilter::parseMMap(
const MarkupNode &Element)
const {
521 if (!checkNumFieldsAtLeast(Element, 3))
526 if (
Type !=
"load") {
528 reportLocation(
Type.begin());
531 if (!checkNumFields(Element, 6))
535 auto It = Modules.
find(
ID);
536 if (It == Modules.
end()) {
538 reportLocation(Element.
Fields[3].begin());
542 parseAddr(Element.
Fields[5]));
543 return MMap{
Addr,
Size, It->second.get(), std::move(Mode),
548std::optional<uint64_t> MarkupFilter::parseAddr(
StringRef Str)
const {
550 reportTypeError(Str,
"address");
553 if (
all_of(Str, [](
char C) {
return C ==
'0'; }))
555 if (!Str.starts_with(
"0x")) {
556 reportTypeError(Str,
"address");
560 if (Str.drop_front(2).getAsInteger(16,
Addr)) {
561 reportTypeError(Str,
"address");
568std::optional<uint64_t> MarkupFilter::parseModuleID(
StringRef Str)
const {
570 if (Str.getAsInteger(0,
ID)) {
571 reportTypeError(Str,
"module ID");
578std::optional<uint64_t> MarkupFilter::parseSize(
StringRef Str)
const {
580 if (Str.getAsInteger(0,
ID)) {
581 reportTypeError(Str,
"size");
588std::optional<uint64_t> MarkupFilter::parseFrameNumber(
StringRef Str)
const {
590 if (Str.getAsInteger(10,
ID)) {
591 reportTypeError(Str,
"frame number");
601 reportTypeError(Str,
"build ID");
606std::optional<std::string> MarkupFilter::parseMode(
StringRef Str)
const {
608 reportTypeError(Str,
"mode");
619 if (!Remainder.
empty()) {
620 reportTypeError(Str,
"mode");
628std::optional<MarkupFilter::PCType>
629MarkupFilter::parsePCType(
StringRef Str)
const {
630 std::optional<MarkupFilter::PCType>
Type =
632 .Case(
"ra", MarkupFilter::PCType::ReturnAddress)
633 .
Case(
"pc", MarkupFilter::PCType::PreciseCode)
636 reportTypeError(Str,
"PC type");
641 if (
any_of(
Node.Tag, [](
char C) { return C <
'a' || C >
'z'; })) {
643 reportLocation(
Node.Tag.begin());
649bool MarkupFilter::checkNumFields(
const MarkupNode &Element,
654 << (Warn ?
"warning: " :
"error: ") <<
"expected " <<
Size
655 <<
" field(s); found " << Element.
Fields.size() <<
"\n";
656 reportLocation(Element.
Tag.
end());
662bool MarkupFilter::checkNumFieldsAtLeast(
const MarkupNode &Element,
666 <<
"expected at least " <<
Size <<
" field(s); found "
667 << Element.
Fields.size() <<
"\n";
668 reportLocation(Element.
Tag.
end());
674void MarkupFilter::warnNumFieldsAtMost(
const MarkupNode &Element,
679 <<
"expected at most " <<
Size <<
" field(s); found "
680 << Element.
Fields.size() <<
"\n";
681 reportLocation(Element.
Tag.
end());
687 reportLocation(Str.begin());
703const MarkupFilter::MMap *
704MarkupFilter::getOverlappingMMap(
const MMap &Map)
const {
706 auto I = MMaps.upper_bound(
Map.Addr);
707 if (
I != MMaps.end() &&
Map.contains(
I->second.Addr))
712 if (
I != MMaps.begin()) {
714 if (
I->second.contains(
Map.Addr))
721const MarkupFilter::MMap *MarkupFilter::getContainingMMap(
uint64_t Addr)
const {
723 auto I = MMaps.lower_bound(
Addr);
724 if (
I != MMaps.end() &&
I->second.contains(
Addr))
728 if (
I == MMaps.begin())
731 return I->second.contains(
Addr) ? &
I->second :
nullptr;
739 return Type == MarkupFilter::PCType::ReturnAddress ?
Addr - 1 :
Addr;
742StringRef MarkupFilter::lineEnding()
const {
752 return Addr - this->Addr + ModuleRelativeAddr;
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
This file contains several declarations for the debuginfod client and server.
#define ASSIGN_OR_RETURN_NONE(TYPE, NAME, EXPR)
This file declares a filter that replaces symbolizer markup with human-readable expressions.
This file declares the log symbolizer markup data model and parser.
uint64_t IntrinsicInst * II
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file implements the StringSwitch template, which mimics a switch() statement whose cases are str...
iterator find(const_arg_type_t< KeyT > Val)
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&... Args)
Tagged union holding either a T or a Error.
Error takeError()
Take ownership of the stored error.
A Module instance is used to store all the information related to an LLVM module.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
constexpr bool empty() const
empty - Check if the string is empty.
bool ends_with(StringRef Suffix) const
Check if this string ends with the given Suffix.
bool consume_front_insensitive(StringRef Prefix)
Returns true if this StringRef has the given prefix, ignoring case, and removes that prefix.
A switch()-like statement whose cases are string literals.
StringSwitch & Case(StringLiteral S, T Value)
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
The instances of the Type class are immutable: once they are created, they are never changed.
LLVM Value Representation.
An RAII object that temporarily switches an output stream to a specific color.
static raw_ostream & warning()
Convenience method for printing "warning: " to stderr.
static raw_ostream & error()
Convenience method for printing "error: " to stderr.
static void defaultErrorHandler(Error Err)
Implement default handling for Error.
This class implements an extremely fast bulk output stream that can only output to a stream.
virtual raw_ostream & changeColor(enum Colors Color, bool Bold=false, bool BG=false)
Changes the foreground color of text that will be output from this point forward.
virtual raw_ostream & resetColor()
Resets the colors to terminal defaults.
Expected< DIInliningInfo > symbolizeInlinedCode(const ObjectFile &Obj, object::SectionedAddress ModuleOffset)
Expected< DILineInfo > symbolizeCode(const ObjectFile &Obj, object::SectionedAddress ModuleOffset)
Expected< DIGlobal > symbolizeData(const ObjectFile &Obj, object::SectionedAddress ModuleOffset)
MarkupFilter(raw_ostream &OS, LLVMSymbolizer &Symbolizer, std::optional< bool > ColorsEnabled=std::nullopt)
void filter(std::string &&InputLine)
Filters a line containing symbolizer markup and writes the human-readable results to the output strea...
void finish()
Records that the input stream has ended and writes any deferred output.
constexpr char TypeName[]
Key for Kernel::Arg::Metadata::mTypeName.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
@ C
The default llvm calling convention, compatible with C.
BuildID parseBuildID(StringRef Str)
Parses a build ID from a hex string.
static std::string toHex(uint64_t V)
const_iterator begin(StringRef path, Style style=Style::native)
Get begin iterator over path.
This is an optimization pass for GlobalISel generic memory operations.
void stable_sort(R &&Range)
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
auto formatv(const char *Fmt, Ts &&...Vals) -> formatv_object< decltype(std::make_tuple(support::detail::build_format_adapter(std::forward< Ts >(Vals))...))>
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
std::string demangle(std::string_view MangledName)
Attempt to demangle a string using different demangling schemes.
A format-neutral container for source line information.
A node of symbolizer markup.
SmallVector< StringRef > Fields
If this represents an element with fields, a list of the field contents.
StringRef Tag
If this represents an element, the tag. Otherwise, empty.