File: | build/source/lldb/source/Core/FormatEntity.cpp |
Warning: | line 962, column 20 Called C++ object pointer is null |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | //===-- FormatEntity.cpp --------------------------------------------------===// | ||||
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 "lldb/Core/FormatEntity.h" | ||||
10 | |||||
11 | #include "lldb/Core/Address.h" | ||||
12 | #include "lldb/Core/AddressRange.h" | ||||
13 | #include "lldb/Core/Debugger.h" | ||||
14 | #include "lldb/Core/DumpRegisterValue.h" | ||||
15 | #include "lldb/Core/Module.h" | ||||
16 | #include "lldb/Core/ValueObject.h" | ||||
17 | #include "lldb/Core/ValueObjectVariable.h" | ||||
18 | #include "lldb/DataFormatters/DataVisualization.h" | ||||
19 | #include "lldb/DataFormatters/FormatClasses.h" | ||||
20 | #include "lldb/DataFormatters/FormatManager.h" | ||||
21 | #include "lldb/DataFormatters/TypeSummary.h" | ||||
22 | #include "lldb/Expression/ExpressionVariable.h" | ||||
23 | #include "lldb/Interpreter/CommandInterpreter.h" | ||||
24 | #include "lldb/Symbol/Block.h" | ||||
25 | #include "lldb/Symbol/CompileUnit.h" | ||||
26 | #include "lldb/Symbol/CompilerType.h" | ||||
27 | #include "lldb/Symbol/Function.h" | ||||
28 | #include "lldb/Symbol/LineEntry.h" | ||||
29 | #include "lldb/Symbol/Symbol.h" | ||||
30 | #include "lldb/Symbol/SymbolContext.h" | ||||
31 | #include "lldb/Symbol/VariableList.h" | ||||
32 | #include "lldb/Target/ExecutionContext.h" | ||||
33 | #include "lldb/Target/ExecutionContextScope.h" | ||||
34 | #include "lldb/Target/Language.h" | ||||
35 | #include "lldb/Target/Process.h" | ||||
36 | #include "lldb/Target/RegisterContext.h" | ||||
37 | #include "lldb/Target/SectionLoadList.h" | ||||
38 | #include "lldb/Target/StackFrame.h" | ||||
39 | #include "lldb/Target/StopInfo.h" | ||||
40 | #include "lldb/Target/Target.h" | ||||
41 | #include "lldb/Target/Thread.h" | ||||
42 | #include "lldb/Utility/AnsiTerminal.h" | ||||
43 | #include "lldb/Utility/ArchSpec.h" | ||||
44 | #include "lldb/Utility/CompletionRequest.h" | ||||
45 | #include "lldb/Utility/ConstString.h" | ||||
46 | #include "lldb/Utility/FileSpec.h" | ||||
47 | #include "lldb/Utility/LLDBLog.h" | ||||
48 | #include "lldb/Utility/Log.h" | ||||
49 | #include "lldb/Utility/RegisterValue.h" | ||||
50 | #include "lldb/Utility/Status.h" | ||||
51 | #include "lldb/Utility/Stream.h" | ||||
52 | #include "lldb/Utility/StreamString.h" | ||||
53 | #include "lldb/Utility/StringList.h" | ||||
54 | #include "lldb/Utility/StructuredData.h" | ||||
55 | #include "lldb/lldb-defines.h" | ||||
56 | #include "lldb/lldb-forward.h" | ||||
57 | #include "llvm/ADT/STLExtras.h" | ||||
58 | #include "llvm/ADT/StringRef.h" | ||||
59 | #include "llvm/ADT/Triple.h" | ||||
60 | #include "llvm/Support/Compiler.h" | ||||
61 | |||||
62 | #include <cctype> | ||||
63 | #include <cinttypes> | ||||
64 | #include <cstdio> | ||||
65 | #include <cstdlib> | ||||
66 | #include <cstring> | ||||
67 | #include <memory> | ||||
68 | #include <type_traits> | ||||
69 | #include <utility> | ||||
70 | |||||
71 | namespace lldb_private { | ||||
72 | class ScriptInterpreter; | ||||
73 | } | ||||
74 | namespace lldb_private { | ||||
75 | struct RegisterInfo; | ||||
76 | } | ||||
77 | |||||
78 | using namespace lldb; | ||||
79 | using namespace lldb_private; | ||||
80 | |||||
81 | using Definition = lldb_private::FormatEntity::Entry::Definition; | ||||
82 | using Entry = FormatEntity::Entry; | ||||
83 | using EntryType = FormatEntity::Entry::Type; | ||||
84 | |||||
85 | enum FileKind { FileError = 0, Basename, Dirname, Fullpath }; | ||||
86 | |||||
87 | constexpr Definition g_string_entry[] = { | ||||
88 | Definition("*", EntryType::ParentString)}; | ||||
89 | |||||
90 | constexpr Definition g_addr_entries[] = { | ||||
91 | Definition("load", EntryType::AddressLoad), | ||||
92 | Definition("file", EntryType::AddressFile)}; | ||||
93 | |||||
94 | constexpr Definition g_file_child_entries[] = { | ||||
95 | Definition("basename", EntryType::ParentNumber, FileKind::Basename), | ||||
96 | Definition("dirname", EntryType::ParentNumber, FileKind::Dirname), | ||||
97 | Definition("fullpath", EntryType::ParentNumber, FileKind::Fullpath)}; | ||||
98 | |||||
99 | constexpr Definition g_frame_child_entries[] = { | ||||
100 | Definition("index", EntryType::FrameIndex), | ||||
101 | Definition("pc", EntryType::FrameRegisterPC), | ||||
102 | Definition("fp", EntryType::FrameRegisterFP), | ||||
103 | Definition("sp", EntryType::FrameRegisterSP), | ||||
104 | Definition("flags", EntryType::FrameRegisterFlags), | ||||
105 | Definition("no-debug", EntryType::FrameNoDebug), | ||||
106 | Entry::DefinitionWithChildren("reg", EntryType::FrameRegisterByName, | ||||
107 | g_string_entry), | ||||
108 | Definition("is-artificial", EntryType::FrameIsArtificial), | ||||
109 | }; | ||||
110 | |||||
111 | constexpr Definition g_function_child_entries[] = { | ||||
112 | Definition("id", EntryType::FunctionID), | ||||
113 | Definition("name", EntryType::FunctionName), | ||||
114 | Definition("name-without-args", EntryType::FunctionNameNoArgs), | ||||
115 | Definition("name-with-args", EntryType::FunctionNameWithArgs), | ||||
116 | Definition("mangled-name", EntryType::FunctionMangledName), | ||||
117 | Definition("addr-offset", EntryType::FunctionAddrOffset), | ||||
118 | Definition("concrete-only-addr-offset-no-padding", | ||||
119 | EntryType::FunctionAddrOffsetConcrete), | ||||
120 | Definition("line-offset", EntryType::FunctionLineOffset), | ||||
121 | Definition("pc-offset", EntryType::FunctionPCOffset), | ||||
122 | Definition("initial-function", EntryType::FunctionInitial), | ||||
123 | Definition("changed", EntryType::FunctionChanged), | ||||
124 | Definition("is-optimized", EntryType::FunctionIsOptimized)}; | ||||
125 | |||||
126 | constexpr Definition g_line_child_entries[] = { | ||||
127 | Entry::DefinitionWithChildren("file", EntryType::LineEntryFile, | ||||
128 | g_file_child_entries), | ||||
129 | Definition("number", EntryType::LineEntryLineNumber), | ||||
130 | Definition("column", EntryType::LineEntryColumn), | ||||
131 | Definition("start-addr", EntryType::LineEntryStartAddress), | ||||
132 | Definition("end-addr", EntryType::LineEntryEndAddress), | ||||
133 | }; | ||||
134 | |||||
135 | constexpr Definition g_module_child_entries[] = {Entry::DefinitionWithChildren( | ||||
136 | "file", EntryType::ModuleFile, g_file_child_entries)}; | ||||
137 | |||||
138 | constexpr Definition g_process_child_entries[] = { | ||||
139 | Definition("id", EntryType::ProcessID), | ||||
140 | Definition("name", EntryType::ProcessFile, FileKind::Basename), | ||||
141 | Entry::DefinitionWithChildren("file", EntryType::ProcessFile, | ||||
142 | g_file_child_entries)}; | ||||
143 | |||||
144 | constexpr Definition g_svar_child_entries[] = { | ||||
145 | Definition("*", EntryType::ParentString)}; | ||||
146 | |||||
147 | constexpr Definition g_var_child_entries[] = { | ||||
148 | Definition("*", EntryType::ParentString)}; | ||||
149 | |||||
150 | constexpr Definition g_thread_child_entries[] = { | ||||
151 | Definition("id", EntryType::ThreadID), | ||||
152 | Definition("protocol_id", EntryType::ThreadProtocolID), | ||||
153 | Definition("index", EntryType::ThreadIndexID), | ||||
154 | Entry::DefinitionWithChildren("info", EntryType::ThreadInfo, | ||||
155 | g_string_entry), | ||||
156 | Definition("queue", EntryType::ThreadQueue), | ||||
157 | Definition("name", EntryType::ThreadName), | ||||
158 | Definition("stop-reason", EntryType::ThreadStopReason), | ||||
159 | Definition("stop-reason-raw", EntryType::ThreadStopReasonRaw), | ||||
160 | Definition("return-value", EntryType::ThreadReturnValue), | ||||
161 | Definition("completed-expression", EntryType::ThreadCompletedExpression)}; | ||||
162 | |||||
163 | constexpr Definition g_target_child_entries[] = { | ||||
164 | Definition("arch", EntryType::TargetArch)}; | ||||
165 | |||||
166 | #define _TO_STR2(_val)"_val" #_val | ||||
167 | #define _TO_STR(_val)"_val" _TO_STR2(_val)"_val" | ||||
168 | |||||
169 | constexpr Definition g_ansi_fg_entries[] = { | ||||
170 | Definition("black", | ||||
171 | ANSI_ESC_START"\033[" _TO_STR(ANSI_FG_COLOR_BLACK)"30" ANSI_ESC_END"m"), | ||||
172 | Definition("red", ANSI_ESC_START"\033[" _TO_STR(ANSI_FG_COLOR_RED)"31" ANSI_ESC_END"m"), | ||||
173 | Definition("green", | ||||
174 | ANSI_ESC_START"\033[" _TO_STR(ANSI_FG_COLOR_GREEN)"32" ANSI_ESC_END"m"), | ||||
175 | Definition("yellow", | ||||
176 | ANSI_ESC_START"\033[" _TO_STR(ANSI_FG_COLOR_YELLOW)"33" ANSI_ESC_END"m"), | ||||
177 | Definition("blue", ANSI_ESC_START"\033[" _TO_STR(ANSI_FG_COLOR_BLUE)"34" ANSI_ESC_END"m"), | ||||
178 | Definition("purple", | ||||
179 | ANSI_ESC_START"\033[" _TO_STR(ANSI_FG_COLOR_PURPLE)"35" ANSI_ESC_END"m"), | ||||
180 | Definition("cyan", ANSI_ESC_START"\033[" _TO_STR(ANSI_FG_COLOR_CYAN)"36" ANSI_ESC_END"m"), | ||||
181 | Definition("white", | ||||
182 | ANSI_ESC_START"\033[" _TO_STR(ANSI_FG_COLOR_WHITE)"37" ANSI_ESC_END"m"), | ||||
183 | }; | ||||
184 | |||||
185 | constexpr Definition g_ansi_bg_entries[] = { | ||||
186 | Definition("black", | ||||
187 | ANSI_ESC_START"\033[" _TO_STR(ANSI_BG_COLOR_BLACK)"40" ANSI_ESC_END"m"), | ||||
188 | Definition("red", ANSI_ESC_START"\033[" _TO_STR(ANSI_BG_COLOR_RED)"41" ANSI_ESC_END"m"), | ||||
189 | Definition("green", | ||||
190 | ANSI_ESC_START"\033[" _TO_STR(ANSI_BG_COLOR_GREEN)"42" ANSI_ESC_END"m"), | ||||
191 | Definition("yellow", | ||||
192 | ANSI_ESC_START"\033[" _TO_STR(ANSI_BG_COLOR_YELLOW)"43" ANSI_ESC_END"m"), | ||||
193 | Definition("blue", ANSI_ESC_START"\033[" _TO_STR(ANSI_BG_COLOR_BLUE)"44" ANSI_ESC_END"m"), | ||||
194 | Definition("purple", | ||||
195 | ANSI_ESC_START"\033[" _TO_STR(ANSI_BG_COLOR_PURPLE)"45" ANSI_ESC_END"m"), | ||||
196 | Definition("cyan", ANSI_ESC_START"\033[" _TO_STR(ANSI_BG_COLOR_CYAN)"46" ANSI_ESC_END"m"), | ||||
197 | Definition("white", | ||||
198 | ANSI_ESC_START"\033[" _TO_STR(ANSI_BG_COLOR_WHITE)"47" ANSI_ESC_END"m"), | ||||
199 | }; | ||||
200 | |||||
201 | constexpr Definition g_ansi_entries[] = { | ||||
202 | Entry::DefinitionWithChildren("fg", EntryType::Invalid, g_ansi_fg_entries), | ||||
203 | Entry::DefinitionWithChildren("bg", EntryType::Invalid, g_ansi_bg_entries), | ||||
204 | Definition("normal", ANSI_ESC_START"\033[" _TO_STR(ANSI_CTRL_NORMAL)"0" ANSI_ESC_END"m"), | ||||
205 | Definition("bold", ANSI_ESC_START"\033[" _TO_STR(ANSI_CTRL_BOLD)"1" ANSI_ESC_END"m"), | ||||
206 | Definition("faint", ANSI_ESC_START"\033[" _TO_STR(ANSI_CTRL_FAINT)"2" ANSI_ESC_END"m"), | ||||
207 | Definition("italic", ANSI_ESC_START"\033[" _TO_STR(ANSI_CTRL_ITALIC)"3" ANSI_ESC_END"m"), | ||||
208 | Definition("underline", | ||||
209 | ANSI_ESC_START"\033[" _TO_STR(ANSI_CTRL_UNDERLINE)"4" ANSI_ESC_END"m"), | ||||
210 | Definition("slow-blink", | ||||
211 | ANSI_ESC_START"\033[" _TO_STR(ANSI_CTRL_SLOW_BLINK)"5" ANSI_ESC_END"m"), | ||||
212 | Definition("fast-blink", | ||||
213 | ANSI_ESC_START"\033[" _TO_STR(ANSI_CTRL_FAST_BLINK)"6" ANSI_ESC_END"m"), | ||||
214 | Definition("negative", | ||||
215 | ANSI_ESC_START"\033[" _TO_STR(ANSI_CTRL_IMAGE_NEGATIVE)"7" ANSI_ESC_END"m"), | ||||
216 | Definition("conceal", | ||||
217 | ANSI_ESC_START"\033[" _TO_STR(ANSI_CTRL_CONCEAL)"8" ANSI_ESC_END"m"), | ||||
218 | Definition("crossed-out", | ||||
219 | ANSI_ESC_START"\033[" _TO_STR(ANSI_CTRL_CROSSED_OUT)"9" ANSI_ESC_END"m"), | ||||
220 | }; | ||||
221 | |||||
222 | constexpr Definition g_script_child_entries[] = { | ||||
223 | Definition("frame", EntryType::ScriptFrame), | ||||
224 | Definition("process", EntryType::ScriptProcess), | ||||
225 | Definition("target", EntryType::ScriptTarget), | ||||
226 | Definition("thread", EntryType::ScriptThread), | ||||
227 | Definition("var", EntryType::ScriptVariable), | ||||
228 | Definition("svar", EntryType::ScriptVariableSynthetic), | ||||
229 | Definition("thread", EntryType::ScriptThread)}; | ||||
230 | |||||
231 | constexpr Definition g_top_level_entries[] = { | ||||
232 | Entry::DefinitionWithChildren("addr", EntryType::AddressLoadOrFile, | ||||
233 | g_addr_entries), | ||||
234 | Definition("addr-file-or-load", EntryType::AddressLoadOrFile), | ||||
235 | Entry::DefinitionWithChildren("ansi", EntryType::Invalid, g_ansi_entries), | ||||
236 | Definition("current-pc-arrow", EntryType::CurrentPCArrow), | ||||
237 | Entry::DefinitionWithChildren("file", EntryType::File, | ||||
238 | g_file_child_entries), | ||||
239 | Definition("language", EntryType::Lang), | ||||
240 | Entry::DefinitionWithChildren("frame", EntryType::Invalid, | ||||
241 | g_frame_child_entries), | ||||
242 | Entry::DefinitionWithChildren("function", EntryType::Invalid, | ||||
243 | g_function_child_entries), | ||||
244 | Entry::DefinitionWithChildren("line", EntryType::Invalid, | ||||
245 | g_line_child_entries), | ||||
246 | Entry::DefinitionWithChildren("module", EntryType::Invalid, | ||||
247 | g_module_child_entries), | ||||
248 | Entry::DefinitionWithChildren("process", EntryType::Invalid, | ||||
249 | g_process_child_entries), | ||||
250 | Entry::DefinitionWithChildren("script", EntryType::Invalid, | ||||
251 | g_script_child_entries), | ||||
252 | Entry::DefinitionWithChildren("svar", EntryType::VariableSynthetic, | ||||
253 | g_svar_child_entries, true), | ||||
254 | Entry::DefinitionWithChildren("thread", EntryType::Invalid, | ||||
255 | g_thread_child_entries), | ||||
256 | Entry::DefinitionWithChildren("target", EntryType::Invalid, | ||||
257 | g_target_child_entries), | ||||
258 | Entry::DefinitionWithChildren("var", EntryType::Variable, | ||||
259 | g_var_child_entries, true)}; | ||||
260 | |||||
261 | constexpr Definition g_root = Entry::DefinitionWithChildren( | ||||
262 | "<root>", EntryType::Root, g_top_level_entries); | ||||
263 | |||||
264 | FormatEntity::Entry::Entry(llvm::StringRef s) | ||||
265 | : string(s.data(), s.size()), printf_format(), children(), | ||||
266 | type(Type::String) {} | ||||
267 | |||||
268 | FormatEntity::Entry::Entry(char ch) | ||||
269 | : string(1, ch), printf_format(), children(), type(Type::String) {} | ||||
270 | |||||
271 | void FormatEntity::Entry::AppendChar(char ch) { | ||||
272 | if (children.empty() || children.back().type != Entry::Type::String) | ||||
273 | children.push_back(Entry(ch)); | ||||
274 | else | ||||
275 | children.back().string.append(1, ch); | ||||
276 | } | ||||
277 | |||||
278 | void FormatEntity::Entry::AppendText(const llvm::StringRef &s) { | ||||
279 | if (children.empty() || children.back().type != Entry::Type::String) | ||||
280 | children.push_back(Entry(s)); | ||||
281 | else | ||||
282 | children.back().string.append(s.data(), s.size()); | ||||
283 | } | ||||
284 | |||||
285 | void FormatEntity::Entry::AppendText(const char *cstr) { | ||||
286 | return AppendText(llvm::StringRef(cstr)); | ||||
287 | } | ||||
288 | |||||
289 | Status FormatEntity::Parse(const llvm::StringRef &format_str, Entry &entry) { | ||||
290 | entry.Clear(); | ||||
291 | entry.type = Entry::Type::Root; | ||||
292 | llvm::StringRef modifiable_format(format_str); | ||||
293 | return ParseInternal(modifiable_format, entry, 0); | ||||
294 | } | ||||
295 | |||||
296 | #define ENUM_TO_CSTR(eee) \ | ||||
297 | case FormatEntity::Entry::Type::eee: \ | ||||
298 | return #eee | ||||
299 | |||||
300 | const char *FormatEntity::Entry::TypeToCString(Type t) { | ||||
301 | switch (t) { | ||||
302 | ENUM_TO_CSTR(Invalid); | ||||
303 | ENUM_TO_CSTR(ParentNumber); | ||||
304 | ENUM_TO_CSTR(ParentString); | ||||
305 | ENUM_TO_CSTR(EscapeCode); | ||||
306 | ENUM_TO_CSTR(Root); | ||||
307 | ENUM_TO_CSTR(String); | ||||
308 | ENUM_TO_CSTR(Scope); | ||||
309 | ENUM_TO_CSTR(Variable); | ||||
310 | ENUM_TO_CSTR(VariableSynthetic); | ||||
311 | ENUM_TO_CSTR(ScriptVariable); | ||||
312 | ENUM_TO_CSTR(ScriptVariableSynthetic); | ||||
313 | ENUM_TO_CSTR(AddressLoad); | ||||
314 | ENUM_TO_CSTR(AddressFile); | ||||
315 | ENUM_TO_CSTR(AddressLoadOrFile); | ||||
316 | ENUM_TO_CSTR(ProcessID); | ||||
317 | ENUM_TO_CSTR(ProcessFile); | ||||
318 | ENUM_TO_CSTR(ScriptProcess); | ||||
319 | ENUM_TO_CSTR(ThreadID); | ||||
320 | ENUM_TO_CSTR(ThreadProtocolID); | ||||
321 | ENUM_TO_CSTR(ThreadIndexID); | ||||
322 | ENUM_TO_CSTR(ThreadName); | ||||
323 | ENUM_TO_CSTR(ThreadQueue); | ||||
324 | ENUM_TO_CSTR(ThreadStopReason); | ||||
325 | ENUM_TO_CSTR(ThreadStopReasonRaw); | ||||
326 | ENUM_TO_CSTR(ThreadReturnValue); | ||||
327 | ENUM_TO_CSTR(ThreadCompletedExpression); | ||||
328 | ENUM_TO_CSTR(ScriptThread); | ||||
329 | ENUM_TO_CSTR(ThreadInfo); | ||||
330 | ENUM_TO_CSTR(TargetArch); | ||||
331 | ENUM_TO_CSTR(ScriptTarget); | ||||
332 | ENUM_TO_CSTR(ModuleFile); | ||||
333 | ENUM_TO_CSTR(File); | ||||
334 | ENUM_TO_CSTR(Lang); | ||||
335 | ENUM_TO_CSTR(FrameIndex); | ||||
336 | ENUM_TO_CSTR(FrameNoDebug); | ||||
337 | ENUM_TO_CSTR(FrameRegisterPC); | ||||
338 | ENUM_TO_CSTR(FrameRegisterSP); | ||||
339 | ENUM_TO_CSTR(FrameRegisterFP); | ||||
340 | ENUM_TO_CSTR(FrameRegisterFlags); | ||||
341 | ENUM_TO_CSTR(FrameRegisterByName); | ||||
342 | ENUM_TO_CSTR(FrameIsArtificial); | ||||
343 | ENUM_TO_CSTR(ScriptFrame); | ||||
344 | ENUM_TO_CSTR(FunctionID); | ||||
345 | ENUM_TO_CSTR(FunctionDidChange); | ||||
346 | ENUM_TO_CSTR(FunctionInitialFunction); | ||||
347 | ENUM_TO_CSTR(FunctionName); | ||||
348 | ENUM_TO_CSTR(FunctionNameWithArgs); | ||||
349 | ENUM_TO_CSTR(FunctionNameNoArgs); | ||||
350 | ENUM_TO_CSTR(FunctionMangledName); | ||||
351 | ENUM_TO_CSTR(FunctionAddrOffset); | ||||
352 | ENUM_TO_CSTR(FunctionAddrOffsetConcrete); | ||||
353 | ENUM_TO_CSTR(FunctionLineOffset); | ||||
354 | ENUM_TO_CSTR(FunctionPCOffset); | ||||
355 | ENUM_TO_CSTR(FunctionInitial); | ||||
356 | ENUM_TO_CSTR(FunctionChanged); | ||||
357 | ENUM_TO_CSTR(FunctionIsOptimized); | ||||
358 | ENUM_TO_CSTR(LineEntryFile); | ||||
359 | ENUM_TO_CSTR(LineEntryLineNumber); | ||||
360 | ENUM_TO_CSTR(LineEntryColumn); | ||||
361 | ENUM_TO_CSTR(LineEntryStartAddress); | ||||
362 | ENUM_TO_CSTR(LineEntryEndAddress); | ||||
363 | ENUM_TO_CSTR(CurrentPCArrow); | ||||
364 | } | ||||
365 | return "???"; | ||||
366 | } | ||||
367 | |||||
368 | #undef ENUM_TO_CSTR | ||||
369 | |||||
370 | void FormatEntity::Entry::Dump(Stream &s, int depth) const { | ||||
371 | s.Printf("%*.*s%-20s: ", depth * 2, depth * 2, "", TypeToCString(type)); | ||||
372 | if (fmt != eFormatDefault) | ||||
373 | s.Printf("lldb-format = %s, ", FormatManager::GetFormatAsCString(fmt)); | ||||
374 | if (!string.empty()) | ||||
375 | s.Printf("string = \"%s\"", string.c_str()); | ||||
376 | if (!printf_format.empty()) | ||||
377 | s.Printf("printf_format = \"%s\"", printf_format.c_str()); | ||||
378 | if (number != 0) | ||||
379 | s.Printf("number = %" PRIu64"l" "u" " (0x%" PRIx64"l" "x" "), ", number, number); | ||||
380 | if (deref) | ||||
381 | s.Printf("deref = true, "); | ||||
382 | s.EOL(); | ||||
383 | for (const auto &child : children) { | ||||
384 | child.Dump(s, depth + 1); | ||||
385 | } | ||||
386 | } | ||||
387 | |||||
388 | template <typename T> | ||||
389 | static bool RunScriptFormatKeyword(Stream &s, const SymbolContext *sc, | ||||
390 | const ExecutionContext *exe_ctx, T t, | ||||
391 | const char *script_function_name) { | ||||
392 | Target *target = Target::GetTargetFromContexts(exe_ctx, sc); | ||||
393 | |||||
394 | if (target) { | ||||
395 | ScriptInterpreter *script_interpreter = | ||||
396 | target->GetDebugger().GetScriptInterpreter(); | ||||
397 | if (script_interpreter) { | ||||
398 | Status error; | ||||
399 | std::string script_output; | ||||
400 | |||||
401 | if (script_interpreter->RunScriptFormatKeyword(script_function_name, t, | ||||
402 | script_output, error) && | ||||
403 | error.Success()) { | ||||
404 | s.Printf("%s", script_output.c_str()); | ||||
405 | return true; | ||||
406 | } else { | ||||
407 | s.Printf("<error: %s>", error.AsCString()); | ||||
408 | } | ||||
409 | } | ||||
410 | } | ||||
411 | return false; | ||||
412 | } | ||||
413 | |||||
414 | static bool DumpAddressAndContent(Stream &s, const SymbolContext *sc, | ||||
415 | const ExecutionContext *exe_ctx, | ||||
416 | const Address &addr, | ||||
417 | bool print_file_addr_or_load_addr) { | ||||
418 | Target *target = Target::GetTargetFromContexts(exe_ctx, sc); | ||||
419 | addr_t vaddr = LLDB_INVALID_ADDRESS(18446744073709551615UL); | ||||
420 | if (exe_ctx && !target->GetSectionLoadList().IsEmpty()) | ||||
421 | vaddr = addr.GetLoadAddress(target); | ||||
422 | if (vaddr == LLDB_INVALID_ADDRESS(18446744073709551615UL)) | ||||
423 | vaddr = addr.GetFileAddress(); | ||||
424 | |||||
425 | if (vaddr != LLDB_INVALID_ADDRESS(18446744073709551615UL)) { | ||||
426 | int addr_width = 0; | ||||
427 | if (exe_ctx && target) { | ||||
428 | addr_width = target->GetArchitecture().GetAddressByteSize() * 2; | ||||
429 | } | ||||
430 | if (addr_width == 0) | ||||
431 | addr_width = 16; | ||||
432 | if (print_file_addr_or_load_addr) { | ||||
433 | ExecutionContextScope *exe_scope = nullptr; | ||||
434 | if (exe_ctx) | ||||
435 | exe_scope = exe_ctx->GetBestExecutionContextScope(); | ||||
436 | addr.Dump(&s, exe_scope, Address::DumpStyleLoadAddress, | ||||
437 | Address::DumpStyleModuleWithFileAddress, 0); | ||||
438 | } else { | ||||
439 | s.Printf("0x%*.*" PRIx64"l" "x", addr_width, addr_width, vaddr); | ||||
440 | } | ||||
441 | return true; | ||||
442 | } | ||||
443 | return false; | ||||
444 | } | ||||
445 | |||||
446 | static bool DumpAddressOffsetFromFunction(Stream &s, const SymbolContext *sc, | ||||
447 | const ExecutionContext *exe_ctx, | ||||
448 | const Address &format_addr, | ||||
449 | bool concrete_only, bool no_padding, | ||||
450 | bool print_zero_offsets) { | ||||
451 | if (format_addr.IsValid()) { | ||||
452 | Address func_addr; | ||||
453 | |||||
454 | if (sc) { | ||||
455 | if (sc->function) { | ||||
456 | func_addr = sc->function->GetAddressRange().GetBaseAddress(); | ||||
457 | if (sc->block && !concrete_only) { | ||||
458 | // Check to make sure we aren't in an inline function. If we are, use | ||||
459 | // the inline block range that contains "format_addr" since blocks | ||||
460 | // can be discontiguous. | ||||
461 | Block *inline_block = sc->block->GetContainingInlinedBlock(); | ||||
462 | AddressRange inline_range; | ||||
463 | if (inline_block && inline_block->GetRangeContainingAddress( | ||||
464 | format_addr, inline_range)) | ||||
465 | func_addr = inline_range.GetBaseAddress(); | ||||
466 | } | ||||
467 | } else if (sc->symbol && sc->symbol->ValueIsAddress()) | ||||
468 | func_addr = sc->symbol->GetAddressRef(); | ||||
469 | } | ||||
470 | |||||
471 | if (func_addr.IsValid()) { | ||||
472 | const char *addr_offset_padding = no_padding ? "" : " "; | ||||
473 | |||||
474 | if (func_addr.GetSection() == format_addr.GetSection()) { | ||||
475 | addr_t func_file_addr = func_addr.GetFileAddress(); | ||||
476 | addr_t addr_file_addr = format_addr.GetFileAddress(); | ||||
477 | if (addr_file_addr > func_file_addr || | ||||
478 | (addr_file_addr == func_file_addr && print_zero_offsets)) { | ||||
479 | s.Printf("%s+%s%" PRIu64"l" "u", addr_offset_padding, addr_offset_padding, | ||||
480 | addr_file_addr - func_file_addr); | ||||
481 | } else if (addr_file_addr < func_file_addr) { | ||||
482 | s.Printf("%s-%s%" PRIu64"l" "u", addr_offset_padding, addr_offset_padding, | ||||
483 | func_file_addr - addr_file_addr); | ||||
484 | } | ||||
485 | return true; | ||||
486 | } else { | ||||
487 | Target *target = Target::GetTargetFromContexts(exe_ctx, sc); | ||||
488 | if (target) { | ||||
489 | addr_t func_load_addr = func_addr.GetLoadAddress(target); | ||||
490 | addr_t addr_load_addr = format_addr.GetLoadAddress(target); | ||||
491 | if (addr_load_addr > func_load_addr || | ||||
492 | (addr_load_addr == func_load_addr && print_zero_offsets)) { | ||||
493 | s.Printf("%s+%s%" PRIu64"l" "u", addr_offset_padding, addr_offset_padding, | ||||
494 | addr_load_addr - func_load_addr); | ||||
495 | } else if (addr_load_addr < func_load_addr) { | ||||
496 | s.Printf("%s-%s%" PRIu64"l" "u", addr_offset_padding, addr_offset_padding, | ||||
497 | func_load_addr - addr_load_addr); | ||||
498 | } | ||||
499 | return true; | ||||
500 | } | ||||
501 | } | ||||
502 | } | ||||
503 | } | ||||
504 | return false; | ||||
505 | } | ||||
506 | |||||
507 | static bool ScanBracketedRange(llvm::StringRef subpath, | ||||
508 | size_t &close_bracket_index, | ||||
509 | const char *&var_name_final_if_array_range, | ||||
510 | int64_t &index_lower, int64_t &index_higher) { | ||||
511 | Log *log = GetLog(LLDBLog::DataFormatters); | ||||
512 | close_bracket_index = llvm::StringRef::npos; | ||||
513 | const size_t open_bracket_index = subpath.find('['); | ||||
514 | if (open_bracket_index == llvm::StringRef::npos) { | ||||
515 | LLDB_LOGF(log,do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Printf("[ScanBracketedRange] no bracketed range, skipping entirely" ); } while (0) | ||||
516 | "[ScanBracketedRange] no bracketed range, skipping entirely")do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Printf("[ScanBracketedRange] no bracketed range, skipping entirely" ); } while (0); | ||||
517 | return false; | ||||
518 | } | ||||
519 | |||||
520 | close_bracket_index = subpath.find(']', open_bracket_index + 1); | ||||
521 | |||||
522 | if (close_bracket_index == llvm::StringRef::npos) { | ||||
523 | LLDB_LOGF(log,do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Printf("[ScanBracketedRange] no bracketed range, skipping entirely" ); } while (0) | ||||
524 | "[ScanBracketedRange] no bracketed range, skipping entirely")do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Printf("[ScanBracketedRange] no bracketed range, skipping entirely" ); } while (0); | ||||
525 | return false; | ||||
526 | } else { | ||||
527 | var_name_final_if_array_range = subpath.data() + open_bracket_index; | ||||
528 | |||||
529 | if (close_bracket_index - open_bracket_index == 1) { | ||||
530 | LLDB_LOGF(do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Printf("[ScanBracketedRange] '[]' detected.. going from 0 to end of data" ); } while (0) | ||||
531 | log,do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Printf("[ScanBracketedRange] '[]' detected.. going from 0 to end of data" ); } while (0) | ||||
532 | "[ScanBracketedRange] '[]' detected.. going from 0 to end of data")do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Printf("[ScanBracketedRange] '[]' detected.. going from 0 to end of data" ); } while (0); | ||||
533 | index_lower = 0; | ||||
534 | } else { | ||||
535 | const size_t separator_index = subpath.find('-', open_bracket_index + 1); | ||||
536 | |||||
537 | if (separator_index == llvm::StringRef::npos) { | ||||
538 | const char *index_lower_cstr = subpath.data() + open_bracket_index + 1; | ||||
539 | index_lower = ::strtoul(index_lower_cstr, nullptr, 0); | ||||
540 | index_higher = index_lower; | ||||
541 | LLDB_LOGF(log,do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Printf("[ScanBracketedRange] [%" "l" "d" "] detected, high index is same" , index_lower); } while (0) | ||||
542 | "[ScanBracketedRange] [%" PRId64do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Printf("[ScanBracketedRange] [%" "l" "d" "] detected, high index is same" , index_lower); } while (0) | ||||
543 | "] detected, high index is same",do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Printf("[ScanBracketedRange] [%" "l" "d" "] detected, high index is same" , index_lower); } while (0) | ||||
544 | index_lower)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Printf("[ScanBracketedRange] [%" "l" "d" "] detected, high index is same" , index_lower); } while (0); | ||||
545 | } else { | ||||
546 | const char *index_lower_cstr = subpath.data() + open_bracket_index + 1; | ||||
547 | const char *index_higher_cstr = subpath.data() + separator_index + 1; | ||||
548 | index_lower = ::strtoul(index_lower_cstr, nullptr, 0); | ||||
549 | index_higher = ::strtoul(index_higher_cstr, nullptr, 0); | ||||
550 | LLDB_LOGF(log,do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Printf("[ScanBracketedRange] [%" "l" "d" "-%" "l" "d" "] detected", index_lower, index_higher); } while (0 ) | ||||
551 | "[ScanBracketedRange] [%" PRId64 "-%" PRId64 "] detected",do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Printf("[ScanBracketedRange] [%" "l" "d" "-%" "l" "d" "] detected", index_lower, index_higher); } while (0 ) | ||||
552 | index_lower, index_higher)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Printf("[ScanBracketedRange] [%" "l" "d" "-%" "l" "d" "] detected", index_lower, index_higher); } while (0 ); | ||||
553 | } | ||||
554 | if (index_lower > index_higher && index_higher > 0) { | ||||
555 | LLDB_LOGF(log, "[ScanBracketedRange] swapping indices")do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Printf("[ScanBracketedRange] swapping indices" ); } while (0); | ||||
556 | const int64_t temp = index_lower; | ||||
557 | index_lower = index_higher; | ||||
558 | index_higher = temp; | ||||
559 | } | ||||
560 | } | ||||
561 | } | ||||
562 | return true; | ||||
563 | } | ||||
564 | |||||
565 | static bool DumpFile(Stream &s, const FileSpec &file, FileKind file_kind) { | ||||
566 | switch (file_kind) { | ||||
567 | case FileKind::FileError: | ||||
568 | break; | ||||
569 | |||||
570 | case FileKind::Basename: | ||||
571 | if (file.GetFilename()) { | ||||
572 | s << file.GetFilename(); | ||||
573 | return true; | ||||
574 | } | ||||
575 | break; | ||||
576 | |||||
577 | case FileKind::Dirname: | ||||
578 | if (file.GetDirectory()) { | ||||
579 | s << file.GetDirectory(); | ||||
580 | return true; | ||||
581 | } | ||||
582 | break; | ||||
583 | |||||
584 | case FileKind::Fullpath: | ||||
585 | if (file) { | ||||
586 | s << file; | ||||
587 | return true; | ||||
588 | } | ||||
589 | break; | ||||
590 | } | ||||
591 | return false; | ||||
592 | } | ||||
593 | |||||
594 | static bool DumpRegister(Stream &s, StackFrame *frame, RegisterKind reg_kind, | ||||
595 | uint32_t reg_num, Format format) { | ||||
596 | if (frame) { | ||||
597 | RegisterContext *reg_ctx = frame->GetRegisterContext().get(); | ||||
598 | |||||
599 | if (reg_ctx) { | ||||
600 | const uint32_t lldb_reg_num = | ||||
601 | reg_ctx->ConvertRegisterKindToRegisterNumber(reg_kind, reg_num); | ||||
602 | if (lldb_reg_num != LLDB_INVALID_REGNUM(4294967295U)) { | ||||
603 | const RegisterInfo *reg_info = | ||||
604 | reg_ctx->GetRegisterInfoAtIndex(lldb_reg_num); | ||||
605 | if (reg_info) { | ||||
606 | RegisterValue reg_value; | ||||
607 | if (reg_ctx->ReadRegister(reg_info, reg_value)) { | ||||
608 | DumpRegisterValue(reg_value, &s, reg_info, false, false, format); | ||||
609 | return true; | ||||
610 | } | ||||
611 | } | ||||
612 | } | ||||
613 | } | ||||
614 | } | ||||
615 | return false; | ||||
616 | } | ||||
617 | |||||
618 | static ValueObjectSP ExpandIndexedExpression(ValueObject *valobj, size_t index, | ||||
619 | bool deref_pointer) { | ||||
620 | Log *log = GetLog(LLDBLog::DataFormatters); | ||||
621 | const char *ptr_deref_format = "[%d]"; | ||||
622 | std::string ptr_deref_buffer(10, 0); | ||||
623 | ::sprintf(&ptr_deref_buffer[0], ptr_deref_format, index); | ||||
624 | LLDB_LOGF(log, "[ExpandIndexedExpression] name to deref: %s",do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Printf("[ExpandIndexedExpression] name to deref: %s" , ptr_deref_buffer.c_str()); } while (0) | ||||
625 | ptr_deref_buffer.c_str())do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Printf("[ExpandIndexedExpression] name to deref: %s" , ptr_deref_buffer.c_str()); } while (0); | ||||
626 | ValueObject::GetValueForExpressionPathOptions options; | ||||
627 | ValueObject::ExpressionPathEndResultType final_value_type; | ||||
628 | ValueObject::ExpressionPathScanEndReason reason_to_stop; | ||||
629 | ValueObject::ExpressionPathAftermath what_next = | ||||
630 | (deref_pointer ? ValueObject::eExpressionPathAftermathDereference | ||||
631 | : ValueObject::eExpressionPathAftermathNothing); | ||||
632 | ValueObjectSP item = valobj->GetValueForExpressionPath( | ||||
633 | ptr_deref_buffer.c_str(), &reason_to_stop, &final_value_type, options, | ||||
634 | &what_next); | ||||
635 | if (!item) { | ||||
636 | LLDB_LOGF(log,do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Printf("[ExpandIndexedExpression] ERROR: why stopping = %d," " final_value_type %d", reason_to_stop, final_value_type); } while (0) | ||||
637 | "[ExpandIndexedExpression] ERROR: why stopping = %d,"do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Printf("[ExpandIndexedExpression] ERROR: why stopping = %d," " final_value_type %d", reason_to_stop, final_value_type); } while (0) | ||||
638 | " final_value_type %d",do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Printf("[ExpandIndexedExpression] ERROR: why stopping = %d," " final_value_type %d", reason_to_stop, final_value_type); } while (0) | ||||
639 | reason_to_stop, final_value_type)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Printf("[ExpandIndexedExpression] ERROR: why stopping = %d," " final_value_type %d", reason_to_stop, final_value_type); } while (0); | ||||
640 | } else { | ||||
641 | LLDB_LOGF(log,do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Printf("[ExpandIndexedExpression] ALL RIGHT: why stopping = %d," " final_value_type %d", reason_to_stop, final_value_type); } while (0) | ||||
642 | "[ExpandIndexedExpression] ALL RIGHT: why stopping = %d,"do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Printf("[ExpandIndexedExpression] ALL RIGHT: why stopping = %d," " final_value_type %d", reason_to_stop, final_value_type); } while (0) | ||||
643 | " final_value_type %d",do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Printf("[ExpandIndexedExpression] ALL RIGHT: why stopping = %d," " final_value_type %d", reason_to_stop, final_value_type); } while (0) | ||||
644 | reason_to_stop, final_value_type)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Printf("[ExpandIndexedExpression] ALL RIGHT: why stopping = %d," " final_value_type %d", reason_to_stop, final_value_type); } while (0); | ||||
645 | } | ||||
646 | return item; | ||||
647 | } | ||||
648 | |||||
649 | static char ConvertValueObjectStyleToChar( | ||||
650 | ValueObject::ValueObjectRepresentationStyle style) { | ||||
651 | switch (style) { | ||||
652 | case ValueObject::eValueObjectRepresentationStyleLanguageSpecific: | ||||
653 | return '@'; | ||||
654 | case ValueObject::eValueObjectRepresentationStyleValue: | ||||
655 | return 'V'; | ||||
656 | case ValueObject::eValueObjectRepresentationStyleLocation: | ||||
657 | return 'L'; | ||||
658 | case ValueObject::eValueObjectRepresentationStyleSummary: | ||||
659 | return 'S'; | ||||
660 | case ValueObject::eValueObjectRepresentationStyleChildrenCount: | ||||
661 | return '#'; | ||||
662 | case ValueObject::eValueObjectRepresentationStyleType: | ||||
663 | return 'T'; | ||||
664 | case ValueObject::eValueObjectRepresentationStyleName: | ||||
665 | return 'N'; | ||||
666 | case ValueObject::eValueObjectRepresentationStyleExpressionPath: | ||||
667 | return '>'; | ||||
668 | } | ||||
669 | return '\0'; | ||||
670 | } | ||||
671 | |||||
672 | static bool DumpValue(Stream &s, const SymbolContext *sc, | ||||
673 | const ExecutionContext *exe_ctx, | ||||
674 | const FormatEntity::Entry &entry, ValueObject *valobj) { | ||||
675 | if (valobj == nullptr) | ||||
| |||||
676 | return false; | ||||
677 | |||||
678 | Log *log = GetLog(LLDBLog::DataFormatters); | ||||
679 | Format custom_format = eFormatInvalid; | ||||
680 | ValueObject::ValueObjectRepresentationStyle val_obj_display = | ||||
681 | entry.string.empty() | ||||
682 | ? ValueObject::eValueObjectRepresentationStyleValue | ||||
683 | : ValueObject::eValueObjectRepresentationStyleSummary; | ||||
684 | |||||
685 | bool do_deref_pointer = entry.deref; | ||||
686 | bool is_script = false; | ||||
687 | switch (entry.type) { | ||||
688 | case FormatEntity::Entry::Type::ScriptVariable: | ||||
689 | is_script = true; | ||||
690 | break; | ||||
691 | |||||
692 | case FormatEntity::Entry::Type::Variable: | ||||
693 | custom_format = entry.fmt; | ||||
694 | val_obj_display = (ValueObject::ValueObjectRepresentationStyle)entry.number; | ||||
695 | break; | ||||
696 | |||||
697 | case FormatEntity::Entry::Type::ScriptVariableSynthetic: | ||||
698 | is_script = true; | ||||
699 | [[fallthrough]]; | ||||
700 | case FormatEntity::Entry::Type::VariableSynthetic: | ||||
701 | custom_format = entry.fmt; | ||||
702 | val_obj_display = (ValueObject::ValueObjectRepresentationStyle)entry.number; | ||||
703 | if (!valobj->IsSynthetic()) { | ||||
704 | valobj = valobj->GetSyntheticValue().get(); | ||||
705 | if (valobj == nullptr) | ||||
706 | return false; | ||||
707 | } | ||||
708 | break; | ||||
709 | |||||
710 | default: | ||||
711 | return false; | ||||
712 | } | ||||
713 | |||||
714 | ValueObject::ExpressionPathAftermath what_next = | ||||
715 | (do_deref_pointer ? ValueObject::eExpressionPathAftermathDereference | ||||
716 | : ValueObject::eExpressionPathAftermathNothing); | ||||
717 | ValueObject::GetValueForExpressionPathOptions options; | ||||
718 | options.DontCheckDotVsArrowSyntax() | ||||
719 | .DoAllowBitfieldSyntax() | ||||
720 | .DoAllowFragileIVar() | ||||
721 | .SetSyntheticChildrenTraversal( | ||||
722 | ValueObject::GetValueForExpressionPathOptions:: | ||||
723 | SyntheticChildrenTraversal::Both); | ||||
724 | ValueObject *target = nullptr; | ||||
725 | const char *var_name_final_if_array_range = nullptr; | ||||
726 | size_t close_bracket_index = llvm::StringRef::npos; | ||||
727 | int64_t index_lower = -1; | ||||
728 | int64_t index_higher = -1; | ||||
729 | bool is_array_range = false; | ||||
730 | bool was_plain_var = false; | ||||
731 | bool was_var_format = false; | ||||
732 | bool was_var_indexed = false; | ||||
733 | ValueObject::ExpressionPathScanEndReason reason_to_stop = | ||||
734 | ValueObject::eExpressionPathScanEndReasonEndOfString; | ||||
735 | ValueObject::ExpressionPathEndResultType final_value_type = | ||||
736 | ValueObject::eExpressionPathEndResultTypePlain; | ||||
737 | |||||
738 | if (is_script
| ||||
739 | return RunScriptFormatKeyword(s, sc, exe_ctx, valobj, entry.string.c_str()); | ||||
740 | } | ||||
741 | |||||
742 | llvm::StringRef subpath(entry.string); | ||||
743 | // simplest case ${var}, just print valobj's value | ||||
744 | if (entry.string.empty()) { | ||||
745 | if (entry.printf_format.empty() && entry.fmt == eFormatDefault && | ||||
746 | entry.number == ValueObject::eValueObjectRepresentationStyleValue) | ||||
747 | was_plain_var = true; | ||||
748 | else | ||||
749 | was_var_format = true; | ||||
750 | target = valobj; | ||||
751 | } else // this is ${var.something} or multiple .something nested | ||||
752 | { | ||||
753 | if (entry.string[0] == '[') | ||||
754 | was_var_indexed = true; | ||||
755 | ScanBracketedRange(subpath, close_bracket_index, | ||||
756 | var_name_final_if_array_range, index_lower, | ||||
757 | index_higher); | ||||
758 | |||||
759 | Status error; | ||||
760 | |||||
761 | const std::string &expr_path = entry.string; | ||||
762 | |||||
763 | LLDB_LOGF(log, "[Debugger::FormatPrompt] symbol to expand: %s",do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Printf("[Debugger::FormatPrompt] symbol to expand: %s" , expr_path.c_str()); } while (0) | ||||
764 | expr_path.c_str())do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Printf("[Debugger::FormatPrompt] symbol to expand: %s" , expr_path.c_str()); } while (0); | ||||
765 | |||||
766 | target = | ||||
767 | valobj | ||||
768 | ->GetValueForExpressionPath(expr_path.c_str(), &reason_to_stop, | ||||
769 | &final_value_type, options, &what_next) | ||||
770 | .get(); | ||||
771 | |||||
772 | if (!target) { | ||||
773 | LLDB_LOGF(log,do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Printf("[Debugger::FormatPrompt] ERROR: why stopping = %d," " final_value_type %d", reason_to_stop, final_value_type); } while (0) | ||||
774 | "[Debugger::FormatPrompt] ERROR: why stopping = %d,"do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Printf("[Debugger::FormatPrompt] ERROR: why stopping = %d," " final_value_type %d", reason_to_stop, final_value_type); } while (0) | ||||
775 | " final_value_type %d",do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Printf("[Debugger::FormatPrompt] ERROR: why stopping = %d," " final_value_type %d", reason_to_stop, final_value_type); } while (0) | ||||
776 | reason_to_stop, final_value_type)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Printf("[Debugger::FormatPrompt] ERROR: why stopping = %d," " final_value_type %d", reason_to_stop, final_value_type); } while (0); | ||||
777 | return false; | ||||
778 | } else { | ||||
779 | LLDB_LOGF(log,do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Printf("[Debugger::FormatPrompt] ALL RIGHT: why stopping = %d," " final_value_type %d", reason_to_stop, final_value_type); } while (0) | ||||
780 | "[Debugger::FormatPrompt] ALL RIGHT: why stopping = %d,"do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Printf("[Debugger::FormatPrompt] ALL RIGHT: why stopping = %d," " final_value_type %d", reason_to_stop, final_value_type); } while (0) | ||||
781 | " final_value_type %d",do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Printf("[Debugger::FormatPrompt] ALL RIGHT: why stopping = %d," " final_value_type %d", reason_to_stop, final_value_type); } while (0) | ||||
782 | reason_to_stop, final_value_type)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Printf("[Debugger::FormatPrompt] ALL RIGHT: why stopping = %d," " final_value_type %d", reason_to_stop, final_value_type); } while (0); | ||||
783 | target = target | ||||
784 | ->GetQualifiedRepresentationIfAvailable( | ||||
785 | target->GetDynamicValueType(), true) | ||||
786 | .get(); | ||||
787 | } | ||||
788 | } | ||||
789 | |||||
790 | is_array_range = | ||||
791 | (final_value_type == | ||||
792 | ValueObject::eExpressionPathEndResultTypeBoundedRange || | ||||
793 | final_value_type == | ||||
794 | ValueObject::eExpressionPathEndResultTypeUnboundedRange); | ||||
795 | |||||
796 | do_deref_pointer = | ||||
797 | (what_next == ValueObject::eExpressionPathAftermathDereference); | ||||
798 | |||||
799 | if (do_deref_pointer
| ||||
800 | // I have not deref-ed yet, let's do it | ||||
801 | // this happens when we are not going through | ||||
802 | // GetValueForVariableExpressionPath to get to the target ValueObject | ||||
803 | Status error; | ||||
804 | target = target->Dereference(error).get(); | ||||
805 | if (error.Fail()) { | ||||
806 | LLDB_LOGF(log, "[Debugger::FormatPrompt] ERROR: %s\n",do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Printf("[Debugger::FormatPrompt] ERROR: %s\n" , error.AsCString("unknown")); } while (0) | ||||
807 | error.AsCString("unknown"))do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Printf("[Debugger::FormatPrompt] ERROR: %s\n" , error.AsCString("unknown")); } while (0); | ||||
808 | return false; | ||||
809 | } | ||||
810 | do_deref_pointer = false; | ||||
811 | } | ||||
812 | |||||
813 | if (!target) { | ||||
814 | LLDB_LOGF(log, "[Debugger::FormatPrompt] could not calculate target for "do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Printf("[Debugger::FormatPrompt] could not calculate target for " "prompt expression"); } while (0) | ||||
815 | "prompt expression")do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Printf("[Debugger::FormatPrompt] could not calculate target for " "prompt expression"); } while (0); | ||||
816 | return false; | ||||
817 | } | ||||
818 | |||||
819 | // we do not want to use the summary for a bitfield of type T:n if we were | ||||
820 | // originally dealing with just a T - that would get us into an endless | ||||
821 | // recursion | ||||
822 | if (target->IsBitfield() && was_var_indexed) { | ||||
823 | // TODO: check for a (T:n)-specific summary - we should still obey that | ||||
824 | StreamString bitfield_name; | ||||
825 | bitfield_name.Printf("%s:%d", target->GetTypeName().AsCString(), | ||||
826 | target->GetBitfieldBitSize()); | ||||
827 | auto type_sp = std::make_shared<TypeNameSpecifierImpl>( | ||||
828 | bitfield_name.GetString(), lldb::eFormatterMatchExact); | ||||
829 | if (val_obj_display == | ||||
830 | ValueObject::eValueObjectRepresentationStyleSummary && | ||||
831 | !DataVisualization::GetSummaryForType(type_sp)) | ||||
832 | val_obj_display = ValueObject::eValueObjectRepresentationStyleValue; | ||||
833 | } | ||||
834 | |||||
835 | // TODO use flags for these | ||||
836 | const uint32_t type_info_flags = | ||||
837 | target->GetCompilerType().GetTypeInfo(nullptr); | ||||
838 | bool is_array = (type_info_flags & eTypeIsArray) != 0; | ||||
839 | bool is_pointer = (type_info_flags & eTypeIsPointer) != 0; | ||||
840 | bool is_aggregate = target->GetCompilerType().IsAggregateType(); | ||||
841 | |||||
842 | if ((is_array
| ||||
843 | val_obj_display == | ||||
844 | ValueObject::eValueObjectRepresentationStyleValue) // this should be | ||||
845 | // wrong, but there | ||||
846 | // are some | ||||
847 | // exceptions | ||||
848 | { | ||||
849 | StreamString str_temp; | ||||
850 | LLDB_LOGF(log,do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Printf("[Debugger::FormatPrompt] I am into array || pointer && !range" ); } while (0) | ||||
851 | "[Debugger::FormatPrompt] I am into array || pointer && !range")do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Printf("[Debugger::FormatPrompt] I am into array || pointer && !range" ); } while (0); | ||||
852 | |||||
853 | if (target->HasSpecialPrintableRepresentation(val_obj_display, | ||||
854 | custom_format)) { | ||||
855 | // try to use the special cases | ||||
856 | bool success = target->DumpPrintableRepresentation( | ||||
857 | str_temp, val_obj_display, custom_format); | ||||
858 | LLDB_LOGF(log, "[Debugger::FormatPrompt] special cases did%s match",do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Printf("[Debugger::FormatPrompt] special cases did%s match" , success ? "" : "n't"); } while (0) | ||||
859 | success ? "" : "n't")do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Printf("[Debugger::FormatPrompt] special cases did%s match" , success ? "" : "n't"); } while (0); | ||||
860 | |||||
861 | // should not happen | ||||
862 | if (success) | ||||
863 | s << str_temp.GetString(); | ||||
864 | return true; | ||||
865 | } else { | ||||
866 | if (was_plain_var) // if ${var} | ||||
867 | { | ||||
868 | s << target->GetTypeName() << " @ " << target->GetLocationAsCString(); | ||||
869 | } else if (is_pointer) // if pointer, value is the address stored | ||||
870 | { | ||||
871 | target->DumpPrintableRepresentation( | ||||
872 | s, val_obj_display, custom_format, | ||||
873 | ValueObject::PrintableRepresentationSpecialCases::eDisable); | ||||
874 | } | ||||
875 | return true; | ||||
876 | } | ||||
877 | } | ||||
878 | |||||
879 | // if directly trying to print ${var}, and this is an aggregate, display a | ||||
880 | // nice type @ location message | ||||
881 | if (is_aggregate && was_plain_var) { | ||||
882 | s << target->GetTypeName() << " @ " << target->GetLocationAsCString(); | ||||
883 | return true; | ||||
884 | } | ||||
885 | |||||
886 | // if directly trying to print ${var%V}, and this is an aggregate, do not let | ||||
887 | // the user do it | ||||
888 | if (is_aggregate
| ||||
889 | ((was_var_format && | ||||
890 | val_obj_display == | ||||
891 | ValueObject::eValueObjectRepresentationStyleValue))) { | ||||
892 | s << "<invalid use of aggregate type>"; | ||||
893 | return true; | ||||
894 | } | ||||
895 | |||||
896 | if (!is_array_range
| ||||
897 | LLDB_LOGF(log,do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Printf("[Debugger::FormatPrompt] dumping ordinary printable output" ); } while (0) | ||||
898 | "[Debugger::FormatPrompt] dumping ordinary printable output")do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Printf("[Debugger::FormatPrompt] dumping ordinary printable output" ); } while (0); | ||||
899 | return target->DumpPrintableRepresentation(s, val_obj_display, | ||||
900 | custom_format); | ||||
901 | } else { | ||||
902 | LLDB_LOGF(log,do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Printf("[Debugger::FormatPrompt] checking if I can handle as array" ); } while (0) | ||||
903 | "[Debugger::FormatPrompt] checking if I can handle as array")do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Printf("[Debugger::FormatPrompt] checking if I can handle as array" ); } while (0); | ||||
904 | if (!is_array
| ||||
905 | return false; | ||||
906 | LLDB_LOGF(log, "[Debugger::FormatPrompt] handle as array")do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Printf("[Debugger::FormatPrompt] handle as array" ); } while (0); | ||||
907 | StreamString special_directions_stream; | ||||
908 | llvm::StringRef special_directions; | ||||
909 | if (close_bracket_index
| ||||
910 | subpath.size() > close_bracket_index) { | ||||
911 | ConstString additional_data(subpath.drop_front(close_bracket_index + 1)); | ||||
912 | special_directions_stream.Printf("${%svar%s", do_deref_pointer ? "*" : "", | ||||
913 | additional_data.GetCString()); | ||||
914 | |||||
915 | if (entry.fmt != eFormatDefault) { | ||||
916 | const char format_char = | ||||
917 | FormatManager::GetFormatAsFormatChar(entry.fmt); | ||||
918 | if (format_char != '\0') | ||||
919 | special_directions_stream.Printf("%%%c", format_char); | ||||
920 | else { | ||||
921 | const char *format_cstr = | ||||
922 | FormatManager::GetFormatAsCString(entry.fmt); | ||||
923 | special_directions_stream.Printf("%%%s", format_cstr); | ||||
924 | } | ||||
925 | } else if (entry.number != 0) { | ||||
926 | const char style_char = ConvertValueObjectStyleToChar( | ||||
927 | (ValueObject::ValueObjectRepresentationStyle)entry.number); | ||||
928 | if (style_char) | ||||
929 | special_directions_stream.Printf("%%%c", style_char); | ||||
930 | } | ||||
931 | special_directions_stream.PutChar('}'); | ||||
932 | special_directions = | ||||
933 | llvm::StringRef(special_directions_stream.GetString()); | ||||
934 | } | ||||
935 | |||||
936 | // let us display items index_lower thru index_higher of this array | ||||
937 | s.PutChar('['); | ||||
938 | |||||
939 | if (index_higher < 0) | ||||
940 | index_higher = valobj->GetNumChildren() - 1; | ||||
941 | |||||
942 | uint32_t max_num_children = | ||||
943 | target->GetTargetSP()->GetMaximumNumberOfChildrenToDisplay(); | ||||
944 | |||||
945 | bool success = true; | ||||
946 | for (int64_t index = index_lower; index <= index_higher; ++index) { | ||||
947 | ValueObject *item = ExpandIndexedExpression(target, index, false).get(); | ||||
948 | |||||
949 | if (!item) { | ||||
950 | LLDB_LOGF(log,do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Printf("[Debugger::FormatPrompt] ERROR in getting child item at " "index %" "l" "d", index); } while (0) | ||||
951 | "[Debugger::FormatPrompt] ERROR in getting child item at "do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Printf("[Debugger::FormatPrompt] ERROR in getting child item at " "index %" "l" "d", index); } while (0) | ||||
952 | "index %" PRId64,do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Printf("[Debugger::FormatPrompt] ERROR in getting child item at " "index %" "l" "d", index); } while (0) | ||||
953 | index)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Printf("[Debugger::FormatPrompt] ERROR in getting child item at " "index %" "l" "d", index); } while (0); | ||||
954 | } else { | ||||
955 | LLDB_LOGF(do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Printf("[Debugger::FormatPrompt] special_directions for child item: %s" , special_directions.data() ? special_directions.data() : "") ; } while (0) | ||||
956 | log,do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Printf("[Debugger::FormatPrompt] special_directions for child item: %s" , special_directions.data() ? special_directions.data() : "") ; } while (0) | ||||
957 | "[Debugger::FormatPrompt] special_directions for child item: %s",do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Printf("[Debugger::FormatPrompt] special_directions for child item: %s" , special_directions.data() ? special_directions.data() : "") ; } while (0) | ||||
958 | special_directions.data() ? special_directions.data() : "")do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Printf("[Debugger::FormatPrompt] special_directions for child item: %s" , special_directions.data() ? special_directions.data() : "") ; } while (0); | ||||
959 | } | ||||
960 | |||||
961 | if (special_directions.empty()) { | ||||
962 | success &= item->DumpPrintableRepresentation(s, val_obj_display, | ||||
| |||||
963 | custom_format); | ||||
964 | } else { | ||||
965 | success &= FormatEntity::FormatStringRef( | ||||
966 | special_directions, s, sc, exe_ctx, nullptr, item, false, false); | ||||
967 | } | ||||
968 | |||||
969 | if (--max_num_children == 0) { | ||||
970 | s.PutCString(", ..."); | ||||
971 | break; | ||||
972 | } | ||||
973 | |||||
974 | if (index < index_higher) | ||||
975 | s.PutChar(','); | ||||
976 | } | ||||
977 | s.PutChar(']'); | ||||
978 | return success; | ||||
979 | } | ||||
980 | } | ||||
981 | |||||
982 | static bool DumpRegister(Stream &s, StackFrame *frame, const char *reg_name, | ||||
983 | Format format) { | ||||
984 | if (frame) { | ||||
985 | RegisterContext *reg_ctx = frame->GetRegisterContext().get(); | ||||
986 | |||||
987 | if (reg_ctx) { | ||||
988 | const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoByName(reg_name); | ||||
989 | if (reg_info) { | ||||
990 | RegisterValue reg_value; | ||||
991 | if (reg_ctx->ReadRegister(reg_info, reg_value)) { | ||||
992 | DumpRegisterValue(reg_value, &s, reg_info, false, false, format); | ||||
993 | return true; | ||||
994 | } | ||||
995 | } | ||||
996 | } | ||||
997 | } | ||||
998 | return false; | ||||
999 | } | ||||
1000 | |||||
1001 | static bool FormatThreadExtendedInfoRecurse( | ||||
1002 | const FormatEntity::Entry &entry, | ||||
1003 | const StructuredData::ObjectSP &thread_info_dictionary, | ||||
1004 | const SymbolContext *sc, const ExecutionContext *exe_ctx, Stream &s) { | ||||
1005 | llvm::StringRef path(entry.string); | ||||
1006 | |||||
1007 | StructuredData::ObjectSP value = | ||||
1008 | thread_info_dictionary->GetObjectForDotSeparatedPath(path); | ||||
1009 | |||||
1010 | if (value) { | ||||
1011 | if (value->GetType() == eStructuredDataTypeInteger) { | ||||
1012 | const char *token_format = "0x%4.4" PRIx64"l" "x"; | ||||
1013 | if (!entry.printf_format.empty()) | ||||
1014 | token_format = entry.printf_format.c_str(); | ||||
1015 | s.Printf(token_format, value->GetAsInteger()->GetValue()); | ||||
1016 | return true; | ||||
1017 | } else if (value->GetType() == eStructuredDataTypeFloat) { | ||||
1018 | s.Printf("%f", value->GetAsFloat()->GetValue()); | ||||
1019 | return true; | ||||
1020 | } else if (value->GetType() == eStructuredDataTypeString) { | ||||
1021 | s.Format("{0}", value->GetAsString()->GetValue()); | ||||
1022 | return true; | ||||
1023 | } else if (value->GetType() == eStructuredDataTypeArray) { | ||||
1024 | if (value->GetAsArray()->GetSize() > 0) { | ||||
1025 | s.Printf("%zu", value->GetAsArray()->GetSize()); | ||||
1026 | return true; | ||||
1027 | } | ||||
1028 | } else if (value->GetType() == eStructuredDataTypeDictionary) { | ||||
1029 | s.Printf("%zu", | ||||
1030 | value->GetAsDictionary()->GetKeys()->GetAsArray()->GetSize()); | ||||
1031 | return true; | ||||
1032 | } | ||||
1033 | } | ||||
1034 | |||||
1035 | return false; | ||||
1036 | } | ||||
1037 | |||||
1038 | static inline bool IsToken(const char *var_name_begin, const char *var) { | ||||
1039 | return (::strncmp(var_name_begin, var, strlen(var)) == 0); | ||||
1040 | } | ||||
1041 | |||||
1042 | /// Parses the basename out of a demangled function name | ||||
1043 | /// that may include function arguments. Supports | ||||
1044 | /// template functions. | ||||
1045 | /// | ||||
1046 | /// Returns pointers to the opening and closing parenthesis of | ||||
1047 | /// `full_name`. Can return nullptr for either parenthesis if | ||||
1048 | /// none is exists. | ||||
1049 | static std::pair<char const *, char const *> | ||||
1050 | ParseBaseName(char const *full_name) { | ||||
1051 | const char *open_paren = strchr(full_name, '('); | ||||
1052 | const char *close_paren = nullptr; | ||||
1053 | const char *generic = strchr(full_name, '<'); | ||||
1054 | // if before the arguments list begins there is a template sign | ||||
1055 | // then scan to the end of the generic args before you try to find | ||||
1056 | // the arguments list | ||||
1057 | if (generic && open_paren && generic < open_paren) { | ||||
1058 | int generic_depth = 1; | ||||
1059 | ++generic; | ||||
1060 | for (; *generic && generic_depth > 0; generic++) { | ||||
1061 | if (*generic == '<') | ||||
1062 | generic_depth++; | ||||
1063 | if (*generic == '>') | ||||
1064 | generic_depth--; | ||||
1065 | } | ||||
1066 | if (*generic) | ||||
1067 | open_paren = strchr(generic, '('); | ||||
1068 | else | ||||
1069 | open_paren = nullptr; | ||||
1070 | } | ||||
1071 | |||||
1072 | if (open_paren) { | ||||
1073 | if (IsToken(open_paren, "(anonymous namespace)")) { | ||||
1074 | open_paren = strchr(open_paren + strlen("(anonymous namespace)"), '('); | ||||
1075 | if (open_paren) | ||||
1076 | close_paren = strchr(open_paren, ')'); | ||||
1077 | } else | ||||
1078 | close_paren = strchr(open_paren, ')'); | ||||
1079 | } | ||||
1080 | |||||
1081 | return {open_paren, close_paren}; | ||||
1082 | } | ||||
1083 | |||||
1084 | /// Writes out the function name in 'full_name' to 'out_stream' | ||||
1085 | /// but replaces each argument type with the variable name | ||||
1086 | /// and the corresponding pretty-printed value | ||||
1087 | static void PrettyPrintFunctionNameWithArgs(Stream &out_stream, | ||||
1088 | char const *full_name, | ||||
1089 | ExecutionContextScope *exe_scope, | ||||
1090 | VariableList const &args) { | ||||
1091 | auto [open_paren, close_paren] = ParseBaseName(full_name); | ||||
1092 | if (open_paren) | ||||
1093 | out_stream.Write(full_name, open_paren - full_name + 1); | ||||
1094 | else { | ||||
1095 | out_stream.PutCString(full_name); | ||||
1096 | out_stream.PutChar('('); | ||||
1097 | } | ||||
1098 | |||||
1099 | FormatEntity::PrettyPrintFunctionArguments(out_stream, args, exe_scope); | ||||
1100 | |||||
1101 | if (close_paren) | ||||
1102 | out_stream.PutCString(close_paren); | ||||
1103 | else | ||||
1104 | out_stream.PutChar(')'); | ||||
1105 | } | ||||
1106 | |||||
1107 | bool FormatEntity::FormatStringRef(const llvm::StringRef &format_str, Stream &s, | ||||
1108 | const SymbolContext *sc, | ||||
1109 | const ExecutionContext *exe_ctx, | ||||
1110 | const Address *addr, ValueObject *valobj, | ||||
1111 | bool function_changed, | ||||
1112 | bool initial_function) { | ||||
1113 | if (!format_str.empty()) { | ||||
1114 | FormatEntity::Entry root; | ||||
1115 | Status error = FormatEntity::Parse(format_str, root); | ||||
1116 | if (error.Success()) { | ||||
1117 | return FormatEntity::Format(root, s, sc, exe_ctx, addr, valobj, | ||||
1118 | function_changed, initial_function); | ||||
1119 | } | ||||
1120 | } | ||||
1121 | return false; | ||||
1122 | } | ||||
1123 | |||||
1124 | bool FormatEntity::FormatCString(const char *format, Stream &s, | ||||
1125 | const SymbolContext *sc, | ||||
1126 | const ExecutionContext *exe_ctx, | ||||
1127 | const Address *addr, ValueObject *valobj, | ||||
1128 | bool function_changed, bool initial_function) { | ||||
1129 | if (format && format[0]) { | ||||
1130 | FormatEntity::Entry root; | ||||
1131 | llvm::StringRef format_str(format); | ||||
1132 | Status error = FormatEntity::Parse(format_str, root); | ||||
1133 | if (error.Success()) { | ||||
1134 | return FormatEntity::Format(root, s, sc, exe_ctx, addr, valobj, | ||||
1135 | function_changed, initial_function); | ||||
1136 | } | ||||
1137 | } | ||||
1138 | return false; | ||||
1139 | } | ||||
1140 | |||||
1141 | bool FormatEntity::Format(const Entry &entry, Stream &s, | ||||
1142 | const SymbolContext *sc, | ||||
1143 | const ExecutionContext *exe_ctx, const Address *addr, | ||||
1144 | ValueObject *valobj, bool function_changed, | ||||
1145 | bool initial_function) { | ||||
1146 | switch (entry.type) { | ||||
1147 | case Entry::Type::Invalid: | ||||
1148 | case Entry::Type::ParentNumber: // Only used for | ||||
1149 | // FormatEntity::Entry::Definition encoding | ||||
1150 | case Entry::Type::ParentString: // Only used for | ||||
1151 | // FormatEntity::Entry::Definition encoding | ||||
1152 | return false; | ||||
1153 | case Entry::Type::EscapeCode: | ||||
1154 | if (exe_ctx) { | ||||
1155 | if (Target *target = exe_ctx->GetTargetPtr()) { | ||||
1156 | Debugger &debugger = target->GetDebugger(); | ||||
1157 | if (debugger.GetUseColor()) { | ||||
1158 | s.PutCString(entry.string); | ||||
1159 | } | ||||
1160 | } | ||||
1161 | } | ||||
1162 | // Always return true, so colors being disabled is transparent. | ||||
1163 | return true; | ||||
1164 | |||||
1165 | case Entry::Type::Root: | ||||
1166 | for (const auto &child : entry.children) { | ||||
1167 | if (!Format(child, s, sc, exe_ctx, addr, valobj, function_changed, | ||||
1168 | initial_function)) { | ||||
1169 | return false; // If any item of root fails, then the formatting fails | ||||
1170 | } | ||||
1171 | } | ||||
1172 | return true; // Only return true if all items succeeded | ||||
1173 | |||||
1174 | case Entry::Type::String: | ||||
1175 | s.PutCString(entry.string); | ||||
1176 | return true; | ||||
1177 | |||||
1178 | case Entry::Type::Scope: { | ||||
1179 | StreamString scope_stream; | ||||
1180 | bool success = false; | ||||
1181 | for (const auto &child : entry.children) { | ||||
1182 | success = Format(child, scope_stream, sc, exe_ctx, addr, valobj, | ||||
1183 | function_changed, initial_function); | ||||
1184 | if (!success) | ||||
1185 | break; | ||||
1186 | } | ||||
1187 | // Only if all items in a scope succeed, then do we print the output into | ||||
1188 | // the main stream | ||||
1189 | if (success) | ||||
1190 | s.Write(scope_stream.GetString().data(), scope_stream.GetString().size()); | ||||
1191 | } | ||||
1192 | return true; // Scopes always successfully print themselves | ||||
1193 | |||||
1194 | case Entry::Type::Variable: | ||||
1195 | case Entry::Type::VariableSynthetic: | ||||
1196 | case Entry::Type::ScriptVariable: | ||||
1197 | case Entry::Type::ScriptVariableSynthetic: | ||||
1198 | return DumpValue(s, sc, exe_ctx, entry, valobj); | ||||
1199 | |||||
1200 | case Entry::Type::AddressFile: | ||||
1201 | case Entry::Type::AddressLoad: | ||||
1202 | case Entry::Type::AddressLoadOrFile: | ||||
1203 | return ( | ||||
1204 | addr != nullptr && addr->IsValid() && | ||||
1205 | DumpAddressAndContent(s, sc, exe_ctx, *addr, | ||||
1206 | entry.type == Entry::Type::AddressLoadOrFile)); | ||||
1207 | |||||
1208 | case Entry::Type::ProcessID: | ||||
1209 | if (exe_ctx) { | ||||
1210 | Process *process = exe_ctx->GetProcessPtr(); | ||||
1211 | if (process) { | ||||
1212 | const char *format = "%" PRIu64"l" "u"; | ||||
1213 | if (!entry.printf_format.empty()) | ||||
1214 | format = entry.printf_format.c_str(); | ||||
1215 | s.Printf(format, process->GetID()); | ||||
1216 | return true; | ||||
1217 | } | ||||
1218 | } | ||||
1219 | return false; | ||||
1220 | |||||
1221 | case Entry::Type::ProcessFile: | ||||
1222 | if (exe_ctx) { | ||||
1223 | Process *process = exe_ctx->GetProcessPtr(); | ||||
1224 | if (process) { | ||||
1225 | Module *exe_module = process->GetTarget().GetExecutableModulePointer(); | ||||
1226 | if (exe_module) { | ||||
1227 | if (DumpFile(s, exe_module->GetFileSpec(), (FileKind)entry.number)) | ||||
1228 | return true; | ||||
1229 | } | ||||
1230 | } | ||||
1231 | } | ||||
1232 | return false; | ||||
1233 | |||||
1234 | case Entry::Type::ScriptProcess: | ||||
1235 | if (exe_ctx) { | ||||
1236 | Process *process = exe_ctx->GetProcessPtr(); | ||||
1237 | if (process) | ||||
1238 | return RunScriptFormatKeyword(s, sc, exe_ctx, process, | ||||
1239 | entry.string.c_str()); | ||||
1240 | } | ||||
1241 | return false; | ||||
1242 | |||||
1243 | case Entry::Type::ThreadID: | ||||
1244 | if (exe_ctx) { | ||||
1245 | Thread *thread = exe_ctx->GetThreadPtr(); | ||||
1246 | if (thread) { | ||||
1247 | const char *format = "0x%4.4" PRIx64"l" "x"; | ||||
1248 | if (!entry.printf_format.empty()) { | ||||
1249 | // Watch for the special "tid" format... | ||||
1250 | if (entry.printf_format == "tid") { | ||||
1251 | // TODO(zturner): Rather than hardcoding this to be platform | ||||
1252 | // specific, it should be controlled by a setting and the default | ||||
1253 | // value of the setting can be different depending on the platform. | ||||
1254 | Target &target = thread->GetProcess()->GetTarget(); | ||||
1255 | ArchSpec arch(target.GetArchitecture()); | ||||
1256 | llvm::Triple::OSType ostype = arch.IsValid() | ||||
1257 | ? arch.GetTriple().getOS() | ||||
1258 | : llvm::Triple::UnknownOS; | ||||
1259 | if ((ostype == llvm::Triple::FreeBSD) || | ||||
1260 | (ostype == llvm::Triple::Linux) || | ||||
1261 | (ostype == llvm::Triple::NetBSD)) { | ||||
1262 | format = "%" PRIu64"l" "u"; | ||||
1263 | } | ||||
1264 | } else { | ||||
1265 | format = entry.printf_format.c_str(); | ||||
1266 | } | ||||
1267 | } | ||||
1268 | s.Printf(format, thread->GetID()); | ||||
1269 | return true; | ||||
1270 | } | ||||
1271 | } | ||||
1272 | return false; | ||||
1273 | |||||
1274 | case Entry::Type::ThreadProtocolID: | ||||
1275 | if (exe_ctx) { | ||||
1276 | Thread *thread = exe_ctx->GetThreadPtr(); | ||||
1277 | if (thread) { | ||||
1278 | const char *format = "0x%4.4" PRIx64"l" "x"; | ||||
1279 | if (!entry.printf_format.empty()) | ||||
1280 | format = entry.printf_format.c_str(); | ||||
1281 | s.Printf(format, thread->GetProtocolID()); | ||||
1282 | return true; | ||||
1283 | } | ||||
1284 | } | ||||
1285 | return false; | ||||
1286 | |||||
1287 | case Entry::Type::ThreadIndexID: | ||||
1288 | if (exe_ctx) { | ||||
1289 | Thread *thread = exe_ctx->GetThreadPtr(); | ||||
1290 | if (thread) { | ||||
1291 | const char *format = "%" PRIu32"u"; | ||||
1292 | if (!entry.printf_format.empty()) | ||||
1293 | format = entry.printf_format.c_str(); | ||||
1294 | s.Printf(format, thread->GetIndexID()); | ||||
1295 | return true; | ||||
1296 | } | ||||
1297 | } | ||||
1298 | return false; | ||||
1299 | |||||
1300 | case Entry::Type::ThreadName: | ||||
1301 | if (exe_ctx) { | ||||
1302 | Thread *thread = exe_ctx->GetThreadPtr(); | ||||
1303 | if (thread) { | ||||
1304 | const char *cstr = thread->GetName(); | ||||
1305 | if (cstr && cstr[0]) { | ||||
1306 | s.PutCString(cstr); | ||||
1307 | return true; | ||||
1308 | } | ||||
1309 | } | ||||
1310 | } | ||||
1311 | return false; | ||||
1312 | |||||
1313 | case Entry::Type::ThreadQueue: | ||||
1314 | if (exe_ctx) { | ||||
1315 | Thread *thread = exe_ctx->GetThreadPtr(); | ||||
1316 | if (thread) { | ||||
1317 | const char *cstr = thread->GetQueueName(); | ||||
1318 | if (cstr && cstr[0]) { | ||||
1319 | s.PutCString(cstr); | ||||
1320 | return true; | ||||
1321 | } | ||||
1322 | } | ||||
1323 | } | ||||
1324 | return false; | ||||
1325 | |||||
1326 | case Entry::Type::ThreadStopReason: | ||||
1327 | if (exe_ctx) { | ||||
1328 | if (Thread *thread = exe_ctx->GetThreadPtr()) { | ||||
1329 | std::string stop_description = thread->GetStopDescription(); | ||||
1330 | if (!stop_description.empty()) { | ||||
1331 | s.PutCString(stop_description); | ||||
1332 | return true; | ||||
1333 | } | ||||
1334 | } | ||||
1335 | } | ||||
1336 | return false; | ||||
1337 | |||||
1338 | case Entry::Type::ThreadStopReasonRaw: | ||||
1339 | if (exe_ctx) { | ||||
1340 | if (Thread *thread = exe_ctx->GetThreadPtr()) { | ||||
1341 | std::string stop_description = thread->GetStopDescriptionRaw(); | ||||
1342 | if (!stop_description.empty()) { | ||||
1343 | s.PutCString(stop_description); | ||||
1344 | return true; | ||||
1345 | } | ||||
1346 | } | ||||
1347 | } | ||||
1348 | return false; | ||||
1349 | |||||
1350 | case Entry::Type::ThreadReturnValue: | ||||
1351 | if (exe_ctx) { | ||||
1352 | Thread *thread = exe_ctx->GetThreadPtr(); | ||||
1353 | if (thread) { | ||||
1354 | StopInfoSP stop_info_sp = thread->GetStopInfo(); | ||||
1355 | if (stop_info_sp && stop_info_sp->IsValid()) { | ||||
1356 | ValueObjectSP return_valobj_sp = | ||||
1357 | StopInfo::GetReturnValueObject(stop_info_sp); | ||||
1358 | if (return_valobj_sp) { | ||||
1359 | return_valobj_sp->Dump(s); | ||||
1360 | return true; | ||||
1361 | } | ||||
1362 | } | ||||
1363 | } | ||||
1364 | } | ||||
1365 | return false; | ||||
1366 | |||||
1367 | case Entry::Type::ThreadCompletedExpression: | ||||
1368 | if (exe_ctx) { | ||||
1369 | Thread *thread = exe_ctx->GetThreadPtr(); | ||||
1370 | if (thread) { | ||||
1371 | StopInfoSP stop_info_sp = thread->GetStopInfo(); | ||||
1372 | if (stop_info_sp && stop_info_sp->IsValid()) { | ||||
1373 | ExpressionVariableSP expression_var_sp = | ||||
1374 | StopInfo::GetExpressionVariable(stop_info_sp); | ||||
1375 | if (expression_var_sp && expression_var_sp->GetValueObject()) { | ||||
1376 | expression_var_sp->GetValueObject()->Dump(s); | ||||
1377 | return true; | ||||
1378 | } | ||||
1379 | } | ||||
1380 | } | ||||
1381 | } | ||||
1382 | return false; | ||||
1383 | |||||
1384 | case Entry::Type::ScriptThread: | ||||
1385 | if (exe_ctx) { | ||||
1386 | Thread *thread = exe_ctx->GetThreadPtr(); | ||||
1387 | if (thread) | ||||
1388 | return RunScriptFormatKeyword(s, sc, exe_ctx, thread, | ||||
1389 | entry.string.c_str()); | ||||
1390 | } | ||||
1391 | return false; | ||||
1392 | |||||
1393 | case Entry::Type::ThreadInfo: | ||||
1394 | if (exe_ctx) { | ||||
1395 | Thread *thread = exe_ctx->GetThreadPtr(); | ||||
1396 | if (thread) { | ||||
1397 | StructuredData::ObjectSP object_sp = thread->GetExtendedInfo(); | ||||
1398 | if (object_sp && | ||||
1399 | object_sp->GetType() == eStructuredDataTypeDictionary) { | ||||
1400 | if (FormatThreadExtendedInfoRecurse(entry, object_sp, sc, exe_ctx, s)) | ||||
1401 | return true; | ||||
1402 | } | ||||
1403 | } | ||||
1404 | } | ||||
1405 | return false; | ||||
1406 | |||||
1407 | case Entry::Type::TargetArch: | ||||
1408 | if (exe_ctx) { | ||||
1409 | Target *target = exe_ctx->GetTargetPtr(); | ||||
1410 | if (target) { | ||||
1411 | const ArchSpec &arch = target->GetArchitecture(); | ||||
1412 | if (arch.IsValid()) { | ||||
1413 | s.PutCString(arch.GetArchitectureName()); | ||||
1414 | return true; | ||||
1415 | } | ||||
1416 | } | ||||
1417 | } | ||||
1418 | return false; | ||||
1419 | |||||
1420 | case Entry::Type::ScriptTarget: | ||||
1421 | if (exe_ctx) { | ||||
1422 | Target *target = exe_ctx->GetTargetPtr(); | ||||
1423 | if (target) | ||||
1424 | return RunScriptFormatKeyword(s, sc, exe_ctx, target, | ||||
1425 | entry.string.c_str()); | ||||
1426 | } | ||||
1427 | return false; | ||||
1428 | |||||
1429 | case Entry::Type::ModuleFile: | ||||
1430 | if (sc) { | ||||
1431 | Module *module = sc->module_sp.get(); | ||||
1432 | if (module) { | ||||
1433 | if (DumpFile(s, module->GetFileSpec(), (FileKind)entry.number)) | ||||
1434 | return true; | ||||
1435 | } | ||||
1436 | } | ||||
1437 | return false; | ||||
1438 | |||||
1439 | case Entry::Type::File: | ||||
1440 | if (sc) { | ||||
1441 | CompileUnit *cu = sc->comp_unit; | ||||
1442 | if (cu) { | ||||
1443 | if (DumpFile(s, cu->GetPrimaryFile(), (FileKind)entry.number)) | ||||
1444 | return true; | ||||
1445 | } | ||||
1446 | } | ||||
1447 | return false; | ||||
1448 | |||||
1449 | case Entry::Type::Lang: | ||||
1450 | if (sc) { | ||||
1451 | CompileUnit *cu = sc->comp_unit; | ||||
1452 | if (cu) { | ||||
1453 | const char *lang_name = | ||||
1454 | Language::GetNameForLanguageType(cu->GetLanguage()); | ||||
1455 | if (lang_name) { | ||||
1456 | s.PutCString(lang_name); | ||||
1457 | return true; | ||||
1458 | } | ||||
1459 | } | ||||
1460 | } | ||||
1461 | return false; | ||||
1462 | |||||
1463 | case Entry::Type::FrameIndex: | ||||
1464 | if (exe_ctx) { | ||||
1465 | StackFrame *frame = exe_ctx->GetFramePtr(); | ||||
1466 | if (frame) { | ||||
1467 | const char *format = "%" PRIu32"u"; | ||||
1468 | if (!entry.printf_format.empty()) | ||||
1469 | format = entry.printf_format.c_str(); | ||||
1470 | s.Printf(format, frame->GetFrameIndex()); | ||||
1471 | return true; | ||||
1472 | } | ||||
1473 | } | ||||
1474 | return false; | ||||
1475 | |||||
1476 | case Entry::Type::FrameRegisterPC: | ||||
1477 | if (exe_ctx) { | ||||
1478 | StackFrame *frame = exe_ctx->GetFramePtr(); | ||||
1479 | if (frame) { | ||||
1480 | const Address &pc_addr = frame->GetFrameCodeAddress(); | ||||
1481 | if (pc_addr.IsValid()) { | ||||
1482 | if (DumpAddressAndContent(s, sc, exe_ctx, pc_addr, false)) | ||||
1483 | return true; | ||||
1484 | } | ||||
1485 | } | ||||
1486 | } | ||||
1487 | return false; | ||||
1488 | |||||
1489 | case Entry::Type::FrameRegisterSP: | ||||
1490 | if (exe_ctx) { | ||||
1491 | StackFrame *frame = exe_ctx->GetFramePtr(); | ||||
1492 | if (frame) { | ||||
1493 | if (DumpRegister(s, frame, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP1, | ||||
1494 | (lldb::Format)entry.number)) | ||||
1495 | return true; | ||||
1496 | } | ||||
1497 | } | ||||
1498 | return false; | ||||
1499 | |||||
1500 | case Entry::Type::FrameRegisterFP: | ||||
1501 | if (exe_ctx) { | ||||
1502 | StackFrame *frame = exe_ctx->GetFramePtr(); | ||||
1503 | if (frame) { | ||||
1504 | if (DumpRegister(s, frame, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FP2, | ||||
1505 | (lldb::Format)entry.number)) | ||||
1506 | return true; | ||||
1507 | } | ||||
1508 | } | ||||
1509 | return false; | ||||
1510 | |||||
1511 | case Entry::Type::FrameRegisterFlags: | ||||
1512 | if (exe_ctx) { | ||||
1513 | StackFrame *frame = exe_ctx->GetFramePtr(); | ||||
1514 | if (frame) { | ||||
1515 | if (DumpRegister(s, frame, eRegisterKindGeneric, | ||||
1516 | LLDB_REGNUM_GENERIC_FLAGS4, (lldb::Format)entry.number)) | ||||
1517 | return true; | ||||
1518 | } | ||||
1519 | } | ||||
1520 | return false; | ||||
1521 | |||||
1522 | case Entry::Type::FrameNoDebug: | ||||
1523 | if (exe_ctx) { | ||||
1524 | StackFrame *frame = exe_ctx->GetFramePtr(); | ||||
1525 | if (frame) { | ||||
1526 | return !frame->HasDebugInformation(); | ||||
1527 | } | ||||
1528 | } | ||||
1529 | return true; | ||||
1530 | |||||
1531 | case Entry::Type::FrameRegisterByName: | ||||
1532 | if (exe_ctx) { | ||||
1533 | StackFrame *frame = exe_ctx->GetFramePtr(); | ||||
1534 | if (frame) { | ||||
1535 | if (DumpRegister(s, frame, entry.string.c_str(), | ||||
1536 | (lldb::Format)entry.number)) | ||||
1537 | return true; | ||||
1538 | } | ||||
1539 | } | ||||
1540 | return false; | ||||
1541 | |||||
1542 | case Entry::Type::FrameIsArtificial: { | ||||
1543 | if (exe_ctx) | ||||
1544 | if (StackFrame *frame = exe_ctx->GetFramePtr()) | ||||
1545 | return frame->IsArtificial(); | ||||
1546 | return false; | ||||
1547 | } | ||||
1548 | |||||
1549 | case Entry::Type::ScriptFrame: | ||||
1550 | if (exe_ctx) { | ||||
1551 | StackFrame *frame = exe_ctx->GetFramePtr(); | ||||
1552 | if (frame) | ||||
1553 | return RunScriptFormatKeyword(s, sc, exe_ctx, frame, | ||||
1554 | entry.string.c_str()); | ||||
1555 | } | ||||
1556 | return false; | ||||
1557 | |||||
1558 | case Entry::Type::FunctionID: | ||||
1559 | if (sc) { | ||||
1560 | if (sc->function) { | ||||
1561 | s.Printf("function{0x%8.8" PRIx64"l" "x" "}", sc->function->GetID()); | ||||
1562 | return true; | ||||
1563 | } else if (sc->symbol) { | ||||
1564 | s.Printf("symbol[%u]", sc->symbol->GetID()); | ||||
1565 | return true; | ||||
1566 | } | ||||
1567 | } | ||||
1568 | return false; | ||||
1569 | |||||
1570 | case Entry::Type::FunctionDidChange: | ||||
1571 | return function_changed; | ||||
1572 | |||||
1573 | case Entry::Type::FunctionInitialFunction: | ||||
1574 | return initial_function; | ||||
1575 | |||||
1576 | case Entry::Type::FunctionName: { | ||||
1577 | if (!sc) | ||||
1578 | return false; | ||||
1579 | |||||
1580 | Language *language_plugin = nullptr; | ||||
1581 | bool language_plugin_handled = false; | ||||
1582 | StreamString ss; | ||||
1583 | |||||
1584 | if (sc->function) | ||||
1585 | language_plugin = Language::FindPlugin(sc->function->GetLanguage()); | ||||
1586 | else if (sc->symbol) | ||||
1587 | language_plugin = Language::FindPlugin(sc->symbol->GetLanguage()); | ||||
1588 | |||||
1589 | if (language_plugin) | ||||
1590 | language_plugin_handled = language_plugin->GetFunctionDisplayName( | ||||
1591 | sc, exe_ctx, Language::FunctionNameRepresentation::eName, ss); | ||||
1592 | |||||
1593 | if (language_plugin_handled) { | ||||
1594 | s << ss.GetString(); | ||||
1595 | return true; | ||||
1596 | } else { | ||||
1597 | const char *name = nullptr; | ||||
1598 | if (sc->function) | ||||
1599 | name = sc->function->GetName().AsCString(nullptr); | ||||
1600 | else if (sc->symbol) | ||||
1601 | name = sc->symbol->GetName().AsCString(nullptr); | ||||
1602 | |||||
1603 | if (name) { | ||||
1604 | s.PutCString(name); | ||||
1605 | |||||
1606 | if (sc->block) { | ||||
1607 | Block *inline_block = sc->block->GetContainingInlinedBlock(); | ||||
1608 | if (inline_block) { | ||||
1609 | const InlineFunctionInfo *inline_info = | ||||
1610 | sc->block->GetInlinedFunctionInfo(); | ||||
1611 | if (inline_info) { | ||||
1612 | s.PutCString(" [inlined] "); | ||||
1613 | inline_info->GetName().Dump(&s); | ||||
1614 | } | ||||
1615 | } | ||||
1616 | } | ||||
1617 | return true; | ||||
1618 | } | ||||
1619 | } | ||||
1620 | } | ||||
1621 | return false; | ||||
1622 | |||||
1623 | case Entry::Type::FunctionNameNoArgs: { | ||||
1624 | if (!sc) | ||||
1625 | return false; | ||||
1626 | |||||
1627 | Language *language_plugin = nullptr; | ||||
1628 | bool language_plugin_handled = false; | ||||
1629 | StreamString ss; | ||||
1630 | if (sc->function) | ||||
1631 | language_plugin = Language::FindPlugin(sc->function->GetLanguage()); | ||||
1632 | else if (sc->symbol) | ||||
1633 | language_plugin = Language::FindPlugin(sc->symbol->GetLanguage()); | ||||
1634 | |||||
1635 | if (language_plugin) | ||||
1636 | language_plugin_handled = language_plugin->GetFunctionDisplayName( | ||||
1637 | sc, exe_ctx, Language::FunctionNameRepresentation::eNameWithNoArgs, | ||||
1638 | ss); | ||||
1639 | |||||
1640 | if (language_plugin_handled) { | ||||
1641 | s << ss.GetString(); | ||||
1642 | return true; | ||||
1643 | } else { | ||||
1644 | ConstString name; | ||||
1645 | if (sc->function) | ||||
1646 | name = sc->function->GetNameNoArguments(); | ||||
1647 | else if (sc->symbol) | ||||
1648 | name = sc->symbol->GetNameNoArguments(); | ||||
1649 | if (name) { | ||||
1650 | s.PutCString(name.GetCString()); | ||||
1651 | return true; | ||||
1652 | } | ||||
1653 | } | ||||
1654 | } | ||||
1655 | return false; | ||||
1656 | |||||
1657 | case Entry::Type::FunctionNameWithArgs: { | ||||
1658 | if (!sc) | ||||
1659 | return false; | ||||
1660 | |||||
1661 | Language *language_plugin = nullptr; | ||||
1662 | bool language_plugin_handled = false; | ||||
1663 | StreamString ss; | ||||
1664 | if (sc->function) | ||||
1665 | language_plugin = Language::FindPlugin(sc->function->GetLanguage()); | ||||
1666 | else if (sc->symbol) | ||||
1667 | language_plugin = Language::FindPlugin(sc->symbol->GetLanguage()); | ||||
1668 | |||||
1669 | if (language_plugin) | ||||
1670 | language_plugin_handled = language_plugin->GetFunctionDisplayName( | ||||
1671 | sc, exe_ctx, Language::FunctionNameRepresentation::eNameWithArgs, ss); | ||||
1672 | |||||
1673 | if (language_plugin_handled) { | ||||
1674 | s << ss.GetString(); | ||||
1675 | return true; | ||||
1676 | } else { | ||||
1677 | // Print the function name with arguments in it | ||||
1678 | if (sc->function) { | ||||
1679 | ExecutionContextScope *exe_scope = | ||||
1680 | exe_ctx ? exe_ctx->GetBestExecutionContextScope() : nullptr; | ||||
1681 | const char *cstr = sc->function->GetName().AsCString(nullptr); | ||||
1682 | if (cstr) { | ||||
1683 | const InlineFunctionInfo *inline_info = nullptr; | ||||
1684 | VariableListSP variable_list_sp; | ||||
1685 | bool get_function_vars = true; | ||||
1686 | if (sc->block) { | ||||
1687 | Block *inline_block = sc->block->GetContainingInlinedBlock(); | ||||
1688 | |||||
1689 | if (inline_block) { | ||||
1690 | get_function_vars = false; | ||||
1691 | inline_info = sc->block->GetInlinedFunctionInfo(); | ||||
1692 | if (inline_info) | ||||
1693 | variable_list_sp = inline_block->GetBlockVariableList(true); | ||||
1694 | } | ||||
1695 | } | ||||
1696 | |||||
1697 | if (get_function_vars) { | ||||
1698 | variable_list_sp = | ||||
1699 | sc->function->GetBlock(true).GetBlockVariableList(true); | ||||
1700 | } | ||||
1701 | |||||
1702 | if (inline_info) { | ||||
1703 | s.PutCString(cstr); | ||||
1704 | s.PutCString(" [inlined] "); | ||||
1705 | cstr = inline_info->GetName().GetCString(); | ||||
1706 | } | ||||
1707 | |||||
1708 | VariableList args; | ||||
1709 | if (variable_list_sp) | ||||
1710 | variable_list_sp->AppendVariablesWithScope( | ||||
1711 | eValueTypeVariableArgument, args); | ||||
1712 | if (args.GetSize() > 0) { | ||||
1713 | PrettyPrintFunctionNameWithArgs(s, cstr, exe_scope, args); | ||||
1714 | } else { | ||||
1715 | s.PutCString(cstr); | ||||
1716 | } | ||||
1717 | return true; | ||||
1718 | } | ||||
1719 | } else if (sc->symbol) { | ||||
1720 | const char *cstr = sc->symbol->GetName().AsCString(nullptr); | ||||
1721 | if (cstr) { | ||||
1722 | s.PutCString(cstr); | ||||
1723 | return true; | ||||
1724 | } | ||||
1725 | } | ||||
1726 | } | ||||
1727 | } | ||||
1728 | return false; | ||||
1729 | |||||
1730 | case Entry::Type::FunctionMangledName: { | ||||
1731 | if (!sc) | ||||
1732 | return false; | ||||
1733 | |||||
1734 | const char *name = nullptr; | ||||
1735 | if (sc->symbol) | ||||
1736 | name = | ||||
1737 | sc->symbol->GetMangled().GetName(Mangled::ePreferMangled).AsCString(); | ||||
1738 | else if (sc->function) | ||||
1739 | name = sc->function->GetMangled() | ||||
1740 | .GetName(Mangled::ePreferMangled) | ||||
1741 | .AsCString(); | ||||
1742 | |||||
1743 | if (!name) | ||||
1744 | return false; | ||||
1745 | s.PutCString(name); | ||||
1746 | |||||
1747 | if (sc->block && sc->block->GetContainingInlinedBlock()) { | ||||
1748 | if (const InlineFunctionInfo *inline_info = | ||||
1749 | sc->block->GetInlinedFunctionInfo()) { | ||||
1750 | s.PutCString(" [inlined] "); | ||||
1751 | inline_info->GetName().Dump(&s); | ||||
1752 | } | ||||
1753 | } | ||||
1754 | return true; | ||||
1755 | } | ||||
1756 | case Entry::Type::FunctionAddrOffset: | ||||
1757 | if (addr) { | ||||
1758 | if (DumpAddressOffsetFromFunction(s, sc, exe_ctx, *addr, false, false, | ||||
1759 | false)) | ||||
1760 | return true; | ||||
1761 | } | ||||
1762 | return false; | ||||
1763 | |||||
1764 | case Entry::Type::FunctionAddrOffsetConcrete: | ||||
1765 | if (addr) { | ||||
1766 | if (DumpAddressOffsetFromFunction(s, sc, exe_ctx, *addr, true, true, | ||||
1767 | true)) | ||||
1768 | return true; | ||||
1769 | } | ||||
1770 | return false; | ||||
1771 | |||||
1772 | case Entry::Type::FunctionLineOffset: | ||||
1773 | if (sc) | ||||
1774 | return (DumpAddressOffsetFromFunction( | ||||
1775 | s, sc, exe_ctx, sc->line_entry.range.GetBaseAddress(), false, false, | ||||
1776 | false)); | ||||
1777 | return false; | ||||
1778 | |||||
1779 | case Entry::Type::FunctionPCOffset: | ||||
1780 | if (exe_ctx) { | ||||
1781 | StackFrame *frame = exe_ctx->GetFramePtr(); | ||||
1782 | if (frame) { | ||||
1783 | if (DumpAddressOffsetFromFunction(s, sc, exe_ctx, | ||||
1784 | frame->GetFrameCodeAddress(), false, | ||||
1785 | false, false)) | ||||
1786 | return true; | ||||
1787 | } | ||||
1788 | } | ||||
1789 | return false; | ||||
1790 | |||||
1791 | case Entry::Type::FunctionChanged: | ||||
1792 | return function_changed; | ||||
1793 | |||||
1794 | case Entry::Type::FunctionIsOptimized: { | ||||
1795 | bool is_optimized = false; | ||||
1796 | if (sc && sc->function && sc->function->GetIsOptimized()) { | ||||
1797 | is_optimized = true; | ||||
1798 | } | ||||
1799 | return is_optimized; | ||||
1800 | } | ||||
1801 | |||||
1802 | case Entry::Type::FunctionInitial: | ||||
1803 | return initial_function; | ||||
1804 | |||||
1805 | case Entry::Type::LineEntryFile: | ||||
1806 | if (sc && sc->line_entry.IsValid()) { | ||||
1807 | Module *module = sc->module_sp.get(); | ||||
1808 | if (module) { | ||||
1809 | if (DumpFile(s, sc->line_entry.file, (FileKind)entry.number)) | ||||
1810 | return true; | ||||
1811 | } | ||||
1812 | } | ||||
1813 | return false; | ||||
1814 | |||||
1815 | case Entry::Type::LineEntryLineNumber: | ||||
1816 | if (sc && sc->line_entry.IsValid()) { | ||||
1817 | const char *format = "%" PRIu32"u"; | ||||
1818 | if (!entry.printf_format.empty()) | ||||
1819 | format = entry.printf_format.c_str(); | ||||
1820 | s.Printf(format, sc->line_entry.line); | ||||
1821 | return true; | ||||
1822 | } | ||||
1823 | return false; | ||||
1824 | |||||
1825 | case Entry::Type::LineEntryColumn: | ||||
1826 | if (sc && sc->line_entry.IsValid() && sc->line_entry.column) { | ||||
1827 | const char *format = "%" PRIu32"u"; | ||||
1828 | if (!entry.printf_format.empty()) | ||||
1829 | format = entry.printf_format.c_str(); | ||||
1830 | s.Printf(format, sc->line_entry.column); | ||||
1831 | return true; | ||||
1832 | } | ||||
1833 | return false; | ||||
1834 | |||||
1835 | case Entry::Type::LineEntryStartAddress: | ||||
1836 | case Entry::Type::LineEntryEndAddress: | ||||
1837 | if (sc && sc->line_entry.range.GetBaseAddress().IsValid()) { | ||||
1838 | Address addr = sc->line_entry.range.GetBaseAddress(); | ||||
1839 | |||||
1840 | if (entry.type == Entry::Type::LineEntryEndAddress) | ||||
1841 | addr.Slide(sc->line_entry.range.GetByteSize()); | ||||
1842 | if (DumpAddressAndContent(s, sc, exe_ctx, addr, false)) | ||||
1843 | return true; | ||||
1844 | } | ||||
1845 | return false; | ||||
1846 | |||||
1847 | case Entry::Type::CurrentPCArrow: | ||||
1848 | if (addr && exe_ctx && exe_ctx->GetFramePtr()) { | ||||
1849 | RegisterContextSP reg_ctx = | ||||
1850 | exe_ctx->GetFramePtr()->GetRegisterContextSP(); | ||||
1851 | if (reg_ctx) { | ||||
1852 | addr_t pc_loadaddr = reg_ctx->GetPC(); | ||||
1853 | if (pc_loadaddr != LLDB_INVALID_ADDRESS(18446744073709551615UL)) { | ||||
1854 | Address pc; | ||||
1855 | pc.SetLoadAddress(pc_loadaddr, exe_ctx->GetTargetPtr()); | ||||
1856 | if (pc == *addr) { | ||||
1857 | s.Printf("-> "); | ||||
1858 | return true; | ||||
1859 | } | ||||
1860 | } | ||||
1861 | } | ||||
1862 | s.Printf(" "); | ||||
1863 | return true; | ||||
1864 | } | ||||
1865 | return false; | ||||
1866 | } | ||||
1867 | return false; | ||||
1868 | } | ||||
1869 | |||||
1870 | static bool DumpCommaSeparatedChildEntryNames(Stream &s, | ||||
1871 | const Definition *parent) { | ||||
1872 | if (parent->children) { | ||||
1873 | const size_t n = parent->num_children; | ||||
1874 | for (size_t i = 0; i < n; ++i) { | ||||
1875 | if (i > 0) | ||||
1876 | s.PutCString(", "); | ||||
1877 | s.Printf("\"%s\"", parent->children[i].name); | ||||
1878 | } | ||||
1879 | return true; | ||||
1880 | } | ||||
1881 | return false; | ||||
1882 | } | ||||
1883 | |||||
1884 | static Status ParseEntry(const llvm::StringRef &format_str, | ||||
1885 | const Definition *parent, FormatEntity::Entry &entry) { | ||||
1886 | Status error; | ||||
1887 | |||||
1888 | const size_t sep_pos = format_str.find_first_of(".[:"); | ||||
1889 | const char sep_char = | ||||
1890 | (sep_pos == llvm::StringRef::npos) ? '\0' : format_str[sep_pos]; | ||||
1891 | llvm::StringRef key = format_str.substr(0, sep_pos); | ||||
1892 | |||||
1893 | const size_t n = parent->num_children; | ||||
1894 | for (size_t i = 0; i < n; ++i) { | ||||
1895 | const Definition *entry_def = parent->children + i; | ||||
1896 | if (key.equals(entry_def->name) || entry_def->name[0] == '*') { | ||||
1897 | llvm::StringRef value; | ||||
1898 | if (sep_char) | ||||
1899 | value = | ||||
1900 | format_str.substr(sep_pos + (entry_def->keep_separator ? 0 : 1)); | ||||
1901 | switch (entry_def->type) { | ||||
1902 | case FormatEntity::Entry::Type::ParentString: | ||||
1903 | entry.string = format_str.str(); | ||||
1904 | return error; // Success | ||||
1905 | |||||
1906 | case FormatEntity::Entry::Type::ParentNumber: | ||||
1907 | entry.number = entry_def->data; | ||||
1908 | return error; // Success | ||||
1909 | |||||
1910 | case FormatEntity::Entry::Type::EscapeCode: | ||||
1911 | entry.type = entry_def->type; | ||||
1912 | entry.string = entry_def->string; | ||||
1913 | return error; // Success | ||||
1914 | |||||
1915 | default: | ||||
1916 | entry.type = entry_def->type; | ||||
1917 | break; | ||||
1918 | } | ||||
1919 | |||||
1920 | if (value.empty()) { | ||||
1921 | if (entry_def->type == FormatEntity::Entry::Type::Invalid) { | ||||
1922 | if (entry_def->children) { | ||||
1923 | StreamString error_strm; | ||||
1924 | error_strm.Printf("'%s' can't be specified on its own, you must " | ||||
1925 | "access one of its children: ", | ||||
1926 | entry_def->name); | ||||
1927 | DumpCommaSeparatedChildEntryNames(error_strm, entry_def); | ||||
1928 | error.SetErrorStringWithFormat("%s", error_strm.GetData()); | ||||
1929 | } else if (sep_char == ':') { | ||||
1930 | // Any value whose separator is a with a ':' means this value has a | ||||
1931 | // string argument that needs to be stored in the entry (like | ||||
1932 | // "${script.var:}"). In this case the string value is the empty | ||||
1933 | // string which is ok. | ||||
1934 | } else { | ||||
1935 | error.SetErrorStringWithFormat("%s", "invalid entry definitions"); | ||||
1936 | } | ||||
1937 | } | ||||
1938 | } else { | ||||
1939 | if (entry_def->children) { | ||||
1940 | error = ParseEntry(value, entry_def, entry); | ||||
1941 | } else if (sep_char == ':') { | ||||
1942 | // Any value whose separator is a with a ':' means this value has a | ||||
1943 | // string argument that needs to be stored in the entry (like | ||||
1944 | // "${script.var:modulename.function}") | ||||
1945 | entry.string = value.str(); | ||||
1946 | } else { | ||||
1947 | error.SetErrorStringWithFormat( | ||||
1948 | "'%s' followed by '%s' but it has no children", key.str().c_str(), | ||||
1949 | value.str().c_str()); | ||||
1950 | } | ||||
1951 | } | ||||
1952 | return error; | ||||
1953 | } | ||||
1954 | } | ||||
1955 | StreamString error_strm; | ||||
1956 | if (parent->type == FormatEntity::Entry::Type::Root) | ||||
1957 | error_strm.Printf( | ||||
1958 | "invalid top level item '%s'. Valid top level items are: ", | ||||
1959 | key.str().c_str()); | ||||
1960 | else | ||||
1961 | error_strm.Printf("invalid member '%s' in '%s'. Valid members are: ", | ||||
1962 | key.str().c_str(), parent->name); | ||||
1963 | DumpCommaSeparatedChildEntryNames(error_strm, parent); | ||||
1964 | error.SetErrorStringWithFormat("%s", error_strm.GetData()); | ||||
1965 | return error; | ||||
1966 | } | ||||
1967 | |||||
1968 | static const Definition *FindEntry(const llvm::StringRef &format_str, | ||||
1969 | const Definition *parent, | ||||
1970 | llvm::StringRef &remainder) { | ||||
1971 | Status error; | ||||
1972 | |||||
1973 | std::pair<llvm::StringRef, llvm::StringRef> p = format_str.split('.'); | ||||
1974 | const size_t n = parent->num_children; | ||||
1975 | for (size_t i = 0; i < n; ++i) { | ||||
1976 | const Definition *entry_def = parent->children + i; | ||||
1977 | if (p.first.equals(entry_def->name) || entry_def->name[0] == '*') { | ||||
1978 | if (p.second.empty()) { | ||||
1979 | if (format_str.back() == '.') | ||||
1980 | remainder = format_str.drop_front(format_str.size() - 1); | ||||
1981 | else | ||||
1982 | remainder = llvm::StringRef(); // Exact match | ||||
1983 | return entry_def; | ||||
1984 | } else { | ||||
1985 | if (entry_def->children) { | ||||
1986 | return FindEntry(p.second, entry_def, remainder); | ||||
1987 | } else { | ||||
1988 | remainder = p.second; | ||||
1989 | return entry_def; | ||||
1990 | } | ||||
1991 | } | ||||
1992 | } | ||||
1993 | } | ||||
1994 | remainder = format_str; | ||||
1995 | return parent; | ||||
1996 | } | ||||
1997 | |||||
1998 | Status FormatEntity::ParseInternal(llvm::StringRef &format, Entry &parent_entry, | ||||
1999 | uint32_t depth) { | ||||
2000 | Status error; | ||||
2001 | while (!format.empty() && error.Success()) { | ||||
2002 | const size_t non_special_chars = format.find_first_of("${}\\"); | ||||
2003 | |||||
2004 | if (non_special_chars == llvm::StringRef::npos) { | ||||
2005 | // No special characters, just string bytes so add them and we are done | ||||
2006 | parent_entry.AppendText(format); | ||||
2007 | return error; | ||||
2008 | } | ||||
2009 | |||||
2010 | if (non_special_chars > 0) { | ||||
2011 | // We have a special character, so add all characters before these as a | ||||
2012 | // plain string | ||||
2013 | parent_entry.AppendText(format.substr(0, non_special_chars)); | ||||
2014 | format = format.drop_front(non_special_chars); | ||||
2015 | } | ||||
2016 | |||||
2017 | switch (format[0]) { | ||||
2018 | case '\0': | ||||
2019 | return error; | ||||
2020 | |||||
2021 | case '{': { | ||||
2022 | format = format.drop_front(); // Skip the '{' | ||||
2023 | Entry scope_entry(Entry::Type::Scope); | ||||
2024 | error = FormatEntity::ParseInternal(format, scope_entry, depth + 1); | ||||
2025 | if (error.Fail()) | ||||
2026 | return error; | ||||
2027 | parent_entry.AppendEntry(std::move(scope_entry)); | ||||
2028 | } break; | ||||
2029 | |||||
2030 | case '}': | ||||
2031 | if (depth == 0) | ||||
2032 | error.SetErrorString("unmatched '}' character"); | ||||
2033 | else | ||||
2034 | format = | ||||
2035 | format | ||||
2036 | .drop_front(); // Skip the '}' as we are at the end of the scope | ||||
2037 | return error; | ||||
2038 | |||||
2039 | case '\\': { | ||||
2040 | format = format.drop_front(); // Skip the '\' character | ||||
2041 | if (format.empty()) { | ||||
2042 | error.SetErrorString( | ||||
2043 | "'\\' character was not followed by another character"); | ||||
2044 | return error; | ||||
2045 | } | ||||
2046 | |||||
2047 | const char desens_char = format[0]; | ||||
2048 | format = format.drop_front(); // Skip the desensitized char character | ||||
2049 | switch (desens_char) { | ||||
2050 | case 'a': | ||||
2051 | parent_entry.AppendChar('\a'); | ||||
2052 | break; | ||||
2053 | case 'b': | ||||
2054 | parent_entry.AppendChar('\b'); | ||||
2055 | break; | ||||
2056 | case 'f': | ||||
2057 | parent_entry.AppendChar('\f'); | ||||
2058 | break; | ||||
2059 | case 'n': | ||||
2060 | parent_entry.AppendChar('\n'); | ||||
2061 | break; | ||||
2062 | case 'r': | ||||
2063 | parent_entry.AppendChar('\r'); | ||||
2064 | break; | ||||
2065 | case 't': | ||||
2066 | parent_entry.AppendChar('\t'); | ||||
2067 | break; | ||||
2068 | case 'v': | ||||
2069 | parent_entry.AppendChar('\v'); | ||||
2070 | break; | ||||
2071 | case '\'': | ||||
2072 | parent_entry.AppendChar('\''); | ||||
2073 | break; | ||||
2074 | case '\\': | ||||
2075 | parent_entry.AppendChar('\\'); | ||||
2076 | break; | ||||
2077 | case '0': | ||||
2078 | // 1 to 3 octal chars | ||||
2079 | { | ||||
2080 | // Make a string that can hold onto the initial zero char, up to 3 | ||||
2081 | // octal digits, and a terminating NULL. | ||||
2082 | char oct_str[5] = {0, 0, 0, 0, 0}; | ||||
2083 | |||||
2084 | int i; | ||||
2085 | for (i = 0; (format[i] >= '0' && format[i] <= '7') && i < 4; ++i) | ||||
2086 | oct_str[i] = format[i]; | ||||
2087 | |||||
2088 | // We don't want to consume the last octal character since the main | ||||
2089 | // for loop will do this for us, so we advance p by one less than i | ||||
2090 | // (even if i is zero) | ||||
2091 | format = format.drop_front(i); | ||||
2092 | unsigned long octal_value = ::strtoul(oct_str, nullptr, 8); | ||||
2093 | if (octal_value <= UINT8_MAX(255)) { | ||||
2094 | parent_entry.AppendChar((char)octal_value); | ||||
2095 | } else { | ||||
2096 | error.SetErrorString("octal number is larger than a single byte"); | ||||
2097 | return error; | ||||
2098 | } | ||||
2099 | } | ||||
2100 | break; | ||||
2101 | |||||
2102 | case 'x': | ||||
2103 | // hex number in the format | ||||
2104 | if (isxdigit(format[0])) { | ||||
2105 | // Make a string that can hold onto two hex chars plus a | ||||
2106 | // NULL terminator | ||||
2107 | char hex_str[3] = {0, 0, 0}; | ||||
2108 | hex_str[0] = format[0]; | ||||
2109 | |||||
2110 | format = format.drop_front(); | ||||
2111 | |||||
2112 | if (isxdigit(format[0])) { | ||||
2113 | hex_str[1] = format[0]; | ||||
2114 | format = format.drop_front(); | ||||
2115 | } | ||||
2116 | |||||
2117 | unsigned long hex_value = strtoul(hex_str, nullptr, 16); | ||||
2118 | if (hex_value <= UINT8_MAX(255)) { | ||||
2119 | parent_entry.AppendChar((char)hex_value); | ||||
2120 | } else { | ||||
2121 | error.SetErrorString("hex number is larger than a single byte"); | ||||
2122 | return error; | ||||
2123 | } | ||||
2124 | } else { | ||||
2125 | parent_entry.AppendChar(desens_char); | ||||
2126 | } | ||||
2127 | break; | ||||
2128 | |||||
2129 | default: | ||||
2130 | // Just desensitize any other character by just printing what came | ||||
2131 | // after the '\' | ||||
2132 | parent_entry.AppendChar(desens_char); | ||||
2133 | break; | ||||
2134 | } | ||||
2135 | } break; | ||||
2136 | |||||
2137 | case '$': | ||||
2138 | if (format.size() == 1) { | ||||
2139 | // '$' at the end of a format string, just print the '$' | ||||
2140 | parent_entry.AppendText("$"); | ||||
2141 | } else { | ||||
2142 | format = format.drop_front(); // Skip the '$' | ||||
2143 | |||||
2144 | if (format[0] == '{') { | ||||
2145 | format = format.drop_front(); // Skip the '{' | ||||
2146 | |||||
2147 | llvm::StringRef variable, variable_format; | ||||
2148 | error = FormatEntity::ExtractVariableInfo(format, variable, | ||||
2149 | variable_format); | ||||
2150 | if (error.Fail()) | ||||
2151 | return error; | ||||
2152 | bool verify_is_thread_id = false; | ||||
2153 | Entry entry; | ||||
2154 | if (!variable_format.empty()) { | ||||
2155 | entry.printf_format = variable_format.str(); | ||||
2156 | |||||
2157 | // If the format contains a '%' we are going to assume this is a | ||||
2158 | // printf style format. So if you want to format your thread ID | ||||
2159 | // using "0x%llx" you can use: ${thread.id%0x%llx} | ||||
2160 | // | ||||
2161 | // If there is no '%' in the format, then it is assumed to be a | ||||
2162 | // LLDB format name, or one of the extended formats specified in | ||||
2163 | // the switch statement below. | ||||
2164 | |||||
2165 | if (entry.printf_format.find('%') == std::string::npos) { | ||||
2166 | bool clear_printf = false; | ||||
2167 | |||||
2168 | if (FormatManager::GetFormatFromCString( | ||||
2169 | entry.printf_format.c_str(), false, entry.fmt)) { | ||||
2170 | // We have an LLDB format, so clear the printf format | ||||
2171 | clear_printf = true; | ||||
2172 | } else if (entry.printf_format.size() == 1) { | ||||
2173 | switch (entry.printf_format[0]) { | ||||
2174 | case '@': // if this is an @ sign, print ObjC description | ||||
2175 | entry.number = ValueObject:: | ||||
2176 | eValueObjectRepresentationStyleLanguageSpecific; | ||||
2177 | clear_printf = true; | ||||
2178 | break; | ||||
2179 | case 'V': // if this is a V, print the value using the default | ||||
2180 | // format | ||||
2181 | entry.number = | ||||
2182 | ValueObject::eValueObjectRepresentationStyleValue; | ||||
2183 | clear_printf = true; | ||||
2184 | break; | ||||
2185 | case 'L': // if this is an L, print the location of the value | ||||
2186 | entry.number = | ||||
2187 | ValueObject::eValueObjectRepresentationStyleLocation; | ||||
2188 | clear_printf = true; | ||||
2189 | break; | ||||
2190 | case 'S': // if this is an S, print the summary after all | ||||
2191 | entry.number = | ||||
2192 | ValueObject::eValueObjectRepresentationStyleSummary; | ||||
2193 | clear_printf = true; | ||||
2194 | break; | ||||
2195 | case '#': // if this is a '#', print the number of children | ||||
2196 | entry.number = | ||||
2197 | ValueObject::eValueObjectRepresentationStyleChildrenCount; | ||||
2198 | clear_printf = true; | ||||
2199 | break; | ||||
2200 | case 'T': // if this is a 'T', print the type | ||||
2201 | entry.number = | ||||
2202 | ValueObject::eValueObjectRepresentationStyleType; | ||||
2203 | clear_printf = true; | ||||
2204 | break; | ||||
2205 | case 'N': // if this is a 'N', print the name | ||||
2206 | entry.number = | ||||
2207 | ValueObject::eValueObjectRepresentationStyleName; | ||||
2208 | clear_printf = true; | ||||
2209 | break; | ||||
2210 | case '>': // if this is a '>', print the expression path | ||||
2211 | entry.number = ValueObject:: | ||||
2212 | eValueObjectRepresentationStyleExpressionPath; | ||||
2213 | clear_printf = true; | ||||
2214 | break; | ||||
2215 | default: | ||||
2216 | error.SetErrorStringWithFormat("invalid format: '%s'", | ||||
2217 | entry.printf_format.c_str()); | ||||
2218 | return error; | ||||
2219 | } | ||||
2220 | } else if (FormatManager::GetFormatFromCString( | ||||
2221 | entry.printf_format.c_str(), true, entry.fmt)) { | ||||
2222 | clear_printf = true; | ||||
2223 | } else if (entry.printf_format == "tid") { | ||||
2224 | verify_is_thread_id = true; | ||||
2225 | } else { | ||||
2226 | error.SetErrorStringWithFormat("invalid format: '%s'", | ||||
2227 | entry.printf_format.c_str()); | ||||
2228 | return error; | ||||
2229 | } | ||||
2230 | |||||
2231 | // Our format string turned out to not be a printf style format | ||||
2232 | // so lets clear the string | ||||
2233 | if (clear_printf) | ||||
2234 | entry.printf_format.clear(); | ||||
2235 | } | ||||
2236 | } | ||||
2237 | |||||
2238 | // Check for dereferences | ||||
2239 | if (variable[0] == '*') { | ||||
2240 | entry.deref = true; | ||||
2241 | variable = variable.drop_front(); | ||||
2242 | } | ||||
2243 | |||||
2244 | error = ParseEntry(variable, &g_root, entry); | ||||
2245 | if (error.Fail()) | ||||
2246 | return error; | ||||
2247 | |||||
2248 | if (verify_is_thread_id) { | ||||
2249 | if (entry.type != Entry::Type::ThreadID && | ||||
2250 | entry.type != Entry::Type::ThreadProtocolID) { | ||||
2251 | error.SetErrorString("the 'tid' format can only be used on " | ||||
2252 | "${thread.id} and ${thread.protocol_id}"); | ||||
2253 | } | ||||
2254 | } | ||||
2255 | |||||
2256 | switch (entry.type) { | ||||
2257 | case Entry::Type::Variable: | ||||
2258 | case Entry::Type::VariableSynthetic: | ||||
2259 | if (entry.number == 0) { | ||||
2260 | if (entry.string.empty()) | ||||
2261 | entry.number = | ||||
2262 | ValueObject::eValueObjectRepresentationStyleValue; | ||||
2263 | else | ||||
2264 | entry.number = | ||||
2265 | ValueObject::eValueObjectRepresentationStyleSummary; | ||||
2266 | } | ||||
2267 | break; | ||||
2268 | default: | ||||
2269 | // Make sure someone didn't try to dereference anything but ${var} | ||||
2270 | // or ${svar} | ||||
2271 | if (entry.deref) { | ||||
2272 | error.SetErrorStringWithFormat( | ||||
2273 | "${%s} can't be dereferenced, only ${var} and ${svar} can.", | ||||
2274 | variable.str().c_str()); | ||||
2275 | return error; | ||||
2276 | } | ||||
2277 | } | ||||
2278 | parent_entry.AppendEntry(std::move(entry)); | ||||
2279 | } | ||||
2280 | } | ||||
2281 | break; | ||||
2282 | } | ||||
2283 | } | ||||
2284 | return error; | ||||
2285 | } | ||||
2286 | |||||
2287 | Status FormatEntity::ExtractVariableInfo(llvm::StringRef &format_str, | ||||
2288 | llvm::StringRef &variable_name, | ||||
2289 | llvm::StringRef &variable_format) { | ||||
2290 | Status error; | ||||
2291 | variable_name = llvm::StringRef(); | ||||
2292 | variable_format = llvm::StringRef(); | ||||
2293 | |||||
2294 | const size_t paren_pos = format_str.find('}'); | ||||
2295 | if (paren_pos != llvm::StringRef::npos) { | ||||
2296 | const size_t percent_pos = format_str.find('%'); | ||||
2297 | if (percent_pos < paren_pos) { | ||||
2298 | if (percent_pos > 0) { | ||||
2299 | if (percent_pos > 1) | ||||
2300 | variable_name = format_str.substr(0, percent_pos); | ||||
2301 | variable_format = | ||||
2302 | format_str.substr(percent_pos + 1, paren_pos - (percent_pos + 1)); | ||||
2303 | } | ||||
2304 | } else { | ||||
2305 | variable_name = format_str.substr(0, paren_pos); | ||||
2306 | } | ||||
2307 | // Strip off elements and the formatting and the trailing '}' | ||||
2308 | format_str = format_str.substr(paren_pos + 1); | ||||
2309 | } else { | ||||
2310 | error.SetErrorStringWithFormat( | ||||
2311 | "missing terminating '}' character for '${%s'", | ||||
2312 | format_str.str().c_str()); | ||||
2313 | } | ||||
2314 | return error; | ||||
2315 | } | ||||
2316 | |||||
2317 | bool FormatEntity::FormatFileSpec(const FileSpec &file_spec, Stream &s, | ||||
2318 | llvm::StringRef variable_name, | ||||
2319 | llvm::StringRef variable_format) { | ||||
2320 | if (variable_name.empty() || variable_name.equals(".fullpath")) { | ||||
2321 | file_spec.Dump(s.AsRawOstream()); | ||||
2322 | return true; | ||||
2323 | } else if (variable_name.equals(".basename")) { | ||||
2324 | s.PutCString(file_spec.GetFilename().GetStringRef()); | ||||
2325 | return true; | ||||
2326 | } else if (variable_name.equals(".dirname")) { | ||||
2327 | s.PutCString(file_spec.GetFilename().GetStringRef()); | ||||
2328 | return true; | ||||
2329 | } | ||||
2330 | return false; | ||||
2331 | } | ||||
2332 | |||||
2333 | static std::string MakeMatch(const llvm::StringRef &prefix, | ||||
2334 | const char *suffix) { | ||||
2335 | std::string match(prefix.str()); | ||||
2336 | match.append(suffix); | ||||
2337 | return match; | ||||
2338 | } | ||||
2339 | |||||
2340 | static void AddMatches(const Definition *def, const llvm::StringRef &prefix, | ||||
2341 | const llvm::StringRef &match_prefix, | ||||
2342 | StringList &matches) { | ||||
2343 | const size_t n = def->num_children; | ||||
2344 | if (n > 0) { | ||||
2345 | for (size_t i = 0; i < n; ++i) { | ||||
2346 | std::string match = prefix.str(); | ||||
2347 | if (match_prefix.empty()) | ||||
2348 | matches.AppendString(MakeMatch(prefix, def->children[i].name)); | ||||
2349 | else if (strncmp(def->children[i].name, match_prefix.data(), | ||||
2350 | match_prefix.size()) == 0) | ||||
2351 | matches.AppendString( | ||||
2352 | MakeMatch(prefix, def->children[i].name + match_prefix.size())); | ||||
2353 | } | ||||
2354 | } | ||||
2355 | } | ||||
2356 | |||||
2357 | void FormatEntity::AutoComplete(CompletionRequest &request) { | ||||
2358 | llvm::StringRef str = request.GetCursorArgumentPrefix(); | ||||
2359 | |||||
2360 | const size_t dollar_pos = str.rfind('$'); | ||||
2361 | if (dollar_pos == llvm::StringRef::npos) | ||||
2362 | return; | ||||
2363 | |||||
2364 | // Hitting TAB after $ at the end of the string add a "{" | ||||
2365 | if (dollar_pos == str.size() - 1) { | ||||
2366 | std::string match = str.str(); | ||||
2367 | match.append("{"); | ||||
2368 | request.AddCompletion(match); | ||||
2369 | return; | ||||
2370 | } | ||||
2371 | |||||
2372 | if (str[dollar_pos + 1] != '{') | ||||
2373 | return; | ||||
2374 | |||||
2375 | const size_t close_pos = str.find('}', dollar_pos + 2); | ||||
2376 | if (close_pos != llvm::StringRef::npos) | ||||
2377 | return; | ||||
2378 | |||||
2379 | const size_t format_pos = str.find('%', dollar_pos + 2); | ||||
2380 | if (format_pos != llvm::StringRef::npos) | ||||
2381 | return; | ||||
2382 | |||||
2383 | llvm::StringRef partial_variable(str.substr(dollar_pos + 2)); | ||||
2384 | if (partial_variable.empty()) { | ||||
2385 | // Suggest all top level entities as we are just past "${" | ||||
2386 | StringList new_matches; | ||||
2387 | AddMatches(&g_root, str, llvm::StringRef(), new_matches); | ||||
2388 | request.AddCompletions(new_matches); | ||||
2389 | return; | ||||
2390 | } | ||||
2391 | |||||
2392 | // We have a partially specified variable, find it | ||||
2393 | llvm::StringRef remainder; | ||||
2394 | const Definition *entry_def = FindEntry(partial_variable, &g_root, remainder); | ||||
2395 | if (!entry_def) | ||||
2396 | return; | ||||
2397 | |||||
2398 | const size_t n = entry_def->num_children; | ||||
2399 | |||||
2400 | if (remainder.empty()) { | ||||
2401 | // Exact match | ||||
2402 | if (n > 0) { | ||||
2403 | // "${thread.info" <TAB> | ||||
2404 | request.AddCompletion(MakeMatch(str, ".")); | ||||
2405 | } else { | ||||
2406 | // "${thread.id" <TAB> | ||||
2407 | request.AddCompletion(MakeMatch(str, "}")); | ||||
2408 | } | ||||
2409 | } else if (remainder.equals(".")) { | ||||
2410 | // "${thread." <TAB> | ||||
2411 | StringList new_matches; | ||||
2412 | AddMatches(entry_def, str, llvm::StringRef(), new_matches); | ||||
2413 | request.AddCompletions(new_matches); | ||||
2414 | } else { | ||||
2415 | // We have a partial match | ||||
2416 | // "${thre" <TAB> | ||||
2417 | StringList new_matches; | ||||
2418 | AddMatches(entry_def, str, remainder, new_matches); | ||||
2419 | request.AddCompletions(new_matches); | ||||
2420 | } | ||||
2421 | } | ||||
2422 | |||||
2423 | void FormatEntity::PrettyPrintFunctionArguments( | ||||
2424 | Stream &out_stream, VariableList const &args, | ||||
2425 | ExecutionContextScope *exe_scope) { | ||||
2426 | const size_t num_args = args.GetSize(); | ||||
2427 | for (size_t arg_idx = 0; arg_idx < num_args; ++arg_idx) { | ||||
2428 | std::string buffer; | ||||
2429 | |||||
2430 | VariableSP var_sp(args.GetVariableAtIndex(arg_idx)); | ||||
2431 | ValueObjectSP var_value_sp(ValueObjectVariable::Create(exe_scope, var_sp)); | ||||
2432 | StreamString ss; | ||||
2433 | llvm::StringRef var_representation; | ||||
2434 | const char *var_name = var_value_sp->GetName().GetCString(); | ||||
2435 | if (var_value_sp->GetCompilerType().IsValid()) { | ||||
2436 | if (exe_scope && exe_scope->CalculateTarget()) | ||||
2437 | var_value_sp = var_value_sp->GetQualifiedRepresentationIfAvailable( | ||||
2438 | exe_scope->CalculateTarget() | ||||
2439 | ->TargetProperties::GetPreferDynamicValue(), | ||||
2440 | exe_scope->CalculateTarget() | ||||
2441 | ->TargetProperties::GetEnableSyntheticValue()); | ||||
2442 | if (var_value_sp->GetCompilerType().IsAggregateType() && | ||||
2443 | DataVisualization::ShouldPrintAsOneLiner(*var_value_sp)) { | ||||
2444 | static StringSummaryFormat format(TypeSummaryImpl::Flags() | ||||
2445 | .SetHideItemNames(false) | ||||
2446 | .SetShowMembersOneLiner(true), | ||||
2447 | ""); | ||||
2448 | format.FormatObject(var_value_sp.get(), buffer, TypeSummaryOptions()); | ||||
2449 | var_representation = buffer; | ||||
2450 | } else | ||||
2451 | var_value_sp->DumpPrintableRepresentation( | ||||
2452 | ss, | ||||
2453 | ValueObject::ValueObjectRepresentationStyle:: | ||||
2454 | eValueObjectRepresentationStyleSummary, | ||||
2455 | eFormatDefault, | ||||
2456 | ValueObject::PrintableRepresentationSpecialCases::eAllow, false); | ||||
2457 | } | ||||
2458 | |||||
2459 | if (!ss.GetString().empty()) | ||||
2460 | var_representation = ss.GetString(); | ||||
2461 | if (arg_idx > 0) | ||||
2462 | out_stream.PutCString(", "); | ||||
2463 | if (var_value_sp->GetError().Success()) { | ||||
2464 | if (!var_representation.empty()) | ||||
2465 | out_stream.Printf("%s=%s", var_name, var_representation.str().c_str()); | ||||
2466 | else | ||||
2467 | out_stream.Printf("%s=%s at %s", var_name, | ||||
2468 | var_value_sp->GetTypeName().GetCString(), | ||||
2469 | var_value_sp->GetLocationAsCString()); | ||||
2470 | } else | ||||
2471 | out_stream.Printf("%s=<unavailable>", var_name); | ||||
2472 | } | ||||
2473 | } |