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