Coverage Report

Created: 2022-01-25 06:29

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