LLVM 22.0.0git
MarkupFilter.h
Go to the documentation of this file.
1//===- MarkupFilter.h -------------------------------------------*- C++ -*-===//
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/// \file
10/// This file declares a filter that replaces symbolizer markup with
11/// human-readable expressions.
12///
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_DEBUGINFO_SYMBOLIZE_MARKUPFILTER_H
16#define LLVM_DEBUGINFO_SYMBOLIZE_MARKUPFILTER_H
17
18#include "llvm/ADT/DenseMap.h"
20#include "llvm/Object/BuildID.h"
24#include <map>
25
26namespace llvm {
27namespace symbolize {
28
29class LLVMSymbolizer;
30
31/// Filter to convert parsed log symbolizer markup elements into human-readable
32/// text.
34public:
36 std::optional<bool> ColorsEnabled = std::nullopt);
37
38 /// Filters a line containing symbolizer markup and writes the human-readable
39 /// results to the output stream.
40 ///
41 /// Invalid or unimplemented markup elements are removed. Some output may be
42 /// deferred until future filter() or finish() call.
43 LLVM_ABI void filter(std::string &&InputLine);
44
45 /// Records that the input stream has ended and writes any deferred output.
46 LLVM_ABI void finish();
47
48private:
49 struct Module {
51 std::string Name;
53 };
54
55 struct MMap {
56 uint64_t Addr;
58 const Module *Mod;
59 std::string Mode; // Lowercase
60 uint64_t ModuleRelativeAddr;
61
62 LLVM_ABI bool contains(uint64_t Addr) const;
63 LLVM_ABI uint64_t getModuleRelativeAddr(uint64_t Addr) const;
64 };
65
66 // An informational module line currently being constructed. As many mmap
67 // elements as possible are folded into one ModuleInfo line.
68 struct ModuleInfoLine {
69 const Module *Mod;
70
72 };
73
74 // The semantics of a possible program counter value.
75 enum class PCType {
76 // The address is a return address and must be adjusted to point to the call
77 // itself.
78 ReturnAddress,
79 // The address is the precise location in the code and needs no adjustment.
80 PreciseCode,
81 };
82
83 bool tryContextualElement(const MarkupNode &Node,
84 const SmallVector<MarkupNode> &DeferredNodes);
85 bool tryMMap(const MarkupNode &Element,
86 const SmallVector<MarkupNode> &DeferredNodes);
87 bool tryReset(const MarkupNode &Element,
88 const SmallVector<MarkupNode> &DeferredNodes);
89 bool tryModule(const MarkupNode &Element,
90 const SmallVector<MarkupNode> &DeferredNodes);
91
92 void beginModuleInfoLine(const Module *M);
93 void endAnyModuleInfoLine();
94
95 void filterNode(const MarkupNode &Node);
96
97 bool tryPresentation(const MarkupNode &Node);
98 bool trySymbol(const MarkupNode &Node);
99 bool tryPC(const MarkupNode &Node);
100 bool tryBackTrace(const MarkupNode &Node);
101 bool tryData(const MarkupNode &Node);
102
103 bool trySGR(const MarkupNode &Node);
104
105 void highlight();
106 void highlightValue();
107 void restoreColor();
108 void resetColor();
109
110 void printRawElement(const MarkupNode &Element);
111 void printValue(Twine Value);
112
113 std::optional<Module> parseModule(const MarkupNode &Element) const;
114 std::optional<MMap> parseMMap(const MarkupNode &Element) const;
115
116 std::optional<uint64_t> parseAddr(StringRef Str) const;
117 std::optional<uint64_t> parseModuleID(StringRef Str) const;
118 std::optional<uint64_t> parseSize(StringRef Str) const;
119 object::BuildID parseBuildID(StringRef Str) const;
120 std::optional<std::string> parseMode(StringRef Str) const;
121 std::optional<PCType> parsePCType(StringRef Str) const;
122 std::optional<uint64_t> parseFrameNumber(StringRef Str) const;
123
124 bool checkTag(const MarkupNode &Node) const;
125 bool checkNumFields(const MarkupNode &Element, size_t Size) const;
126 bool checkNumFieldsAtLeast(const MarkupNode &Element, size_t Size) const;
127 void warnNumFieldsAtMost(const MarkupNode &Element, size_t Size) const;
128
129 void reportTypeError(StringRef Str, StringRef TypeName) const;
130 void reportLocation(StringRef::iterator Loc) const;
131
132 const MMap *getOverlappingMMap(const MMap &Map) const;
133 const MMap *getContainingMMap(uint64_t Addr) const;
134
135 uint64_t adjustAddr(uint64_t Addr, PCType Type) const;
136
137 StringRef lineEnding() const;
138
139 raw_ostream &OS;
140 LLVMSymbolizer &Symbolizer;
141 const bool ColorsEnabled;
142
143 MarkupParser Parser;
144
145 // Current line being filtered.
146 std::string Line;
147
148 // A module info line currently being built. This incorporates as much mmap
149 // information as possible before being emitted.
150 std::optional<ModuleInfoLine> MIL;
151
152 // SGR state.
153 std::optional<raw_ostream::Colors> Color;
154 bool Bold = false;
155
156 // Map from Module ID to Module.
158
159 // Ordered map from starting address to mmap.
160 std::map<uint64_t, MMap> MMaps;
161};
162
163} // end namespace symbolize
164} // end namespace llvm
165
166#endif // LLVM_DEBUGINFO_SYMBOLIZE_MARKUPFILTER_H
This file declares a library for handling Build IDs and using them to find debug info.
#define LLVM_ABI
Definition Compiler.h:213
This file defines the DenseMap class.
This file declares the log symbolizer markup data model and parser.
static cl::opt< RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode > Mode("regalloc-enable-advisor", cl::Hidden, cl::init(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default), cl::desc("Enable regalloc advisor mode"), cl::values(clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default, "default", "Default"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Release, "release", "precompiled"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Development, "development", "for training")))
static bool contains(SmallPtrSetImpl< ConstantExpr * > &Cache, ConstantExpr *Expr, Constant *C)
Definition Value.cpp:480
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.
Definition StringRef.h:55
const char * iterator
Definition StringRef.h:59
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:45
LLVM Value Representation.
Definition Value.h:75
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
LLVM_ABI MarkupFilter(raw_ostream &OS, LLVMSymbolizer &Symbolizer, std::optional< bool > ColorsEnabled=std::nullopt)
LLVM_ABI void filter(std::string &&InputLine)
Filters a line containing symbolizer markup and writes the human-readable results to the output strea...
LLVM_ABI void finish()
Records that the input stream has ended and writes any deferred output.
Parses a log containing symbolizer markup into a sequence of nodes.
Definition Markup.h:52
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
SmallVector< uint8_t, 10 > BuildID
A build ID in binary form.
Definition BuildID.h:26
This is an optimization pass for GlobalISel generic memory operations.
@ Mod
The access may modify the value stored in memory.
Definition ModRef.h:34
A node of symbolizer markup.
Definition Markup.h:34