diff options
| author | Dimitry Andric <dim@FreeBSD.org> | 2020-01-17 20:45:01 +0000 |
|---|---|---|
| committer | Dimitry Andric <dim@FreeBSD.org> | 2020-01-17 20:45:01 +0000 |
| commit | 706b4fc47bbc608932d3b491ae19a3b9cde9497b (patch) | |
| tree | 4adf86a776049cbf7f69a1929c4babcbbef925eb /lldb/source/Core | |
| parent | 7cc9cf2bf09f069cb2dd947ead05d0b54301fb71 (diff) | |
Notes
Diffstat (limited to 'lldb/source/Core')
23 files changed, 4545 insertions, 4470 deletions
diff --git a/lldb/source/Core/Address.cpp b/lldb/source/Core/Address.cpp index a3912bef5a6e..b9a7b4a3894a 100644 --- a/lldb/source/Core/Address.cpp +++ b/lldb/source/Core/Address.cpp @@ -418,13 +418,13 @@ bool Address::Dump(Stream *s, ExecutionContextScope *exe_scope, DumpStyle style, section_sp->DumpName(s); s->Printf(" + %" PRIu64, m_offset); } else { - s->Address(m_offset, addr_size); + DumpAddress(s->AsRawOstream(), m_offset, addr_size); } break; case DumpStyleSectionPointerOffset: s->Printf("(Section *)%p + ", static_cast<void *>(section_sp.get())); - s->Address(m_offset, addr_size); + DumpAddress(s->AsRawOstream(), m_offset, addr_size); break; case DumpStyleModuleWithFileAddress: @@ -444,7 +444,7 @@ bool Address::Dump(Stream *s, ExecutionContextScope *exe_scope, DumpStyle style, return Dump(s, exe_scope, fallback_style, DumpStyleInvalid, addr_size); return false; } - s->Address(file_addr, addr_size); + DumpAddress(s->AsRawOstream(), file_addr, addr_size); if (style == DumpStyleModuleWithFileAddress && section_sp) s->PutChar(']'); } break; @@ -472,7 +472,7 @@ bool Address::Dump(Stream *s, ExecutionContextScope *exe_scope, DumpStyle style, return Dump(s, exe_scope, fallback_style, DumpStyleInvalid, addr_size); return false; } - s->Address(load_addr, addr_size); + DumpAddress(s->AsRawOstream(), load_addr, addr_size); } break; case DumpStyleResolvedDescription: @@ -712,22 +712,20 @@ bool Address::Dump(Stream *s, ExecutionContextScope *exe_scope, DumpStyle style, [](Variable *) { return true; }, &variable_list); - const size_t num_variables = variable_list.GetSize(); - for (size_t var_idx = 0; var_idx < num_variables; ++var_idx) { - Variable *var = variable_list.GetVariableAtIndex(var_idx).get(); - if (var && var->LocationIsValidForAddress(*this)) { + for (const VariableSP &var_sp : variable_list) { + if (var_sp && var_sp->LocationIsValidForAddress(*this)) { s->Indent(); s->Printf(" Variable: id = {0x%8.8" PRIx64 "}, name = \"%s\"", - var->GetID(), var->GetName().GetCString()); - Type *type = var->GetType(); + var_sp->GetID(), var_sp->GetName().GetCString()); + Type *type = var_sp->GetType(); if (type) s->Printf(", type = \"%s\"", type->GetName().GetCString()); else s->PutCString(", type = <unknown>"); s->PutCString(", location = "); - var->DumpLocationForAddress(s, *this); + var_sp->DumpLocationForAddress(s, *this); s->PutCString(", decl = "); - var->GetDeclaration().DumpStopContext(s, false); + var_sp->GetDeclaration().DumpStopContext(s, false); s->EOL(); } } @@ -756,7 +754,8 @@ bool Address::Dump(Stream *s, ExecutionContextScope *exe_scope, DumpStyle style, if (dereferenced_addr.Dump(&strm, exe_scope, DumpStyleResolvedDescription, DumpStyleInvalid, addr_size)) { - s->Address(dereferenced_load_addr, addr_size, " -> ", " "); + DumpAddress(s->AsRawOstream(), dereferenced_load_addr, addr_size, + " -> ", " "); s->Write(strm.GetString().data(), strm.GetSize()); return true; } diff --git a/lldb/source/Core/AddressRange.cpp b/lldb/source/Core/AddressRange.cpp index 71eec3c19607..83a1e54157d8 100644 --- a/lldb/source/Core/AddressRange.cpp +++ b/lldb/source/Core/AddressRange.cpp @@ -161,7 +161,8 @@ bool AddressRange::Dump(Stream *s, Target *target, Address::DumpStyle style, s->PutChar('['); m_base_addr.Dump(s, target, style, fallback_style); s->PutChar('-'); - s->Address(m_base_addr.GetOffset() + GetByteSize(), addr_size); + DumpAddress(s->AsRawOstream(), m_base_addr.GetOffset() + GetByteSize(), + addr_size); s->PutChar(')'); return true; break; @@ -185,7 +186,8 @@ bool AddressRange::Dump(Stream *s, Target *target, Address::DumpStyle style, s->Printf("%s", module_sp->GetFileSpec().GetFilename().AsCString( "<Unknown>")); } - s->AddressRange(vmaddr, vmaddr + GetByteSize(), addr_size); + DumpAddressRange(s->AsRawOstream(), vmaddr, vmaddr + GetByteSize(), + addr_size); return true; } else if (fallback_style != Address::DumpStyleInvalid) { return Dump(s, target, fallback_style, Address::DumpStyleInvalid); diff --git a/lldb/source/Core/AddressResolverFileLine.cpp b/lldb/source/Core/AddressResolverFileLine.cpp index 4a14260c6c72..4122b5d3b747 100644 --- a/lldb/source/Core/AddressResolverFileLine.cpp +++ b/lldb/source/Core/AddressResolverFileLine.cpp @@ -40,14 +40,13 @@ Searcher::CallbackReturn AddressResolverFileLine::SearchCallback(SearchFilter &filter, SymbolContext &context, Address *addr) { SymbolContextList sc_list; - uint32_t sc_list_size; CompileUnit *cu = context.comp_unit; Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS)); - sc_list_size = - cu->ResolveSymbolContext(m_file_spec, m_line_number, m_inlines, false, - eSymbolContextEverything, sc_list); + cu->ResolveSymbolContext(m_file_spec, m_line_number, m_inlines, false, + eSymbolContextEverything, sc_list); + uint32_t sc_list_size = sc_list.GetSize(); for (uint32_t i = 0; i < sc_list_size; i++) { SymbolContext sc; if (sc_list.GetContextAtIndex(i, sc)) { diff --git a/lldb/source/Core/Debugger.cpp b/lldb/source/Core/Debugger.cpp index 18397d00dcaa..33f72a0896cb 100644 --- a/lldb/source/Core/Debugger.cpp +++ b/lldb/source/Core/Debugger.cpp @@ -708,8 +708,8 @@ Debugger::Debugger(lldb::LogOutputCallback log_callback, void *baton) m_source_manager_up(), m_source_file_cache(), m_command_interpreter_up( std::make_unique<CommandInterpreter>(*this, false)), - m_script_interpreter_sp(), m_input_reader_stack(), m_instance_name(), - m_loaded_plugins(), m_event_handler_thread(), m_io_handler_thread(), + m_input_reader_stack(), m_instance_name(), m_loaded_plugins(), + m_event_handler_thread(), m_io_handler_thread(), m_sync_broadcaster(nullptr, "lldb.debugger.sync"), m_forward_listener_sp(), m_clear_once() { char instance_cstr[256]; @@ -1198,17 +1198,21 @@ bool Debugger::EnableLog(llvm::StringRef channel, error_stream); } -ScriptInterpreter *Debugger::GetScriptInterpreter(bool can_create) { +ScriptInterpreter * +Debugger::GetScriptInterpreter(bool can_create, + llvm::Optional<lldb::ScriptLanguage> language) { std::lock_guard<std::recursive_mutex> locker(m_script_interpreter_mutex); + lldb::ScriptLanguage script_language = + language ? *language : GetScriptLanguage(); - if (!m_script_interpreter_sp) { + if (!m_script_interpreters[script_language]) { if (!can_create) return nullptr; - m_script_interpreter_sp = PluginManager::GetScriptInterpreterForLanguage( - GetScriptLanguage(), *this); + m_script_interpreters[script_language] = + PluginManager::GetScriptInterpreterForLanguage(script_language, *this); } - return m_script_interpreter_sp.get(); + return m_script_interpreters[script_language].get(); } SourceManager &Debugger::GetSourceManager() { @@ -1452,7 +1456,7 @@ void Debugger::DefaultEventHandler() { done = true; } else if (event_type & CommandInterpreter::eBroadcastBitAsynchronousErrorData) { - const char *data = reinterpret_cast<const char *>( + const char *data = static_cast<const char *>( EventDataBytes::GetBytesFromEvent(event_sp.get())); if (data && data[0]) { StreamSP error_sp(GetAsyncErrorStream()); @@ -1463,7 +1467,7 @@ void Debugger::DefaultEventHandler() { } } else if (event_type & CommandInterpreter:: eBroadcastBitAsynchronousOutputData) { - const char *data = reinterpret_cast<const char *>( + const char *data = static_cast<const char *>( EventDataBytes::GetBytesFromEvent(event_sp.get())); if (data && data[0]) { StreamSP output_sp(GetAsyncOutputStream()); diff --git a/lldb/source/Core/Disassembler.cpp b/lldb/source/Core/Disassembler.cpp index 89ae25cbad64..33172cc8868e 100644 --- a/lldb/source/Core/Disassembler.cpp +++ b/lldb/source/Core/Disassembler.cpp @@ -1101,15 +1101,22 @@ void InstructionList::Append(lldb::InstructionSP &inst_sp) { uint32_t InstructionList::GetIndexOfNextBranchInstruction(uint32_t start, Target &target, - bool ignore_calls) const { + bool ignore_calls, + bool *found_calls) const { size_t num_instructions = m_instructions.size(); uint32_t next_branch = UINT32_MAX; size_t i; + + if (found_calls) + *found_calls = false; for (i = start; i < num_instructions; i++) { if (m_instructions[i]->DoesBranch()) { - if (ignore_calls && m_instructions[i]->IsCall()) + if (ignore_calls && m_instructions[i]->IsCall()) { + if (found_calls) + *found_calls = true; continue; + } next_branch = i; break; } diff --git a/lldb/source/Core/DumpDataExtractor.cpp b/lldb/source/Core/DumpDataExtractor.cpp index 12e98de2675c..b5e1071b72ae 100644 --- a/lldb/source/Core/DumpDataExtractor.cpp +++ b/lldb/source/Core/DumpDataExtractor.cpp @@ -467,9 +467,10 @@ lldb::offset_t lldb_private::DumpDataExtractor( } break; case eFormatPointer: - s->Address(DE.GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, - item_bit_offset), - sizeof(addr_t)); + DumpAddress(s->AsRawOstream(), + DE.GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, + item_bit_offset), + sizeof(addr_t)); break; case eFormatComplexInteger: { diff --git a/lldb/source/Core/FileLineResolver.cpp b/lldb/source/Core/FileLineResolver.cpp index 01df295398a8..7d91d1a3e472 100644 --- a/lldb/source/Core/FileLineResolver.cpp +++ b/lldb/source/Core/FileLineResolver.cpp @@ -36,8 +36,8 @@ FileLineResolver::SearchCallback(SearchFilter &filter, SymbolContext &context, Address *addr) { CompileUnit *cu = context.comp_unit; - if (m_inlines || - m_file_spec.Compare(*cu, m_file_spec, (bool)m_file_spec.GetDirectory())) { + if (m_inlines || m_file_spec.Compare(cu->GetPrimaryFile(), m_file_spec, + (bool)m_file_spec.GetDirectory())) { uint32_t start_file_idx = 0; uint32_t file_idx = cu->GetSupportFiles().FindFileIndex(start_file_idx, m_file_spec, false); diff --git a/lldb/source/Core/FileSpecList.cpp b/lldb/source/Core/FileSpecList.cpp index 95133faf7502..6651324fa362 100644 --- a/lldb/source/Core/FileSpecList.cpp +++ b/lldb/source/Core/FileSpecList.cpp @@ -47,7 +47,7 @@ void FileSpecList::Clear() { m_files.clear(); } void FileSpecList::Dump(Stream *s, const char *separator_cstr) const { collection::const_iterator pos, end = m_files.end(); for (pos = m_files.begin(); pos != end; ++pos) { - pos->Dump(s); + pos->Dump(s->AsRawOstream()); if (separator_cstr && ((pos + 1) != end)) s->PutCString(separator_cstr); } diff --git a/lldb/source/Core/FormatEntity.cpp b/lldb/source/Core/FormatEntity.cpp index c90828f40989..7aa1eced34f3 100644 --- a/lldb/source/Core/FormatEntity.cpp +++ b/lldb/source/Core/FormatEntity.cpp @@ -125,6 +125,7 @@ static FormatEntity::Entry::Definition g_function_child_entries[] = { ENTRY("name", FunctionName), ENTRY("name-without-args", FunctionNameNoArgs), ENTRY("name-with-args", FunctionNameWithArgs), + ENTRY("mangled-name", FunctionMangledName), ENTRY("addr-offset", FunctionAddrOffset), ENTRY("concrete-only-addr-offset-no-padding", FunctionAddrOffsetConcrete), ENTRY("line-offset", FunctionLineOffset), @@ -351,6 +352,7 @@ const char *FormatEntity::Entry::TypeToCString(Type t) { ENUM_TO_CSTR(FunctionName); ENUM_TO_CSTR(FunctionNameWithArgs); ENUM_TO_CSTR(FunctionNameNoArgs); + ENUM_TO_CSTR(FunctionMangledName); ENUM_TO_CSTR(FunctionAddrOffset); ENUM_TO_CSTR(FunctionAddrOffsetConcrete); ENUM_TO_CSTR(FunctionLineOffset); @@ -414,9 +416,10 @@ static bool RunScriptFormatKeyword(Stream &s, const SymbolContext *sc, return false; } -static bool DumpAddress(Stream &s, const SymbolContext *sc, - const ExecutionContext *exe_ctx, const Address &addr, - bool print_file_addr_or_load_addr) { +static bool DumpAddressAndContent(Stream &s, const SymbolContext *sc, + const ExecutionContext *exe_ctx, + const Address &addr, + bool print_file_addr_or_load_addr) { Target *target = Target::GetTargetFromContexts(exe_ctx, sc); addr_t vaddr = LLDB_INVALID_ADDRESS; if (exe_ctx && !target->GetSectionLoadList().IsEmpty()) @@ -1145,9 +1148,10 @@ bool FormatEntity::Format(const Entry &entry, Stream &s, case Entry::Type::AddressFile: case Entry::Type::AddressLoad: case Entry::Type::AddressLoadOrFile: - return (addr != nullptr && addr->IsValid() && - DumpAddress(s, sc, exe_ctx, *addr, - entry.type == Entry::Type::AddressLoadOrFile)); + return ( + addr != nullptr && addr->IsValid() && + DumpAddressAndContent(s, sc, exe_ctx, *addr, + entry.type == Entry::Type::AddressLoadOrFile)); case Entry::Type::ProcessID: if (exe_ctx) { @@ -1376,8 +1380,7 @@ bool FormatEntity::Format(const Entry &entry, Stream &s, if (sc) { CompileUnit *cu = sc->comp_unit; if (cu) { - // CompileUnit is a FileSpec - if (DumpFile(s, *cu, (FileKind)entry.number)) + if (DumpFile(s, cu->GetPrimaryFile(), (FileKind)entry.number)) return true; } } @@ -1416,7 +1419,7 @@ bool FormatEntity::Format(const Entry &entry, Stream &s, if (frame) { const Address &pc_addr = frame->GetFrameCodeAddress(); if (pc_addr.IsValid()) { - if (DumpAddress(s, sc, exe_ctx, pc_addr, false)) + if (DumpAddressAndContent(s, sc, exe_ctx, pc_addr, false)) return true; } } @@ -1744,6 +1747,31 @@ bool FormatEntity::Format(const Entry &entry, Stream &s, } return false; + case Entry::Type::FunctionMangledName: { + const char *name = nullptr; + if (sc->symbol) + name = sc->symbol->GetMangled() + .GetName(sc->symbol->GetLanguage(), Mangled::ePreferMangled) + .AsCString(); + else if (sc->function) + name = sc->function->GetMangled() + .GetName(sc->symbol->GetLanguage(), Mangled::ePreferMangled) + .AsCString(); + + if (!name) + return false; + s.PutCString(name); + + if (sc->block->GetContainingInlinedBlock()) { + if (const InlineFunctionInfo *inline_info = + sc->block->GetInlinedFunctionInfo()) { + s.PutCString(" [inlined] "); + inline_info->GetName(sc->function->GetLanguage()).Dump(&s); + } + } + return true; + } + case Entry::Type::FunctionAddrOffset: if (addr) { if (DumpAddressOffsetFromFunction(s, sc, exe_ctx, *addr, false, false, @@ -1828,7 +1856,7 @@ bool FormatEntity::Format(const Entry &entry, Stream &s, if (entry.type == Entry::Type::LineEntryEndAddress) addr.Slide(sc->line_entry.range.GetByteSize()); - if (DumpAddress(s, sc, exe_ctx, addr, false)) + if (DumpAddressAndContent(s, sc, exe_ctx, addr, false)) return true; } return false; @@ -2309,7 +2337,7 @@ bool FormatEntity::FormatFileSpec(const FileSpec &file_spec, Stream &s, llvm::StringRef variable_name, llvm::StringRef variable_format) { if (variable_name.empty() || variable_name.equals(".fullpath")) { - file_spec.Dump(&s); + file_spec.Dump(s.AsRawOstream()); return true; } else if (variable_name.equals(".basename")) { s.PutCString(file_spec.GetFilename().AsCString("")); diff --git a/lldb/source/Core/IOHandler.cpp b/lldb/source/Core/IOHandler.cpp index 46be29f6fbf9..4021a6d7f327 100644 --- a/lldb/source/Core/IOHandler.cpp +++ b/lldb/source/Core/IOHandler.cpp @@ -8,11 +8,6 @@ #include "lldb/Core/IOHandler.h" -#ifndef LLDB_DISABLE_CURSES -#include <curses.h> -#include <panel.h> -#endif - #if defined(__APPLE__) #include <deque> #endif @@ -20,6 +15,7 @@ #include "lldb/Core/Debugger.h" #include "lldb/Core/StreamFile.h" +#include "lldb/Host/Config.h" #include "lldb/Host/File.h" #include "lldb/Utility/Predicate.h" #include "lldb/Utility/Status.h" @@ -27,29 +23,11 @@ #include "lldb/Utility/StringList.h" #include "lldb/lldb-forward.h" -#ifndef LLDB_DISABLE_LIBEDIT +#if LLDB_ENABLE_LIBEDIT #include "lldb/Host/Editline.h" #endif #include "lldb/Interpreter/CommandCompletions.h" #include "lldb/Interpreter/CommandInterpreter.h" -#ifndef LLDB_DISABLE_CURSES -#include "lldb/Breakpoint/BreakpointLocation.h" -#include "lldb/Core/Module.h" -#include "lldb/Core/ValueObject.h" -#include "lldb/Core/ValueObjectRegister.h" -#include "lldb/Symbol/Block.h" -#include "lldb/Symbol/Function.h" -#include "lldb/Symbol/Symbol.h" -#include "lldb/Symbol/VariableList.h" -#include "lldb/Target/Process.h" -#include "lldb/Target/RegisterContext.h" -#include "lldb/Target/StackFrame.h" -#include "lldb/Target/StopInfo.h" -#include "lldb/Target/Target.h" -#include "lldb/Target/Thread.h" -#include "lldb/Utility/State.h" -#endif - #include "llvm/ADT/StringRef.h" #ifdef _WIN32 @@ -74,7 +52,6 @@ using llvm::None; using llvm::Optional; using llvm::StringRef; - IOHandler::IOHandler(Debugger &debugger, IOHandler::Type type) : IOHandler(debugger, type, FileSP(), // Adopt STDIN from top input reader @@ -257,7 +234,7 @@ IOHandlerEditline::IOHandlerEditline( IOHandlerDelegate &delegate, repro::DataRecorder *data_recorder) : IOHandler(debugger, type, input_sp, output_sp, error_sp, flags, data_recorder), -#ifndef LLDB_DISABLE_LIBEDIT +#if LLDB_ENABLE_LIBEDIT m_editline_up(), #endif m_delegate(delegate), m_prompt(), m_continuation_prompt(), @@ -267,7 +244,7 @@ IOHandlerEditline::IOHandlerEditline( m_editing(false) { SetPrompt(prompt); -#ifndef LLDB_DISABLE_LIBEDIT +#if LLDB_ENABLE_LIBEDIT bool use_editline = false; use_editline = GetInputFILE() && GetOutputFILE() && GetErrorFILE() && @@ -295,7 +272,7 @@ IOHandlerEditline::IOHandlerEditline( } IOHandlerEditline::~IOHandlerEditline() { -#ifndef LLDB_DISABLE_LIBEDIT +#if LLDB_ENABLE_LIBEDIT m_editline_up.reset(); #endif } @@ -331,7 +308,7 @@ static Optional<std::string> SplitLineEOF(std::string &line_buffer) { } bool IOHandlerEditline::GetLine(std::string &line, bool &interrupted) { -#ifndef LLDB_DISABLE_LIBEDIT +#if LLDB_ENABLE_LIBEDIT if (m_editline_up) { bool b = m_editline_up->GetLine(line, interrupted); if (b && m_data_recorder) @@ -425,7 +402,7 @@ bool IOHandlerEditline::GetLine(std::string &line, bool &interrupted) { return (bool)got_line; } -#ifndef LLDB_DISABLE_LIBEDIT +#if LLDB_ENABLE_LIBEDIT bool IOHandlerEditline::IsInputCompleteCallback(Editline *editline, StringList &lines, void *baton) { @@ -452,14 +429,14 @@ void IOHandlerEditline::AutoCompleteCallback(CompletionRequest &request, #endif const char *IOHandlerEditline::GetPrompt() { -#ifndef LLDB_DISABLE_LIBEDIT +#if LLDB_ENABLE_LIBEDIT if (m_editline_up) { return m_editline_up->GetPrompt(); } else { #endif if (m_prompt.empty()) return nullptr; -#ifndef LLDB_DISABLE_LIBEDIT +#if LLDB_ENABLE_LIBEDIT } #endif return m_prompt.c_str(); @@ -468,7 +445,7 @@ const char *IOHandlerEditline::GetPrompt() { bool IOHandlerEditline::SetPrompt(llvm::StringRef prompt) { m_prompt = prompt; -#ifndef LLDB_DISABLE_LIBEDIT +#if LLDB_ENABLE_LIBEDIT if (m_editline_up) m_editline_up->SetPrompt(m_prompt.empty() ? nullptr : m_prompt.c_str()); #endif @@ -483,7 +460,7 @@ const char *IOHandlerEditline::GetContinuationPrompt() { void IOHandlerEditline::SetContinuationPrompt(llvm::StringRef prompt) { m_continuation_prompt = prompt; -#ifndef LLDB_DISABLE_LIBEDIT +#if LLDB_ENABLE_LIBEDIT if (m_editline_up) m_editline_up->SetContinuationPrompt(m_continuation_prompt.empty() ? nullptr @@ -496,7 +473,7 @@ void IOHandlerEditline::SetBaseLineNumber(uint32_t line) { } uint32_t IOHandlerEditline::GetCurrentLineIndex() const { -#ifndef LLDB_DISABLE_LIBEDIT +#if LLDB_ENABLE_LIBEDIT if (m_editline_up) return m_editline_up->GetCurrentLine(); #endif @@ -507,7 +484,7 @@ bool IOHandlerEditline::GetLines(StringList &lines, bool &interrupted) { m_current_lines_ptr = &lines; bool success = false; -#ifndef LLDB_DISABLE_LIBEDIT +#if LLDB_ENABLE_LIBEDIT if (m_editline_up) { return m_editline_up->GetLines(m_base_line_number, lines, interrupted); } else { @@ -537,7 +514,7 @@ bool IOHandlerEditline::GetLines(StringList &lines, bool &interrupted) { } } success = lines.GetSize() > 0; -#ifndef LLDB_DISABLE_LIBEDIT +#if LLDB_ENABLE_LIBEDIT } #endif return success; @@ -577,7 +554,7 @@ void IOHandlerEditline::Run() { } void IOHandlerEditline::Cancel() { -#ifndef LLDB_DISABLE_LIBEDIT +#if LLDB_ENABLE_LIBEDIT if (m_editline_up) m_editline_up->Cancel(); #endif @@ -588,7 +565,7 @@ bool IOHandlerEditline::Interrupt() { if (m_delegate.IOHandlerInterrupt(*this)) return true; -#ifndef LLDB_DISABLE_LIBEDIT +#if LLDB_ENABLE_LIBEDIT if (m_editline_up) return m_editline_up->Interrupt(); #endif @@ -596,14 +573,14 @@ bool IOHandlerEditline::Interrupt() { } void IOHandlerEditline::GotEOF() { -#ifndef LLDB_DISABLE_LIBEDIT +#if LLDB_ENABLE_LIBEDIT if (m_editline_up) m_editline_up->Interrupt(); #endif } void IOHandlerEditline::PrintAsync(Stream *stream, const char *s, size_t len) { -#ifndef LLDB_DISABLE_LIBEDIT +#if LLDB_ENABLE_LIBEDIT if (m_editline_up) m_editline_up->PrintAsync(stream, s, len); else @@ -631,3995 +608,3 @@ void IOHandlerEditline::PrintAsync(Stream *stream, const char *s, size_t len) { #endif } } - -// we may want curses to be disabled for some builds for instance, windows -#ifndef LLDB_DISABLE_CURSES - -#define KEY_RETURN 10 -#define KEY_ESCAPE 27 - -namespace curses { -class Menu; -class MenuDelegate; -class Window; -class WindowDelegate; -typedef std::shared_ptr<Menu> MenuSP; -typedef std::shared_ptr<MenuDelegate> MenuDelegateSP; -typedef std::shared_ptr<Window> WindowSP; -typedef std::shared_ptr<WindowDelegate> WindowDelegateSP; -typedef std::vector<MenuSP> Menus; -typedef std::vector<WindowSP> Windows; -typedef std::vector<WindowDelegateSP> WindowDelegates; - -#if 0 -type summary add -s "x=${var.x}, y=${var.y}" curses::Point -type summary add -s "w=${var.width}, h=${var.height}" curses::Size -type summary add -s "${var.origin%S} ${var.size%S}" curses::Rect -#endif - -struct Point { - int x; - int y; - - Point(int _x = 0, int _y = 0) : x(_x), y(_y) {} - - void Clear() { - x = 0; - y = 0; - } - - Point &operator+=(const Point &rhs) { - x += rhs.x; - y += rhs.y; - return *this; - } - - void Dump() { printf("(x=%i, y=%i)\n", x, y); } -}; - -bool operator==(const Point &lhs, const Point &rhs) { - return lhs.x == rhs.x && lhs.y == rhs.y; -} - -bool operator!=(const Point &lhs, const Point &rhs) { - return lhs.x != rhs.x || lhs.y != rhs.y; -} - -struct Size { - int width; - int height; - Size(int w = 0, int h = 0) : width(w), height(h) {} - - void Clear() { - width = 0; - height = 0; - } - - void Dump() { printf("(w=%i, h=%i)\n", width, height); } -}; - -bool operator==(const Size &lhs, const Size &rhs) { - return lhs.width == rhs.width && lhs.height == rhs.height; -} - -bool operator!=(const Size &lhs, const Size &rhs) { - return lhs.width != rhs.width || lhs.height != rhs.height; -} - -struct Rect { - Point origin; - Size size; - - Rect() : origin(), size() {} - - Rect(const Point &p, const Size &s) : origin(p), size(s) {} - - void Clear() { - origin.Clear(); - size.Clear(); - } - - void Dump() { - printf("(x=%i, y=%i), w=%i, h=%i)\n", origin.x, origin.y, size.width, - size.height); - } - - void Inset(int w, int h) { - if (size.width > w * 2) - size.width -= w * 2; - origin.x += w; - - if (size.height > h * 2) - size.height -= h * 2; - origin.y += h; - } - - // Return a status bar rectangle which is the last line of this rectangle. - // This rectangle will be modified to not include the status bar area. - Rect MakeStatusBar() { - Rect status_bar; - if (size.height > 1) { - status_bar.origin.x = origin.x; - status_bar.origin.y = size.height; - status_bar.size.width = size.width; - status_bar.size.height = 1; - --size.height; - } - return status_bar; - } - - // Return a menubar rectangle which is the first line of this rectangle. This - // rectangle will be modified to not include the menubar area. - Rect MakeMenuBar() { - Rect menubar; - if (size.height > 1) { - menubar.origin.x = origin.x; - menubar.origin.y = origin.y; - menubar.size.width = size.width; - menubar.size.height = 1; - ++origin.y; - --size.height; - } - return menubar; - } - - void HorizontalSplitPercentage(float top_percentage, Rect &top, - Rect &bottom) const { - float top_height = top_percentage * size.height; - HorizontalSplit(top_height, top, bottom); - } - - void HorizontalSplit(int top_height, Rect &top, Rect &bottom) const { - top = *this; - if (top_height < size.height) { - top.size.height = top_height; - bottom.origin.x = origin.x; - bottom.origin.y = origin.y + top.size.height; - bottom.size.width = size.width; - bottom.size.height = size.height - top.size.height; - } else { - bottom.Clear(); - } - } - - void VerticalSplitPercentage(float left_percentage, Rect &left, - Rect &right) const { - float left_width = left_percentage * size.width; - VerticalSplit(left_width, left, right); - } - - void VerticalSplit(int left_width, Rect &left, Rect &right) const { - left = *this; - if (left_width < size.width) { - left.size.width = left_width; - right.origin.x = origin.x + left.size.width; - right.origin.y = origin.y; - right.size.width = size.width - left.size.width; - right.size.height = size.height; - } else { - right.Clear(); - } - } -}; - -bool operator==(const Rect &lhs, const Rect &rhs) { - return lhs.origin == rhs.origin && lhs.size == rhs.size; -} - -bool operator!=(const Rect &lhs, const Rect &rhs) { - return lhs.origin != rhs.origin || lhs.size != rhs.size; -} - -enum HandleCharResult { - eKeyNotHandled = 0, - eKeyHandled = 1, - eQuitApplication = 2 -}; - -enum class MenuActionResult { - Handled, - NotHandled, - Quit // Exit all menus and quit -}; - -struct KeyHelp { - int ch; - const char *description; -}; - -class WindowDelegate { -public: - virtual ~WindowDelegate() = default; - - virtual bool WindowDelegateDraw(Window &window, bool force) { - return false; // Drawing not handled - } - - virtual HandleCharResult WindowDelegateHandleChar(Window &window, int key) { - return eKeyNotHandled; - } - - virtual const char *WindowDelegateGetHelpText() { return nullptr; } - - virtual KeyHelp *WindowDelegateGetKeyHelp() { return nullptr; } -}; - -class HelpDialogDelegate : public WindowDelegate { -public: - HelpDialogDelegate(const char *text, KeyHelp *key_help_array); - - ~HelpDialogDelegate() override; - - bool WindowDelegateDraw(Window &window, bool force) override; - - HandleCharResult WindowDelegateHandleChar(Window &window, int key) override; - - size_t GetNumLines() const { return m_text.GetSize(); } - - size_t GetMaxLineLength() const { return m_text.GetMaxStringLength(); } - -protected: - StringList m_text; - int m_first_visible_line; -}; - -class Window { -public: - Window(const char *name) - : m_name(name), m_window(nullptr), m_panel(nullptr), m_parent(nullptr), - m_subwindows(), m_delegate_sp(), m_curr_active_window_idx(UINT32_MAX), - m_prev_active_window_idx(UINT32_MAX), m_delete(false), - m_needs_update(true), m_can_activate(true), m_is_subwin(false) {} - - Window(const char *name, WINDOW *w, bool del = true) - : m_name(name), m_window(nullptr), m_panel(nullptr), m_parent(nullptr), - m_subwindows(), m_delegate_sp(), m_curr_active_window_idx(UINT32_MAX), - m_prev_active_window_idx(UINT32_MAX), m_delete(del), - m_needs_update(true), m_can_activate(true), m_is_subwin(false) { - if (w) - Reset(w); - } - - Window(const char *name, const Rect &bounds) - : m_name(name), m_window(nullptr), m_parent(nullptr), m_subwindows(), - m_delegate_sp(), m_curr_active_window_idx(UINT32_MAX), - m_prev_active_window_idx(UINT32_MAX), m_delete(true), - m_needs_update(true), m_can_activate(true), m_is_subwin(false) { - Reset(::newwin(bounds.size.height, bounds.size.width, bounds.origin.y, - bounds.origin.y)); - } - - virtual ~Window() { - RemoveSubWindows(); - Reset(); - } - - void Reset(WINDOW *w = nullptr, bool del = true) { - if (m_window == w) - return; - - if (m_panel) { - ::del_panel(m_panel); - m_panel = nullptr; - } - if (m_window && m_delete) { - ::delwin(m_window); - m_window = nullptr; - m_delete = false; - } - if (w) { - m_window = w; - m_panel = ::new_panel(m_window); - m_delete = del; - } - } - - void AttributeOn(attr_t attr) { ::wattron(m_window, attr); } - void AttributeOff(attr_t attr) { ::wattroff(m_window, attr); } - void Box(chtype v_char = ACS_VLINE, chtype h_char = ACS_HLINE) { - ::box(m_window, v_char, h_char); - } - void Clear() { ::wclear(m_window); } - void Erase() { ::werase(m_window); } - Rect GetBounds() { - return Rect(GetParentOrigin(), GetSize()); - } // Get the rectangle in our parent window - int GetChar() { return ::wgetch(m_window); } - int GetCursorX() { return getcurx(m_window); } - int GetCursorY() { return getcury(m_window); } - Rect GetFrame() { - return Rect(Point(), GetSize()); - } // Get our rectangle in our own coordinate system - Point GetParentOrigin() { return Point(GetParentX(), GetParentY()); } - Size GetSize() { return Size(GetWidth(), GetHeight()); } - int GetParentX() { return getparx(m_window); } - int GetParentY() { return getpary(m_window); } - int GetMaxX() { return getmaxx(m_window); } - int GetMaxY() { return getmaxy(m_window); } - int GetWidth() { return GetMaxX(); } - int GetHeight() { return GetMaxY(); } - void MoveCursor(int x, int y) { ::wmove(m_window, y, x); } - void MoveWindow(int x, int y) { MoveWindow(Point(x, y)); } - void Resize(int w, int h) { ::wresize(m_window, h, w); } - void Resize(const Size &size) { - ::wresize(m_window, size.height, size.width); - } - void PutChar(int ch) { ::waddch(m_window, ch); } - void PutCString(const char *s, int len = -1) { ::waddnstr(m_window, s, len); } - void SetBackground(int color_pair_idx) { - ::wbkgd(m_window, COLOR_PAIR(color_pair_idx)); - } - - void PutCStringTruncated(const char *s, int right_pad) { - int bytes_left = GetWidth() - GetCursorX(); - if (bytes_left > right_pad) { - bytes_left -= right_pad; - ::waddnstr(m_window, s, bytes_left); - } - } - - void MoveWindow(const Point &origin) { - const bool moving_window = origin != GetParentOrigin(); - if (m_is_subwin && moving_window) { - // Can't move subwindows, must delete and re-create - Size size = GetSize(); - Reset(::subwin(m_parent->m_window, size.height, size.width, origin.y, - origin.x), - true); - } else { - ::mvwin(m_window, origin.y, origin.x); - } - } - - void SetBounds(const Rect &bounds) { - const bool moving_window = bounds.origin != GetParentOrigin(); - if (m_is_subwin && moving_window) { - // Can't move subwindows, must delete and re-create - Reset(::subwin(m_parent->m_window, bounds.size.height, bounds.size.width, - bounds.origin.y, bounds.origin.x), - true); - } else { - if (moving_window) - MoveWindow(bounds.origin); - Resize(bounds.size); - } - } - - void Printf(const char *format, ...) __attribute__((format(printf, 2, 3))) { - va_list args; - va_start(args, format); - vwprintw(m_window, format, args); - va_end(args); - } - - void Touch() { - ::touchwin(m_window); - if (m_parent) - m_parent->Touch(); - } - - WindowSP CreateSubWindow(const char *name, const Rect &bounds, - bool make_active) { - auto get_window = [this, &bounds]() { - return m_window - ? ::subwin(m_window, bounds.size.height, bounds.size.width, - bounds.origin.y, bounds.origin.x) - : ::newwin(bounds.size.height, bounds.size.width, - bounds.origin.y, bounds.origin.x); - }; - WindowSP subwindow_sp = std::make_shared<Window>(name, get_window(), true); - subwindow_sp->m_is_subwin = subwindow_sp.operator bool(); - subwindow_sp->m_parent = this; - if (make_active) { - m_prev_active_window_idx = m_curr_active_window_idx; - m_curr_active_window_idx = m_subwindows.size(); - } - m_subwindows.push_back(subwindow_sp); - ::top_panel(subwindow_sp->m_panel); - m_needs_update = true; - return subwindow_sp; - } - - bool RemoveSubWindow(Window *window) { - Windows::iterator pos, end = m_subwindows.end(); - size_t i = 0; - for (pos = m_subwindows.begin(); pos != end; ++pos, ++i) { - if ((*pos).get() == window) { - if (m_prev_active_window_idx == i) - m_prev_active_window_idx = UINT32_MAX; - else if (m_prev_active_window_idx != UINT32_MAX && - m_prev_active_window_idx > i) - --m_prev_active_window_idx; - - if (m_curr_active_window_idx == i) - m_curr_active_window_idx = UINT32_MAX; - else if (m_curr_active_window_idx != UINT32_MAX && - m_curr_active_window_idx > i) - --m_curr_active_window_idx; - window->Erase(); - m_subwindows.erase(pos); - m_needs_update = true; - if (m_parent) - m_parent->Touch(); - else - ::touchwin(stdscr); - return true; - } - } - return false; - } - - WindowSP FindSubWindow(const char *name) { - Windows::iterator pos, end = m_subwindows.end(); - size_t i = 0; - for (pos = m_subwindows.begin(); pos != end; ++pos, ++i) { - if ((*pos)->m_name == name) - return *pos; - } - return WindowSP(); - } - - void RemoveSubWindows() { - m_curr_active_window_idx = UINT32_MAX; - m_prev_active_window_idx = UINT32_MAX; - for (Windows::iterator pos = m_subwindows.begin(); - pos != m_subwindows.end(); pos = m_subwindows.erase(pos)) { - (*pos)->Erase(); - } - if (m_parent) - m_parent->Touch(); - else - ::touchwin(stdscr); - } - - WINDOW *get() { return m_window; } - - operator WINDOW *() { return m_window; } - - // Window drawing utilities - void DrawTitleBox(const char *title, const char *bottom_message = nullptr) { - attr_t attr = 0; - if (IsActive()) - attr = A_BOLD | COLOR_PAIR(2); - else - attr = 0; - if (attr) - AttributeOn(attr); - - Box(); - MoveCursor(3, 0); - - if (title && title[0]) { - PutChar('<'); - PutCString(title); - PutChar('>'); - } - - if (bottom_message && bottom_message[0]) { - int bottom_message_length = strlen(bottom_message); - int x = GetWidth() - 3 - (bottom_message_length + 2); - - if (x > 0) { - MoveCursor(x, GetHeight() - 1); - PutChar('['); - PutCString(bottom_message); - PutChar(']'); - } else { - MoveCursor(1, GetHeight() - 1); - PutChar('['); - PutCStringTruncated(bottom_message, 1); - } - } - if (attr) - AttributeOff(attr); - } - - virtual void Draw(bool force) { - if (m_delegate_sp && m_delegate_sp->WindowDelegateDraw(*this, force)) - return; - - for (auto &subwindow_sp : m_subwindows) - subwindow_sp->Draw(force); - } - - bool CreateHelpSubwindow() { - if (m_delegate_sp) { - const char *text = m_delegate_sp->WindowDelegateGetHelpText(); - KeyHelp *key_help = m_delegate_sp->WindowDelegateGetKeyHelp(); - if ((text && text[0]) || key_help) { - std::unique_ptr<HelpDialogDelegate> help_delegate_up( - new HelpDialogDelegate(text, key_help)); - const size_t num_lines = help_delegate_up->GetNumLines(); - const size_t max_length = help_delegate_up->GetMaxLineLength(); - Rect bounds = GetBounds(); - bounds.Inset(1, 1); - if (max_length + 4 < static_cast<size_t>(bounds.size.width)) { - bounds.origin.x += (bounds.size.width - max_length + 4) / 2; - bounds.size.width = max_length + 4; - } else { - if (bounds.size.width > 100) { - const int inset_w = bounds.size.width / 4; - bounds.origin.x += inset_w; - bounds.size.width -= 2 * inset_w; - } - } - - if (num_lines + 2 < static_cast<size_t>(bounds.size.height)) { - bounds.origin.y += (bounds.size.height - num_lines + 2) / 2; - bounds.size.height = num_lines + 2; - } else { - if (bounds.size.height > 100) { - const int inset_h = bounds.size.height / 4; - bounds.origin.y += inset_h; - bounds.size.height -= 2 * inset_h; - } - } - WindowSP help_window_sp; - Window *parent_window = GetParent(); - if (parent_window) - help_window_sp = parent_window->CreateSubWindow("Help", bounds, true); - else - help_window_sp = CreateSubWindow("Help", bounds, true); - help_window_sp->SetDelegate( - WindowDelegateSP(help_delegate_up.release())); - return true; - } - } - return false; - } - - virtual HandleCharResult HandleChar(int key) { - // Always check the active window first - HandleCharResult result = eKeyNotHandled; - WindowSP active_window_sp = GetActiveWindow(); - if (active_window_sp) { - result = active_window_sp->HandleChar(key); - if (result != eKeyNotHandled) - return result; - } - - if (m_delegate_sp) { - result = m_delegate_sp->WindowDelegateHandleChar(*this, key); - if (result != eKeyNotHandled) - return result; - } - - // Then check for any windows that want any keys that weren't handled. This - // is typically only for a menubar. Make a copy of the subwindows in case - // any HandleChar() functions muck with the subwindows. If we don't do - // this, we can crash when iterating over the subwindows. - Windows subwindows(m_subwindows); - for (auto subwindow_sp : subwindows) { - if (!subwindow_sp->m_can_activate) { - HandleCharResult result = subwindow_sp->HandleChar(key); - if (result != eKeyNotHandled) - return result; - } - } - - return eKeyNotHandled; - } - - WindowSP GetActiveWindow() { - if (!m_subwindows.empty()) { - if (m_curr_active_window_idx >= m_subwindows.size()) { - if (m_prev_active_window_idx < m_subwindows.size()) { - m_curr_active_window_idx = m_prev_active_window_idx; - m_prev_active_window_idx = UINT32_MAX; - } else if (IsActive()) { - m_prev_active_window_idx = UINT32_MAX; - m_curr_active_window_idx = UINT32_MAX; - - // Find first window that wants to be active if this window is active - const size_t num_subwindows = m_subwindows.size(); - for (size_t i = 0; i < num_subwindows; ++i) { - if (m_subwindows[i]->GetCanBeActive()) { - m_curr_active_window_idx = i; - break; - } - } - } - } - - if (m_curr_active_window_idx < m_subwindows.size()) - return m_subwindows[m_curr_active_window_idx]; - } - return WindowSP(); - } - - bool GetCanBeActive() const { return m_can_activate; } - - void SetCanBeActive(bool b) { m_can_activate = b; } - - void SetDelegate(const WindowDelegateSP &delegate_sp) { - m_delegate_sp = delegate_sp; - } - - Window *GetParent() const { return m_parent; } - - bool IsActive() const { - if (m_parent) - return m_parent->GetActiveWindow().get() == this; - else - return true; // Top level window is always active - } - - void SelectNextWindowAsActive() { - // Move active focus to next window - const size_t num_subwindows = m_subwindows.size(); - if (m_curr_active_window_idx == UINT32_MAX) { - uint32_t idx = 0; - for (auto subwindow_sp : m_subwindows) { - if (subwindow_sp->GetCanBeActive()) { - m_curr_active_window_idx = idx; - break; - } - ++idx; - } - } else if (m_curr_active_window_idx + 1 < num_subwindows) { - bool handled = false; - m_prev_active_window_idx = m_curr_active_window_idx; - for (size_t idx = m_curr_active_window_idx + 1; idx < num_subwindows; - ++idx) { - if (m_subwindows[idx]->GetCanBeActive()) { - m_curr_active_window_idx = idx; - handled = true; - break; - } - } - if (!handled) { - for (size_t idx = 0; idx <= m_prev_active_window_idx; ++idx) { - if (m_subwindows[idx]->GetCanBeActive()) { - m_curr_active_window_idx = idx; - break; - } - } - } - } else { - m_prev_active_window_idx = m_curr_active_window_idx; - for (size_t idx = 0; idx < num_subwindows; ++idx) { - if (m_subwindows[idx]->GetCanBeActive()) { - m_curr_active_window_idx = idx; - break; - } - } - } - } - - const char *GetName() const { return m_name.c_str(); } - -protected: - std::string m_name; - WINDOW *m_window; - PANEL *m_panel; - Window *m_parent; - Windows m_subwindows; - WindowDelegateSP m_delegate_sp; - uint32_t m_curr_active_window_idx; - uint32_t m_prev_active_window_idx; - bool m_delete; - bool m_needs_update; - bool m_can_activate; - bool m_is_subwin; - -private: - DISALLOW_COPY_AND_ASSIGN(Window); -}; - -class MenuDelegate { -public: - virtual ~MenuDelegate() = default; - - virtual MenuActionResult MenuDelegateAction(Menu &menu) = 0; -}; - -class Menu : public WindowDelegate { -public: - enum class Type { Invalid, Bar, Item, Separator }; - - // Menubar or separator constructor - Menu(Type type); - - // Menuitem constructor - Menu(const char *name, const char *key_name, int key_value, - uint64_t identifier); - - ~Menu() override = default; - - const MenuDelegateSP &GetDelegate() const { return m_delegate_sp; } - - void SetDelegate(const MenuDelegateSP &delegate_sp) { - m_delegate_sp = delegate_sp; - } - - void RecalculateNameLengths(); - - void AddSubmenu(const MenuSP &menu_sp); - - int DrawAndRunMenu(Window &window); - - void DrawMenuTitle(Window &window, bool highlight); - - bool WindowDelegateDraw(Window &window, bool force) override; - - HandleCharResult WindowDelegateHandleChar(Window &window, int key) override; - - MenuActionResult ActionPrivate(Menu &menu) { - MenuActionResult result = MenuActionResult::NotHandled; - if (m_delegate_sp) { - result = m_delegate_sp->MenuDelegateAction(menu); - if (result != MenuActionResult::NotHandled) - return result; - } else if (m_parent) { - result = m_parent->ActionPrivate(menu); - if (result != MenuActionResult::NotHandled) - return result; - } - return m_canned_result; - } - - MenuActionResult Action() { - // Call the recursive action so it can try to handle it with the menu - // delegate, and if not, try our parent menu - return ActionPrivate(*this); - } - - void SetCannedResult(MenuActionResult result) { m_canned_result = result; } - - Menus &GetSubmenus() { return m_submenus; } - - const Menus &GetSubmenus() const { return m_submenus; } - - int GetSelectedSubmenuIndex() const { return m_selected; } - - void SetSelectedSubmenuIndex(int idx) { m_selected = idx; } - - Type GetType() const { return m_type; } - - int GetStartingColumn() const { return m_start_col; } - - void SetStartingColumn(int col) { m_start_col = col; } - - int GetKeyValue() const { return m_key_value; } - - std::string &GetName() { return m_name; } - - int GetDrawWidth() const { - return m_max_submenu_name_length + m_max_submenu_key_name_length + 8; - } - - uint64_t GetIdentifier() const { return m_identifier; } - - void SetIdentifier(uint64_t identifier) { m_identifier = identifier; } - -protected: - std::string m_name; - std::string m_key_name; - uint64_t m_identifier; - Type m_type; - int m_key_value; - int m_start_col; - int m_max_submenu_name_length; - int m_max_submenu_key_name_length; - int m_selected; - Menu *m_parent; - Menus m_submenus; - WindowSP m_menu_window_sp; - MenuActionResult m_canned_result; - MenuDelegateSP m_delegate_sp; -}; - -// Menubar or separator constructor -Menu::Menu(Type type) - : m_name(), m_key_name(), m_identifier(0), m_type(type), m_key_value(0), - m_start_col(0), m_max_submenu_name_length(0), - m_max_submenu_key_name_length(0), m_selected(0), m_parent(nullptr), - m_submenus(), m_canned_result(MenuActionResult::NotHandled), - m_delegate_sp() {} - -// Menuitem constructor -Menu::Menu(const char *name, const char *key_name, int key_value, - uint64_t identifier) - : m_name(), m_key_name(), m_identifier(identifier), m_type(Type::Invalid), - m_key_value(key_value), m_start_col(0), m_max_submenu_name_length(0), - m_max_submenu_key_name_length(0), m_selected(0), m_parent(nullptr), - m_submenus(), m_canned_result(MenuActionResult::NotHandled), - m_delegate_sp() { - if (name && name[0]) { - m_name = name; - m_type = Type::Item; - if (key_name && key_name[0]) - m_key_name = key_name; - } else { - m_type = Type::Separator; - } -} - -void Menu::RecalculateNameLengths() { - m_max_submenu_name_length = 0; - m_max_submenu_key_name_length = 0; - Menus &submenus = GetSubmenus(); - const size_t num_submenus = submenus.size(); - for (size_t i = 0; i < num_submenus; ++i) { - Menu *submenu = submenus[i].get(); - if (static_cast<size_t>(m_max_submenu_name_length) < submenu->m_name.size()) - m_max_submenu_name_length = submenu->m_name.size(); - if (static_cast<size_t>(m_max_submenu_key_name_length) < - submenu->m_key_name.size()) - m_max_submenu_key_name_length = submenu->m_key_name.size(); - } -} - -void Menu::AddSubmenu(const MenuSP &menu_sp) { - menu_sp->m_parent = this; - if (static_cast<size_t>(m_max_submenu_name_length) < menu_sp->m_name.size()) - m_max_submenu_name_length = menu_sp->m_name.size(); - if (static_cast<size_t>(m_max_submenu_key_name_length) < - menu_sp->m_key_name.size()) - m_max_submenu_key_name_length = menu_sp->m_key_name.size(); - m_submenus.push_back(menu_sp); -} - -void Menu::DrawMenuTitle(Window &window, bool highlight) { - if (m_type == Type::Separator) { - window.MoveCursor(0, window.GetCursorY()); - window.PutChar(ACS_LTEE); - int width = window.GetWidth(); - if (width > 2) { - width -= 2; - for (int i = 0; i < width; ++i) - window.PutChar(ACS_HLINE); - } - window.PutChar(ACS_RTEE); - } else { - const int shortcut_key = m_key_value; - bool underlined_shortcut = false; - const attr_t hilgight_attr = A_REVERSE; - if (highlight) - window.AttributeOn(hilgight_attr); - if (isprint(shortcut_key)) { - size_t lower_pos = m_name.find(tolower(shortcut_key)); - size_t upper_pos = m_name.find(toupper(shortcut_key)); - const char *name = m_name.c_str(); - size_t pos = std::min<size_t>(lower_pos, upper_pos); - if (pos != std::string::npos) { - underlined_shortcut = true; - if (pos > 0) { - window.PutCString(name, pos); - name += pos; - } - const attr_t shortcut_attr = A_UNDERLINE | A_BOLD; - window.AttributeOn(shortcut_attr); - window.PutChar(name[0]); - window.AttributeOff(shortcut_attr); - name++; - if (name[0]) - window.PutCString(name); - } - } - - if (!underlined_shortcut) { - window.PutCString(m_name.c_str()); - } - - if (highlight) - window.AttributeOff(hilgight_attr); - - if (m_key_name.empty()) { - if (!underlined_shortcut && isprint(m_key_value)) { - window.AttributeOn(COLOR_PAIR(3)); - window.Printf(" (%c)", m_key_value); - window.AttributeOff(COLOR_PAIR(3)); - } - } else { - window.AttributeOn(COLOR_PAIR(3)); - window.Printf(" (%s)", m_key_name.c_str()); - window.AttributeOff(COLOR_PAIR(3)); - } - } -} - -bool Menu::WindowDelegateDraw(Window &window, bool force) { - Menus &submenus = GetSubmenus(); - const size_t num_submenus = submenus.size(); - const int selected_idx = GetSelectedSubmenuIndex(); - Menu::Type menu_type = GetType(); - switch (menu_type) { - case Menu::Type::Bar: { - window.SetBackground(2); - window.MoveCursor(0, 0); - for (size_t i = 0; i < num_submenus; ++i) { - Menu *menu = submenus[i].get(); - if (i > 0) - window.PutChar(' '); - menu->SetStartingColumn(window.GetCursorX()); - window.PutCString("| "); - menu->DrawMenuTitle(window, false); - } - window.PutCString(" |"); - } break; - - case Menu::Type::Item: { - int y = 1; - int x = 3; - // Draw the menu - int cursor_x = 0; - int cursor_y = 0; - window.Erase(); - window.SetBackground(2); - window.Box(); - for (size_t i = 0; i < num_submenus; ++i) { - const bool is_selected = (i == static_cast<size_t>(selected_idx)); - window.MoveCursor(x, y + i); - if (is_selected) { - // Remember where we want the cursor to be - cursor_x = x - 1; - cursor_y = y + i; - } - submenus[i]->DrawMenuTitle(window, is_selected); - } - window.MoveCursor(cursor_x, cursor_y); - } break; - - default: - case Menu::Type::Separator: - break; - } - return true; // Drawing handled... -} - -HandleCharResult Menu::WindowDelegateHandleChar(Window &window, int key) { - HandleCharResult result = eKeyNotHandled; - - Menus &submenus = GetSubmenus(); - const size_t num_submenus = submenus.size(); - const int selected_idx = GetSelectedSubmenuIndex(); - Menu::Type menu_type = GetType(); - if (menu_type == Menu::Type::Bar) { - MenuSP run_menu_sp; - switch (key) { - case KEY_DOWN: - case KEY_UP: - // Show last menu or first menu - if (selected_idx < static_cast<int>(num_submenus)) - run_menu_sp = submenus[selected_idx]; - else if (!submenus.empty()) - run_menu_sp = submenus.front(); - result = eKeyHandled; - break; - - case KEY_RIGHT: - ++m_selected; - if (m_selected >= static_cast<int>(num_submenus)) - m_selected = 0; - if (m_selected < static_cast<int>(num_submenus)) - run_menu_sp = submenus[m_selected]; - else if (!submenus.empty()) - run_menu_sp = submenus.front(); - result = eKeyHandled; - break; - - case KEY_LEFT: - --m_selected; - if (m_selected < 0) - m_selected = num_submenus - 1; - if (m_selected < static_cast<int>(num_submenus)) - run_menu_sp = submenus[m_selected]; - else if (!submenus.empty()) - run_menu_sp = submenus.front(); - result = eKeyHandled; - break; - - default: - for (size_t i = 0; i < num_submenus; ++i) { - if (submenus[i]->GetKeyValue() == key) { - SetSelectedSubmenuIndex(i); - run_menu_sp = submenus[i]; - result = eKeyHandled; - break; - } - } - break; - } - - if (run_menu_sp) { - // Run the action on this menu in case we need to populate the menu with - // dynamic content and also in case check marks, and any other menu - // decorations need to be calculated - if (run_menu_sp->Action() == MenuActionResult::Quit) - return eQuitApplication; - - Rect menu_bounds; - menu_bounds.origin.x = run_menu_sp->GetStartingColumn(); - menu_bounds.origin.y = 1; - menu_bounds.size.width = run_menu_sp->GetDrawWidth(); - menu_bounds.size.height = run_menu_sp->GetSubmenus().size() + 2; - if (m_menu_window_sp) - window.GetParent()->RemoveSubWindow(m_menu_window_sp.get()); - - m_menu_window_sp = window.GetParent()->CreateSubWindow( - run_menu_sp->GetName().c_str(), menu_bounds, true); - m_menu_window_sp->SetDelegate(run_menu_sp); - } - } else if (menu_type == Menu::Type::Item) { - switch (key) { - case KEY_DOWN: - if (m_submenus.size() > 1) { - const int start_select = m_selected; - while (++m_selected != start_select) { - if (static_cast<size_t>(m_selected) >= num_submenus) - m_selected = 0; - if (m_submenus[m_selected]->GetType() == Type::Separator) - continue; - else - break; - } - return eKeyHandled; - } - break; - - case KEY_UP: - if (m_submenus.size() > 1) { - const int start_select = m_selected; - while (--m_selected != start_select) { - if (m_selected < static_cast<int>(0)) - m_selected = num_submenus - 1; - if (m_submenus[m_selected]->GetType() == Type::Separator) - continue; - else - break; - } - return eKeyHandled; - } - break; - - case KEY_RETURN: - if (static_cast<size_t>(selected_idx) < num_submenus) { - if (submenus[selected_idx]->Action() == MenuActionResult::Quit) - return eQuitApplication; - window.GetParent()->RemoveSubWindow(&window); - return eKeyHandled; - } - break; - - case KEY_ESCAPE: // Beware: pressing escape key has 1 to 2 second delay in - // case other chars are entered for escaped sequences - window.GetParent()->RemoveSubWindow(&window); - return eKeyHandled; - - default: - for (size_t i = 0; i < num_submenus; ++i) { - Menu *menu = submenus[i].get(); - if (menu->GetKeyValue() == key) { - SetSelectedSubmenuIndex(i); - window.GetParent()->RemoveSubWindow(&window); - if (menu->Action() == MenuActionResult::Quit) - return eQuitApplication; - return eKeyHandled; - } - } - break; - } - } else if (menu_type == Menu::Type::Separator) { - } - return result; -} - -class Application { -public: - Application(FILE *in, FILE *out) - : m_window_sp(), m_screen(nullptr), m_in(in), m_out(out) {} - - ~Application() { - m_window_delegates.clear(); - m_window_sp.reset(); - if (m_screen) { - ::delscreen(m_screen); - m_screen = nullptr; - } - } - - void Initialize() { - ::setlocale(LC_ALL, ""); - ::setlocale(LC_CTYPE, ""); - m_screen = ::newterm(nullptr, m_out, m_in); - ::start_color(); - ::curs_set(0); - ::noecho(); - ::keypad(stdscr, TRUE); - } - - void Terminate() { ::endwin(); } - - void Run(Debugger &debugger) { - bool done = false; - int delay_in_tenths_of_a_second = 1; - - // Alas the threading model in curses is a bit lame so we need to resort to - // polling every 0.5 seconds. We could poll for stdin ourselves and then - // pass the keys down but then we need to translate all of the escape - // sequences ourselves. So we resort to polling for input because we need - // to receive async process events while in this loop. - - halfdelay(delay_in_tenths_of_a_second); // Poll using some number of tenths - // of seconds seconds when calling - // Window::GetChar() - - ListenerSP listener_sp( - Listener::MakeListener("lldb.IOHandler.curses.Application")); - ConstString broadcaster_class_target(Target::GetStaticBroadcasterClass()); - ConstString broadcaster_class_process(Process::GetStaticBroadcasterClass()); - ConstString broadcaster_class_thread(Thread::GetStaticBroadcasterClass()); - debugger.EnableForwardEvents(listener_sp); - - bool update = true; -#if defined(__APPLE__) - std::deque<int> escape_chars; -#endif - - while (!done) { - if (update) { - m_window_sp->Draw(false); - // All windows should be calling Window::DeferredRefresh() instead of - // Window::Refresh() so we can do a single update and avoid any screen - // blinking - update_panels(); - - // Cursor hiding isn't working on MacOSX, so hide it in the top left - // corner - m_window_sp->MoveCursor(0, 0); - - doupdate(); - update = false; - } - -#if defined(__APPLE__) - // Terminal.app doesn't map its function keys correctly, F1-F4 default - // to: \033OP, \033OQ, \033OR, \033OS, so lets take care of this here if - // possible - int ch; - if (escape_chars.empty()) - ch = m_window_sp->GetChar(); - else { - ch = escape_chars.front(); - escape_chars.pop_front(); - } - if (ch == KEY_ESCAPE) { - int ch2 = m_window_sp->GetChar(); - if (ch2 == 'O') { - int ch3 = m_window_sp->GetChar(); - switch (ch3) { - case 'P': - ch = KEY_F(1); - break; - case 'Q': - ch = KEY_F(2); - break; - case 'R': - ch = KEY_F(3); - break; - case 'S': - ch = KEY_F(4); - break; - default: - escape_chars.push_back(ch2); - if (ch3 != -1) - escape_chars.push_back(ch3); - break; - } - } else if (ch2 != -1) - escape_chars.push_back(ch2); - } -#else - int ch = m_window_sp->GetChar(); - -#endif - if (ch == -1) { - if (feof(m_in) || ferror(m_in)) { - done = true; - } else { - // Just a timeout from using halfdelay(), check for events - EventSP event_sp; - while (listener_sp->PeekAtNextEvent()) { - listener_sp->GetEvent(event_sp, std::chrono::seconds(0)); - - if (event_sp) { - Broadcaster *broadcaster = event_sp->GetBroadcaster(); - if (broadcaster) { - // uint32_t event_type = event_sp->GetType(); - ConstString broadcaster_class( - broadcaster->GetBroadcasterClass()); - if (broadcaster_class == broadcaster_class_process) { - debugger.GetCommandInterpreter().UpdateExecutionContext( - nullptr); - update = true; - continue; // Don't get any key, just update our view - } - } - } - } - } - } else { - HandleCharResult key_result = m_window_sp->HandleChar(ch); - switch (key_result) { - case eKeyHandled: - debugger.GetCommandInterpreter().UpdateExecutionContext(nullptr); - update = true; - break; - case eKeyNotHandled: - break; - case eQuitApplication: - done = true; - break; - } - } - } - - debugger.CancelForwardEvents(listener_sp); - } - - WindowSP &GetMainWindow() { - if (!m_window_sp) - m_window_sp = std::make_shared<Window>("main", stdscr, false); - return m_window_sp; - } - -protected: - WindowSP m_window_sp; - WindowDelegates m_window_delegates; - SCREEN *m_screen; - FILE *m_in; - FILE *m_out; -}; - -} // namespace curses - -using namespace curses; - -struct Row { - ValueObjectManager value; - Row *parent; - // The process stop ID when the children were calculated. - uint32_t children_stop_id; - int row_idx; - int x; - int y; - bool might_have_children; - bool expanded; - bool calculated_children; - std::vector<Row> children; - - Row(const ValueObjectSP &v, Row *p) - : value(v, lldb::eDynamicDontRunTarget, true), parent(p), row_idx(0), - x(1), y(1), might_have_children(v ? v->MightHaveChildren() : false), - expanded(false), calculated_children(false), children() {} - - size_t GetDepth() const { - if (parent) - return 1 + parent->GetDepth(); - return 0; - } - - void Expand() { expanded = true; } - - std::vector<Row> &GetChildren() { - ProcessSP process_sp = value.GetProcessSP(); - auto stop_id = process_sp->GetStopID(); - if (process_sp && stop_id != children_stop_id) { - children_stop_id = stop_id; - calculated_children = false; - } - if (!calculated_children) { - children.clear(); - calculated_children = true; - ValueObjectSP valobj = value.GetSP(); - if (valobj) { - const size_t num_children = valobj->GetNumChildren(); - for (size_t i = 0; i < num_children; ++i) { - children.push_back(Row(valobj->GetChildAtIndex(i, true), this)); - } - } - } - return children; - } - - void Unexpand() { - expanded = false; - calculated_children = false; - children.clear(); - } - - void DrawTree(Window &window) { - if (parent) - parent->DrawTreeForChild(window, this, 0); - - if (might_have_children) { - // It we can get UTF8 characters to work we should try to use the - // "symbol" UTF8 string below - // const char *symbol = ""; - // if (row.expanded) - // symbol = "\xe2\x96\xbd "; - // else - // symbol = "\xe2\x96\xb7 "; - // window.PutCString (symbol); - - // The ACS_DARROW and ACS_RARROW don't look very nice they are just a 'v' - // or '>' character... - // if (expanded) - // window.PutChar (ACS_DARROW); - // else - // window.PutChar (ACS_RARROW); - // Since we can't find any good looking right arrow/down arrow symbols, - // just use a diamond... - window.PutChar(ACS_DIAMOND); - window.PutChar(ACS_HLINE); - } - } - - void DrawTreeForChild(Window &window, Row *child, uint32_t reverse_depth) { - if (parent) - parent->DrawTreeForChild(window, this, reverse_depth + 1); - - if (&GetChildren().back() == child) { - // Last child - if (reverse_depth == 0) { - window.PutChar(ACS_LLCORNER); - window.PutChar(ACS_HLINE); - } else { - window.PutChar(' '); - window.PutChar(' '); - } - } else { - if (reverse_depth == 0) { - window.PutChar(ACS_LTEE); - window.PutChar(ACS_HLINE); - } else { - window.PutChar(ACS_VLINE); - window.PutChar(' '); - } - } - } -}; - -struct DisplayOptions { - bool show_types; -}; - -class TreeItem; - -class TreeDelegate { -public: - TreeDelegate() = default; - virtual ~TreeDelegate() = default; - - virtual void TreeDelegateDrawTreeItem(TreeItem &item, Window &window) = 0; - virtual void TreeDelegateGenerateChildren(TreeItem &item) = 0; - virtual bool TreeDelegateItemSelected( - TreeItem &item) = 0; // Return true if we need to update views -}; - -typedef std::shared_ptr<TreeDelegate> TreeDelegateSP; - -class TreeItem { -public: - TreeItem(TreeItem *parent, TreeDelegate &delegate, bool might_have_children) - : m_parent(parent), m_delegate(delegate), m_user_data(nullptr), - m_identifier(0), m_row_idx(-1), m_children(), - m_might_have_children(might_have_children), m_is_expanded(false) {} - - TreeItem &operator=(const TreeItem &rhs) { - if (this != &rhs) { - m_parent = rhs.m_parent; - m_delegate = rhs.m_delegate; - m_user_data = rhs.m_user_data; - m_identifier = rhs.m_identifier; - m_row_idx = rhs.m_row_idx; - m_children = rhs.m_children; - m_might_have_children = rhs.m_might_have_children; - m_is_expanded = rhs.m_is_expanded; - } - return *this; - } - - size_t GetDepth() const { - if (m_parent) - return 1 + m_parent->GetDepth(); - return 0; - } - - int GetRowIndex() const { return m_row_idx; } - - void ClearChildren() { m_children.clear(); } - - void Resize(size_t n, const TreeItem &t) { m_children.resize(n, t); } - - TreeItem &operator[](size_t i) { return m_children[i]; } - - void SetRowIndex(int row_idx) { m_row_idx = row_idx; } - - size_t GetNumChildren() { - m_delegate.TreeDelegateGenerateChildren(*this); - return m_children.size(); - } - - void ItemWasSelected() { m_delegate.TreeDelegateItemSelected(*this); } - - void CalculateRowIndexes(int &row_idx) { - SetRowIndex(row_idx); - ++row_idx; - - const bool expanded = IsExpanded(); - - // The root item must calculate its children, or we must calculate the - // number of children if the item is expanded - if (m_parent == nullptr || expanded) - GetNumChildren(); - - for (auto &item : m_children) { - if (expanded) - item.CalculateRowIndexes(row_idx); - else - item.SetRowIndex(-1); - } - } - - TreeItem *GetParent() { return m_parent; } - - bool IsExpanded() const { return m_is_expanded; } - - void Expand() { m_is_expanded = true; } - - void Unexpand() { m_is_expanded = false; } - - bool Draw(Window &window, const int first_visible_row, - const uint32_t selected_row_idx, int &row_idx, int &num_rows_left) { - if (num_rows_left <= 0) - return false; - - if (m_row_idx >= first_visible_row) { - window.MoveCursor(2, row_idx + 1); - - if (m_parent) - m_parent->DrawTreeForChild(window, this, 0); - - if (m_might_have_children) { - // It we can get UTF8 characters to work we should try to use the - // "symbol" UTF8 string below - // const char *symbol = ""; - // if (row.expanded) - // symbol = "\xe2\x96\xbd "; - // else - // symbol = "\xe2\x96\xb7 "; - // window.PutCString (symbol); - - // The ACS_DARROW and ACS_RARROW don't look very nice they are just a - // 'v' or '>' character... - // if (expanded) - // window.PutChar (ACS_DARROW); - // else - // window.PutChar (ACS_RARROW); - // Since we can't find any good looking right arrow/down arrow symbols, - // just use a diamond... - window.PutChar(ACS_DIAMOND); - window.PutChar(ACS_HLINE); - } - bool highlight = (selected_row_idx == static_cast<size_t>(m_row_idx)) && - window.IsActive(); - - if (highlight) - window.AttributeOn(A_REVERSE); - - m_delegate.TreeDelegateDrawTreeItem(*this, window); - - if (highlight) - window.AttributeOff(A_REVERSE); - ++row_idx; - --num_rows_left; - } - - if (num_rows_left <= 0) - return false; // We are done drawing... - - if (IsExpanded()) { - for (auto &item : m_children) { - // If we displayed all the rows and item.Draw() returns false we are - // done drawing and can exit this for loop - if (!item.Draw(window, first_visible_row, selected_row_idx, row_idx, - num_rows_left)) - break; - } - } - return num_rows_left >= 0; // Return true if not done drawing yet - } - - void DrawTreeForChild(Window &window, TreeItem *child, - uint32_t reverse_depth) { - if (m_parent) - m_parent->DrawTreeForChild(window, this, reverse_depth + 1); - - if (&m_children.back() == child) { - // Last child - if (reverse_depth == 0) { - window.PutChar(ACS_LLCORNER); - window.PutChar(ACS_HLINE); - } else { - window.PutChar(' '); - window.PutChar(' '); - } - } else { - if (reverse_depth == 0) { - window.PutChar(ACS_LTEE); - window.PutChar(ACS_HLINE); - } else { - window.PutChar(ACS_VLINE); - window.PutChar(' '); - } - } - } - - TreeItem *GetItemForRowIndex(uint32_t row_idx) { - if (static_cast<uint32_t>(m_row_idx) == row_idx) - return this; - if (m_children.empty()) - return nullptr; - if (IsExpanded()) { - for (auto &item : m_children) { - TreeItem *selected_item_ptr = item.GetItemForRowIndex(row_idx); - if (selected_item_ptr) - return selected_item_ptr; - } - } - return nullptr; - } - - void *GetUserData() const { return m_user_data; } - - void SetUserData(void *user_data) { m_user_data = user_data; } - - uint64_t GetIdentifier() const { return m_identifier; } - - void SetIdentifier(uint64_t identifier) { m_identifier = identifier; } - - void SetMightHaveChildren(bool b) { m_might_have_children = b; } - -protected: - TreeItem *m_parent; - TreeDelegate &m_delegate; - void *m_user_data; - uint64_t m_identifier; - int m_row_idx; // Zero based visible row index, -1 if not visible or for the - // root item - std::vector<TreeItem> m_children; - bool m_might_have_children; - bool m_is_expanded; -}; - -class TreeWindowDelegate : public WindowDelegate { -public: - TreeWindowDelegate(Debugger &debugger, const TreeDelegateSP &delegate_sp) - : m_debugger(debugger), m_delegate_sp(delegate_sp), - m_root(nullptr, *delegate_sp, true), m_selected_item(nullptr), - m_num_rows(0), m_selected_row_idx(0), m_first_visible_row(0), - m_min_x(0), m_min_y(0), m_max_x(0), m_max_y(0) {} - - int NumVisibleRows() const { return m_max_y - m_min_y; } - - bool WindowDelegateDraw(Window &window, bool force) override { - ExecutionContext exe_ctx( - m_debugger.GetCommandInterpreter().GetExecutionContext()); - Process *process = exe_ctx.GetProcessPtr(); - - bool display_content = false; - if (process) { - StateType state = process->GetState(); - if (StateIsStoppedState(state, true)) { - // We are stopped, so it is ok to - display_content = true; - } else if (StateIsRunningState(state)) { - return true; // Don't do any updating when we are running - } - } - - m_min_x = 2; - m_min_y = 1; - m_max_x = window.GetWidth() - 1; - m_max_y = window.GetHeight() - 1; - - window.Erase(); - window.DrawTitleBox(window.GetName()); - - if (display_content) { - const int num_visible_rows = NumVisibleRows(); - m_num_rows = 0; - m_root.CalculateRowIndexes(m_num_rows); - - // If we unexpanded while having something selected our total number of - // rows is less than the num visible rows, then make sure we show all the - // rows by setting the first visible row accordingly. - if (m_first_visible_row > 0 && m_num_rows < num_visible_rows) - m_first_visible_row = 0; - - // Make sure the selected row is always visible - if (m_selected_row_idx < m_first_visible_row) - m_first_visible_row = m_selected_row_idx; - else if (m_first_visible_row + num_visible_rows <= m_selected_row_idx) - m_first_visible_row = m_selected_row_idx - num_visible_rows + 1; - - int row_idx = 0; - int num_rows_left = num_visible_rows; - m_root.Draw(window, m_first_visible_row, m_selected_row_idx, row_idx, - num_rows_left); - // Get the selected row - m_selected_item = m_root.GetItemForRowIndex(m_selected_row_idx); - } else { - m_selected_item = nullptr; - } - - return true; // Drawing handled - } - - const char *WindowDelegateGetHelpText() override { - return "Thread window keyboard shortcuts:"; - } - - KeyHelp *WindowDelegateGetKeyHelp() override { - static curses::KeyHelp g_source_view_key_help[] = { - {KEY_UP, "Select previous item"}, - {KEY_DOWN, "Select next item"}, - {KEY_RIGHT, "Expand the selected item"}, - {KEY_LEFT, - "Unexpand the selected item or select parent if not expanded"}, - {KEY_PPAGE, "Page up"}, - {KEY_NPAGE, "Page down"}, - {'h', "Show help dialog"}, - {' ', "Toggle item expansion"}, - {',', "Page up"}, - {'.', "Page down"}, - {'\0', nullptr}}; - return g_source_view_key_help; - } - - HandleCharResult WindowDelegateHandleChar(Window &window, int c) override { - switch (c) { - case ',': - case KEY_PPAGE: - // Page up key - if (m_first_visible_row > 0) { - if (m_first_visible_row > m_max_y) - m_first_visible_row -= m_max_y; - else - m_first_visible_row = 0; - m_selected_row_idx = m_first_visible_row; - m_selected_item = m_root.GetItemForRowIndex(m_selected_row_idx); - if (m_selected_item) - m_selected_item->ItemWasSelected(); - } - return eKeyHandled; - - case '.': - case KEY_NPAGE: - // Page down key - if (m_num_rows > m_max_y) { - if (m_first_visible_row + m_max_y < m_num_rows) { - m_first_visible_row += m_max_y; - m_selected_row_idx = m_first_visible_row; - m_selected_item = m_root.GetItemForRowIndex(m_selected_row_idx); - if (m_selected_item) - m_selected_item->ItemWasSelected(); - } - } - return eKeyHandled; - - case KEY_UP: - if (m_selected_row_idx > 0) { - --m_selected_row_idx; - m_selected_item = m_root.GetItemForRowIndex(m_selected_row_idx); - if (m_selected_item) - m_selected_item->ItemWasSelected(); - } - return eKeyHandled; - - case KEY_DOWN: - if (m_selected_row_idx + 1 < m_num_rows) { - ++m_selected_row_idx; - m_selected_item = m_root.GetItemForRowIndex(m_selected_row_idx); - if (m_selected_item) - m_selected_item->ItemWasSelected(); - } - return eKeyHandled; - - case KEY_RIGHT: - if (m_selected_item) { - if (!m_selected_item->IsExpanded()) - m_selected_item->Expand(); - } - return eKeyHandled; - - case KEY_LEFT: - if (m_selected_item) { - if (m_selected_item->IsExpanded()) - m_selected_item->Unexpand(); - else if (m_selected_item->GetParent()) { - m_selected_row_idx = m_selected_item->GetParent()->GetRowIndex(); - m_selected_item = m_root.GetItemForRowIndex(m_selected_row_idx); - if (m_selected_item) - m_selected_item->ItemWasSelected(); - } - } - return eKeyHandled; - - case ' ': - // Toggle expansion state when SPACE is pressed - if (m_selected_item) { - if (m_selected_item->IsExpanded()) - m_selected_item->Unexpand(); - else - m_selected_item->Expand(); - } - return eKeyHandled; - - case 'h': - window.CreateHelpSubwindow(); - return eKeyHandled; - - default: - break; - } - return eKeyNotHandled; - } - -protected: - Debugger &m_debugger; - TreeDelegateSP m_delegate_sp; - TreeItem m_root; - TreeItem *m_selected_item; - int m_num_rows; - int m_selected_row_idx; - int m_first_visible_row; - int m_min_x; - int m_min_y; - int m_max_x; - int m_max_y; -}; - -class FrameTreeDelegate : public TreeDelegate { -public: - FrameTreeDelegate() : TreeDelegate() { - FormatEntity::Parse( - "frame #${frame.index}: {${function.name}${function.pc-offset}}}", - m_format); - } - - ~FrameTreeDelegate() override = default; - - void TreeDelegateDrawTreeItem(TreeItem &item, Window &window) override { - Thread *thread = (Thread *)item.GetUserData(); - if (thread) { - const uint64_t frame_idx = item.GetIdentifier(); - StackFrameSP frame_sp = thread->GetStackFrameAtIndex(frame_idx); - if (frame_sp) { - StreamString strm; - const SymbolContext &sc = - frame_sp->GetSymbolContext(eSymbolContextEverything); - ExecutionContext exe_ctx(frame_sp); - if (FormatEntity::Format(m_format, strm, &sc, &exe_ctx, nullptr, - nullptr, false, false)) { - int right_pad = 1; - window.PutCStringTruncated(strm.GetString().str().c_str(), right_pad); - } - } - } - } - - void TreeDelegateGenerateChildren(TreeItem &item) override { - // No children for frames yet... - } - - bool TreeDelegateItemSelected(TreeItem &item) override { - Thread *thread = (Thread *)item.GetUserData(); - if (thread) { - thread->GetProcess()->GetThreadList().SetSelectedThreadByID( - thread->GetID()); - const uint64_t frame_idx = item.GetIdentifier(); - thread->SetSelectedFrameByIndex(frame_idx); - return true; - } - return false; - } - -protected: - FormatEntity::Entry m_format; -}; - -class ThreadTreeDelegate : public TreeDelegate { -public: - ThreadTreeDelegate(Debugger &debugger) - : TreeDelegate(), m_debugger(debugger), m_tid(LLDB_INVALID_THREAD_ID), - m_stop_id(UINT32_MAX) { - FormatEntity::Parse("thread #${thread.index}: tid = ${thread.id}{, stop " - "reason = ${thread.stop-reason}}", - m_format); - } - - ~ThreadTreeDelegate() override = default; - - ProcessSP GetProcess() { - return m_debugger.GetCommandInterpreter() - .GetExecutionContext() - .GetProcessSP(); - } - - ThreadSP GetThread(const TreeItem &item) { - ProcessSP process_sp = GetProcess(); - if (process_sp) - return process_sp->GetThreadList().FindThreadByID(item.GetIdentifier()); - return ThreadSP(); - } - - void TreeDelegateDrawTreeItem(TreeItem &item, Window &window) override { - ThreadSP thread_sp = GetThread(item); - if (thread_sp) { - StreamString strm; - ExecutionContext exe_ctx(thread_sp); - if (FormatEntity::Format(m_format, strm, nullptr, &exe_ctx, nullptr, - nullptr, false, false)) { - int right_pad = 1; - window.PutCStringTruncated(strm.GetString().str().c_str(), right_pad); - } - } - } - - void TreeDelegateGenerateChildren(TreeItem &item) override { - ProcessSP process_sp = GetProcess(); - if (process_sp && process_sp->IsAlive()) { - StateType state = process_sp->GetState(); - if (StateIsStoppedState(state, true)) { - ThreadSP thread_sp = GetThread(item); - if (thread_sp) { - if (m_stop_id == process_sp->GetStopID() && - thread_sp->GetID() == m_tid) - return; // Children are already up to date - if (!m_frame_delegate_sp) { - // Always expand the thread item the first time we show it - m_frame_delegate_sp = std::make_shared<FrameTreeDelegate>(); - } - - m_stop_id = process_sp->GetStopID(); - m_tid = thread_sp->GetID(); - - TreeItem t(&item, *m_frame_delegate_sp, false); - size_t num_frames = thread_sp->GetStackFrameCount(); - item.Resize(num_frames, t); - for (size_t i = 0; i < num_frames; ++i) { - item[i].SetUserData(thread_sp.get()); - item[i].SetIdentifier(i); - } - } - return; - } - } - item.ClearChildren(); - } - - bool TreeDelegateItemSelected(TreeItem &item) override { - ProcessSP process_sp = GetProcess(); - if (process_sp && process_sp->IsAlive()) { - StateType state = process_sp->GetState(); - if (StateIsStoppedState(state, true)) { - ThreadSP thread_sp = GetThread(item); - if (thread_sp) { - ThreadList &thread_list = thread_sp->GetProcess()->GetThreadList(); - std::lock_guard<std::recursive_mutex> guard(thread_list.GetMutex()); - ThreadSP selected_thread_sp = thread_list.GetSelectedThread(); - if (selected_thread_sp->GetID() != thread_sp->GetID()) { - thread_list.SetSelectedThreadByID(thread_sp->GetID()); - return true; - } - } - } - } - return false; - } - -protected: - Debugger &m_debugger; - std::shared_ptr<FrameTreeDelegate> m_frame_delegate_sp; - lldb::user_id_t m_tid; - uint32_t m_stop_id; - FormatEntity::Entry m_format; -}; - -class ThreadsTreeDelegate : public TreeDelegate { -public: - ThreadsTreeDelegate(Debugger &debugger) - : TreeDelegate(), m_thread_delegate_sp(), m_debugger(debugger), - m_stop_id(UINT32_MAX) { - FormatEntity::Parse("process ${process.id}{, name = ${process.name}}", - m_format); - } - - ~ThreadsTreeDelegate() override = default; - - ProcessSP GetProcess() { - return m_debugger.GetCommandInterpreter() - .GetExecutionContext() - .GetProcessSP(); - } - - void TreeDelegateDrawTreeItem(TreeItem &item, Window &window) override { - ProcessSP process_sp = GetProcess(); - if (process_sp && process_sp->IsAlive()) { - StreamString strm; - ExecutionContext exe_ctx(process_sp); - if (FormatEntity::Format(m_format, strm, nullptr, &exe_ctx, nullptr, - nullptr, false, false)) { - int right_pad = 1; - window.PutCStringTruncated(strm.GetString().str().c_str(), right_pad); - } - } - } - - void TreeDelegateGenerateChildren(TreeItem &item) override { - ProcessSP process_sp = GetProcess(); - if (process_sp && process_sp->IsAlive()) { - StateType state = process_sp->GetState(); - if (StateIsStoppedState(state, true)) { - const uint32_t stop_id = process_sp->GetStopID(); - if (m_stop_id == stop_id) - return; // Children are already up to date - - m_stop_id = stop_id; - - if (!m_thread_delegate_sp) { - // Always expand the thread item the first time we show it - // item.Expand(); - m_thread_delegate_sp = - std::make_shared<ThreadTreeDelegate>(m_debugger); - } - - TreeItem t(&item, *m_thread_delegate_sp, false); - ThreadList &threads = process_sp->GetThreadList(); - std::lock_guard<std::recursive_mutex> guard(threads.GetMutex()); - size_t num_threads = threads.GetSize(); - item.Resize(num_threads, t); - for (size_t i = 0; i < num_threads; ++i) { - item[i].SetIdentifier(threads.GetThreadAtIndex(i)->GetID()); - item[i].SetMightHaveChildren(true); - } - return; - } - } - item.ClearChildren(); - } - - bool TreeDelegateItemSelected(TreeItem &item) override { return false; } - -protected: - std::shared_ptr<ThreadTreeDelegate> m_thread_delegate_sp; - Debugger &m_debugger; - uint32_t m_stop_id; - FormatEntity::Entry m_format; -}; - -class ValueObjectListDelegate : public WindowDelegate { -public: - ValueObjectListDelegate() - : m_rows(), m_selected_row(nullptr), m_selected_row_idx(0), - m_first_visible_row(0), m_num_rows(0), m_max_x(0), m_max_y(0) {} - - ValueObjectListDelegate(ValueObjectList &valobj_list) - : m_rows(), m_selected_row(nullptr), m_selected_row_idx(0), - m_first_visible_row(0), m_num_rows(0), m_max_x(0), m_max_y(0) { - SetValues(valobj_list); - } - - ~ValueObjectListDelegate() override = default; - - void SetValues(ValueObjectList &valobj_list) { - m_selected_row = nullptr; - m_selected_row_idx = 0; - m_first_visible_row = 0; - m_num_rows = 0; - m_rows.clear(); - for (auto &valobj_sp : valobj_list.GetObjects()) - m_rows.push_back(Row(valobj_sp, nullptr)); - } - - bool WindowDelegateDraw(Window &window, bool force) override { - m_num_rows = 0; - m_min_x = 2; - m_min_y = 1; - m_max_x = window.GetWidth() - 1; - m_max_y = window.GetHeight() - 1; - - window.Erase(); - window.DrawTitleBox(window.GetName()); - - const int num_visible_rows = NumVisibleRows(); - const int num_rows = CalculateTotalNumberRows(m_rows); - - // If we unexpanded while having something selected our total number of - // rows is less than the num visible rows, then make sure we show all the - // rows by setting the first visible row accordingly. - if (m_first_visible_row > 0 && num_rows < num_visible_rows) - m_first_visible_row = 0; - - // Make sure the selected row is always visible - if (m_selected_row_idx < m_first_visible_row) - m_first_visible_row = m_selected_row_idx; - else if (m_first_visible_row + num_visible_rows <= m_selected_row_idx) - m_first_visible_row = m_selected_row_idx - num_visible_rows + 1; - - DisplayRows(window, m_rows, g_options); - - // Get the selected row - m_selected_row = GetRowForRowIndex(m_selected_row_idx); - // Keep the cursor on the selected row so the highlight and the cursor are - // always on the same line - if (m_selected_row) - window.MoveCursor(m_selected_row->x, m_selected_row->y); - - return true; // Drawing handled - } - - KeyHelp *WindowDelegateGetKeyHelp() override { - static curses::KeyHelp g_source_view_key_help[] = { - {KEY_UP, "Select previous item"}, - {KEY_DOWN, "Select next item"}, - {KEY_RIGHT, "Expand selected item"}, - {KEY_LEFT, "Unexpand selected item or select parent if not expanded"}, - {KEY_PPAGE, "Page up"}, - {KEY_NPAGE, "Page down"}, - {'A', "Format as annotated address"}, - {'b', "Format as binary"}, - {'B', "Format as hex bytes with ASCII"}, - {'c', "Format as character"}, - {'d', "Format as a signed integer"}, - {'D', "Format selected value using the default format for the type"}, - {'f', "Format as float"}, - {'h', "Show help dialog"}, - {'i', "Format as instructions"}, - {'o', "Format as octal"}, - {'p', "Format as pointer"}, - {'s', "Format as C string"}, - {'t', "Toggle showing/hiding type names"}, - {'u', "Format as an unsigned integer"}, - {'x', "Format as hex"}, - {'X', "Format as uppercase hex"}, - {' ', "Toggle item expansion"}, - {',', "Page up"}, - {'.', "Page down"}, - {'\0', nullptr}}; - return g_source_view_key_help; - } - - HandleCharResult WindowDelegateHandleChar(Window &window, int c) override { - switch (c) { - case 'x': - case 'X': - case 'o': - case 's': - case 'u': - case 'd': - case 'D': - case 'i': - case 'A': - case 'p': - case 'c': - case 'b': - case 'B': - case 'f': - // Change the format for the currently selected item - if (m_selected_row) { - auto valobj_sp = m_selected_row->value.GetSP(); - if (valobj_sp) - valobj_sp->SetFormat(FormatForChar(c)); - } - return eKeyHandled; - - case 't': - // Toggle showing type names - g_options.show_types = !g_options.show_types; - return eKeyHandled; - - case ',': - case KEY_PPAGE: - // Page up key - if (m_first_visible_row > 0) { - if (static_cast<int>(m_first_visible_row) > m_max_y) - m_first_visible_row -= m_max_y; - else - m_first_visible_row = 0; - m_selected_row_idx = m_first_visible_row; - } - return eKeyHandled; - - case '.': - case KEY_NPAGE: - // Page down key - if (m_num_rows > static_cast<size_t>(m_max_y)) { - if (m_first_visible_row + m_max_y < m_num_rows) { - m_first_visible_row += m_max_y; - m_selected_row_idx = m_first_visible_row; - } - } - return eKeyHandled; - - case KEY_UP: - if (m_selected_row_idx > 0) - --m_selected_row_idx; - return eKeyHandled; - - case KEY_DOWN: - if (m_selected_row_idx + 1 < m_num_rows) - ++m_selected_row_idx; - return eKeyHandled; - - case KEY_RIGHT: - if (m_selected_row) { - if (!m_selected_row->expanded) - m_selected_row->Expand(); - } - return eKeyHandled; - - case KEY_LEFT: - if (m_selected_row) { - if (m_selected_row->expanded) - m_selected_row->Unexpand(); - else if (m_selected_row->parent) - m_selected_row_idx = m_selected_row->parent->row_idx; - } - return eKeyHandled; - - case ' ': - // Toggle expansion state when SPACE is pressed - if (m_selected_row) { - if (m_selected_row->expanded) - m_selected_row->Unexpand(); - else - m_selected_row->Expand(); - } - return eKeyHandled; - - case 'h': - window.CreateHelpSubwindow(); - return eKeyHandled; - - default: - break; - } - return eKeyNotHandled; - } - -protected: - std::vector<Row> m_rows; - Row *m_selected_row; - uint32_t m_selected_row_idx; - uint32_t m_first_visible_row; - uint32_t m_num_rows; - int m_min_x; - int m_min_y; - int m_max_x; - int m_max_y; - - static Format FormatForChar(int c) { - switch (c) { - case 'x': - return eFormatHex; - case 'X': - return eFormatHexUppercase; - case 'o': - return eFormatOctal; - case 's': - return eFormatCString; - case 'u': - return eFormatUnsigned; - case 'd': - return eFormatDecimal; - case 'D': - return eFormatDefault; - case 'i': - return eFormatInstruction; - case 'A': - return eFormatAddressInfo; - case 'p': - return eFormatPointer; - case 'c': - return eFormatChar; - case 'b': - return eFormatBinary; - case 'B': - return eFormatBytesWithASCII; - case 'f': - return eFormatFloat; - } - return eFormatDefault; - } - - bool DisplayRowObject(Window &window, Row &row, DisplayOptions &options, - bool highlight, bool last_child) { - ValueObject *valobj = row.value.GetSP().get(); - - if (valobj == nullptr) - return false; - - const char *type_name = - options.show_types ? valobj->GetTypeName().GetCString() : nullptr; - const char *name = valobj->GetName().GetCString(); - const char *value = valobj->GetValueAsCString(); - const char *summary = valobj->GetSummaryAsCString(); - - window.MoveCursor(row.x, row.y); - - row.DrawTree(window); - - if (highlight) - window.AttributeOn(A_REVERSE); - - if (type_name && type_name[0]) - window.Printf("(%s) ", type_name); - - if (name && name[0]) - window.PutCString(name); - - attr_t changd_attr = 0; - if (valobj->GetValueDidChange()) - changd_attr = COLOR_PAIR(5) | A_BOLD; - - if (value && value[0]) { - window.PutCString(" = "); - if (changd_attr) - window.AttributeOn(changd_attr); - window.PutCString(value); - if (changd_attr) - window.AttributeOff(changd_attr); - } - - if (summary && summary[0]) { - window.PutChar(' '); - if (changd_attr) - window.AttributeOn(changd_attr); - window.PutCString(summary); - if (changd_attr) - window.AttributeOff(changd_attr); - } - - if (highlight) - window.AttributeOff(A_REVERSE); - - return true; - } - - void DisplayRows(Window &window, std::vector<Row> &rows, - DisplayOptions &options) { - // > 0x25B7 - // \/ 0x25BD - - bool window_is_active = window.IsActive(); - for (auto &row : rows) { - const bool last_child = row.parent && &rows[rows.size() - 1] == &row; - // Save the row index in each Row structure - row.row_idx = m_num_rows; - if ((m_num_rows >= m_first_visible_row) && - ((m_num_rows - m_first_visible_row) < - static_cast<size_t>(NumVisibleRows()))) { - row.x = m_min_x; - row.y = m_num_rows - m_first_visible_row + 1; - if (DisplayRowObject(window, row, options, - window_is_active && - m_num_rows == m_selected_row_idx, - last_child)) { - ++m_num_rows; - } else { - row.x = 0; - row.y = 0; - } - } else { - row.x = 0; - row.y = 0; - ++m_num_rows; - } - - auto &children = row.GetChildren(); - if (row.expanded && !children.empty()) { - DisplayRows(window, children, options); - } - } - } - - int CalculateTotalNumberRows(std::vector<Row> &rows) { - int row_count = 0; - for (auto &row : rows) { - ++row_count; - if (row.expanded) - row_count += CalculateTotalNumberRows(row.GetChildren()); - } - return row_count; - } - - static Row *GetRowForRowIndexImpl(std::vector<Row> &rows, size_t &row_index) { - for (auto &row : rows) { - if (row_index == 0) - return &row; - else { - --row_index; - auto &children = row.GetChildren(); - if (row.expanded && !children.empty()) { - Row *result = GetRowForRowIndexImpl(children, row_index); - if (result) - return result; - } - } - } - return nullptr; - } - - Row *GetRowForRowIndex(size_t row_index) { - return GetRowForRowIndexImpl(m_rows, row_index); - } - - int NumVisibleRows() const { return m_max_y - m_min_y; } - - static DisplayOptions g_options; -}; - -class FrameVariablesWindowDelegate : public ValueObjectListDelegate { -public: - FrameVariablesWindowDelegate(Debugger &debugger) - : ValueObjectListDelegate(), m_debugger(debugger), - m_frame_block(nullptr) {} - - ~FrameVariablesWindowDelegate() override = default; - - const char *WindowDelegateGetHelpText() override { - return "Frame variable window keyboard shortcuts:"; - } - - bool WindowDelegateDraw(Window &window, bool force) override { - ExecutionContext exe_ctx( - m_debugger.GetCommandInterpreter().GetExecutionContext()); - Process *process = exe_ctx.GetProcessPtr(); - Block *frame_block = nullptr; - StackFrame *frame = nullptr; - - if (process) { - StateType state = process->GetState(); - if (StateIsStoppedState(state, true)) { - frame = exe_ctx.GetFramePtr(); - if (frame) - frame_block = frame->GetFrameBlock(); - } else if (StateIsRunningState(state)) { - return true; // Don't do any updating when we are running - } - } - - ValueObjectList local_values; - if (frame_block) { - // Only update the variables if they have changed - if (m_frame_block != frame_block) { - m_frame_block = frame_block; - - VariableList *locals = frame->GetVariableList(true); - if (locals) { - const DynamicValueType use_dynamic = eDynamicDontRunTarget; - const size_t num_locals = locals->GetSize(); - for (size_t i = 0; i < num_locals; ++i) { - ValueObjectSP value_sp = frame->GetValueObjectForFrameVariable( - locals->GetVariableAtIndex(i), use_dynamic); - if (value_sp) { - ValueObjectSP synthetic_value_sp = value_sp->GetSyntheticValue(); - if (synthetic_value_sp) - local_values.Append(synthetic_value_sp); - else - local_values.Append(value_sp); - } - } - // Update the values - SetValues(local_values); - } - } - } else { - m_frame_block = nullptr; - // Update the values with an empty list if there is no frame - SetValues(local_values); - } - - return ValueObjectListDelegate::WindowDelegateDraw(window, force); - } - -protected: - Debugger &m_debugger; - Block *m_frame_block; -}; - -class RegistersWindowDelegate : public ValueObjectListDelegate { -public: - RegistersWindowDelegate(Debugger &debugger) - : ValueObjectListDelegate(), m_debugger(debugger) {} - - ~RegistersWindowDelegate() override = default; - - const char *WindowDelegateGetHelpText() override { - return "Register window keyboard shortcuts:"; - } - - bool WindowDelegateDraw(Window &window, bool force) override { - ExecutionContext exe_ctx( - m_debugger.GetCommandInterpreter().GetExecutionContext()); - StackFrame *frame = exe_ctx.GetFramePtr(); - - ValueObjectList value_list; - if (frame) { - if (frame->GetStackID() != m_stack_id) { - m_stack_id = frame->GetStackID(); - RegisterContextSP reg_ctx(frame->GetRegisterContext()); - if (reg_ctx) { - const uint32_t num_sets = reg_ctx->GetRegisterSetCount(); - for (uint32_t set_idx = 0; set_idx < num_sets; ++set_idx) { - value_list.Append( - ValueObjectRegisterSet::Create(frame, reg_ctx, set_idx)); - } - } - SetValues(value_list); - } - } else { - Process *process = exe_ctx.GetProcessPtr(); - if (process && process->IsAlive()) - return true; // Don't do any updating if we are running - else { - // Update the values with an empty list if there is no process or the - // process isn't alive anymore - SetValues(value_list); - } - } - return ValueObjectListDelegate::WindowDelegateDraw(window, force); - } - -protected: - Debugger &m_debugger; - StackID m_stack_id; -}; - -static const char *CursesKeyToCString(int ch) { - static char g_desc[32]; - if (ch >= KEY_F0 && ch < KEY_F0 + 64) { - snprintf(g_desc, sizeof(g_desc), "F%u", ch - KEY_F0); - return g_desc; - } - switch (ch) { - case KEY_DOWN: - return "down"; - case KEY_UP: - return "up"; - case KEY_LEFT: - return "left"; - case KEY_RIGHT: - return "right"; - case KEY_HOME: - return "home"; - case KEY_BACKSPACE: - return "backspace"; - case KEY_DL: - return "delete-line"; - case KEY_IL: - return "insert-line"; - case KEY_DC: - return "delete-char"; - case KEY_IC: - return "insert-char"; - case KEY_CLEAR: - return "clear"; - case KEY_EOS: - return "clear-to-eos"; - case KEY_EOL: - return "clear-to-eol"; - case KEY_SF: - return "scroll-forward"; - case KEY_SR: - return "scroll-backward"; - case KEY_NPAGE: - return "page-down"; - case KEY_PPAGE: - return "page-up"; - case KEY_STAB: - return "set-tab"; - case KEY_CTAB: - return "clear-tab"; - case KEY_CATAB: - return "clear-all-tabs"; - case KEY_ENTER: - return "enter"; - case KEY_PRINT: - return "print"; - case KEY_LL: - return "lower-left key"; - case KEY_A1: - return "upper left of keypad"; - case KEY_A3: - return "upper right of keypad"; - case KEY_B2: - return "center of keypad"; - case KEY_C1: - return "lower left of keypad"; - case KEY_C3: - return "lower right of keypad"; - case KEY_BTAB: - return "back-tab key"; - case KEY_BEG: - return "begin key"; - case KEY_CANCEL: - return "cancel key"; - case KEY_CLOSE: - return "close key"; - case KEY_COMMAND: - return "command key"; - case KEY_COPY: - return "copy key"; - case KEY_CREATE: - return "create key"; - case KEY_END: - return "end key"; - case KEY_EXIT: - return "exit key"; - case KEY_FIND: - return "find key"; - case KEY_HELP: - return "help key"; - case KEY_MARK: - return "mark key"; - case KEY_MESSAGE: - return "message key"; - case KEY_MOVE: - return "move key"; - case KEY_NEXT: - return "next key"; - case KEY_OPEN: - return "open key"; - case KEY_OPTIONS: - return "options key"; - case KEY_PREVIOUS: - return "previous key"; - case KEY_REDO: - return "redo key"; - case KEY_REFERENCE: - return "reference key"; - case KEY_REFRESH: - return "refresh key"; - case KEY_REPLACE: - return "replace key"; - case KEY_RESTART: - return "restart key"; - case KEY_RESUME: - return "resume key"; - case KEY_SAVE: - return "save key"; - case KEY_SBEG: - return "shifted begin key"; - case KEY_SCANCEL: - return "shifted cancel key"; - case KEY_SCOMMAND: - return "shifted command key"; - case KEY_SCOPY: - return "shifted copy key"; - case KEY_SCREATE: - return "shifted create key"; - case KEY_SDC: - return "shifted delete-character key"; - case KEY_SDL: - return "shifted delete-line key"; - case KEY_SELECT: - return "select key"; - case KEY_SEND: - return "shifted end key"; - case KEY_SEOL: - return "shifted clear-to-end-of-line key"; - case KEY_SEXIT: - return "shifted exit key"; - case KEY_SFIND: - return "shifted find key"; - case KEY_SHELP: - return "shifted help key"; - case KEY_SHOME: - return "shifted home key"; - case KEY_SIC: - return "shifted insert-character key"; - case KEY_SLEFT: - return "shifted left-arrow key"; - case KEY_SMESSAGE: - return "shifted message key"; - case KEY_SMOVE: - return "shifted move key"; - case KEY_SNEXT: - return "shifted next key"; - case KEY_SOPTIONS: - return "shifted options key"; - case KEY_SPREVIOUS: - return "shifted previous key"; - case KEY_SPRINT: - return "shifted print key"; - case KEY_SREDO: - return "shifted redo key"; - case KEY_SREPLACE: - return "shifted replace key"; - case KEY_SRIGHT: - return "shifted right-arrow key"; - case KEY_SRSUME: - return "shifted resume key"; - case KEY_SSAVE: - return "shifted save key"; - case KEY_SSUSPEND: - return "shifted suspend key"; - case KEY_SUNDO: - return "shifted undo key"; - case KEY_SUSPEND: - return "suspend key"; - case KEY_UNDO: - return "undo key"; - case KEY_MOUSE: - return "Mouse event has occurred"; - case KEY_RESIZE: - return "Terminal resize event"; -#ifdef KEY_EVENT - case KEY_EVENT: - return "We were interrupted by an event"; -#endif - case KEY_RETURN: - return "return"; - case ' ': - return "space"; - case '\t': - return "tab"; - case KEY_ESCAPE: - return "escape"; - default: - if (isprint(ch)) - snprintf(g_desc, sizeof(g_desc), "%c", ch); - else - snprintf(g_desc, sizeof(g_desc), "\\x%2.2x", ch); - return g_desc; - } - return nullptr; -} - -HelpDialogDelegate::HelpDialogDelegate(const char *text, - KeyHelp *key_help_array) - : m_text(), m_first_visible_line(0) { - if (text && text[0]) { - m_text.SplitIntoLines(text); - m_text.AppendString(""); - } - if (key_help_array) { - for (KeyHelp *key = key_help_array; key->ch; ++key) { - StreamString key_description; - key_description.Printf("%10s - %s", CursesKeyToCString(key->ch), - key->description); - m_text.AppendString(key_description.GetString()); - } - } -} - -HelpDialogDelegate::~HelpDialogDelegate() = default; - -bool HelpDialogDelegate::WindowDelegateDraw(Window &window, bool force) { - window.Erase(); - const int window_height = window.GetHeight(); - int x = 2; - int y = 1; - const int min_y = y; - const int max_y = window_height - 1 - y; - const size_t num_visible_lines = max_y - min_y + 1; - const size_t num_lines = m_text.GetSize(); - const char *bottom_message; - if (num_lines <= num_visible_lines) - bottom_message = "Press any key to exit"; - else - bottom_message = "Use arrows to scroll, any other key to exit"; - window.DrawTitleBox(window.GetName(), bottom_message); - while (y <= max_y) { - window.MoveCursor(x, y); - window.PutCStringTruncated( - m_text.GetStringAtIndex(m_first_visible_line + y - min_y), 1); - ++y; - } - return true; -} - -HandleCharResult HelpDialogDelegate::WindowDelegateHandleChar(Window &window, - int key) { - bool done = false; - const size_t num_lines = m_text.GetSize(); - const size_t num_visible_lines = window.GetHeight() - 2; - - if (num_lines <= num_visible_lines) { - done = true; - // If we have all lines visible and don't need scrolling, then any key - // press will cause us to exit - } else { - switch (key) { - case KEY_UP: - if (m_first_visible_line > 0) - --m_first_visible_line; - break; - - case KEY_DOWN: - if (m_first_visible_line + num_visible_lines < num_lines) - ++m_first_visible_line; - break; - - case KEY_PPAGE: - case ',': - if (m_first_visible_line > 0) { - if (static_cast<size_t>(m_first_visible_line) >= num_visible_lines) - m_first_visible_line -= num_visible_lines; - else - m_first_visible_line = 0; - } - break; - - case KEY_NPAGE: - case '.': - if (m_first_visible_line + num_visible_lines < num_lines) { - m_first_visible_line += num_visible_lines; - if (static_cast<size_t>(m_first_visible_line) > num_lines) - m_first_visible_line = num_lines - num_visible_lines; - } - break; - - default: - done = true; - break; - } - } - if (done) - window.GetParent()->RemoveSubWindow(&window); - return eKeyHandled; -} - -class ApplicationDelegate : public WindowDelegate, public MenuDelegate { -public: - enum { - eMenuID_LLDB = 1, - eMenuID_LLDBAbout, - eMenuID_LLDBExit, - - eMenuID_Target, - eMenuID_TargetCreate, - eMenuID_TargetDelete, - - eMenuID_Process, - eMenuID_ProcessAttach, - eMenuID_ProcessDetach, - eMenuID_ProcessLaunch, - eMenuID_ProcessContinue, - eMenuID_ProcessHalt, - eMenuID_ProcessKill, - - eMenuID_Thread, - eMenuID_ThreadStepIn, - eMenuID_ThreadStepOver, - eMenuID_ThreadStepOut, - - eMenuID_View, - eMenuID_ViewBacktrace, - eMenuID_ViewRegisters, - eMenuID_ViewSource, - eMenuID_ViewVariables, - - eMenuID_Help, - eMenuID_HelpGUIHelp - }; - - ApplicationDelegate(Application &app, Debugger &debugger) - : WindowDelegate(), MenuDelegate(), m_app(app), m_debugger(debugger) {} - - ~ApplicationDelegate() override = default; - - bool WindowDelegateDraw(Window &window, bool force) override { - return false; // Drawing not handled, let standard window drawing happen - } - - HandleCharResult WindowDelegateHandleChar(Window &window, int key) override { - switch (key) { - case '\t': - window.SelectNextWindowAsActive(); - return eKeyHandled; - - case 'h': - window.CreateHelpSubwindow(); - return eKeyHandled; - - case KEY_ESCAPE: - return eQuitApplication; - - default: - break; - } - return eKeyNotHandled; - } - - const char *WindowDelegateGetHelpText() override { - return "Welcome to the LLDB curses GUI.\n\n" - "Press the TAB key to change the selected view.\n" - "Each view has its own keyboard shortcuts, press 'h' to open a " - "dialog to display them.\n\n" - "Common key bindings for all views:"; - } - - KeyHelp *WindowDelegateGetKeyHelp() override { - static curses::KeyHelp g_source_view_key_help[] = { - {'\t', "Select next view"}, - {'h', "Show help dialog with view specific key bindings"}, - {',', "Page up"}, - {'.', "Page down"}, - {KEY_UP, "Select previous"}, - {KEY_DOWN, "Select next"}, - {KEY_LEFT, "Unexpand or select parent"}, - {KEY_RIGHT, "Expand"}, - {KEY_PPAGE, "Page up"}, - {KEY_NPAGE, "Page down"}, - {'\0', nullptr}}; - return g_source_view_key_help; - } - - MenuActionResult MenuDelegateAction(Menu &menu) override { - switch (menu.GetIdentifier()) { - case eMenuID_ThreadStepIn: { - ExecutionContext exe_ctx = - m_debugger.GetCommandInterpreter().GetExecutionContext(); - if (exe_ctx.HasThreadScope()) { - Process *process = exe_ctx.GetProcessPtr(); - if (process && process->IsAlive() && - StateIsStoppedState(process->GetState(), true)) - exe_ctx.GetThreadRef().StepIn(true); - } - } - return MenuActionResult::Handled; - - case eMenuID_ThreadStepOut: { - ExecutionContext exe_ctx = - m_debugger.GetCommandInterpreter().GetExecutionContext(); - if (exe_ctx.HasThreadScope()) { - Process *process = exe_ctx.GetProcessPtr(); - if (process && process->IsAlive() && - StateIsStoppedState(process->GetState(), true)) - exe_ctx.GetThreadRef().StepOut(); - } - } - return MenuActionResult::Handled; - - case eMenuID_ThreadStepOver: { - ExecutionContext exe_ctx = - m_debugger.GetCommandInterpreter().GetExecutionContext(); - if (exe_ctx.HasThreadScope()) { - Process *process = exe_ctx.GetProcessPtr(); - if (process && process->IsAlive() && - StateIsStoppedState(process->GetState(), true)) - exe_ctx.GetThreadRef().StepOver(true); - } - } - return MenuActionResult::Handled; - - case eMenuID_ProcessContinue: { - ExecutionContext exe_ctx = - m_debugger.GetCommandInterpreter().GetExecutionContext(); - if (exe_ctx.HasProcessScope()) { - Process *process = exe_ctx.GetProcessPtr(); - if (process && process->IsAlive() && - StateIsStoppedState(process->GetState(), true)) - process->Resume(); - } - } - return MenuActionResult::Handled; - - case eMenuID_ProcessKill: { - ExecutionContext exe_ctx = - m_debugger.GetCommandInterpreter().GetExecutionContext(); - if (exe_ctx.HasProcessScope()) { - Process *process = exe_ctx.GetProcessPtr(); - if (process && process->IsAlive()) - process->Destroy(false); - } - } - return MenuActionResult::Handled; - - case eMenuID_ProcessHalt: { - ExecutionContext exe_ctx = - m_debugger.GetCommandInterpreter().GetExecutionContext(); - if (exe_ctx.HasProcessScope()) { - Process *process = exe_ctx.GetProcessPtr(); - if (process && process->IsAlive()) - process->Halt(); - } - } - return MenuActionResult::Handled; - - case eMenuID_ProcessDetach: { - ExecutionContext exe_ctx = - m_debugger.GetCommandInterpreter().GetExecutionContext(); - if (exe_ctx.HasProcessScope()) { - Process *process = exe_ctx.GetProcessPtr(); - if (process && process->IsAlive()) - process->Detach(false); - } - } - return MenuActionResult::Handled; - - case eMenuID_Process: { - // Populate the menu with all of the threads if the process is stopped - // when the Process menu gets selected and is about to display its - // submenu. - Menus &submenus = menu.GetSubmenus(); - ExecutionContext exe_ctx = - m_debugger.GetCommandInterpreter().GetExecutionContext(); - Process *process = exe_ctx.GetProcessPtr(); - if (process && process->IsAlive() && - StateIsStoppedState(process->GetState(), true)) { - if (submenus.size() == 7) - menu.AddSubmenu(MenuSP(new Menu(Menu::Type::Separator))); - else if (submenus.size() > 8) - submenus.erase(submenus.begin() + 8, submenus.end()); - - ThreadList &threads = process->GetThreadList(); - std::lock_guard<std::recursive_mutex> guard(threads.GetMutex()); - size_t num_threads = threads.GetSize(); - for (size_t i = 0; i < num_threads; ++i) { - ThreadSP thread_sp = threads.GetThreadAtIndex(i); - char menu_char = '\0'; - if (i < 9) - menu_char = '1' + i; - StreamString thread_menu_title; - thread_menu_title.Printf("Thread %u", thread_sp->GetIndexID()); - const char *thread_name = thread_sp->GetName(); - if (thread_name && thread_name[0]) - thread_menu_title.Printf(" %s", thread_name); - else { - const char *queue_name = thread_sp->GetQueueName(); - if (queue_name && queue_name[0]) - thread_menu_title.Printf(" %s", queue_name); - } - menu.AddSubmenu( - MenuSP(new Menu(thread_menu_title.GetString().str().c_str(), - nullptr, menu_char, thread_sp->GetID()))); - } - } else if (submenus.size() > 7) { - // Remove the separator and any other thread submenu items that were - // previously added - submenus.erase(submenus.begin() + 7, submenus.end()); - } - // Since we are adding and removing items we need to recalculate the name - // lengths - menu.RecalculateNameLengths(); - } - return MenuActionResult::Handled; - - case eMenuID_ViewVariables: { - WindowSP main_window_sp = m_app.GetMainWindow(); - WindowSP source_window_sp = main_window_sp->FindSubWindow("Source"); - WindowSP variables_window_sp = main_window_sp->FindSubWindow("Variables"); - WindowSP registers_window_sp = main_window_sp->FindSubWindow("Registers"); - const Rect source_bounds = source_window_sp->GetBounds(); - - if (variables_window_sp) { - const Rect variables_bounds = variables_window_sp->GetBounds(); - - main_window_sp->RemoveSubWindow(variables_window_sp.get()); - - if (registers_window_sp) { - // We have a registers window, so give all the area back to the - // registers window - Rect registers_bounds = variables_bounds; - registers_bounds.size.width = source_bounds.size.width; - registers_window_sp->SetBounds(registers_bounds); - } else { - // We have no registers window showing so give the bottom area back - // to the source view - source_window_sp->Resize(source_bounds.size.width, - source_bounds.size.height + - variables_bounds.size.height); - } - } else { - Rect new_variables_rect; - if (registers_window_sp) { - // We have a registers window so split the area of the registers - // window into two columns where the left hand side will be the - // variables and the right hand side will be the registers - const Rect variables_bounds = registers_window_sp->GetBounds(); - Rect new_registers_rect; - variables_bounds.VerticalSplitPercentage(0.50, new_variables_rect, - new_registers_rect); - registers_window_sp->SetBounds(new_registers_rect); - } else { - // No variables window, grab the bottom part of the source window - Rect new_source_rect; - source_bounds.HorizontalSplitPercentage(0.70, new_source_rect, - new_variables_rect); - source_window_sp->SetBounds(new_source_rect); - } - WindowSP new_window_sp = main_window_sp->CreateSubWindow( - "Variables", new_variables_rect, false); - new_window_sp->SetDelegate( - WindowDelegateSP(new FrameVariablesWindowDelegate(m_debugger))); - } - touchwin(stdscr); - } - return MenuActionResult::Handled; - - case eMenuID_ViewRegisters: { - WindowSP main_window_sp = m_app.GetMainWindow(); - WindowSP source_window_sp = main_window_sp->FindSubWindow("Source"); - WindowSP variables_window_sp = main_window_sp->FindSubWindow("Variables"); - WindowSP registers_window_sp = main_window_sp->FindSubWindow("Registers"); - const Rect source_bounds = source_window_sp->GetBounds(); - - if (registers_window_sp) { - if (variables_window_sp) { - const Rect variables_bounds = variables_window_sp->GetBounds(); - - // We have a variables window, so give all the area back to the - // variables window - variables_window_sp->Resize(variables_bounds.size.width + - registers_window_sp->GetWidth(), - variables_bounds.size.height); - } else { - // We have no variables window showing so give the bottom area back - // to the source view - source_window_sp->Resize(source_bounds.size.width, - source_bounds.size.height + - registers_window_sp->GetHeight()); - } - main_window_sp->RemoveSubWindow(registers_window_sp.get()); - } else { - Rect new_regs_rect; - if (variables_window_sp) { - // We have a variables window, split it into two columns where the - // left hand side will be the variables and the right hand side will - // be the registers - const Rect variables_bounds = variables_window_sp->GetBounds(); - Rect new_vars_rect; - variables_bounds.VerticalSplitPercentage(0.50, new_vars_rect, - new_regs_rect); - variables_window_sp->SetBounds(new_vars_rect); - } else { - // No registers window, grab the bottom part of the source window - Rect new_source_rect; - source_bounds.HorizontalSplitPercentage(0.70, new_source_rect, - new_regs_rect); - source_window_sp->SetBounds(new_source_rect); - } - WindowSP new_window_sp = - main_window_sp->CreateSubWindow("Registers", new_regs_rect, false); - new_window_sp->SetDelegate( - WindowDelegateSP(new RegistersWindowDelegate(m_debugger))); - } - touchwin(stdscr); - } - return MenuActionResult::Handled; - - case eMenuID_HelpGUIHelp: - m_app.GetMainWindow()->CreateHelpSubwindow(); - return MenuActionResult::Handled; - - default: - break; - } - - return MenuActionResult::NotHandled; - } - -protected: - Application &m_app; - Debugger &m_debugger; -}; - -class StatusBarWindowDelegate : public WindowDelegate { -public: - StatusBarWindowDelegate(Debugger &debugger) : m_debugger(debugger) { - FormatEntity::Parse("Thread: ${thread.id%tid}", m_format); - } - - ~StatusBarWindowDelegate() override = default; - - bool WindowDelegateDraw(Window &window, bool force) override { - ExecutionContext exe_ctx = - m_debugger.GetCommandInterpreter().GetExecutionContext(); - Process *process = exe_ctx.GetProcessPtr(); - Thread *thread = exe_ctx.GetThreadPtr(); - StackFrame *frame = exe_ctx.GetFramePtr(); - window.Erase(); - window.SetBackground(2); - window.MoveCursor(0, 0); - if (process) { - const StateType state = process->GetState(); - window.Printf("Process: %5" PRIu64 " %10s", process->GetID(), - StateAsCString(state)); - - if (StateIsStoppedState(state, true)) { - StreamString strm; - if (thread && FormatEntity::Format(m_format, strm, nullptr, &exe_ctx, - nullptr, nullptr, false, false)) { - window.MoveCursor(40, 0); - window.PutCStringTruncated(strm.GetString().str().c_str(), 1); - } - - window.MoveCursor(60, 0); - if (frame) - window.Printf("Frame: %3u PC = 0x%16.16" PRIx64, - frame->GetFrameIndex(), - frame->GetFrameCodeAddress().GetOpcodeLoadAddress( - exe_ctx.GetTargetPtr())); - } else if (state == eStateExited) { - const char *exit_desc = process->GetExitDescription(); - const int exit_status = process->GetExitStatus(); - if (exit_desc && exit_desc[0]) - window.Printf(" with status = %i (%s)", exit_status, exit_desc); - else - window.Printf(" with status = %i", exit_status); - } - } - return true; - } - -protected: - Debugger &m_debugger; - FormatEntity::Entry m_format; -}; - -class SourceFileWindowDelegate : public WindowDelegate { -public: - SourceFileWindowDelegate(Debugger &debugger) - : WindowDelegate(), m_debugger(debugger), m_sc(), m_file_sp(), - m_disassembly_scope(nullptr), m_disassembly_sp(), m_disassembly_range(), - m_title(), m_line_width(4), m_selected_line(0), m_pc_line(0), - m_stop_id(0), m_frame_idx(UINT32_MAX), m_first_visible_line(0), - m_min_x(0), m_min_y(0), m_max_x(0), m_max_y(0) {} - - ~SourceFileWindowDelegate() override = default; - - void Update(const SymbolContext &sc) { m_sc = sc; } - - uint32_t NumVisibleLines() const { return m_max_y - m_min_y; } - - const char *WindowDelegateGetHelpText() override { - return "Source/Disassembly window keyboard shortcuts:"; - } - - KeyHelp *WindowDelegateGetKeyHelp() override { - static curses::KeyHelp g_source_view_key_help[] = { - {KEY_RETURN, "Run to selected line with one shot breakpoint"}, - {KEY_UP, "Select previous source line"}, - {KEY_DOWN, "Select next source line"}, - {KEY_PPAGE, "Page up"}, - {KEY_NPAGE, "Page down"}, - {'b', "Set breakpoint on selected source/disassembly line"}, - {'c', "Continue process"}, - {'d', "Detach and resume process"}, - {'D', "Detach with process suspended"}, - {'h', "Show help dialog"}, - {'k', "Kill process"}, - {'n', "Step over (source line)"}, - {'N', "Step over (single instruction)"}, - {'o', "Step out"}, - {'s', "Step in (source line)"}, - {'S', "Step in (single instruction)"}, - {',', "Page up"}, - {'.', "Page down"}, - {'\0', nullptr}}; - return g_source_view_key_help; - } - - bool WindowDelegateDraw(Window &window, bool force) override { - ExecutionContext exe_ctx = - m_debugger.GetCommandInterpreter().GetExecutionContext(); - Process *process = exe_ctx.GetProcessPtr(); - Thread *thread = nullptr; - - bool update_location = false; - if (process) { - StateType state = process->GetState(); - if (StateIsStoppedState(state, true)) { - // We are stopped, so it is ok to - update_location = true; - } - } - - m_min_x = 1; - m_min_y = 2; - m_max_x = window.GetMaxX() - 1; - m_max_y = window.GetMaxY() - 1; - - const uint32_t num_visible_lines = NumVisibleLines(); - StackFrameSP frame_sp; - bool set_selected_line_to_pc = false; - - if (update_location) { - const bool process_alive = process ? process->IsAlive() : false; - bool thread_changed = false; - if (process_alive) { - thread = exe_ctx.GetThreadPtr(); - if (thread) { - frame_sp = thread->GetSelectedFrame(); - auto tid = thread->GetID(); - thread_changed = tid != m_tid; - m_tid = tid; - } else { - if (m_tid != LLDB_INVALID_THREAD_ID) { - thread_changed = true; - m_tid = LLDB_INVALID_THREAD_ID; - } - } - } - const uint32_t stop_id = process ? process->GetStopID() : 0; - const bool stop_id_changed = stop_id != m_stop_id; - bool frame_changed = false; - m_stop_id = stop_id; - m_title.Clear(); - if (frame_sp) { - m_sc = frame_sp->GetSymbolContext(eSymbolContextEverything); - if (m_sc.module_sp) { - m_title.Printf( - "%s", m_sc.module_sp->GetFileSpec().GetFilename().GetCString()); - ConstString func_name = m_sc.GetFunctionName(); - if (func_name) - m_title.Printf("`%s", func_name.GetCString()); - } - const uint32_t frame_idx = frame_sp->GetFrameIndex(); - frame_changed = frame_idx != m_frame_idx; - m_frame_idx = frame_idx; - } else { - m_sc.Clear(true); - frame_changed = m_frame_idx != UINT32_MAX; - m_frame_idx = UINT32_MAX; - } - - const bool context_changed = - thread_changed || frame_changed || stop_id_changed; - - if (process_alive) { - if (m_sc.line_entry.IsValid()) { - m_pc_line = m_sc.line_entry.line; - if (m_pc_line != UINT32_MAX) - --m_pc_line; // Convert to zero based line number... - // Update the selected line if the stop ID changed... - if (context_changed) - m_selected_line = m_pc_line; - - if (m_file_sp && m_file_sp->FileSpecMatches(m_sc.line_entry.file)) { - // Same file, nothing to do, we should either have the lines or not - // (source file missing) - if (m_selected_line >= static_cast<size_t>(m_first_visible_line)) { - if (m_selected_line >= m_first_visible_line + num_visible_lines) - m_first_visible_line = m_selected_line - 10; - } else { - if (m_selected_line > 10) - m_first_visible_line = m_selected_line - 10; - else - m_first_visible_line = 0; - } - } else { - // File changed, set selected line to the line with the PC - m_selected_line = m_pc_line; - m_file_sp = - m_debugger.GetSourceManager().GetFile(m_sc.line_entry.file); - if (m_file_sp) { - const size_t num_lines = m_file_sp->GetNumLines(); - m_line_width = 1; - for (size_t n = num_lines; n >= 10; n = n / 10) - ++m_line_width; - - if (num_lines < num_visible_lines || - m_selected_line < num_visible_lines) - m_first_visible_line = 0; - else - m_first_visible_line = m_selected_line - 10; - } - } - } else { - m_file_sp.reset(); - } - - if (!m_file_sp || m_file_sp->GetNumLines() == 0) { - // Show disassembly - bool prefer_file_cache = false; - if (m_sc.function) { - if (m_disassembly_scope != m_sc.function) { - m_disassembly_scope = m_sc.function; - m_disassembly_sp = m_sc.function->GetInstructions( - exe_ctx, nullptr, prefer_file_cache); - if (m_disassembly_sp) { - set_selected_line_to_pc = true; - m_disassembly_range = m_sc.function->GetAddressRange(); - } else { - m_disassembly_range.Clear(); - } - } else { - set_selected_line_to_pc = context_changed; - } - } else if (m_sc.symbol) { - if (m_disassembly_scope != m_sc.symbol) { - m_disassembly_scope = m_sc.symbol; - m_disassembly_sp = m_sc.symbol->GetInstructions( - exe_ctx, nullptr, prefer_file_cache); - if (m_disassembly_sp) { - set_selected_line_to_pc = true; - m_disassembly_range.GetBaseAddress() = - m_sc.symbol->GetAddress(); - m_disassembly_range.SetByteSize(m_sc.symbol->GetByteSize()); - } else { - m_disassembly_range.Clear(); - } - } else { - set_selected_line_to_pc = context_changed; - } - } - } - } else { - m_pc_line = UINT32_MAX; - } - } - - const int window_width = window.GetWidth(); - window.Erase(); - window.DrawTitleBox("Sources"); - if (!m_title.GetString().empty()) { - window.AttributeOn(A_REVERSE); - window.MoveCursor(1, 1); - window.PutChar(' '); - window.PutCStringTruncated(m_title.GetString().str().c_str(), 1); - int x = window.GetCursorX(); - if (x < window_width - 1) { - window.Printf("%*s", window_width - x - 1, ""); - } - window.AttributeOff(A_REVERSE); - } - - Target *target = exe_ctx.GetTargetPtr(); - const size_t num_source_lines = GetNumSourceLines(); - if (num_source_lines > 0) { - // Display source - BreakpointLines bp_lines; - if (target) { - BreakpointList &bp_list = target->GetBreakpointList(); - const size_t num_bps = bp_list.GetSize(); - for (size_t bp_idx = 0; bp_idx < num_bps; ++bp_idx) { - BreakpointSP bp_sp = bp_list.GetBreakpointAtIndex(bp_idx); - const size_t num_bps_locs = bp_sp->GetNumLocations(); - for (size_t bp_loc_idx = 0; bp_loc_idx < num_bps_locs; ++bp_loc_idx) { - BreakpointLocationSP bp_loc_sp = - bp_sp->GetLocationAtIndex(bp_loc_idx); - LineEntry bp_loc_line_entry; - if (bp_loc_sp->GetAddress().CalculateSymbolContextLineEntry( - bp_loc_line_entry)) { - if (m_file_sp->GetFileSpec() == bp_loc_line_entry.file) { - bp_lines.insert(bp_loc_line_entry.line); - } - } - } - } - } - - const attr_t selected_highlight_attr = A_REVERSE; - const attr_t pc_highlight_attr = COLOR_PAIR(1); - - for (size_t i = 0; i < num_visible_lines; ++i) { - const uint32_t curr_line = m_first_visible_line + i; - if (curr_line < num_source_lines) { - const int line_y = m_min_y + i; - window.MoveCursor(1, line_y); - const bool is_pc_line = curr_line == m_pc_line; - const bool line_is_selected = m_selected_line == curr_line; - // Highlight the line as the PC line first, then if the selected line - // isn't the same as the PC line, highlight it differently - attr_t highlight_attr = 0; - attr_t bp_attr = 0; - if (is_pc_line) - highlight_attr = pc_highlight_attr; - else if (line_is_selected) - highlight_attr = selected_highlight_attr; - - if (bp_lines.find(curr_line + 1) != bp_lines.end()) - bp_attr = COLOR_PAIR(2); - - if (bp_attr) - window.AttributeOn(bp_attr); - - window.Printf(" %*u ", m_line_width, curr_line + 1); - - if (bp_attr) - window.AttributeOff(bp_attr); - - window.PutChar(ACS_VLINE); - // Mark the line with the PC with a diamond - if (is_pc_line) - window.PutChar(ACS_DIAMOND); - else - window.PutChar(' '); - - if (highlight_attr) - window.AttributeOn(highlight_attr); - const uint32_t line_len = - m_file_sp->GetLineLength(curr_line + 1, false); - if (line_len > 0) - window.PutCString(m_file_sp->PeekLineData(curr_line + 1), line_len); - - if (is_pc_line && frame_sp && - frame_sp->GetConcreteFrameIndex() == 0) { - StopInfoSP stop_info_sp; - if (thread) - stop_info_sp = thread->GetStopInfo(); - if (stop_info_sp) { - const char *stop_description = stop_info_sp->GetDescription(); - if (stop_description && stop_description[0]) { - size_t stop_description_len = strlen(stop_description); - int desc_x = window_width - stop_description_len - 16; - window.Printf("%*s", desc_x - window.GetCursorX(), ""); - // window.MoveCursor(window_width - stop_description_len - 15, - // line_y); - window.Printf("<<< Thread %u: %s ", thread->GetIndexID(), - stop_description); - } - } else { - window.Printf("%*s", window_width - window.GetCursorX() - 1, ""); - } - } - if (highlight_attr) - window.AttributeOff(highlight_attr); - } else { - break; - } - } - } else { - size_t num_disassembly_lines = GetNumDisassemblyLines(); - if (num_disassembly_lines > 0) { - // Display disassembly - BreakpointAddrs bp_file_addrs; - Target *target = exe_ctx.GetTargetPtr(); - if (target) { - BreakpointList &bp_list = target->GetBreakpointList(); - const size_t num_bps = bp_list.GetSize(); - for (size_t bp_idx = 0; bp_idx < num_bps; ++bp_idx) { - BreakpointSP bp_sp = bp_list.GetBreakpointAtIndex(bp_idx); - const size_t num_bps_locs = bp_sp->GetNumLocations(); - for (size_t bp_loc_idx = 0; bp_loc_idx < num_bps_locs; - ++bp_loc_idx) { - BreakpointLocationSP bp_loc_sp = - bp_sp->GetLocationAtIndex(bp_loc_idx); - LineEntry bp_loc_line_entry; - const lldb::addr_t file_addr = - bp_loc_sp->GetAddress().GetFileAddress(); - if (file_addr != LLDB_INVALID_ADDRESS) { - if (m_disassembly_range.ContainsFileAddress(file_addr)) - bp_file_addrs.insert(file_addr); - } - } - } - } - - const attr_t selected_highlight_attr = A_REVERSE; - const attr_t pc_highlight_attr = COLOR_PAIR(1); - - StreamString strm; - - InstructionList &insts = m_disassembly_sp->GetInstructionList(); - Address pc_address; - - if (frame_sp) - pc_address = frame_sp->GetFrameCodeAddress(); - const uint32_t pc_idx = - pc_address.IsValid() - ? insts.GetIndexOfInstructionAtAddress(pc_address) - : UINT32_MAX; - if (set_selected_line_to_pc) { - m_selected_line = pc_idx; - } - - const uint32_t non_visible_pc_offset = (num_visible_lines / 5); - if (static_cast<size_t>(m_first_visible_line) >= num_disassembly_lines) - m_first_visible_line = 0; - - if (pc_idx < num_disassembly_lines) { - if (pc_idx < static_cast<uint32_t>(m_first_visible_line) || - pc_idx >= m_first_visible_line + num_visible_lines) - m_first_visible_line = pc_idx - non_visible_pc_offset; - } - - for (size_t i = 0; i < num_visible_lines; ++i) { - const uint32_t inst_idx = m_first_visible_line + i; - Instruction *inst = insts.GetInstructionAtIndex(inst_idx).get(); - if (!inst) - break; - - const int line_y = m_min_y + i; - window.MoveCursor(1, line_y); - const bool is_pc_line = frame_sp && inst_idx == pc_idx; - const bool line_is_selected = m_selected_line == inst_idx; - // Highlight the line as the PC line first, then if the selected line - // isn't the same as the PC line, highlight it differently - attr_t highlight_attr = 0; - attr_t bp_attr = 0; - if (is_pc_line) - highlight_attr = pc_highlight_attr; - else if (line_is_selected) - highlight_attr = selected_highlight_attr; - - if (bp_file_addrs.find(inst->GetAddress().GetFileAddress()) != - bp_file_addrs.end()) - bp_attr = COLOR_PAIR(2); - - if (bp_attr) - window.AttributeOn(bp_attr); - - window.Printf(" 0x%16.16llx ", - static_cast<unsigned long long>( - inst->GetAddress().GetLoadAddress(target))); - - if (bp_attr) - window.AttributeOff(bp_attr); - - window.PutChar(ACS_VLINE); - // Mark the line with the PC with a diamond - if (is_pc_line) - window.PutChar(ACS_DIAMOND); - else - window.PutChar(' '); - - if (highlight_attr) - window.AttributeOn(highlight_attr); - - const char *mnemonic = inst->GetMnemonic(&exe_ctx); - const char *operands = inst->GetOperands(&exe_ctx); - const char *comment = inst->GetComment(&exe_ctx); - - if (mnemonic != nullptr && mnemonic[0] == '\0') - mnemonic = nullptr; - if (operands != nullptr && operands[0] == '\0') - operands = nullptr; - if (comment != nullptr && comment[0] == '\0') - comment = nullptr; - - strm.Clear(); - - if (mnemonic != nullptr && operands != nullptr && comment != nullptr) - strm.Printf("%-8s %-25s ; %s", mnemonic, operands, comment); - else if (mnemonic != nullptr && operands != nullptr) - strm.Printf("%-8s %s", mnemonic, operands); - else if (mnemonic != nullptr) - strm.Printf("%s", mnemonic); - - int right_pad = 1; - window.PutCStringTruncated(strm.GetData(), right_pad); - - if (is_pc_line && frame_sp && - frame_sp->GetConcreteFrameIndex() == 0) { - StopInfoSP stop_info_sp; - if (thread) - stop_info_sp = thread->GetStopInfo(); - if (stop_info_sp) { - const char *stop_description = stop_info_sp->GetDescription(); - if (stop_description && stop_description[0]) { - size_t stop_description_len = strlen(stop_description); - int desc_x = window_width - stop_description_len - 16; - window.Printf("%*s", desc_x - window.GetCursorX(), ""); - // window.MoveCursor(window_width - stop_description_len - 15, - // line_y); - window.Printf("<<< Thread %u: %s ", thread->GetIndexID(), - stop_description); - } - } else { - window.Printf("%*s", window_width - window.GetCursorX() - 1, ""); - } - } - if (highlight_attr) - window.AttributeOff(highlight_attr); - } - } - } - return true; // Drawing handled - } - - size_t GetNumLines() { - size_t num_lines = GetNumSourceLines(); - if (num_lines == 0) - num_lines = GetNumDisassemblyLines(); - return num_lines; - } - - size_t GetNumSourceLines() const { - if (m_file_sp) - return m_file_sp->GetNumLines(); - return 0; - } - - size_t GetNumDisassemblyLines() const { - if (m_disassembly_sp) - return m_disassembly_sp->GetInstructionList().GetSize(); - return 0; - } - - HandleCharResult WindowDelegateHandleChar(Window &window, int c) override { - const uint32_t num_visible_lines = NumVisibleLines(); - const size_t num_lines = GetNumLines(); - - switch (c) { - case ',': - case KEY_PPAGE: - // Page up key - if (static_cast<uint32_t>(m_first_visible_line) > num_visible_lines) - m_first_visible_line -= num_visible_lines; - else - m_first_visible_line = 0; - m_selected_line = m_first_visible_line; - return eKeyHandled; - - case '.': - case KEY_NPAGE: - // Page down key - { - if (m_first_visible_line + num_visible_lines < num_lines) - m_first_visible_line += num_visible_lines; - else if (num_lines < num_visible_lines) - m_first_visible_line = 0; - else - m_first_visible_line = num_lines - num_visible_lines; - m_selected_line = m_first_visible_line; - } - return eKeyHandled; - - case KEY_UP: - if (m_selected_line > 0) { - m_selected_line--; - if (static_cast<size_t>(m_first_visible_line) > m_selected_line) - m_first_visible_line = m_selected_line; - } - return eKeyHandled; - - case KEY_DOWN: - if (m_selected_line + 1 < num_lines) { - m_selected_line++; - if (m_first_visible_line + num_visible_lines < m_selected_line) - m_first_visible_line++; - } - return eKeyHandled; - - case '\r': - case '\n': - case KEY_ENTER: - // Set a breakpoint and run to the line using a one shot breakpoint - if (GetNumSourceLines() > 0) { - ExecutionContext exe_ctx = - m_debugger.GetCommandInterpreter().GetExecutionContext(); - if (exe_ctx.HasProcessScope() && exe_ctx.GetProcessRef().IsAlive()) { - BreakpointSP bp_sp = exe_ctx.GetTargetRef().CreateBreakpoint( - nullptr, // Don't limit the breakpoint to certain modules - m_file_sp->GetFileSpec(), // Source file - m_selected_line + - 1, // Source line number (m_selected_line is zero based) - 0, // Unspecified column. - 0, // No offset - eLazyBoolCalculate, // Check inlines using global setting - eLazyBoolCalculate, // Skip prologue using global setting, - false, // internal - false, // request_hardware - eLazyBoolCalculate); // move_to_nearest_code - // Make breakpoint one shot - bp_sp->GetOptions()->SetOneShot(true); - exe_ctx.GetProcessRef().Resume(); - } - } else if (m_selected_line < GetNumDisassemblyLines()) { - const Instruction *inst = m_disassembly_sp->GetInstructionList() - .GetInstructionAtIndex(m_selected_line) - .get(); - ExecutionContext exe_ctx = - m_debugger.GetCommandInterpreter().GetExecutionContext(); - if (exe_ctx.HasTargetScope()) { - Address addr = inst->GetAddress(); - BreakpointSP bp_sp = exe_ctx.GetTargetRef().CreateBreakpoint( - addr, // lldb_private::Address - false, // internal - false); // request_hardware - // Make breakpoint one shot - bp_sp->GetOptions()->SetOneShot(true); - exe_ctx.GetProcessRef().Resume(); - } - } - return eKeyHandled; - - case 'b': // 'b' == toggle breakpoint on currently selected line - if (m_selected_line < GetNumSourceLines()) { - ExecutionContext exe_ctx = - m_debugger.GetCommandInterpreter().GetExecutionContext(); - if (exe_ctx.HasTargetScope()) { - BreakpointSP bp_sp = exe_ctx.GetTargetRef().CreateBreakpoint( - nullptr, // Don't limit the breakpoint to certain modules - m_file_sp->GetFileSpec(), // Source file - m_selected_line + - 1, // Source line number (m_selected_line is zero based) - 0, // No column specified. - 0, // No offset - eLazyBoolCalculate, // Check inlines using global setting - eLazyBoolCalculate, // Skip prologue using global setting, - false, // internal - false, // request_hardware - eLazyBoolCalculate); // move_to_nearest_code - } - } else if (m_selected_line < GetNumDisassemblyLines()) { - const Instruction *inst = m_disassembly_sp->GetInstructionList() - .GetInstructionAtIndex(m_selected_line) - .get(); - ExecutionContext exe_ctx = - m_debugger.GetCommandInterpreter().GetExecutionContext(); - if (exe_ctx.HasTargetScope()) { - Address addr = inst->GetAddress(); - BreakpointSP bp_sp = exe_ctx.GetTargetRef().CreateBreakpoint( - addr, // lldb_private::Address - false, // internal - false); // request_hardware - } - } - return eKeyHandled; - - case 'd': // 'd' == detach and let run - case 'D': // 'D' == detach and keep stopped - { - ExecutionContext exe_ctx = - m_debugger.GetCommandInterpreter().GetExecutionContext(); - if (exe_ctx.HasProcessScope()) - exe_ctx.GetProcessRef().Detach(c == 'D'); - } - return eKeyHandled; - - case 'k': - // 'k' == kill - { - ExecutionContext exe_ctx = - m_debugger.GetCommandInterpreter().GetExecutionContext(); - if (exe_ctx.HasProcessScope()) - exe_ctx.GetProcessRef().Destroy(false); - } - return eKeyHandled; - - case 'c': - // 'c' == continue - { - ExecutionContext exe_ctx = - m_debugger.GetCommandInterpreter().GetExecutionContext(); - if (exe_ctx.HasProcessScope()) - exe_ctx.GetProcessRef().Resume(); - } - return eKeyHandled; - - case 'o': - // 'o' == step out - { - ExecutionContext exe_ctx = - m_debugger.GetCommandInterpreter().GetExecutionContext(); - if (exe_ctx.HasThreadScope() && - StateIsStoppedState(exe_ctx.GetProcessRef().GetState(), true)) { - exe_ctx.GetThreadRef().StepOut(); - } - } - return eKeyHandled; - - case 'n': // 'n' == step over - case 'N': // 'N' == step over instruction - { - ExecutionContext exe_ctx = - m_debugger.GetCommandInterpreter().GetExecutionContext(); - if (exe_ctx.HasThreadScope() && - StateIsStoppedState(exe_ctx.GetProcessRef().GetState(), true)) { - bool source_step = (c == 'n'); - exe_ctx.GetThreadRef().StepOver(source_step); - } - } - return eKeyHandled; - - case 's': // 's' == step into - case 'S': // 'S' == step into instruction - { - ExecutionContext exe_ctx = - m_debugger.GetCommandInterpreter().GetExecutionContext(); - if (exe_ctx.HasThreadScope() && - StateIsStoppedState(exe_ctx.GetProcessRef().GetState(), true)) { - bool source_step = (c == 's'); - exe_ctx.GetThreadRef().StepIn(source_step); - } - } - return eKeyHandled; - - case 'h': - window.CreateHelpSubwindow(); - return eKeyHandled; - - default: - break; - } - return eKeyNotHandled; - } - -protected: - typedef std::set<uint32_t> BreakpointLines; - typedef std::set<lldb::addr_t> BreakpointAddrs; - - Debugger &m_debugger; - SymbolContext m_sc; - SourceManager::FileSP m_file_sp; - SymbolContextScope *m_disassembly_scope; - lldb::DisassemblerSP m_disassembly_sp; - AddressRange m_disassembly_range; - StreamString m_title; - lldb::user_id_t m_tid; - int m_line_width; - uint32_t m_selected_line; // The selected line - uint32_t m_pc_line; // The line with the PC - uint32_t m_stop_id; - uint32_t m_frame_idx; - int m_first_visible_line; - int m_min_x; - int m_min_y; - int m_max_x; - int m_max_y; -}; - -DisplayOptions ValueObjectListDelegate::g_options = {true}; - -IOHandlerCursesGUI::IOHandlerCursesGUI(Debugger &debugger) - : IOHandler(debugger, IOHandler::Type::Curses) {} - -void IOHandlerCursesGUI::Activate() { - IOHandler::Activate(); - if (!m_app_ap) { - m_app_ap.reset(new Application(GetInputFILE(), GetOutputFILE())); - - // This is both a window and a menu delegate - std::shared_ptr<ApplicationDelegate> app_delegate_sp( - new ApplicationDelegate(*m_app_ap, m_debugger)); - - MenuDelegateSP app_menu_delegate_sp = - std::static_pointer_cast<MenuDelegate>(app_delegate_sp); - MenuSP lldb_menu_sp( - new Menu("LLDB", "F1", KEY_F(1), ApplicationDelegate::eMenuID_LLDB)); - MenuSP exit_menuitem_sp( - new Menu("Exit", nullptr, 'x', ApplicationDelegate::eMenuID_LLDBExit)); - exit_menuitem_sp->SetCannedResult(MenuActionResult::Quit); - lldb_menu_sp->AddSubmenu(MenuSP(new Menu( - "About LLDB", nullptr, 'a', ApplicationDelegate::eMenuID_LLDBAbout))); - lldb_menu_sp->AddSubmenu(MenuSP(new Menu(Menu::Type::Separator))); - lldb_menu_sp->AddSubmenu(exit_menuitem_sp); - - MenuSP target_menu_sp(new Menu("Target", "F2", KEY_F(2), - ApplicationDelegate::eMenuID_Target)); - target_menu_sp->AddSubmenu(MenuSP(new Menu( - "Create", nullptr, 'c', ApplicationDelegate::eMenuID_TargetCreate))); - target_menu_sp->AddSubmenu(MenuSP(new Menu( - "Delete", nullptr, 'd', ApplicationDelegate::eMenuID_TargetDelete))); - - MenuSP process_menu_sp(new Menu("Process", "F3", KEY_F(3), - ApplicationDelegate::eMenuID_Process)); - process_menu_sp->AddSubmenu(MenuSP(new Menu( - "Attach", nullptr, 'a', ApplicationDelegate::eMenuID_ProcessAttach))); - process_menu_sp->AddSubmenu(MenuSP(new Menu( - "Detach", nullptr, 'd', ApplicationDelegate::eMenuID_ProcessDetach))); - process_menu_sp->AddSubmenu(MenuSP(new Menu( - "Launch", nullptr, 'l', ApplicationDelegate::eMenuID_ProcessLaunch))); - process_menu_sp->AddSubmenu(MenuSP(new Menu(Menu::Type::Separator))); - process_menu_sp->AddSubmenu( - MenuSP(new Menu("Continue", nullptr, 'c', - ApplicationDelegate::eMenuID_ProcessContinue))); - process_menu_sp->AddSubmenu(MenuSP(new Menu( - "Halt", nullptr, 'h', ApplicationDelegate::eMenuID_ProcessHalt))); - process_menu_sp->AddSubmenu(MenuSP(new Menu( - "Kill", nullptr, 'k', ApplicationDelegate::eMenuID_ProcessKill))); - - MenuSP thread_menu_sp(new Menu("Thread", "F4", KEY_F(4), - ApplicationDelegate::eMenuID_Thread)); - thread_menu_sp->AddSubmenu(MenuSP(new Menu( - "Step In", nullptr, 'i', ApplicationDelegate::eMenuID_ThreadStepIn))); - thread_menu_sp->AddSubmenu( - MenuSP(new Menu("Step Over", nullptr, 'v', - ApplicationDelegate::eMenuID_ThreadStepOver))); - thread_menu_sp->AddSubmenu(MenuSP(new Menu( - "Step Out", nullptr, 'o', ApplicationDelegate::eMenuID_ThreadStepOut))); - - MenuSP view_menu_sp( - new Menu("View", "F5", KEY_F(5), ApplicationDelegate::eMenuID_View)); - view_menu_sp->AddSubmenu( - MenuSP(new Menu("Backtrace", nullptr, 'b', - ApplicationDelegate::eMenuID_ViewBacktrace))); - view_menu_sp->AddSubmenu( - MenuSP(new Menu("Registers", nullptr, 'r', - ApplicationDelegate::eMenuID_ViewRegisters))); - view_menu_sp->AddSubmenu(MenuSP(new Menu( - "Source", nullptr, 's', ApplicationDelegate::eMenuID_ViewSource))); - view_menu_sp->AddSubmenu( - MenuSP(new Menu("Variables", nullptr, 'v', - ApplicationDelegate::eMenuID_ViewVariables))); - - MenuSP help_menu_sp( - new Menu("Help", "F6", KEY_F(6), ApplicationDelegate::eMenuID_Help)); - help_menu_sp->AddSubmenu(MenuSP(new Menu( - "GUI Help", nullptr, 'g', ApplicationDelegate::eMenuID_HelpGUIHelp))); - - m_app_ap->Initialize(); - WindowSP &main_window_sp = m_app_ap->GetMainWindow(); - - MenuSP menubar_sp(new Menu(Menu::Type::Bar)); - menubar_sp->AddSubmenu(lldb_menu_sp); - menubar_sp->AddSubmenu(target_menu_sp); - menubar_sp->AddSubmenu(process_menu_sp); - menubar_sp->AddSubmenu(thread_menu_sp); - menubar_sp->AddSubmenu(view_menu_sp); - menubar_sp->AddSubmenu(help_menu_sp); - menubar_sp->SetDelegate(app_menu_delegate_sp); - - Rect content_bounds = main_window_sp->GetFrame(); - Rect menubar_bounds = content_bounds.MakeMenuBar(); - Rect status_bounds = content_bounds.MakeStatusBar(); - Rect source_bounds; - Rect variables_bounds; - Rect threads_bounds; - Rect source_variables_bounds; - content_bounds.VerticalSplitPercentage(0.80, source_variables_bounds, - threads_bounds); - source_variables_bounds.HorizontalSplitPercentage(0.70, source_bounds, - variables_bounds); - - WindowSP menubar_window_sp = - main_window_sp->CreateSubWindow("Menubar", menubar_bounds, false); - // Let the menubar get keys if the active window doesn't handle the keys - // that are typed so it can respond to menubar key presses. - menubar_window_sp->SetCanBeActive( - false); // Don't let the menubar become the active window - menubar_window_sp->SetDelegate(menubar_sp); - - WindowSP source_window_sp( - main_window_sp->CreateSubWindow("Source", source_bounds, true)); - WindowSP variables_window_sp( - main_window_sp->CreateSubWindow("Variables", variables_bounds, false)); - WindowSP threads_window_sp( - main_window_sp->CreateSubWindow("Threads", threads_bounds, false)); - WindowSP status_window_sp( - main_window_sp->CreateSubWindow("Status", status_bounds, false)); - status_window_sp->SetCanBeActive( - false); // Don't let the status bar become the active window - main_window_sp->SetDelegate( - std::static_pointer_cast<WindowDelegate>(app_delegate_sp)); - source_window_sp->SetDelegate( - WindowDelegateSP(new SourceFileWindowDelegate(m_debugger))); - variables_window_sp->SetDelegate( - WindowDelegateSP(new FrameVariablesWindowDelegate(m_debugger))); - TreeDelegateSP thread_delegate_sp(new ThreadsTreeDelegate(m_debugger)); - threads_window_sp->SetDelegate(WindowDelegateSP( - new TreeWindowDelegate(m_debugger, thread_delegate_sp))); - status_window_sp->SetDelegate( - WindowDelegateSP(new StatusBarWindowDelegate(m_debugger))); - - // Show the main help window once the first time the curses GUI is launched - static bool g_showed_help = false; - if (!g_showed_help) { - g_showed_help = true; - main_window_sp->CreateHelpSubwindow(); - } - - init_pair(1, COLOR_WHITE, COLOR_BLUE); - init_pair(2, COLOR_BLACK, COLOR_WHITE); - init_pair(3, COLOR_MAGENTA, COLOR_WHITE); - init_pair(4, COLOR_MAGENTA, COLOR_BLACK); - init_pair(5, COLOR_RED, COLOR_BLACK); - } -} - -void IOHandlerCursesGUI::Deactivate() { m_app_ap->Terminate(); } - -void IOHandlerCursesGUI::Run() { - m_app_ap->Run(m_debugger); - SetIsDone(true); -} - -IOHandlerCursesGUI::~IOHandlerCursesGUI() = default; - -void IOHandlerCursesGUI::Cancel() {} - -bool IOHandlerCursesGUI::Interrupt() { return false; } - -void IOHandlerCursesGUI::GotEOF() {} - -#endif // LLDB_DISABLE_CURSES diff --git a/lldb/source/Core/IOHandlerCursesGUI.cpp b/lldb/source/Core/IOHandlerCursesGUI.cpp new file mode 100644 index 000000000000..1661b465efaf --- /dev/null +++ b/lldb/source/Core/IOHandlerCursesGUI.cpp @@ -0,0 +1,4067 @@ +//===-- IOHandlerCursesGUI.cpp ----------------------------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "lldb/Core/IOHandlerCursesGUI.h" +#include "lldb/Host/Config.h" + +#if LLDB_ENABLE_CURSES +#include <curses.h> +#include <panel.h> +#endif + +#if defined(__APPLE__) +#include <deque> +#endif +#include <string> + +#include "lldb/Core/Debugger.h" +#include "lldb/Core/StreamFile.h" +#include "lldb/Host/File.h" +#include "lldb/Utility/Predicate.h" +#include "lldb/Utility/Status.h" +#include "lldb/Utility/StreamString.h" +#include "lldb/Utility/StringList.h" +#include "lldb/lldb-forward.h" + +#include "lldb/Interpreter/CommandCompletions.h" +#include "lldb/Interpreter/CommandInterpreter.h" + +#if LLDB_ENABLE_CURSES +#include "lldb/Breakpoint/BreakpointLocation.h" +#include "lldb/Core/Module.h" +#include "lldb/Core/ValueObject.h" +#include "lldb/Core/ValueObjectRegister.h" +#include "lldb/Symbol/Block.h" +#include "lldb/Symbol/Function.h" +#include "lldb/Symbol/Symbol.h" +#include "lldb/Symbol/VariableList.h" +#include "lldb/Target/Process.h" +#include "lldb/Target/RegisterContext.h" +#include "lldb/Target/StackFrame.h" +#include "lldb/Target/StopInfo.h" +#include "lldb/Target/Target.h" +#include "lldb/Target/Thread.h" +#include "lldb/Utility/State.h" +#endif + +#include "llvm/ADT/StringRef.h" + +#ifdef _WIN32 +#include "lldb/Host/windows/windows.h" +#endif + +#include <memory> +#include <mutex> + +#include <assert.h> +#include <ctype.h> +#include <errno.h> +#include <locale.h> +#include <stdint.h> +#include <stdio.h> +#include <string.h> +#include <type_traits> + +using namespace lldb; +using namespace lldb_private; +using llvm::None; +using llvm::Optional; +using llvm::StringRef; + +// we may want curses to be disabled for some builds for instance, windows +#if LLDB_ENABLE_CURSES + +#define KEY_RETURN 10 +#define KEY_ESCAPE 27 + +namespace curses { +class Menu; +class MenuDelegate; +class Window; +class WindowDelegate; +typedef std::shared_ptr<Menu> MenuSP; +typedef std::shared_ptr<MenuDelegate> MenuDelegateSP; +typedef std::shared_ptr<Window> WindowSP; +typedef std::shared_ptr<WindowDelegate> WindowDelegateSP; +typedef std::vector<MenuSP> Menus; +typedef std::vector<WindowSP> Windows; +typedef std::vector<WindowDelegateSP> WindowDelegates; + +#if 0 +type summary add -s "x=${var.x}, y=${var.y}" curses::Point +type summary add -s "w=${var.width}, h=${var.height}" curses::Size +type summary add -s "${var.origin%S} ${var.size%S}" curses::Rect +#endif + +struct Point { + int x; + int y; + + Point(int _x = 0, int _y = 0) : x(_x), y(_y) {} + + void Clear() { + x = 0; + y = 0; + } + + Point &operator+=(const Point &rhs) { + x += rhs.x; + y += rhs.y; + return *this; + } + + void Dump() { printf("(x=%i, y=%i)\n", x, y); } +}; + +bool operator==(const Point &lhs, const Point &rhs) { + return lhs.x == rhs.x && lhs.y == rhs.y; +} + +bool operator!=(const Point &lhs, const Point &rhs) { + return lhs.x != rhs.x || lhs.y != rhs.y; +} + +struct Size { + int width; + int height; + Size(int w = 0, int h = 0) : width(w), height(h) {} + + void Clear() { + width = 0; + height = 0; + } + + void Dump() { printf("(w=%i, h=%i)\n", width, height); } +}; + +bool operator==(const Size &lhs, const Size &rhs) { + return lhs.width == rhs.width && lhs.height == rhs.height; +} + +bool operator!=(const Size &lhs, const Size &rhs) { + return lhs.width != rhs.width || lhs.height != rhs.height; +} + +struct Rect { + Point origin; + Size size; + + Rect() : origin(), size() {} + + Rect(const Point &p, const Size &s) : origin(p), size(s) {} + + void Clear() { + origin.Clear(); + size.Clear(); + } + + void Dump() { + printf("(x=%i, y=%i), w=%i, h=%i)\n", origin.x, origin.y, size.width, + size.height); + } + + void Inset(int w, int h) { + if (size.width > w * 2) + size.width -= w * 2; + origin.x += w; + + if (size.height > h * 2) + size.height -= h * 2; + origin.y += h; + } + + // Return a status bar rectangle which is the last line of this rectangle. + // This rectangle will be modified to not include the status bar area. + Rect MakeStatusBar() { + Rect status_bar; + if (size.height > 1) { + status_bar.origin.x = origin.x; + status_bar.origin.y = size.height; + status_bar.size.width = size.width; + status_bar.size.height = 1; + --size.height; + } + return status_bar; + } + + // Return a menubar rectangle which is the first line of this rectangle. This + // rectangle will be modified to not include the menubar area. + Rect MakeMenuBar() { + Rect menubar; + if (size.height > 1) { + menubar.origin.x = origin.x; + menubar.origin.y = origin.y; + menubar.size.width = size.width; + menubar.size.height = 1; + ++origin.y; + --size.height; + } + return menubar; + } + + void HorizontalSplitPercentage(float top_percentage, Rect &top, + Rect &bottom) const { + float top_height = top_percentage * size.height; + HorizontalSplit(top_height, top, bottom); + } + + void HorizontalSplit(int top_height, Rect &top, Rect &bottom) const { + top = *this; + if (top_height < size.height) { + top.size.height = top_height; + bottom.origin.x = origin.x; + bottom.origin.y = origin.y + top.size.height; + bottom.size.width = size.width; + bottom.size.height = size.height - top.size.height; + } else { + bottom.Clear(); + } + } + + void VerticalSplitPercentage(float left_percentage, Rect &left, + Rect &right) const { + float left_width = left_percentage * size.width; + VerticalSplit(left_width, left, right); + } + + void VerticalSplit(int left_width, Rect &left, Rect &right) const { + left = *this; + if (left_width < size.width) { + left.size.width = left_width; + right.origin.x = origin.x + left.size.width; + right.origin.y = origin.y; + right.size.width = size.width - left.size.width; + right.size.height = size.height; + } else { + right.Clear(); + } + } +}; + +bool operator==(const Rect &lhs, const Rect &rhs) { + return lhs.origin == rhs.origin && lhs.size == rhs.size; +} + +bool operator!=(const Rect &lhs, const Rect &rhs) { + return lhs.origin != rhs.origin || lhs.size != rhs.size; +} + +enum HandleCharResult { + eKeyNotHandled = 0, + eKeyHandled = 1, + eQuitApplication = 2 +}; + +enum class MenuActionResult { + Handled, + NotHandled, + Quit // Exit all menus and quit +}; + +struct KeyHelp { + int ch; + const char *description; +}; + +class WindowDelegate { +public: + virtual ~WindowDelegate() = default; + + virtual bool WindowDelegateDraw(Window &window, bool force) { + return false; // Drawing not handled + } + + virtual HandleCharResult WindowDelegateHandleChar(Window &window, int key) { + return eKeyNotHandled; + } + + virtual const char *WindowDelegateGetHelpText() { return nullptr; } + + virtual KeyHelp *WindowDelegateGetKeyHelp() { return nullptr; } +}; + +class HelpDialogDelegate : public WindowDelegate { +public: + HelpDialogDelegate(const char *text, KeyHelp *key_help_array); + + ~HelpDialogDelegate() override; + + bool WindowDelegateDraw(Window &window, bool force) override; + + HandleCharResult WindowDelegateHandleChar(Window &window, int key) override; + + size_t GetNumLines() const { return m_text.GetSize(); } + + size_t GetMaxLineLength() const { return m_text.GetMaxStringLength(); } + +protected: + StringList m_text; + int m_first_visible_line; +}; + +class Window { +public: + Window(const char *name) + : m_name(name), m_window(nullptr), m_panel(nullptr), m_parent(nullptr), + m_subwindows(), m_delegate_sp(), m_curr_active_window_idx(UINT32_MAX), + m_prev_active_window_idx(UINT32_MAX), m_delete(false), + m_needs_update(true), m_can_activate(true), m_is_subwin(false) {} + + Window(const char *name, WINDOW *w, bool del = true) + : m_name(name), m_window(nullptr), m_panel(nullptr), m_parent(nullptr), + m_subwindows(), m_delegate_sp(), m_curr_active_window_idx(UINT32_MAX), + m_prev_active_window_idx(UINT32_MAX), m_delete(del), + m_needs_update(true), m_can_activate(true), m_is_subwin(false) { + if (w) + Reset(w); + } + + Window(const char *name, const Rect &bounds) + : m_name(name), m_window(nullptr), m_parent(nullptr), m_subwindows(), + m_delegate_sp(), m_curr_active_window_idx(UINT32_MAX), + m_prev_active_window_idx(UINT32_MAX), m_delete(true), + m_needs_update(true), m_can_activate(true), m_is_subwin(false) { + Reset(::newwin(bounds.size.height, bounds.size.width, bounds.origin.y, + bounds.origin.y)); + } + + virtual ~Window() { + RemoveSubWindows(); + Reset(); + } + + void Reset(WINDOW *w = nullptr, bool del = true) { + if (m_window == w) + return; + + if (m_panel) { + ::del_panel(m_panel); + m_panel = nullptr; + } + if (m_window && m_delete) { + ::delwin(m_window); + m_window = nullptr; + m_delete = false; + } + if (w) { + m_window = w; + m_panel = ::new_panel(m_window); + m_delete = del; + } + } + + void AttributeOn(attr_t attr) { ::wattron(m_window, attr); } + void AttributeOff(attr_t attr) { ::wattroff(m_window, attr); } + void Box(chtype v_char = ACS_VLINE, chtype h_char = ACS_HLINE) { + ::box(m_window, v_char, h_char); + } + void Clear() { ::wclear(m_window); } + void Erase() { ::werase(m_window); } + Rect GetBounds() { + return Rect(GetParentOrigin(), GetSize()); + } // Get the rectangle in our parent window + int GetChar() { return ::wgetch(m_window); } + int GetCursorX() { return getcurx(m_window); } + int GetCursorY() { return getcury(m_window); } + Rect GetFrame() { + return Rect(Point(), GetSize()); + } // Get our rectangle in our own coordinate system + Point GetParentOrigin() { return Point(GetParentX(), GetParentY()); } + Size GetSize() { return Size(GetWidth(), GetHeight()); } + int GetParentX() { return getparx(m_window); } + int GetParentY() { return getpary(m_window); } + int GetMaxX() { return getmaxx(m_window); } + int GetMaxY() { return getmaxy(m_window); } + int GetWidth() { return GetMaxX(); } + int GetHeight() { return GetMaxY(); } + void MoveCursor(int x, int y) { ::wmove(m_window, y, x); } + void MoveWindow(int x, int y) { MoveWindow(Point(x, y)); } + void Resize(int w, int h) { ::wresize(m_window, h, w); } + void Resize(const Size &size) { + ::wresize(m_window, size.height, size.width); + } + void PutChar(int ch) { ::waddch(m_window, ch); } + void PutCString(const char *s, int len = -1) { ::waddnstr(m_window, s, len); } + void SetBackground(int color_pair_idx) { + ::wbkgd(m_window, COLOR_PAIR(color_pair_idx)); + } + + void PutCStringTruncated(const char *s, int right_pad) { + int bytes_left = GetWidth() - GetCursorX(); + if (bytes_left > right_pad) { + bytes_left -= right_pad; + ::waddnstr(m_window, s, bytes_left); + } + } + + void MoveWindow(const Point &origin) { + const bool moving_window = origin != GetParentOrigin(); + if (m_is_subwin && moving_window) { + // Can't move subwindows, must delete and re-create + Size size = GetSize(); + Reset(::subwin(m_parent->m_window, size.height, size.width, origin.y, + origin.x), + true); + } else { + ::mvwin(m_window, origin.y, origin.x); + } + } + + void SetBounds(const Rect &bounds) { + const bool moving_window = bounds.origin != GetParentOrigin(); + if (m_is_subwin && moving_window) { + // Can't move subwindows, must delete and re-create + Reset(::subwin(m_parent->m_window, bounds.size.height, bounds.size.width, + bounds.origin.y, bounds.origin.x), + true); + } else { + if (moving_window) + MoveWindow(bounds.origin); + Resize(bounds.size); + } + } + + void Printf(const char *format, ...) __attribute__((format(printf, 2, 3))) { + va_list args; + va_start(args, format); + vwprintw(m_window, format, args); + va_end(args); + } + + void Touch() { + ::touchwin(m_window); + if (m_parent) + m_parent->Touch(); + } + + WindowSP CreateSubWindow(const char *name, const Rect &bounds, + bool make_active) { + auto get_window = [this, &bounds]() { + return m_window + ? ::subwin(m_window, bounds.size.height, bounds.size.width, + bounds.origin.y, bounds.origin.x) + : ::newwin(bounds.size.height, bounds.size.width, + bounds.origin.y, bounds.origin.x); + }; + WindowSP subwindow_sp = std::make_shared<Window>(name, get_window(), true); + subwindow_sp->m_is_subwin = subwindow_sp.operator bool(); + subwindow_sp->m_parent = this; + if (make_active) { + m_prev_active_window_idx = m_curr_active_window_idx; + m_curr_active_window_idx = m_subwindows.size(); + } + m_subwindows.push_back(subwindow_sp); + ::top_panel(subwindow_sp->m_panel); + m_needs_update = true; + return subwindow_sp; + } + + bool RemoveSubWindow(Window *window) { + Windows::iterator pos, end = m_subwindows.end(); + size_t i = 0; + for (pos = m_subwindows.begin(); pos != end; ++pos, ++i) { + if ((*pos).get() == window) { + if (m_prev_active_window_idx == i) + m_prev_active_window_idx = UINT32_MAX; + else if (m_prev_active_window_idx != UINT32_MAX && + m_prev_active_window_idx > i) + --m_prev_active_window_idx; + + if (m_curr_active_window_idx == i) + m_curr_active_window_idx = UINT32_MAX; + else if (m_curr_active_window_idx != UINT32_MAX && + m_curr_active_window_idx > i) + --m_curr_active_window_idx; + window->Erase(); + m_subwindows.erase(pos); + m_needs_update = true; + if (m_parent) + m_parent->Touch(); + else + ::touchwin(stdscr); + return true; + } + } + return false; + } + + WindowSP FindSubWindow(const char *name) { + Windows::iterator pos, end = m_subwindows.end(); + size_t i = 0; + for (pos = m_subwindows.begin(); pos != end; ++pos, ++i) { + if ((*pos)->m_name == name) + return *pos; + } + return WindowSP(); + } + + void RemoveSubWindows() { + m_curr_active_window_idx = UINT32_MAX; + m_prev_active_window_idx = UINT32_MAX; + for (Windows::iterator pos = m_subwindows.begin(); + pos != m_subwindows.end(); pos = m_subwindows.erase(pos)) { + (*pos)->Erase(); + } + if (m_parent) + m_parent->Touch(); + else + ::touchwin(stdscr); + } + + WINDOW *get() { return m_window; } + + operator WINDOW *() { return m_window; } + + // Window drawing utilities + void DrawTitleBox(const char *title, const char *bottom_message = nullptr) { + attr_t attr = 0; + if (IsActive()) + attr = A_BOLD | COLOR_PAIR(2); + else + attr = 0; + if (attr) + AttributeOn(attr); + + Box(); + MoveCursor(3, 0); + + if (title && title[0]) { + PutChar('<'); + PutCString(title); + PutChar('>'); + } + + if (bottom_message && bottom_message[0]) { + int bottom_message_length = strlen(bottom_message); + int x = GetWidth() - 3 - (bottom_message_length + 2); + + if (x > 0) { + MoveCursor(x, GetHeight() - 1); + PutChar('['); + PutCString(bottom_message); + PutChar(']'); + } else { + MoveCursor(1, GetHeight() - 1); + PutChar('['); + PutCStringTruncated(bottom_message, 1); + } + } + if (attr) + AttributeOff(attr); + } + + virtual void Draw(bool force) { + if (m_delegate_sp && m_delegate_sp->WindowDelegateDraw(*this, force)) + return; + + for (auto &subwindow_sp : m_subwindows) + subwindow_sp->Draw(force); + } + + bool CreateHelpSubwindow() { + if (m_delegate_sp) { + const char *text = m_delegate_sp->WindowDelegateGetHelpText(); + KeyHelp *key_help = m_delegate_sp->WindowDelegateGetKeyHelp(); + if ((text && text[0]) || key_help) { + std::unique_ptr<HelpDialogDelegate> help_delegate_up( + new HelpDialogDelegate(text, key_help)); + const size_t num_lines = help_delegate_up->GetNumLines(); + const size_t max_length = help_delegate_up->GetMaxLineLength(); + Rect bounds = GetBounds(); + bounds.Inset(1, 1); + if (max_length + 4 < static_cast<size_t>(bounds.size.width)) { + bounds.origin.x += (bounds.size.width - max_length + 4) / 2; + bounds.size.width = max_length + 4; + } else { + if (bounds.size.width > 100) { + const int inset_w = bounds.size.width / 4; + bounds.origin.x += inset_w; + bounds.size.width -= 2 * inset_w; + } + } + + if (num_lines + 2 < static_cast<size_t>(bounds.size.height)) { + bounds.origin.y += (bounds.size.height - num_lines + 2) / 2; + bounds.size.height = num_lines + 2; + } else { + if (bounds.size.height > 100) { + const int inset_h = bounds.size.height / 4; + bounds.origin.y += inset_h; + bounds.size.height -= 2 * inset_h; + } + } + WindowSP help_window_sp; + Window *parent_window = GetParent(); + if (parent_window) + help_window_sp = parent_window->CreateSubWindow("Help", bounds, true); + else + help_window_sp = CreateSubWindow("Help", bounds, true); + help_window_sp->SetDelegate( + WindowDelegateSP(help_delegate_up.release())); + return true; + } + } + return false; + } + + virtual HandleCharResult HandleChar(int key) { + // Always check the active window first + HandleCharResult result = eKeyNotHandled; + WindowSP active_window_sp = GetActiveWindow(); + if (active_window_sp) { + result = active_window_sp->HandleChar(key); + if (result != eKeyNotHandled) + return result; + } + + if (m_delegate_sp) { + result = m_delegate_sp->WindowDelegateHandleChar(*this, key); + if (result != eKeyNotHandled) + return result; + } + + // Then check for any windows that want any keys that weren't handled. This + // is typically only for a menubar. Make a copy of the subwindows in case + // any HandleChar() functions muck with the subwindows. If we don't do + // this, we can crash when iterating over the subwindows. + Windows subwindows(m_subwindows); + for (auto subwindow_sp : subwindows) { + if (!subwindow_sp->m_can_activate) { + HandleCharResult result = subwindow_sp->HandleChar(key); + if (result != eKeyNotHandled) + return result; + } + } + + return eKeyNotHandled; + } + + WindowSP GetActiveWindow() { + if (!m_subwindows.empty()) { + if (m_curr_active_window_idx >= m_subwindows.size()) { + if (m_prev_active_window_idx < m_subwindows.size()) { + m_curr_active_window_idx = m_prev_active_window_idx; + m_prev_active_window_idx = UINT32_MAX; + } else if (IsActive()) { + m_prev_active_window_idx = UINT32_MAX; + m_curr_active_window_idx = UINT32_MAX; + + // Find first window that wants to be active if this window is active + const size_t num_subwindows = m_subwindows.size(); + for (size_t i = 0; i < num_subwindows; ++i) { + if (m_subwindows[i]->GetCanBeActive()) { + m_curr_active_window_idx = i; + break; + } + } + } + } + + if (m_curr_active_window_idx < m_subwindows.size()) + return m_subwindows[m_curr_active_window_idx]; + } + return WindowSP(); + } + + bool GetCanBeActive() const { return m_can_activate; } + + void SetCanBeActive(bool b) { m_can_activate = b; } + + void SetDelegate(const WindowDelegateSP &delegate_sp) { + m_delegate_sp = delegate_sp; + } + + Window *GetParent() const { return m_parent; } + + bool IsActive() const { + if (m_parent) + return m_parent->GetActiveWindow().get() == this; + else + return true; // Top level window is always active + } + + void SelectNextWindowAsActive() { + // Move active focus to next window + const size_t num_subwindows = m_subwindows.size(); + if (m_curr_active_window_idx == UINT32_MAX) { + uint32_t idx = 0; + for (auto subwindow_sp : m_subwindows) { + if (subwindow_sp->GetCanBeActive()) { + m_curr_active_window_idx = idx; + break; + } + ++idx; + } + } else if (m_curr_active_window_idx + 1 < num_subwindows) { + bool handled = false; + m_prev_active_window_idx = m_curr_active_window_idx; + for (size_t idx = m_curr_active_window_idx + 1; idx < num_subwindows; + ++idx) { + if (m_subwindows[idx]->GetCanBeActive()) { + m_curr_active_window_idx = idx; + handled = true; + break; + } + } + if (!handled) { + for (size_t idx = 0; idx <= m_prev_active_window_idx; ++idx) { + if (m_subwindows[idx]->GetCanBeActive()) { + m_curr_active_window_idx = idx; + break; + } + } + } + } else { + m_prev_active_window_idx = m_curr_active_window_idx; + for (size_t idx = 0; idx < num_subwindows; ++idx) { + if (m_subwindows[idx]->GetCanBeActive()) { + m_curr_active_window_idx = idx; + break; + } + } + } + } + + const char *GetName() const { return m_name.c_str(); } + +protected: + std::string m_name; + WINDOW *m_window; + PANEL *m_panel; + Window *m_parent; + Windows m_subwindows; + WindowDelegateSP m_delegate_sp; + uint32_t m_curr_active_window_idx; + uint32_t m_prev_active_window_idx; + bool m_delete; + bool m_needs_update; + bool m_can_activate; + bool m_is_subwin; + +private: + DISALLOW_COPY_AND_ASSIGN(Window); +}; + +class MenuDelegate { +public: + virtual ~MenuDelegate() = default; + + virtual MenuActionResult MenuDelegateAction(Menu &menu) = 0; +}; + +class Menu : public WindowDelegate { +public: + enum class Type { Invalid, Bar, Item, Separator }; + + // Menubar or separator constructor + Menu(Type type); + + // Menuitem constructor + Menu(const char *name, const char *key_name, int key_value, + uint64_t identifier); + + ~Menu() override = default; + + const MenuDelegateSP &GetDelegate() const { return m_delegate_sp; } + + void SetDelegate(const MenuDelegateSP &delegate_sp) { + m_delegate_sp = delegate_sp; + } + + void RecalculateNameLengths(); + + void AddSubmenu(const MenuSP &menu_sp); + + int DrawAndRunMenu(Window &window); + + void DrawMenuTitle(Window &window, bool highlight); + + bool WindowDelegateDraw(Window &window, bool force) override; + + HandleCharResult WindowDelegateHandleChar(Window &window, int key) override; + + MenuActionResult ActionPrivate(Menu &menu) { + MenuActionResult result = MenuActionResult::NotHandled; + if (m_delegate_sp) { + result = m_delegate_sp->MenuDelegateAction(menu); + if (result != MenuActionResult::NotHandled) + return result; + } else if (m_parent) { + result = m_parent->ActionPrivate(menu); + if (result != MenuActionResult::NotHandled) + return result; + } + return m_canned_result; + } + + MenuActionResult Action() { + // Call the recursive action so it can try to handle it with the menu + // delegate, and if not, try our parent menu + return ActionPrivate(*this); + } + + void SetCannedResult(MenuActionResult result) { m_canned_result = result; } + + Menus &GetSubmenus() { return m_submenus; } + + const Menus &GetSubmenus() const { return m_submenus; } + + int GetSelectedSubmenuIndex() const { return m_selected; } + + void SetSelectedSubmenuIndex(int idx) { m_selected = idx; } + + Type GetType() const { return m_type; } + + int GetStartingColumn() const { return m_start_col; } + + void SetStartingColumn(int col) { m_start_col = col; } + + int GetKeyValue() const { return m_key_value; } + + std::string &GetName() { return m_name; } + + int GetDrawWidth() const { + return m_max_submenu_name_length + m_max_submenu_key_name_length + 8; + } + + uint64_t GetIdentifier() const { return m_identifier; } + + void SetIdentifier(uint64_t identifier) { m_identifier = identifier; } + +protected: + std::string m_name; + std::string m_key_name; + uint64_t m_identifier; + Type m_type; + int m_key_value; + int m_start_col; + int m_max_submenu_name_length; + int m_max_submenu_key_name_length; + int m_selected; + Menu *m_parent; + Menus m_submenus; + WindowSP m_menu_window_sp; + MenuActionResult m_canned_result; + MenuDelegateSP m_delegate_sp; +}; + +// Menubar or separator constructor +Menu::Menu(Type type) + : m_name(), m_key_name(), m_identifier(0), m_type(type), m_key_value(0), + m_start_col(0), m_max_submenu_name_length(0), + m_max_submenu_key_name_length(0), m_selected(0), m_parent(nullptr), + m_submenus(), m_canned_result(MenuActionResult::NotHandled), + m_delegate_sp() {} + +// Menuitem constructor +Menu::Menu(const char *name, const char *key_name, int key_value, + uint64_t identifier) + : m_name(), m_key_name(), m_identifier(identifier), m_type(Type::Invalid), + m_key_value(key_value), m_start_col(0), m_max_submenu_name_length(0), + m_max_submenu_key_name_length(0), m_selected(0), m_parent(nullptr), + m_submenus(), m_canned_result(MenuActionResult::NotHandled), + m_delegate_sp() { + if (name && name[0]) { + m_name = name; + m_type = Type::Item; + if (key_name && key_name[0]) + m_key_name = key_name; + } else { + m_type = Type::Separator; + } +} + +void Menu::RecalculateNameLengths() { + m_max_submenu_name_length = 0; + m_max_submenu_key_name_length = 0; + Menus &submenus = GetSubmenus(); + const size_t num_submenus = submenus.size(); + for (size_t i = 0; i < num_submenus; ++i) { + Menu *submenu = submenus[i].get(); + if (static_cast<size_t>(m_max_submenu_name_length) < submenu->m_name.size()) + m_max_submenu_name_length = submenu->m_name.size(); + if (static_cast<size_t>(m_max_submenu_key_name_length) < + submenu->m_key_name.size()) + m_max_submenu_key_name_length = submenu->m_key_name.size(); + } +} + +void Menu::AddSubmenu(const MenuSP &menu_sp) { + menu_sp->m_parent = this; + if (static_cast<size_t>(m_max_submenu_name_length) < menu_sp->m_name.size()) + m_max_submenu_name_length = menu_sp->m_name.size(); + if (static_cast<size_t>(m_max_submenu_key_name_length) < + menu_sp->m_key_name.size()) + m_max_submenu_key_name_length = menu_sp->m_key_name.size(); + m_submenus.push_back(menu_sp); +} + +void Menu::DrawMenuTitle(Window &window, bool highlight) { + if (m_type == Type::Separator) { + window.MoveCursor(0, window.GetCursorY()); + window.PutChar(ACS_LTEE); + int width = window.GetWidth(); + if (width > 2) { + width -= 2; + for (int i = 0; i < width; ++i) + window.PutChar(ACS_HLINE); + } + window.PutChar(ACS_RTEE); + } else { + const int shortcut_key = m_key_value; + bool underlined_shortcut = false; + const attr_t hilgight_attr = A_REVERSE; + if (highlight) + window.AttributeOn(hilgight_attr); + if (isprint(shortcut_key)) { + size_t lower_pos = m_name.find(tolower(shortcut_key)); + size_t upper_pos = m_name.find(toupper(shortcut_key)); + const char *name = m_name.c_str(); + size_t pos = std::min<size_t>(lower_pos, upper_pos); + if (pos != std::string::npos) { + underlined_shortcut = true; + if (pos > 0) { + window.PutCString(name, pos); + name += pos; + } + const attr_t shortcut_attr = A_UNDERLINE | A_BOLD; + window.AttributeOn(shortcut_attr); + window.PutChar(name[0]); + window.AttributeOff(shortcut_attr); + name++; + if (name[0]) + window.PutCString(name); + } + } + + if (!underlined_shortcut) { + window.PutCString(m_name.c_str()); + } + + if (highlight) + window.AttributeOff(hilgight_attr); + + if (m_key_name.empty()) { + if (!underlined_shortcut && isprint(m_key_value)) { + window.AttributeOn(COLOR_PAIR(3)); + window.Printf(" (%c)", m_key_value); + window.AttributeOff(COLOR_PAIR(3)); + } + } else { + window.AttributeOn(COLOR_PAIR(3)); + window.Printf(" (%s)", m_key_name.c_str()); + window.AttributeOff(COLOR_PAIR(3)); + } + } +} + +bool Menu::WindowDelegateDraw(Window &window, bool force) { + Menus &submenus = GetSubmenus(); + const size_t num_submenus = submenus.size(); + const int selected_idx = GetSelectedSubmenuIndex(); + Menu::Type menu_type = GetType(); + switch (menu_type) { + case Menu::Type::Bar: { + window.SetBackground(2); + window.MoveCursor(0, 0); + for (size_t i = 0; i < num_submenus; ++i) { + Menu *menu = submenus[i].get(); + if (i > 0) + window.PutChar(' '); + menu->SetStartingColumn(window.GetCursorX()); + window.PutCString("| "); + menu->DrawMenuTitle(window, false); + } + window.PutCString(" |"); + } break; + + case Menu::Type::Item: { + int y = 1; + int x = 3; + // Draw the menu + int cursor_x = 0; + int cursor_y = 0; + window.Erase(); + window.SetBackground(2); + window.Box(); + for (size_t i = 0; i < num_submenus; ++i) { + const bool is_selected = (i == static_cast<size_t>(selected_idx)); + window.MoveCursor(x, y + i); + if (is_selected) { + // Remember where we want the cursor to be + cursor_x = x - 1; + cursor_y = y + i; + } + submenus[i]->DrawMenuTitle(window, is_selected); + } + window.MoveCursor(cursor_x, cursor_y); + } break; + + default: + case Menu::Type::Separator: + break; + } + return true; // Drawing handled... +} + +HandleCharResult Menu::WindowDelegateHandleChar(Window &window, int key) { + HandleCharResult result = eKeyNotHandled; + + Menus &submenus = GetSubmenus(); + const size_t num_submenus = submenus.size(); + const int selected_idx = GetSelectedSubmenuIndex(); + Menu::Type menu_type = GetType(); + if (menu_type == Menu::Type::Bar) { + MenuSP run_menu_sp; + switch (key) { + case KEY_DOWN: + case KEY_UP: + // Show last menu or first menu + if (selected_idx < static_cast<int>(num_submenus)) + run_menu_sp = submenus[selected_idx]; + else if (!submenus.empty()) + run_menu_sp = submenus.front(); + result = eKeyHandled; + break; + + case KEY_RIGHT: + ++m_selected; + if (m_selected >= static_cast<int>(num_submenus)) + m_selected = 0; + if (m_selected < static_cast<int>(num_submenus)) + run_menu_sp = submenus[m_selected]; + else if (!submenus.empty()) + run_menu_sp = submenus.front(); + result = eKeyHandled; + break; + + case KEY_LEFT: + --m_selected; + if (m_selected < 0) + m_selected = num_submenus - 1; + if (m_selected < static_cast<int>(num_submenus)) + run_menu_sp = submenus[m_selected]; + else if (!submenus.empty()) + run_menu_sp = submenus.front(); + result = eKeyHandled; + break; + + default: + for (size_t i = 0; i < num_submenus; ++i) { + if (submenus[i]->GetKeyValue() == key) { + SetSelectedSubmenuIndex(i); + run_menu_sp = submenus[i]; + result = eKeyHandled; + break; + } + } + break; + } + + if (run_menu_sp) { + // Run the action on this menu in case we need to populate the menu with + // dynamic content and also in case check marks, and any other menu + // decorations need to be calculated + if (run_menu_sp->Action() == MenuActionResult::Quit) + return eQuitApplication; + + Rect menu_bounds; + menu_bounds.origin.x = run_menu_sp->GetStartingColumn(); + menu_bounds.origin.y = 1; + menu_bounds.size.width = run_menu_sp->GetDrawWidth(); + menu_bounds.size.height = run_menu_sp->GetSubmenus().size() + 2; + if (m_menu_window_sp) + window.GetParent()->RemoveSubWindow(m_menu_window_sp.get()); + + m_menu_window_sp = window.GetParent()->CreateSubWindow( + run_menu_sp->GetName().c_str(), menu_bounds, true); + m_menu_window_sp->SetDelegate(run_menu_sp); + } + } else if (menu_type == Menu::Type::Item) { + switch (key) { + case KEY_DOWN: + if (m_submenus.size() > 1) { + const int start_select = m_selected; + while (++m_selected != start_select) { + if (static_cast<size_t>(m_selected) >= num_submenus) + m_selected = 0; + if (m_submenus[m_selected]->GetType() == Type::Separator) + continue; + else + break; + } + return eKeyHandled; + } + break; + + case KEY_UP: + if (m_submenus.size() > 1) { + const int start_select = m_selected; + while (--m_selected != start_select) { + if (m_selected < static_cast<int>(0)) + m_selected = num_submenus - 1; + if (m_submenus[m_selected]->GetType() == Type::Separator) + continue; + else + break; + } + return eKeyHandled; + } + break; + + case KEY_RETURN: + if (static_cast<size_t>(selected_idx) < num_submenus) { + if (submenus[selected_idx]->Action() == MenuActionResult::Quit) + return eQuitApplication; + window.GetParent()->RemoveSubWindow(&window); + return eKeyHandled; + } + break; + + case KEY_ESCAPE: // Beware: pressing escape key has 1 to 2 second delay in + // case other chars are entered for escaped sequences + window.GetParent()->RemoveSubWindow(&window); + return eKeyHandled; + + default: + for (size_t i = 0; i < num_submenus; ++i) { + Menu *menu = submenus[i].get(); + if (menu->GetKeyValue() == key) { + SetSelectedSubmenuIndex(i); + window.GetParent()->RemoveSubWindow(&window); + if (menu->Action() == MenuActionResult::Quit) + return eQuitApplication; + return eKeyHandled; + } + } + break; + } + } else if (menu_type == Menu::Type::Separator) { + } + return result; +} + +class Application { +public: + Application(FILE *in, FILE *out) + : m_window_sp(), m_screen(nullptr), m_in(in), m_out(out) {} + + ~Application() { + m_window_delegates.clear(); + m_window_sp.reset(); + if (m_screen) { + ::delscreen(m_screen); + m_screen = nullptr; + } + } + + void Initialize() { + ::setlocale(LC_ALL, ""); + ::setlocale(LC_CTYPE, ""); + m_screen = ::newterm(nullptr, m_out, m_in); + ::start_color(); + ::curs_set(0); + ::noecho(); + ::keypad(stdscr, TRUE); + } + + void Terminate() { ::endwin(); } + + void Run(Debugger &debugger) { + bool done = false; + int delay_in_tenths_of_a_second = 1; + + // Alas the threading model in curses is a bit lame so we need to resort to + // polling every 0.5 seconds. We could poll for stdin ourselves and then + // pass the keys down but then we need to translate all of the escape + // sequences ourselves. So we resort to polling for input because we need + // to receive async process events while in this loop. + + halfdelay(delay_in_tenths_of_a_second); // Poll using some number of tenths + // of seconds seconds when calling + // Window::GetChar() + + ListenerSP listener_sp( + Listener::MakeListener("lldb.IOHandler.curses.Application")); + ConstString broadcaster_class_target(Target::GetStaticBroadcasterClass()); + ConstString broadcaster_class_process(Process::GetStaticBroadcasterClass()); + ConstString broadcaster_class_thread(Thread::GetStaticBroadcasterClass()); + debugger.EnableForwardEvents(listener_sp); + + bool update = true; +#if defined(__APPLE__) + std::deque<int> escape_chars; +#endif + + while (!done) { + if (update) { + m_window_sp->Draw(false); + // All windows should be calling Window::DeferredRefresh() instead of + // Window::Refresh() so we can do a single update and avoid any screen + // blinking + update_panels(); + + // Cursor hiding isn't working on MacOSX, so hide it in the top left + // corner + m_window_sp->MoveCursor(0, 0); + + doupdate(); + update = false; + } + +#if defined(__APPLE__) + // Terminal.app doesn't map its function keys correctly, F1-F4 default + // to: \033OP, \033OQ, \033OR, \033OS, so lets take care of this here if + // possible + int ch; + if (escape_chars.empty()) + ch = m_window_sp->GetChar(); + else { + ch = escape_chars.front(); + escape_chars.pop_front(); + } + if (ch == KEY_ESCAPE) { + int ch2 = m_window_sp->GetChar(); + if (ch2 == 'O') { + int ch3 = m_window_sp->GetChar(); + switch (ch3) { + case 'P': + ch = KEY_F(1); + break; + case 'Q': + ch = KEY_F(2); + break; + case 'R': + ch = KEY_F(3); + break; + case 'S': + ch = KEY_F(4); + break; + default: + escape_chars.push_back(ch2); + if (ch3 != -1) + escape_chars.push_back(ch3); + break; + } + } else if (ch2 != -1) + escape_chars.push_back(ch2); + } +#else + int ch = m_window_sp->GetChar(); + +#endif + if (ch == -1) { + if (feof(m_in) || ferror(m_in)) { + done = true; + } else { + // Just a timeout from using halfdelay(), check for events + EventSP event_sp; + while (listener_sp->PeekAtNextEvent()) { + listener_sp->GetEvent(event_sp, std::chrono::seconds(0)); + + if (event_sp) { + Broadcaster *broadcaster = event_sp->GetBroadcaster(); + if (broadcaster) { + // uint32_t event_type = event_sp->GetType(); + ConstString broadcaster_class( + broadcaster->GetBroadcasterClass()); + if (broadcaster_class == broadcaster_class_process) { + debugger.GetCommandInterpreter().UpdateExecutionContext( + nullptr); + update = true; + continue; // Don't get any key, just update our view + } + } + } + } + } + } else { + HandleCharResult key_result = m_window_sp->HandleChar(ch); + switch (key_result) { + case eKeyHandled: + debugger.GetCommandInterpreter().UpdateExecutionContext(nullptr); + update = true; + break; + case eKeyNotHandled: + break; + case eQuitApplication: + done = true; + break; + } + } + } + + debugger.CancelForwardEvents(listener_sp); + } + + WindowSP &GetMainWindow() { + if (!m_window_sp) + m_window_sp = std::make_shared<Window>("main", stdscr, false); + return m_window_sp; + } + +protected: + WindowSP m_window_sp; + WindowDelegates m_window_delegates; + SCREEN *m_screen; + FILE *m_in; + FILE *m_out; +}; + +} // namespace curses + +using namespace curses; + +struct Row { + ValueObjectManager value; + Row *parent; + // The process stop ID when the children were calculated. + uint32_t children_stop_id; + int row_idx; + int x; + int y; + bool might_have_children; + bool expanded; + bool calculated_children; + std::vector<Row> children; + + Row(const ValueObjectSP &v, Row *p) + : value(v, lldb::eDynamicDontRunTarget, true), parent(p), row_idx(0), + x(1), y(1), might_have_children(v ? v->MightHaveChildren() : false), + expanded(false), calculated_children(false), children() {} + + size_t GetDepth() const { + if (parent) + return 1 + parent->GetDepth(); + return 0; + } + + void Expand() { expanded = true; } + + std::vector<Row> &GetChildren() { + ProcessSP process_sp = value.GetProcessSP(); + auto stop_id = process_sp->GetStopID(); + if (process_sp && stop_id != children_stop_id) { + children_stop_id = stop_id; + calculated_children = false; + } + if (!calculated_children) { + children.clear(); + calculated_children = true; + ValueObjectSP valobj = value.GetSP(); + if (valobj) { + const size_t num_children = valobj->GetNumChildren(); + for (size_t i = 0; i < num_children; ++i) { + children.push_back(Row(valobj->GetChildAtIndex(i, true), this)); + } + } + } + return children; + } + + void Unexpand() { + expanded = false; + calculated_children = false; + children.clear(); + } + + void DrawTree(Window &window) { + if (parent) + parent->DrawTreeForChild(window, this, 0); + + if (might_have_children) { + // It we can get UTF8 characters to work we should try to use the + // "symbol" UTF8 string below + // const char *symbol = ""; + // if (row.expanded) + // symbol = "\xe2\x96\xbd "; + // else + // symbol = "\xe2\x96\xb7 "; + // window.PutCString (symbol); + + // The ACS_DARROW and ACS_RARROW don't look very nice they are just a 'v' + // or '>' character... + // if (expanded) + // window.PutChar (ACS_DARROW); + // else + // window.PutChar (ACS_RARROW); + // Since we can't find any good looking right arrow/down arrow symbols, + // just use a diamond... + window.PutChar(ACS_DIAMOND); + window.PutChar(ACS_HLINE); + } + } + + void DrawTreeForChild(Window &window, Row *child, uint32_t reverse_depth) { + if (parent) + parent->DrawTreeForChild(window, this, reverse_depth + 1); + + if (&GetChildren().back() == child) { + // Last child + if (reverse_depth == 0) { + window.PutChar(ACS_LLCORNER); + window.PutChar(ACS_HLINE); + } else { + window.PutChar(' '); + window.PutChar(' '); + } + } else { + if (reverse_depth == 0) { + window.PutChar(ACS_LTEE); + window.PutChar(ACS_HLINE); + } else { + window.PutChar(ACS_VLINE); + window.PutChar(' '); + } + } + } +}; + +struct DisplayOptions { + bool show_types; +}; + +class TreeItem; + +class TreeDelegate { +public: + TreeDelegate() = default; + virtual ~TreeDelegate() = default; + + virtual void TreeDelegateDrawTreeItem(TreeItem &item, Window &window) = 0; + virtual void TreeDelegateGenerateChildren(TreeItem &item) = 0; + virtual bool TreeDelegateItemSelected( + TreeItem &item) = 0; // Return true if we need to update views +}; + +typedef std::shared_ptr<TreeDelegate> TreeDelegateSP; + +class TreeItem { +public: + TreeItem(TreeItem *parent, TreeDelegate &delegate, bool might_have_children) + : m_parent(parent), m_delegate(delegate), m_user_data(nullptr), + m_identifier(0), m_row_idx(-1), m_children(), + m_might_have_children(might_have_children), m_is_expanded(false) {} + + TreeItem &operator=(const TreeItem &rhs) { + if (this != &rhs) { + m_parent = rhs.m_parent; + m_delegate = rhs.m_delegate; + m_user_data = rhs.m_user_data; + m_identifier = rhs.m_identifier; + m_row_idx = rhs.m_row_idx; + m_children = rhs.m_children; + m_might_have_children = rhs.m_might_have_children; + m_is_expanded = rhs.m_is_expanded; + } + return *this; + } + + TreeItem(const TreeItem &) = default; + + size_t GetDepth() const { + if (m_parent) + return 1 + m_parent->GetDepth(); + return 0; + } + + int GetRowIndex() const { return m_row_idx; } + + void ClearChildren() { m_children.clear(); } + + void Resize(size_t n, const TreeItem &t) { m_children.resize(n, t); } + + TreeItem &operator[](size_t i) { return m_children[i]; } + + void SetRowIndex(int row_idx) { m_row_idx = row_idx; } + + size_t GetNumChildren() { + m_delegate.TreeDelegateGenerateChildren(*this); + return m_children.size(); + } + + void ItemWasSelected() { m_delegate.TreeDelegateItemSelected(*this); } + + void CalculateRowIndexes(int &row_idx) { + SetRowIndex(row_idx); + ++row_idx; + + const bool expanded = IsExpanded(); + + // The root item must calculate its children, or we must calculate the + // number of children if the item is expanded + if (m_parent == nullptr || expanded) + GetNumChildren(); + + for (auto &item : m_children) { + if (expanded) + item.CalculateRowIndexes(row_idx); + else + item.SetRowIndex(-1); + } + } + + TreeItem *GetParent() { return m_parent; } + + bool IsExpanded() const { return m_is_expanded; } + + void Expand() { m_is_expanded = true; } + + void Unexpand() { m_is_expanded = false; } + + bool Draw(Window &window, const int first_visible_row, + const uint32_t selected_row_idx, int &row_idx, int &num_rows_left) { + if (num_rows_left <= 0) + return false; + + if (m_row_idx >= first_visible_row) { + window.MoveCursor(2, row_idx + 1); + + if (m_parent) + m_parent->DrawTreeForChild(window, this, 0); + + if (m_might_have_children) { + // It we can get UTF8 characters to work we should try to use the + // "symbol" UTF8 string below + // const char *symbol = ""; + // if (row.expanded) + // symbol = "\xe2\x96\xbd "; + // else + // symbol = "\xe2\x96\xb7 "; + // window.PutCString (symbol); + + // The ACS_DARROW and ACS_RARROW don't look very nice they are just a + // 'v' or '>' character... + // if (expanded) + // window.PutChar (ACS_DARROW); + // else + // window.PutChar (ACS_RARROW); + // Since we can't find any good looking right arrow/down arrow symbols, + // just use a diamond... + window.PutChar(ACS_DIAMOND); + window.PutChar(ACS_HLINE); + } + bool highlight = (selected_row_idx == static_cast<size_t>(m_row_idx)) && + window.IsActive(); + + if (highlight) + window.AttributeOn(A_REVERSE); + + m_delegate.TreeDelegateDrawTreeItem(*this, window); + + if (highlight) + window.AttributeOff(A_REVERSE); + ++row_idx; + --num_rows_left; + } + + if (num_rows_left <= 0) + return false; // We are done drawing... + + if (IsExpanded()) { + for (auto &item : m_children) { + // If we displayed all the rows and item.Draw() returns false we are + // done drawing and can exit this for loop + if (!item.Draw(window, first_visible_row, selected_row_idx, row_idx, + num_rows_left)) + break; + } + } + return num_rows_left >= 0; // Return true if not done drawing yet + } + + void DrawTreeForChild(Window &window, TreeItem *child, + uint32_t reverse_depth) { + if (m_parent) + m_parent->DrawTreeForChild(window, this, reverse_depth + 1); + + if (&m_children.back() == child) { + // Last child + if (reverse_depth == 0) { + window.PutChar(ACS_LLCORNER); + window.PutChar(ACS_HLINE); + } else { + window.PutChar(' '); + window.PutChar(' '); + } + } else { + if (reverse_depth == 0) { + window.PutChar(ACS_LTEE); + window.PutChar(ACS_HLINE); + } else { + window.PutChar(ACS_VLINE); + window.PutChar(' '); + } + } + } + + TreeItem *GetItemForRowIndex(uint32_t row_idx) { + if (static_cast<uint32_t>(m_row_idx) == row_idx) + return this; + if (m_children.empty()) + return nullptr; + if (IsExpanded()) { + for (auto &item : m_children) { + TreeItem *selected_item_ptr = item.GetItemForRowIndex(row_idx); + if (selected_item_ptr) + return selected_item_ptr; + } + } + return nullptr; + } + + void *GetUserData() const { return m_user_data; } + + void SetUserData(void *user_data) { m_user_data = user_data; } + + uint64_t GetIdentifier() const { return m_identifier; } + + void SetIdentifier(uint64_t identifier) { m_identifier = identifier; } + + void SetMightHaveChildren(bool b) { m_might_have_children = b; } + +protected: + TreeItem *m_parent; + TreeDelegate &m_delegate; + void *m_user_data; + uint64_t m_identifier; + int m_row_idx; // Zero based visible row index, -1 if not visible or for the + // root item + std::vector<TreeItem> m_children; + bool m_might_have_children; + bool m_is_expanded; +}; + +class TreeWindowDelegate : public WindowDelegate { +public: + TreeWindowDelegate(Debugger &debugger, const TreeDelegateSP &delegate_sp) + : m_debugger(debugger), m_delegate_sp(delegate_sp), + m_root(nullptr, *delegate_sp, true), m_selected_item(nullptr), + m_num_rows(0), m_selected_row_idx(0), m_first_visible_row(0), + m_min_x(0), m_min_y(0), m_max_x(0), m_max_y(0) {} + + int NumVisibleRows() const { return m_max_y - m_min_y; } + + bool WindowDelegateDraw(Window &window, bool force) override { + ExecutionContext exe_ctx( + m_debugger.GetCommandInterpreter().GetExecutionContext()); + Process *process = exe_ctx.GetProcessPtr(); + + bool display_content = false; + if (process) { + StateType state = process->GetState(); + if (StateIsStoppedState(state, true)) { + // We are stopped, so it is ok to + display_content = true; + } else if (StateIsRunningState(state)) { + return true; // Don't do any updating when we are running + } + } + + m_min_x = 2; + m_min_y = 1; + m_max_x = window.GetWidth() - 1; + m_max_y = window.GetHeight() - 1; + + window.Erase(); + window.DrawTitleBox(window.GetName()); + + if (display_content) { + const int num_visible_rows = NumVisibleRows(); + m_num_rows = 0; + m_root.CalculateRowIndexes(m_num_rows); + + // If we unexpanded while having something selected our total number of + // rows is less than the num visible rows, then make sure we show all the + // rows by setting the first visible row accordingly. + if (m_first_visible_row > 0 && m_num_rows < num_visible_rows) + m_first_visible_row = 0; + + // Make sure the selected row is always visible + if (m_selected_row_idx < m_first_visible_row) + m_first_visible_row = m_selected_row_idx; + else if (m_first_visible_row + num_visible_rows <= m_selected_row_idx) + m_first_visible_row = m_selected_row_idx - num_visible_rows + 1; + + int row_idx = 0; + int num_rows_left = num_visible_rows; + m_root.Draw(window, m_first_visible_row, m_selected_row_idx, row_idx, + num_rows_left); + // Get the selected row + m_selected_item = m_root.GetItemForRowIndex(m_selected_row_idx); + } else { + m_selected_item = nullptr; + } + + return true; // Drawing handled + } + + const char *WindowDelegateGetHelpText() override { + return "Thread window keyboard shortcuts:"; + } + + KeyHelp *WindowDelegateGetKeyHelp() override { + static curses::KeyHelp g_source_view_key_help[] = { + {KEY_UP, "Select previous item"}, + {KEY_DOWN, "Select next item"}, + {KEY_RIGHT, "Expand the selected item"}, + {KEY_LEFT, + "Unexpand the selected item or select parent if not expanded"}, + {KEY_PPAGE, "Page up"}, + {KEY_NPAGE, "Page down"}, + {'h', "Show help dialog"}, + {' ', "Toggle item expansion"}, + {',', "Page up"}, + {'.', "Page down"}, + {'\0', nullptr}}; + return g_source_view_key_help; + } + + HandleCharResult WindowDelegateHandleChar(Window &window, int c) override { + switch (c) { + case ',': + case KEY_PPAGE: + // Page up key + if (m_first_visible_row > 0) { + if (m_first_visible_row > m_max_y) + m_first_visible_row -= m_max_y; + else + m_first_visible_row = 0; + m_selected_row_idx = m_first_visible_row; + m_selected_item = m_root.GetItemForRowIndex(m_selected_row_idx); + if (m_selected_item) + m_selected_item->ItemWasSelected(); + } + return eKeyHandled; + + case '.': + case KEY_NPAGE: + // Page down key + if (m_num_rows > m_max_y) { + if (m_first_visible_row + m_max_y < m_num_rows) { + m_first_visible_row += m_max_y; + m_selected_row_idx = m_first_visible_row; + m_selected_item = m_root.GetItemForRowIndex(m_selected_row_idx); + if (m_selected_item) + m_selected_item->ItemWasSelected(); + } + } + return eKeyHandled; + + case KEY_UP: + if (m_selected_row_idx > 0) { + --m_selected_row_idx; + m_selected_item = m_root.GetItemForRowIndex(m_selected_row_idx); + if (m_selected_item) + m_selected_item->ItemWasSelected(); + } + return eKeyHandled; + + case KEY_DOWN: + if (m_selected_row_idx + 1 < m_num_rows) { + ++m_selected_row_idx; + m_selected_item = m_root.GetItemForRowIndex(m_selected_row_idx); + if (m_selected_item) + m_selected_item->ItemWasSelected(); + } + return eKeyHandled; + + case KEY_RIGHT: + if (m_selected_item) { + if (!m_selected_item->IsExpanded()) + m_selected_item->Expand(); + } + return eKeyHandled; + + case KEY_LEFT: + if (m_selected_item) { + if (m_selected_item->IsExpanded()) + m_selected_item->Unexpand(); + else if (m_selected_item->GetParent()) { + m_selected_row_idx = m_selected_item->GetParent()->GetRowIndex(); + m_selected_item = m_root.GetItemForRowIndex(m_selected_row_idx); + if (m_selected_item) + m_selected_item->ItemWasSelected(); + } + } + return eKeyHandled; + + case ' ': + // Toggle expansion state when SPACE is pressed + if (m_selected_item) { + if (m_selected_item->IsExpanded()) + m_selected_item->Unexpand(); + else + m_selected_item->Expand(); + } + return eKeyHandled; + + case 'h': + window.CreateHelpSubwindow(); + return eKeyHandled; + + default: + break; + } + return eKeyNotHandled; + } + +protected: + Debugger &m_debugger; + TreeDelegateSP m_delegate_sp; + TreeItem m_root; + TreeItem *m_selected_item; + int m_num_rows; + int m_selected_row_idx; + int m_first_visible_row; + int m_min_x; + int m_min_y; + int m_max_x; + int m_max_y; +}; + +class FrameTreeDelegate : public TreeDelegate { +public: + FrameTreeDelegate() : TreeDelegate() { + FormatEntity::Parse( + "frame #${frame.index}: {${function.name}${function.pc-offset}}}", + m_format); + } + + ~FrameTreeDelegate() override = default; + + void TreeDelegateDrawTreeItem(TreeItem &item, Window &window) override { + Thread *thread = (Thread *)item.GetUserData(); + if (thread) { + const uint64_t frame_idx = item.GetIdentifier(); + StackFrameSP frame_sp = thread->GetStackFrameAtIndex(frame_idx); + if (frame_sp) { + StreamString strm; + const SymbolContext &sc = + frame_sp->GetSymbolContext(eSymbolContextEverything); + ExecutionContext exe_ctx(frame_sp); + if (FormatEntity::Format(m_format, strm, &sc, &exe_ctx, nullptr, + nullptr, false, false)) { + int right_pad = 1; + window.PutCStringTruncated(strm.GetString().str().c_str(), right_pad); + } + } + } + } + + void TreeDelegateGenerateChildren(TreeItem &item) override { + // No children for frames yet... + } + + bool TreeDelegateItemSelected(TreeItem &item) override { + Thread *thread = (Thread *)item.GetUserData(); + if (thread) { + thread->GetProcess()->GetThreadList().SetSelectedThreadByID( + thread->GetID()); + const uint64_t frame_idx = item.GetIdentifier(); + thread->SetSelectedFrameByIndex(frame_idx); + return true; + } + return false; + } + +protected: + FormatEntity::Entry m_format; +}; + +class ThreadTreeDelegate : public TreeDelegate { +public: + ThreadTreeDelegate(Debugger &debugger) + : TreeDelegate(), m_debugger(debugger), m_tid(LLDB_INVALID_THREAD_ID), + m_stop_id(UINT32_MAX) { + FormatEntity::Parse("thread #${thread.index}: tid = ${thread.id}{, stop " + "reason = ${thread.stop-reason}}", + m_format); + } + + ~ThreadTreeDelegate() override = default; + + ProcessSP GetProcess() { + return m_debugger.GetCommandInterpreter() + .GetExecutionContext() + .GetProcessSP(); + } + + ThreadSP GetThread(const TreeItem &item) { + ProcessSP process_sp = GetProcess(); + if (process_sp) + return process_sp->GetThreadList().FindThreadByID(item.GetIdentifier()); + return ThreadSP(); + } + + void TreeDelegateDrawTreeItem(TreeItem &item, Window &window) override { + ThreadSP thread_sp = GetThread(item); + if (thread_sp) { + StreamString strm; + ExecutionContext exe_ctx(thread_sp); + if (FormatEntity::Format(m_format, strm, nullptr, &exe_ctx, nullptr, + nullptr, false, false)) { + int right_pad = 1; + window.PutCStringTruncated(strm.GetString().str().c_str(), right_pad); + } + } + } + + void TreeDelegateGenerateChildren(TreeItem &item) override { + ProcessSP process_sp = GetProcess(); + if (process_sp && process_sp->IsAlive()) { + StateType state = process_sp->GetState(); + if (StateIsStoppedState(state, true)) { + ThreadSP thread_sp = GetThread(item); + if (thread_sp) { + if (m_stop_id == process_sp->GetStopID() && + thread_sp->GetID() == m_tid) + return; // Children are already up to date + if (!m_frame_delegate_sp) { + // Always expand the thread item the first time we show it + m_frame_delegate_sp = std::make_shared<FrameTreeDelegate>(); + } + + m_stop_id = process_sp->GetStopID(); + m_tid = thread_sp->GetID(); + + TreeItem t(&item, *m_frame_delegate_sp, false); + size_t num_frames = thread_sp->GetStackFrameCount(); + item.Resize(num_frames, t); + for (size_t i = 0; i < num_frames; ++i) { + item[i].SetUserData(thread_sp.get()); + item[i].SetIdentifier(i); + } + } + return; + } + } + item.ClearChildren(); + } + + bool TreeDelegateItemSelected(TreeItem &item) override { + ProcessSP process_sp = GetProcess(); + if (process_sp && process_sp->IsAlive()) { + StateType state = process_sp->GetState(); + if (StateIsStoppedState(state, true)) { + ThreadSP thread_sp = GetThread(item); + if (thread_sp) { + ThreadList &thread_list = thread_sp->GetProcess()->GetThreadList(); + std::lock_guard<std::recursive_mutex> guard(thread_list.GetMutex()); + ThreadSP selected_thread_sp = thread_list.GetSelectedThread(); + if (selected_thread_sp->GetID() != thread_sp->GetID()) { + thread_list.SetSelectedThreadByID(thread_sp->GetID()); + return true; + } + } + } + } + return false; + } + +protected: + Debugger &m_debugger; + std::shared_ptr<FrameTreeDelegate> m_frame_delegate_sp; + lldb::user_id_t m_tid; + uint32_t m_stop_id; + FormatEntity::Entry m_format; +}; + +class ThreadsTreeDelegate : public TreeDelegate { +public: + ThreadsTreeDelegate(Debugger &debugger) + : TreeDelegate(), m_thread_delegate_sp(), m_debugger(debugger), + m_stop_id(UINT32_MAX) { + FormatEntity::Parse("process ${process.id}{, name = ${process.name}}", + m_format); + } + + ~ThreadsTreeDelegate() override = default; + + ProcessSP GetProcess() { + return m_debugger.GetCommandInterpreter() + .GetExecutionContext() + .GetProcessSP(); + } + + void TreeDelegateDrawTreeItem(TreeItem &item, Window &window) override { + ProcessSP process_sp = GetProcess(); + if (process_sp && process_sp->IsAlive()) { + StreamString strm; + ExecutionContext exe_ctx(process_sp); + if (FormatEntity::Format(m_format, strm, nullptr, &exe_ctx, nullptr, + nullptr, false, false)) { + int right_pad = 1; + window.PutCStringTruncated(strm.GetString().str().c_str(), right_pad); + } + } + } + + void TreeDelegateGenerateChildren(TreeItem &item) override { + ProcessSP process_sp = GetProcess(); + if (process_sp && process_sp->IsAlive()) { + StateType state = process_sp->GetState(); + if (StateIsStoppedState(state, true)) { + const uint32_t stop_id = process_sp->GetStopID(); + if (m_stop_id == stop_id) + return; // Children are already up to date + + m_stop_id = stop_id; + + if (!m_thread_delegate_sp) { + // Always expand the thread item the first time we show it + // item.Expand(); + m_thread_delegate_sp = + std::make_shared<ThreadTreeDelegate>(m_debugger); + } + + TreeItem t(&item, *m_thread_delegate_sp, false); + ThreadList &threads = process_sp->GetThreadList(); + std::lock_guard<std::recursive_mutex> guard(threads.GetMutex()); + size_t num_threads = threads.GetSize(); + item.Resize(num_threads, t); + for (size_t i = 0; i < num_threads; ++i) { + item[i].SetIdentifier(threads.GetThreadAtIndex(i)->GetID()); + item[i].SetMightHaveChildren(true); + } + return; + } + } + item.ClearChildren(); + } + + bool TreeDelegateItemSelected(TreeItem &item) override { return false; } + +protected: + std::shared_ptr<ThreadTreeDelegate> m_thread_delegate_sp; + Debugger &m_debugger; + uint32_t m_stop_id; + FormatEntity::Entry m_format; +}; + +class ValueObjectListDelegate : public WindowDelegate { +public: + ValueObjectListDelegate() + : m_rows(), m_selected_row(nullptr), m_selected_row_idx(0), + m_first_visible_row(0), m_num_rows(0), m_max_x(0), m_max_y(0) {} + + ValueObjectListDelegate(ValueObjectList &valobj_list) + : m_rows(), m_selected_row(nullptr), m_selected_row_idx(0), + m_first_visible_row(0), m_num_rows(0), m_max_x(0), m_max_y(0) { + SetValues(valobj_list); + } + + ~ValueObjectListDelegate() override = default; + + void SetValues(ValueObjectList &valobj_list) { + m_selected_row = nullptr; + m_selected_row_idx = 0; + m_first_visible_row = 0; + m_num_rows = 0; + m_rows.clear(); + for (auto &valobj_sp : valobj_list.GetObjects()) + m_rows.push_back(Row(valobj_sp, nullptr)); + } + + bool WindowDelegateDraw(Window &window, bool force) override { + m_num_rows = 0; + m_min_x = 2; + m_min_y = 1; + m_max_x = window.GetWidth() - 1; + m_max_y = window.GetHeight() - 1; + + window.Erase(); + window.DrawTitleBox(window.GetName()); + + const int num_visible_rows = NumVisibleRows(); + const int num_rows = CalculateTotalNumberRows(m_rows); + + // If we unexpanded while having something selected our total number of + // rows is less than the num visible rows, then make sure we show all the + // rows by setting the first visible row accordingly. + if (m_first_visible_row > 0 && num_rows < num_visible_rows) + m_first_visible_row = 0; + + // Make sure the selected row is always visible + if (m_selected_row_idx < m_first_visible_row) + m_first_visible_row = m_selected_row_idx; + else if (m_first_visible_row + num_visible_rows <= m_selected_row_idx) + m_first_visible_row = m_selected_row_idx - num_visible_rows + 1; + + DisplayRows(window, m_rows, g_options); + + // Get the selected row + m_selected_row = GetRowForRowIndex(m_selected_row_idx); + // Keep the cursor on the selected row so the highlight and the cursor are + // always on the same line + if (m_selected_row) + window.MoveCursor(m_selected_row->x, m_selected_row->y); + + return true; // Drawing handled + } + + KeyHelp *WindowDelegateGetKeyHelp() override { + static curses::KeyHelp g_source_view_key_help[] = { + {KEY_UP, "Select previous item"}, + {KEY_DOWN, "Select next item"}, + {KEY_RIGHT, "Expand selected item"}, + {KEY_LEFT, "Unexpand selected item or select parent if not expanded"}, + {KEY_PPAGE, "Page up"}, + {KEY_NPAGE, "Page down"}, + {'A', "Format as annotated address"}, + {'b', "Format as binary"}, + {'B', "Format as hex bytes with ASCII"}, + {'c', "Format as character"}, + {'d', "Format as a signed integer"}, + {'D', "Format selected value using the default format for the type"}, + {'f', "Format as float"}, + {'h', "Show help dialog"}, + {'i', "Format as instructions"}, + {'o', "Format as octal"}, + {'p', "Format as pointer"}, + {'s', "Format as C string"}, + {'t', "Toggle showing/hiding type names"}, + {'u', "Format as an unsigned integer"}, + {'x', "Format as hex"}, + {'X', "Format as uppercase hex"}, + {' ', "Toggle item expansion"}, + {',', "Page up"}, + {'.', "Page down"}, + {'\0', nullptr}}; + return g_source_view_key_help; + } + + HandleCharResult WindowDelegateHandleChar(Window &window, int c) override { + switch (c) { + case 'x': + case 'X': + case 'o': + case 's': + case 'u': + case 'd': + case 'D': + case 'i': + case 'A': + case 'p': + case 'c': + case 'b': + case 'B': + case 'f': + // Change the format for the currently selected item + if (m_selected_row) { + auto valobj_sp = m_selected_row->value.GetSP(); + if (valobj_sp) + valobj_sp->SetFormat(FormatForChar(c)); + } + return eKeyHandled; + + case 't': + // Toggle showing type names + g_options.show_types = !g_options.show_types; + return eKeyHandled; + + case ',': + case KEY_PPAGE: + // Page up key + if (m_first_visible_row > 0) { + if (static_cast<int>(m_first_visible_row) > m_max_y) + m_first_visible_row -= m_max_y; + else + m_first_visible_row = 0; + m_selected_row_idx = m_first_visible_row; + } + return eKeyHandled; + + case '.': + case KEY_NPAGE: + // Page down key + if (m_num_rows > static_cast<size_t>(m_max_y)) { + if (m_first_visible_row + m_max_y < m_num_rows) { + m_first_visible_row += m_max_y; + m_selected_row_idx = m_first_visible_row; + } + } + return eKeyHandled; + + case KEY_UP: + if (m_selected_row_idx > 0) + --m_selected_row_idx; + return eKeyHandled; + + case KEY_DOWN: + if (m_selected_row_idx + 1 < m_num_rows) + ++m_selected_row_idx; + return eKeyHandled; + + case KEY_RIGHT: + if (m_selected_row) { + if (!m_selected_row->expanded) + m_selected_row->Expand(); + } + return eKeyHandled; + + case KEY_LEFT: + if (m_selected_row) { + if (m_selected_row->expanded) + m_selected_row->Unexpand(); + else if (m_selected_row->parent) + m_selected_row_idx = m_selected_row->parent->row_idx; + } + return eKeyHandled; + + case ' ': + // Toggle expansion state when SPACE is pressed + if (m_selected_row) { + if (m_selected_row->expanded) + m_selected_row->Unexpand(); + else + m_selected_row->Expand(); + } + return eKeyHandled; + + case 'h': + window.CreateHelpSubwindow(); + return eKeyHandled; + + default: + break; + } + return eKeyNotHandled; + } + +protected: + std::vector<Row> m_rows; + Row *m_selected_row; + uint32_t m_selected_row_idx; + uint32_t m_first_visible_row; + uint32_t m_num_rows; + int m_min_x; + int m_min_y; + int m_max_x; + int m_max_y; + + static Format FormatForChar(int c) { + switch (c) { + case 'x': + return eFormatHex; + case 'X': + return eFormatHexUppercase; + case 'o': + return eFormatOctal; + case 's': + return eFormatCString; + case 'u': + return eFormatUnsigned; + case 'd': + return eFormatDecimal; + case 'D': + return eFormatDefault; + case 'i': + return eFormatInstruction; + case 'A': + return eFormatAddressInfo; + case 'p': + return eFormatPointer; + case 'c': + return eFormatChar; + case 'b': + return eFormatBinary; + case 'B': + return eFormatBytesWithASCII; + case 'f': + return eFormatFloat; + } + return eFormatDefault; + } + + bool DisplayRowObject(Window &window, Row &row, DisplayOptions &options, + bool highlight, bool last_child) { + ValueObject *valobj = row.value.GetSP().get(); + + if (valobj == nullptr) + return false; + + const char *type_name = + options.show_types ? valobj->GetTypeName().GetCString() : nullptr; + const char *name = valobj->GetName().GetCString(); + const char *value = valobj->GetValueAsCString(); + const char *summary = valobj->GetSummaryAsCString(); + + window.MoveCursor(row.x, row.y); + + row.DrawTree(window); + + if (highlight) + window.AttributeOn(A_REVERSE); + + if (type_name && type_name[0]) + window.Printf("(%s) ", type_name); + + if (name && name[0]) + window.PutCString(name); + + attr_t changd_attr = 0; + if (valobj->GetValueDidChange()) + changd_attr = COLOR_PAIR(5) | A_BOLD; + + if (value && value[0]) { + window.PutCString(" = "); + if (changd_attr) + window.AttributeOn(changd_attr); + window.PutCString(value); + if (changd_attr) + window.AttributeOff(changd_attr); + } + + if (summary && summary[0]) { + window.PutChar(' '); + if (changd_attr) + window.AttributeOn(changd_attr); + window.PutCString(summary); + if (changd_attr) + window.AttributeOff(changd_attr); + } + + if (highlight) + window.AttributeOff(A_REVERSE); + + return true; + } + + void DisplayRows(Window &window, std::vector<Row> &rows, + DisplayOptions &options) { + // > 0x25B7 + // \/ 0x25BD + + bool window_is_active = window.IsActive(); + for (auto &row : rows) { + const bool last_child = row.parent && &rows[rows.size() - 1] == &row; + // Save the row index in each Row structure + row.row_idx = m_num_rows; + if ((m_num_rows >= m_first_visible_row) && + ((m_num_rows - m_first_visible_row) < + static_cast<size_t>(NumVisibleRows()))) { + row.x = m_min_x; + row.y = m_num_rows - m_first_visible_row + 1; + if (DisplayRowObject(window, row, options, + window_is_active && + m_num_rows == m_selected_row_idx, + last_child)) { + ++m_num_rows; + } else { + row.x = 0; + row.y = 0; + } + } else { + row.x = 0; + row.y = 0; + ++m_num_rows; + } + + auto &children = row.GetChildren(); + if (row.expanded && !children.empty()) { + DisplayRows(window, children, options); + } + } + } + + int CalculateTotalNumberRows(std::vector<Row> &rows) { + int row_count = 0; + for (auto &row : rows) { + ++row_count; + if (row.expanded) + row_count += CalculateTotalNumberRows(row.GetChildren()); + } + return row_count; + } + + static Row *GetRowForRowIndexImpl(std::vector<Row> &rows, size_t &row_index) { + for (auto &row : rows) { + if (row_index == 0) + return &row; + else { + --row_index; + auto &children = row.GetChildren(); + if (row.expanded && !children.empty()) { + Row *result = GetRowForRowIndexImpl(children, row_index); + if (result) + return result; + } + } + } + return nullptr; + } + + Row *GetRowForRowIndex(size_t row_index) { + return GetRowForRowIndexImpl(m_rows, row_index); + } + + int NumVisibleRows() const { return m_max_y - m_min_y; } + + static DisplayOptions g_options; +}; + +class FrameVariablesWindowDelegate : public ValueObjectListDelegate { +public: + FrameVariablesWindowDelegate(Debugger &debugger) + : ValueObjectListDelegate(), m_debugger(debugger), + m_frame_block(nullptr) {} + + ~FrameVariablesWindowDelegate() override = default; + + const char *WindowDelegateGetHelpText() override { + return "Frame variable window keyboard shortcuts:"; + } + + bool WindowDelegateDraw(Window &window, bool force) override { + ExecutionContext exe_ctx( + m_debugger.GetCommandInterpreter().GetExecutionContext()); + Process *process = exe_ctx.GetProcessPtr(); + Block *frame_block = nullptr; + StackFrame *frame = nullptr; + + if (process) { + StateType state = process->GetState(); + if (StateIsStoppedState(state, true)) { + frame = exe_ctx.GetFramePtr(); + if (frame) + frame_block = frame->GetFrameBlock(); + } else if (StateIsRunningState(state)) { + return true; // Don't do any updating when we are running + } + } + + ValueObjectList local_values; + if (frame_block) { + // Only update the variables if they have changed + if (m_frame_block != frame_block) { + m_frame_block = frame_block; + + VariableList *locals = frame->GetVariableList(true); + if (locals) { + const DynamicValueType use_dynamic = eDynamicDontRunTarget; + for (const VariableSP &local_sp : *locals) { + ValueObjectSP value_sp = + frame->GetValueObjectForFrameVariable(local_sp, use_dynamic); + if (value_sp) { + ValueObjectSP synthetic_value_sp = value_sp->GetSyntheticValue(); + if (synthetic_value_sp) + local_values.Append(synthetic_value_sp); + else + local_values.Append(value_sp); + } + } + // Update the values + SetValues(local_values); + } + } + } else { + m_frame_block = nullptr; + // Update the values with an empty list if there is no frame + SetValues(local_values); + } + + return ValueObjectListDelegate::WindowDelegateDraw(window, force); + } + +protected: + Debugger &m_debugger; + Block *m_frame_block; +}; + +class RegistersWindowDelegate : public ValueObjectListDelegate { +public: + RegistersWindowDelegate(Debugger &debugger) + : ValueObjectListDelegate(), m_debugger(debugger) {} + + ~RegistersWindowDelegate() override = default; + + const char *WindowDelegateGetHelpText() override { + return "Register window keyboard shortcuts:"; + } + + bool WindowDelegateDraw(Window &window, bool force) override { + ExecutionContext exe_ctx( + m_debugger.GetCommandInterpreter().GetExecutionContext()); + StackFrame *frame = exe_ctx.GetFramePtr(); + + ValueObjectList value_list; + if (frame) { + if (frame->GetStackID() != m_stack_id) { + m_stack_id = frame->GetStackID(); + RegisterContextSP reg_ctx(frame->GetRegisterContext()); + if (reg_ctx) { + const uint32_t num_sets = reg_ctx->GetRegisterSetCount(); + for (uint32_t set_idx = 0; set_idx < num_sets; ++set_idx) { + value_list.Append( + ValueObjectRegisterSet::Create(frame, reg_ctx, set_idx)); + } + } + SetValues(value_list); + } + } else { + Process *process = exe_ctx.GetProcessPtr(); + if (process && process->IsAlive()) + return true; // Don't do any updating if we are running + else { + // Update the values with an empty list if there is no process or the + // process isn't alive anymore + SetValues(value_list); + } + } + return ValueObjectListDelegate::WindowDelegateDraw(window, force); + } + +protected: + Debugger &m_debugger; + StackID m_stack_id; +}; + +static const char *CursesKeyToCString(int ch) { + static char g_desc[32]; + if (ch >= KEY_F0 && ch < KEY_F0 + 64) { + snprintf(g_desc, sizeof(g_desc), "F%u", ch - KEY_F0); + return g_desc; + } + switch (ch) { + case KEY_DOWN: + return "down"; + case KEY_UP: + return "up"; + case KEY_LEFT: + return "left"; + case KEY_RIGHT: + return "right"; + case KEY_HOME: + return "home"; + case KEY_BACKSPACE: + return "backspace"; + case KEY_DL: + return "delete-line"; + case KEY_IL: + return "insert-line"; + case KEY_DC: + return "delete-char"; + case KEY_IC: + return "insert-char"; + case KEY_CLEAR: + return "clear"; + case KEY_EOS: + return "clear-to-eos"; + case KEY_EOL: + return "clear-to-eol"; + case KEY_SF: + return "scroll-forward"; + case KEY_SR: + return "scroll-backward"; + case KEY_NPAGE: + return "page-down"; + case KEY_PPAGE: + return "page-up"; + case KEY_STAB: + return "set-tab"; + case KEY_CTAB: + return "clear-tab"; + case KEY_CATAB: + return "clear-all-tabs"; + case KEY_ENTER: + return "enter"; + case KEY_PRINT: + return "print"; + case KEY_LL: + return "lower-left key"; + case KEY_A1: + return "upper left of keypad"; + case KEY_A3: + return "upper right of keypad"; + case KEY_B2: + return "center of keypad"; + case KEY_C1: + return "lower left of keypad"; + case KEY_C3: + return "lower right of keypad"; + case KEY_BTAB: + return "back-tab key"; + case KEY_BEG: + return "begin key"; + case KEY_CANCEL: + return "cancel key"; + case KEY_CLOSE: + return "close key"; + case KEY_COMMAND: + return "command key"; + case KEY_COPY: + return "copy key"; + case KEY_CREATE: + return "create key"; + case KEY_END: + return "end key"; + case KEY_EXIT: + return "exit key"; + case KEY_FIND: + return "find key"; + case KEY_HELP: + return "help key"; + case KEY_MARK: + return "mark key"; + case KEY_MESSAGE: + return "message key"; + case KEY_MOVE: + return "move key"; + case KEY_NEXT: + return "next key"; + case KEY_OPEN: + return "open key"; + case KEY_OPTIONS: + return "options key"; + case KEY_PREVIOUS: + return "previous key"; + case KEY_REDO: + return "redo key"; + case KEY_REFERENCE: + return "reference key"; + case KEY_REFRESH: + return "refresh key"; + case KEY_REPLACE: + return "replace key"; + case KEY_RESTART: + return "restart key"; + case KEY_RESUME: + return "resume key"; + case KEY_SAVE: + return "save key"; + case KEY_SBEG: + return "shifted begin key"; + case KEY_SCANCEL: + return "shifted cancel key"; + case KEY_SCOMMAND: + return "shifted command key"; + case KEY_SCOPY: + return "shifted copy key"; + case KEY_SCREATE: + return "shifted create key"; + case KEY_SDC: + return "shifted delete-character key"; + case KEY_SDL: + return "shifted delete-line key"; + case KEY_SELECT: + return "select key"; + case KEY_SEND: + return "shifted end key"; + case KEY_SEOL: + return "shifted clear-to-end-of-line key"; + case KEY_SEXIT: + return "shifted exit key"; + case KEY_SFIND: + return "shifted find key"; + case KEY_SHELP: + return "shifted help key"; + case KEY_SHOME: + return "shifted home key"; + case KEY_SIC: + return "shifted insert-character key"; + case KEY_SLEFT: + return "shifted left-arrow key"; + case KEY_SMESSAGE: + return "shifted message key"; + case KEY_SMOVE: + return "shifted move key"; + case KEY_SNEXT: + return "shifted next key"; + case KEY_SOPTIONS: + return "shifted options key"; + case KEY_SPREVIOUS: + return "shifted previous key"; + case KEY_SPRINT: + return "shifted print key"; + case KEY_SREDO: + return "shifted redo key"; + case KEY_SREPLACE: + return "shifted replace key"; + case KEY_SRIGHT: + return "shifted right-arrow key"; + case KEY_SRSUME: + return "shifted resume key"; + case KEY_SSAVE: + return "shifted save key"; + case KEY_SSUSPEND: + return "shifted suspend key"; + case KEY_SUNDO: + return "shifted undo key"; + case KEY_SUSPEND: + return "suspend key"; + case KEY_UNDO: + return "undo key"; + case KEY_MOUSE: + return "Mouse event has occurred"; + case KEY_RESIZE: + return "Terminal resize event"; +#ifdef KEY_EVENT + case KEY_EVENT: + return "We were interrupted by an event"; +#endif + case KEY_RETURN: + return "return"; + case ' ': + return "space"; + case '\t': + return "tab"; + case KEY_ESCAPE: + return "escape"; + default: + if (isprint(ch)) + snprintf(g_desc, sizeof(g_desc), "%c", ch); + else + snprintf(g_desc, sizeof(g_desc), "\\x%2.2x", ch); + return g_desc; + } + return nullptr; +} + +HelpDialogDelegate::HelpDialogDelegate(const char *text, + KeyHelp *key_help_array) + : m_text(), m_first_visible_line(0) { + if (text && text[0]) { + m_text.SplitIntoLines(text); + m_text.AppendString(""); + } + if (key_help_array) { + for (KeyHelp *key = key_help_array; key->ch; ++key) { + StreamString key_description; + key_description.Printf("%10s - %s", CursesKeyToCString(key->ch), + key->description); + m_text.AppendString(key_description.GetString()); + } + } +} + +HelpDialogDelegate::~HelpDialogDelegate() = default; + +bool HelpDialogDelegate::WindowDelegateDraw(Window &window, bool force) { + window.Erase(); + const int window_height = window.GetHeight(); + int x = 2; + int y = 1; + const int min_y = y; + const int max_y = window_height - 1 - y; + const size_t num_visible_lines = max_y - min_y + 1; + const size_t num_lines = m_text.GetSize(); + const char *bottom_message; + if (num_lines <= num_visible_lines) + bottom_message = "Press any key to exit"; + else + bottom_message = "Use arrows to scroll, any other key to exit"; + window.DrawTitleBox(window.GetName(), bottom_message); + while (y <= max_y) { + window.MoveCursor(x, y); + window.PutCStringTruncated( + m_text.GetStringAtIndex(m_first_visible_line + y - min_y), 1); + ++y; + } + return true; +} + +HandleCharResult HelpDialogDelegate::WindowDelegateHandleChar(Window &window, + int key) { + bool done = false; + const size_t num_lines = m_text.GetSize(); + const size_t num_visible_lines = window.GetHeight() - 2; + + if (num_lines <= num_visible_lines) { + done = true; + // If we have all lines visible and don't need scrolling, then any key + // press will cause us to exit + } else { + switch (key) { + case KEY_UP: + if (m_first_visible_line > 0) + --m_first_visible_line; + break; + + case KEY_DOWN: + if (m_first_visible_line + num_visible_lines < num_lines) + ++m_first_visible_line; + break; + + case KEY_PPAGE: + case ',': + if (m_first_visible_line > 0) { + if (static_cast<size_t>(m_first_visible_line) >= num_visible_lines) + m_first_visible_line -= num_visible_lines; + else + m_first_visible_line = 0; + } + break; + + case KEY_NPAGE: + case '.': + if (m_first_visible_line + num_visible_lines < num_lines) { + m_first_visible_line += num_visible_lines; + if (static_cast<size_t>(m_first_visible_line) > num_lines) + m_first_visible_line = num_lines - num_visible_lines; + } + break; + + default: + done = true; + break; + } + } + if (done) + window.GetParent()->RemoveSubWindow(&window); + return eKeyHandled; +} + +class ApplicationDelegate : public WindowDelegate, public MenuDelegate { +public: + enum { + eMenuID_LLDB = 1, + eMenuID_LLDBAbout, + eMenuID_LLDBExit, + + eMenuID_Target, + eMenuID_TargetCreate, + eMenuID_TargetDelete, + + eMenuID_Process, + eMenuID_ProcessAttach, + eMenuID_ProcessDetach, + eMenuID_ProcessLaunch, + eMenuID_ProcessContinue, + eMenuID_ProcessHalt, + eMenuID_ProcessKill, + + eMenuID_Thread, + eMenuID_ThreadStepIn, + eMenuID_ThreadStepOver, + eMenuID_ThreadStepOut, + + eMenuID_View, + eMenuID_ViewBacktrace, + eMenuID_ViewRegisters, + eMenuID_ViewSource, + eMenuID_ViewVariables, + + eMenuID_Help, + eMenuID_HelpGUIHelp + }; + + ApplicationDelegate(Application &app, Debugger &debugger) + : WindowDelegate(), MenuDelegate(), m_app(app), m_debugger(debugger) {} + + ~ApplicationDelegate() override = default; + + bool WindowDelegateDraw(Window &window, bool force) override { + return false; // Drawing not handled, let standard window drawing happen + } + + HandleCharResult WindowDelegateHandleChar(Window &window, int key) override { + switch (key) { + case '\t': + window.SelectNextWindowAsActive(); + return eKeyHandled; + + case 'h': + window.CreateHelpSubwindow(); + return eKeyHandled; + + case KEY_ESCAPE: + return eQuitApplication; + + default: + break; + } + return eKeyNotHandled; + } + + const char *WindowDelegateGetHelpText() override { + return "Welcome to the LLDB curses GUI.\n\n" + "Press the TAB key to change the selected view.\n" + "Each view has its own keyboard shortcuts, press 'h' to open a " + "dialog to display them.\n\n" + "Common key bindings for all views:"; + } + + KeyHelp *WindowDelegateGetKeyHelp() override { + static curses::KeyHelp g_source_view_key_help[] = { + {'\t', "Select next view"}, + {'h', "Show help dialog with view specific key bindings"}, + {',', "Page up"}, + {'.', "Page down"}, + {KEY_UP, "Select previous"}, + {KEY_DOWN, "Select next"}, + {KEY_LEFT, "Unexpand or select parent"}, + {KEY_RIGHT, "Expand"}, + {KEY_PPAGE, "Page up"}, + {KEY_NPAGE, "Page down"}, + {'\0', nullptr}}; + return g_source_view_key_help; + } + + MenuActionResult MenuDelegateAction(Menu &menu) override { + switch (menu.GetIdentifier()) { + case eMenuID_ThreadStepIn: { + ExecutionContext exe_ctx = + m_debugger.GetCommandInterpreter().GetExecutionContext(); + if (exe_ctx.HasThreadScope()) { + Process *process = exe_ctx.GetProcessPtr(); + if (process && process->IsAlive() && + StateIsStoppedState(process->GetState(), true)) + exe_ctx.GetThreadRef().StepIn(true); + } + } + return MenuActionResult::Handled; + + case eMenuID_ThreadStepOut: { + ExecutionContext exe_ctx = + m_debugger.GetCommandInterpreter().GetExecutionContext(); + if (exe_ctx.HasThreadScope()) { + Process *process = exe_ctx.GetProcessPtr(); + if (process && process->IsAlive() && + StateIsStoppedState(process->GetState(), true)) + exe_ctx.GetThreadRef().StepOut(); + } + } + return MenuActionResult::Handled; + + case eMenuID_ThreadStepOver: { + ExecutionContext exe_ctx = + m_debugger.GetCommandInterpreter().GetExecutionContext(); + if (exe_ctx.HasThreadScope()) { + Process *process = exe_ctx.GetProcessPtr(); + if (process && process->IsAlive() && + StateIsStoppedState(process->GetState(), true)) + exe_ctx.GetThreadRef().StepOver(true); + } + } + return MenuActionResult::Handled; + + case eMenuID_ProcessContinue: { + ExecutionContext exe_ctx = + m_debugger.GetCommandInterpreter().GetExecutionContext(); + if (exe_ctx.HasProcessScope()) { + Process *process = exe_ctx.GetProcessPtr(); + if (process && process->IsAlive() && + StateIsStoppedState(process->GetState(), true)) + process->Resume(); + } + } + return MenuActionResult::Handled; + + case eMenuID_ProcessKill: { + ExecutionContext exe_ctx = + m_debugger.GetCommandInterpreter().GetExecutionContext(); + if (exe_ctx.HasProcessScope()) { + Process *process = exe_ctx.GetProcessPtr(); + if (process && process->IsAlive()) + process->Destroy(false); + } + } + return MenuActionResult::Handled; + + case eMenuID_ProcessHalt: { + ExecutionContext exe_ctx = + m_debugger.GetCommandInterpreter().GetExecutionContext(); + if (exe_ctx.HasProcessScope()) { + Process *process = exe_ctx.GetProcessPtr(); + if (process && process->IsAlive()) + process->Halt(); + } + } + return MenuActionResult::Handled; + + case eMenuID_ProcessDetach: { + ExecutionContext exe_ctx = + m_debugger.GetCommandInterpreter().GetExecutionContext(); + if (exe_ctx.HasProcessScope()) { + Process *process = exe_ctx.GetProcessPtr(); + if (process && process->IsAlive()) + process->Detach(false); + } + } + return MenuActionResult::Handled; + + case eMenuID_Process: { + // Populate the menu with all of the threads if the process is stopped + // when the Process menu gets selected and is about to display its + // submenu. + Menus &submenus = menu.GetSubmenus(); + ExecutionContext exe_ctx = + m_debugger.GetCommandInterpreter().GetExecutionContext(); + Process *process = exe_ctx.GetProcessPtr(); + if (process && process->IsAlive() && + StateIsStoppedState(process->GetState(), true)) { + if (submenus.size() == 7) + menu.AddSubmenu(MenuSP(new Menu(Menu::Type::Separator))); + else if (submenus.size() > 8) + submenus.erase(submenus.begin() + 8, submenus.end()); + + ThreadList &threads = process->GetThreadList(); + std::lock_guard<std::recursive_mutex> guard(threads.GetMutex()); + size_t num_threads = threads.GetSize(); + for (size_t i = 0; i < num_threads; ++i) { + ThreadSP thread_sp = threads.GetThreadAtIndex(i); + char menu_char = '\0'; + if (i < 9) + menu_char = '1' + i; + StreamString thread_menu_title; + thread_menu_title.Printf("Thread %u", thread_sp->GetIndexID()); + const char *thread_name = thread_sp->GetName(); + if (thread_name && thread_name[0]) + thread_menu_title.Printf(" %s", thread_name); + else { + const char *queue_name = thread_sp->GetQueueName(); + if (queue_name && queue_name[0]) + thread_menu_title.Printf(" %s", queue_name); + } + menu.AddSubmenu( + MenuSP(new Menu(thread_menu_title.GetString().str().c_str(), + nullptr, menu_char, thread_sp->GetID()))); + } + } else if (submenus.size() > 7) { + // Remove the separator and any other thread submenu items that were + // previously added + submenus.erase(submenus.begin() + 7, submenus.end()); + } + // Since we are adding and removing items we need to recalculate the name + // lengths + menu.RecalculateNameLengths(); + } + return MenuActionResult::Handled; + + case eMenuID_ViewVariables: { + WindowSP main_window_sp = m_app.GetMainWindow(); + WindowSP source_window_sp = main_window_sp->FindSubWindow("Source"); + WindowSP variables_window_sp = main_window_sp->FindSubWindow("Variables"); + WindowSP registers_window_sp = main_window_sp->FindSubWindow("Registers"); + const Rect source_bounds = source_window_sp->GetBounds(); + + if (variables_window_sp) { + const Rect variables_bounds = variables_window_sp->GetBounds(); + + main_window_sp->RemoveSubWindow(variables_window_sp.get()); + + if (registers_window_sp) { + // We have a registers window, so give all the area back to the + // registers window + Rect registers_bounds = variables_bounds; + registers_bounds.size.width = source_bounds.size.width; + registers_window_sp->SetBounds(registers_bounds); + } else { + // We have no registers window showing so give the bottom area back + // to the source view + source_window_sp->Resize(source_bounds.size.width, + source_bounds.size.height + + variables_bounds.size.height); + } + } else { + Rect new_variables_rect; + if (registers_window_sp) { + // We have a registers window so split the area of the registers + // window into two columns where the left hand side will be the + // variables and the right hand side will be the registers + const Rect variables_bounds = registers_window_sp->GetBounds(); + Rect new_registers_rect; + variables_bounds.VerticalSplitPercentage(0.50, new_variables_rect, + new_registers_rect); + registers_window_sp->SetBounds(new_registers_rect); + } else { + // No variables window, grab the bottom part of the source window + Rect new_source_rect; + source_bounds.HorizontalSplitPercentage(0.70, new_source_rect, + new_variables_rect); + source_window_sp->SetBounds(new_source_rect); + } + WindowSP new_window_sp = main_window_sp->CreateSubWindow( + "Variables", new_variables_rect, false); + new_window_sp->SetDelegate( + WindowDelegateSP(new FrameVariablesWindowDelegate(m_debugger))); + } + touchwin(stdscr); + } + return MenuActionResult::Handled; + + case eMenuID_ViewRegisters: { + WindowSP main_window_sp = m_app.GetMainWindow(); + WindowSP source_window_sp = main_window_sp->FindSubWindow("Source"); + WindowSP variables_window_sp = main_window_sp->FindSubWindow("Variables"); + WindowSP registers_window_sp = main_window_sp->FindSubWindow("Registers"); + const Rect source_bounds = source_window_sp->GetBounds(); + + if (registers_window_sp) { + if (variables_window_sp) { + const Rect variables_bounds = variables_window_sp->GetBounds(); + + // We have a variables window, so give all the area back to the + // variables window + variables_window_sp->Resize(variables_bounds.size.width + + registers_window_sp->GetWidth(), + variables_bounds.size.height); + } else { + // We have no variables window showing so give the bottom area back + // to the source view + source_window_sp->Resize(source_bounds.size.width, + source_bounds.size.height + + registers_window_sp->GetHeight()); + } + main_window_sp->RemoveSubWindow(registers_window_sp.get()); + } else { + Rect new_regs_rect; + if (variables_window_sp) { + // We have a variables window, split it into two columns where the + // left hand side will be the variables and the right hand side will + // be the registers + const Rect variables_bounds = variables_window_sp->GetBounds(); + Rect new_vars_rect; + variables_bounds.VerticalSplitPercentage(0.50, new_vars_rect, + new_regs_rect); + variables_window_sp->SetBounds(new_vars_rect); + } else { + // No registers window, grab the bottom part of the source window + Rect new_source_rect; + source_bounds.HorizontalSplitPercentage(0.70, new_source_rect, + new_regs_rect); + source_window_sp->SetBounds(new_source_rect); + } + WindowSP new_window_sp = + main_window_sp->CreateSubWindow("Registers", new_regs_rect, false); + new_window_sp->SetDelegate( + WindowDelegateSP(new RegistersWindowDelegate(m_debugger))); + } + touchwin(stdscr); + } + return MenuActionResult::Handled; + + case eMenuID_HelpGUIHelp: + m_app.GetMainWindow()->CreateHelpSubwindow(); + return MenuActionResult::Handled; + + default: + break; + } + + return MenuActionResult::NotHandled; + } + +protected: + Application &m_app; + Debugger &m_debugger; +}; + +class StatusBarWindowDelegate : public WindowDelegate { +public: + StatusBarWindowDelegate(Debugger &debugger) : m_debugger(debugger) { + FormatEntity::Parse("Thread: ${thread.id%tid}", m_format); + } + + ~StatusBarWindowDelegate() override = default; + + bool WindowDelegateDraw(Window &window, bool force) override { + ExecutionContext exe_ctx = + m_debugger.GetCommandInterpreter().GetExecutionContext(); + Process *process = exe_ctx.GetProcessPtr(); + Thread *thread = exe_ctx.GetThreadPtr(); + StackFrame *frame = exe_ctx.GetFramePtr(); + window.Erase(); + window.SetBackground(2); + window.MoveCursor(0, 0); + if (process) { + const StateType state = process->GetState(); + window.Printf("Process: %5" PRIu64 " %10s", process->GetID(), + StateAsCString(state)); + + if (StateIsStoppedState(state, true)) { + StreamString strm; + if (thread && FormatEntity::Format(m_format, strm, nullptr, &exe_ctx, + nullptr, nullptr, false, false)) { + window.MoveCursor(40, 0); + window.PutCStringTruncated(strm.GetString().str().c_str(), 1); + } + + window.MoveCursor(60, 0); + if (frame) + window.Printf("Frame: %3u PC = 0x%16.16" PRIx64, + frame->GetFrameIndex(), + frame->GetFrameCodeAddress().GetOpcodeLoadAddress( + exe_ctx.GetTargetPtr())); + } else if (state == eStateExited) { + const char *exit_desc = process->GetExitDescription(); + const int exit_status = process->GetExitStatus(); + if (exit_desc && exit_desc[0]) + window.Printf(" with status = %i (%s)", exit_status, exit_desc); + else + window.Printf(" with status = %i", exit_status); + } + } + return true; + } + +protected: + Debugger &m_debugger; + FormatEntity::Entry m_format; +}; + +class SourceFileWindowDelegate : public WindowDelegate { +public: + SourceFileWindowDelegate(Debugger &debugger) + : WindowDelegate(), m_debugger(debugger), m_sc(), m_file_sp(), + m_disassembly_scope(nullptr), m_disassembly_sp(), m_disassembly_range(), + m_title(), m_line_width(4), m_selected_line(0), m_pc_line(0), + m_stop_id(0), m_frame_idx(UINT32_MAX), m_first_visible_line(0), + m_min_x(0), m_min_y(0), m_max_x(0), m_max_y(0) {} + + ~SourceFileWindowDelegate() override = default; + + void Update(const SymbolContext &sc) { m_sc = sc; } + + uint32_t NumVisibleLines() const { return m_max_y - m_min_y; } + + const char *WindowDelegateGetHelpText() override { + return "Source/Disassembly window keyboard shortcuts:"; + } + + KeyHelp *WindowDelegateGetKeyHelp() override { + static curses::KeyHelp g_source_view_key_help[] = { + {KEY_RETURN, "Run to selected line with one shot breakpoint"}, + {KEY_UP, "Select previous source line"}, + {KEY_DOWN, "Select next source line"}, + {KEY_PPAGE, "Page up"}, + {KEY_NPAGE, "Page down"}, + {'b', "Set breakpoint on selected source/disassembly line"}, + {'c', "Continue process"}, + {'d', "Detach and resume process"}, + {'D', "Detach with process suspended"}, + {'h', "Show help dialog"}, + {'k', "Kill process"}, + {'n', "Step over (source line)"}, + {'N', "Step over (single instruction)"}, + {'o', "Step out"}, + {'s', "Step in (source line)"}, + {'S', "Step in (single instruction)"}, + {',', "Page up"}, + {'.', "Page down"}, + {'\0', nullptr}}; + return g_source_view_key_help; + } + + bool WindowDelegateDraw(Window &window, bool force) override { + ExecutionContext exe_ctx = + m_debugger.GetCommandInterpreter().GetExecutionContext(); + Process *process = exe_ctx.GetProcessPtr(); + Thread *thread = nullptr; + + bool update_location = false; + if (process) { + StateType state = process->GetState(); + if (StateIsStoppedState(state, true)) { + // We are stopped, so it is ok to + update_location = true; + } + } + + m_min_x = 1; + m_min_y = 2; + m_max_x = window.GetMaxX() - 1; + m_max_y = window.GetMaxY() - 1; + + const uint32_t num_visible_lines = NumVisibleLines(); + StackFrameSP frame_sp; + bool set_selected_line_to_pc = false; + + if (update_location) { + const bool process_alive = process ? process->IsAlive() : false; + bool thread_changed = false; + if (process_alive) { + thread = exe_ctx.GetThreadPtr(); + if (thread) { + frame_sp = thread->GetSelectedFrame(); + auto tid = thread->GetID(); + thread_changed = tid != m_tid; + m_tid = tid; + } else { + if (m_tid != LLDB_INVALID_THREAD_ID) { + thread_changed = true; + m_tid = LLDB_INVALID_THREAD_ID; + } + } + } + const uint32_t stop_id = process ? process->GetStopID() : 0; + const bool stop_id_changed = stop_id != m_stop_id; + bool frame_changed = false; + m_stop_id = stop_id; + m_title.Clear(); + if (frame_sp) { + m_sc = frame_sp->GetSymbolContext(eSymbolContextEverything); + if (m_sc.module_sp) { + m_title.Printf( + "%s", m_sc.module_sp->GetFileSpec().GetFilename().GetCString()); + ConstString func_name = m_sc.GetFunctionName(); + if (func_name) + m_title.Printf("`%s", func_name.GetCString()); + } + const uint32_t frame_idx = frame_sp->GetFrameIndex(); + frame_changed = frame_idx != m_frame_idx; + m_frame_idx = frame_idx; + } else { + m_sc.Clear(true); + frame_changed = m_frame_idx != UINT32_MAX; + m_frame_idx = UINT32_MAX; + } + + const bool context_changed = + thread_changed || frame_changed || stop_id_changed; + + if (process_alive) { + if (m_sc.line_entry.IsValid()) { + m_pc_line = m_sc.line_entry.line; + if (m_pc_line != UINT32_MAX) + --m_pc_line; // Convert to zero based line number... + // Update the selected line if the stop ID changed... + if (context_changed) + m_selected_line = m_pc_line; + + if (m_file_sp && m_file_sp->GetFileSpec() == m_sc.line_entry.file) { + // Same file, nothing to do, we should either have the lines or not + // (source file missing) + if (m_selected_line >= static_cast<size_t>(m_first_visible_line)) { + if (m_selected_line >= m_first_visible_line + num_visible_lines) + m_first_visible_line = m_selected_line - 10; + } else { + if (m_selected_line > 10) + m_first_visible_line = m_selected_line - 10; + else + m_first_visible_line = 0; + } + } else { + // File changed, set selected line to the line with the PC + m_selected_line = m_pc_line; + m_file_sp = + m_debugger.GetSourceManager().GetFile(m_sc.line_entry.file); + if (m_file_sp) { + const size_t num_lines = m_file_sp->GetNumLines(); + m_line_width = 1; + for (size_t n = num_lines; n >= 10; n = n / 10) + ++m_line_width; + + if (num_lines < num_visible_lines || + m_selected_line < num_visible_lines) + m_first_visible_line = 0; + else + m_first_visible_line = m_selected_line - 10; + } + } + } else { + m_file_sp.reset(); + } + + if (!m_file_sp || m_file_sp->GetNumLines() == 0) { + // Show disassembly + bool prefer_file_cache = false; + if (m_sc.function) { + if (m_disassembly_scope != m_sc.function) { + m_disassembly_scope = m_sc.function; + m_disassembly_sp = m_sc.function->GetInstructions( + exe_ctx, nullptr, prefer_file_cache); + if (m_disassembly_sp) { + set_selected_line_to_pc = true; + m_disassembly_range = m_sc.function->GetAddressRange(); + } else { + m_disassembly_range.Clear(); + } + } else { + set_selected_line_to_pc = context_changed; + } + } else if (m_sc.symbol) { + if (m_disassembly_scope != m_sc.symbol) { + m_disassembly_scope = m_sc.symbol; + m_disassembly_sp = m_sc.symbol->GetInstructions( + exe_ctx, nullptr, prefer_file_cache); + if (m_disassembly_sp) { + set_selected_line_to_pc = true; + m_disassembly_range.GetBaseAddress() = + m_sc.symbol->GetAddress(); + m_disassembly_range.SetByteSize(m_sc.symbol->GetByteSize()); + } else { + m_disassembly_range.Clear(); + } + } else { + set_selected_line_to_pc = context_changed; + } + } + } + } else { + m_pc_line = UINT32_MAX; + } + } + + const int window_width = window.GetWidth(); + window.Erase(); + window.DrawTitleBox("Sources"); + if (!m_title.GetString().empty()) { + window.AttributeOn(A_REVERSE); + window.MoveCursor(1, 1); + window.PutChar(' '); + window.PutCStringTruncated(m_title.GetString().str().c_str(), 1); + int x = window.GetCursorX(); + if (x < window_width - 1) { + window.Printf("%*s", window_width - x - 1, ""); + } + window.AttributeOff(A_REVERSE); + } + + Target *target = exe_ctx.GetTargetPtr(); + const size_t num_source_lines = GetNumSourceLines(); + if (num_source_lines > 0) { + // Display source + BreakpointLines bp_lines; + if (target) { + BreakpointList &bp_list = target->GetBreakpointList(); + const size_t num_bps = bp_list.GetSize(); + for (size_t bp_idx = 0; bp_idx < num_bps; ++bp_idx) { + BreakpointSP bp_sp = bp_list.GetBreakpointAtIndex(bp_idx); + const size_t num_bps_locs = bp_sp->GetNumLocations(); + for (size_t bp_loc_idx = 0; bp_loc_idx < num_bps_locs; ++bp_loc_idx) { + BreakpointLocationSP bp_loc_sp = + bp_sp->GetLocationAtIndex(bp_loc_idx); + LineEntry bp_loc_line_entry; + if (bp_loc_sp->GetAddress().CalculateSymbolContextLineEntry( + bp_loc_line_entry)) { + if (m_file_sp->GetFileSpec() == bp_loc_line_entry.file) { + bp_lines.insert(bp_loc_line_entry.line); + } + } + } + } + } + + const attr_t selected_highlight_attr = A_REVERSE; + const attr_t pc_highlight_attr = COLOR_PAIR(1); + + for (size_t i = 0; i < num_visible_lines; ++i) { + const uint32_t curr_line = m_first_visible_line + i; + if (curr_line < num_source_lines) { + const int line_y = m_min_y + i; + window.MoveCursor(1, line_y); + const bool is_pc_line = curr_line == m_pc_line; + const bool line_is_selected = m_selected_line == curr_line; + // Highlight the line as the PC line first, then if the selected line + // isn't the same as the PC line, highlight it differently + attr_t highlight_attr = 0; + attr_t bp_attr = 0; + if (is_pc_line) + highlight_attr = pc_highlight_attr; + else if (line_is_selected) + highlight_attr = selected_highlight_attr; + + if (bp_lines.find(curr_line + 1) != bp_lines.end()) + bp_attr = COLOR_PAIR(2); + + if (bp_attr) + window.AttributeOn(bp_attr); + + window.Printf(" %*u ", m_line_width, curr_line + 1); + + if (bp_attr) + window.AttributeOff(bp_attr); + + window.PutChar(ACS_VLINE); + // Mark the line with the PC with a diamond + if (is_pc_line) + window.PutChar(ACS_DIAMOND); + else + window.PutChar(' '); + + if (highlight_attr) + window.AttributeOn(highlight_attr); + const uint32_t line_len = + m_file_sp->GetLineLength(curr_line + 1, false); + if (line_len > 0) + window.PutCString(m_file_sp->PeekLineData(curr_line + 1), line_len); + + if (is_pc_line && frame_sp && + frame_sp->GetConcreteFrameIndex() == 0) { + StopInfoSP stop_info_sp; + if (thread) + stop_info_sp = thread->GetStopInfo(); + if (stop_info_sp) { + const char *stop_description = stop_info_sp->GetDescription(); + if (stop_description && stop_description[0]) { + size_t stop_description_len = strlen(stop_description); + int desc_x = window_width - stop_description_len - 16; + window.Printf("%*s", desc_x - window.GetCursorX(), ""); + // window.MoveCursor(window_width - stop_description_len - 15, + // line_y); + window.Printf("<<< Thread %u: %s ", thread->GetIndexID(), + stop_description); + } + } else { + window.Printf("%*s", window_width - window.GetCursorX() - 1, ""); + } + } + if (highlight_attr) + window.AttributeOff(highlight_attr); + } else { + break; + } + } + } else { + size_t num_disassembly_lines = GetNumDisassemblyLines(); + if (num_disassembly_lines > 0) { + // Display disassembly + BreakpointAddrs bp_file_addrs; + Target *target = exe_ctx.GetTargetPtr(); + if (target) { + BreakpointList &bp_list = target->GetBreakpointList(); + const size_t num_bps = bp_list.GetSize(); + for (size_t bp_idx = 0; bp_idx < num_bps; ++bp_idx) { + BreakpointSP bp_sp = bp_list.GetBreakpointAtIndex(bp_idx); + const size_t num_bps_locs = bp_sp->GetNumLocations(); + for (size_t bp_loc_idx = 0; bp_loc_idx < num_bps_locs; + ++bp_loc_idx) { + BreakpointLocationSP bp_loc_sp = + bp_sp->GetLocationAtIndex(bp_loc_idx); + LineEntry bp_loc_line_entry; + const lldb::addr_t file_addr = + bp_loc_sp->GetAddress().GetFileAddress(); + if (file_addr != LLDB_INVALID_ADDRESS) { + if (m_disassembly_range.ContainsFileAddress(file_addr)) + bp_file_addrs.insert(file_addr); + } + } + } + } + + const attr_t selected_highlight_attr = A_REVERSE; + const attr_t pc_highlight_attr = COLOR_PAIR(1); + + StreamString strm; + + InstructionList &insts = m_disassembly_sp->GetInstructionList(); + Address pc_address; + + if (frame_sp) + pc_address = frame_sp->GetFrameCodeAddress(); + const uint32_t pc_idx = + pc_address.IsValid() + ? insts.GetIndexOfInstructionAtAddress(pc_address) + : UINT32_MAX; + if (set_selected_line_to_pc) { + m_selected_line = pc_idx; + } + + const uint32_t non_visible_pc_offset = (num_visible_lines / 5); + if (static_cast<size_t>(m_first_visible_line) >= num_disassembly_lines) + m_first_visible_line = 0; + + if (pc_idx < num_disassembly_lines) { + if (pc_idx < static_cast<uint32_t>(m_first_visible_line) || + pc_idx >= m_first_visible_line + num_visible_lines) + m_first_visible_line = pc_idx - non_visible_pc_offset; + } + + for (size_t i = 0; i < num_visible_lines; ++i) { + const uint32_t inst_idx = m_first_visible_line + i; + Instruction *inst = insts.GetInstructionAtIndex(inst_idx).get(); + if (!inst) + break; + + const int line_y = m_min_y + i; + window.MoveCursor(1, line_y); + const bool is_pc_line = frame_sp && inst_idx == pc_idx; + const bool line_is_selected = m_selected_line == inst_idx; + // Highlight the line as the PC line first, then if the selected line + // isn't the same as the PC line, highlight it differently + attr_t highlight_attr = 0; + attr_t bp_attr = 0; + if (is_pc_line) + highlight_attr = pc_highlight_attr; + else if (line_is_selected) + highlight_attr = selected_highlight_attr; + + if (bp_file_addrs.find(inst->GetAddress().GetFileAddress()) != + bp_file_addrs.end()) + bp_attr = COLOR_PAIR(2); + + if (bp_attr) + window.AttributeOn(bp_attr); + + window.Printf(" 0x%16.16llx ", + static_cast<unsigned long long>( + inst->GetAddress().GetLoadAddress(target))); + + if (bp_attr) + window.AttributeOff(bp_attr); + + window.PutChar(ACS_VLINE); + // Mark the line with the PC with a diamond + if (is_pc_line) + window.PutChar(ACS_DIAMOND); + else + window.PutChar(' '); + + if (highlight_attr) + window.AttributeOn(highlight_attr); + + const char *mnemonic = inst->GetMnemonic(&exe_ctx); + const char *operands = inst->GetOperands(&exe_ctx); + const char *comment = inst->GetComment(&exe_ctx); + + if (mnemonic != nullptr && mnemonic[0] == '\0') + mnemonic = nullptr; + if (operands != nullptr && operands[0] == '\0') + operands = nullptr; + if (comment != nullptr && comment[0] == '\0') + comment = nullptr; + + strm.Clear(); + + if (mnemonic != nullptr && operands != nullptr && comment != nullptr) + strm.Printf("%-8s %-25s ; %s", mnemonic, operands, comment); + else if (mnemonic != nullptr && operands != nullptr) + strm.Printf("%-8s %s", mnemonic, operands); + else if (mnemonic != nullptr) + strm.Printf("%s", mnemonic); + + int right_pad = 1; + window.PutCStringTruncated(strm.GetData(), right_pad); + + if (is_pc_line && frame_sp && + frame_sp->GetConcreteFrameIndex() == 0) { + StopInfoSP stop_info_sp; + if (thread) + stop_info_sp = thread->GetStopInfo(); + if (stop_info_sp) { + const char *stop_description = stop_info_sp->GetDescription(); + if (stop_description && stop_description[0]) { + size_t stop_description_len = strlen(stop_description); + int desc_x = window_width - stop_description_len - 16; + window.Printf("%*s", desc_x - window.GetCursorX(), ""); + // window.MoveCursor(window_width - stop_description_len - 15, + // line_y); + window.Printf("<<< Thread %u: %s ", thread->GetIndexID(), + stop_description); + } + } else { + window.Printf("%*s", window_width - window.GetCursorX() - 1, ""); + } + } + if (highlight_attr) + window.AttributeOff(highlight_attr); + } + } + } + return true; // Drawing handled + } + + size_t GetNumLines() { + size_t num_lines = GetNumSourceLines(); + if (num_lines == 0) + num_lines = GetNumDisassemblyLines(); + return num_lines; + } + + size_t GetNumSourceLines() const { + if (m_file_sp) + return m_file_sp->GetNumLines(); + return 0; + } + + size_t GetNumDisassemblyLines() const { + if (m_disassembly_sp) + return m_disassembly_sp->GetInstructionList().GetSize(); + return 0; + } + + HandleCharResult WindowDelegateHandleChar(Window &window, int c) override { + const uint32_t num_visible_lines = NumVisibleLines(); + const size_t num_lines = GetNumLines(); + + switch (c) { + case ',': + case KEY_PPAGE: + // Page up key + if (static_cast<uint32_t>(m_first_visible_line) > num_visible_lines) + m_first_visible_line -= num_visible_lines; + else + m_first_visible_line = 0; + m_selected_line = m_first_visible_line; + return eKeyHandled; + + case '.': + case KEY_NPAGE: + // Page down key + { + if (m_first_visible_line + num_visible_lines < num_lines) + m_first_visible_line += num_visible_lines; + else if (num_lines < num_visible_lines) + m_first_visible_line = 0; + else + m_first_visible_line = num_lines - num_visible_lines; + m_selected_line = m_first_visible_line; + } + return eKeyHandled; + + case KEY_UP: + if (m_selected_line > 0) { + m_selected_line--; + if (static_cast<size_t>(m_first_visible_line) > m_selected_line) + m_first_visible_line = m_selected_line; + } + return eKeyHandled; + + case KEY_DOWN: + if (m_selected_line + 1 < num_lines) { + m_selected_line++; + if (m_first_visible_line + num_visible_lines < m_selected_line) + m_first_visible_line++; + } + return eKeyHandled; + + case '\r': + case '\n': + case KEY_ENTER: + // Set a breakpoint and run to the line using a one shot breakpoint + if (GetNumSourceLines() > 0) { + ExecutionContext exe_ctx = + m_debugger.GetCommandInterpreter().GetExecutionContext(); + if (exe_ctx.HasProcessScope() && exe_ctx.GetProcessRef().IsAlive()) { + BreakpointSP bp_sp = exe_ctx.GetTargetRef().CreateBreakpoint( + nullptr, // Don't limit the breakpoint to certain modules + m_file_sp->GetFileSpec(), // Source file + m_selected_line + + 1, // Source line number (m_selected_line is zero based) + 0, // Unspecified column. + 0, // No offset + eLazyBoolCalculate, // Check inlines using global setting + eLazyBoolCalculate, // Skip prologue using global setting, + false, // internal + false, // request_hardware + eLazyBoolCalculate); // move_to_nearest_code + // Make breakpoint one shot + bp_sp->GetOptions()->SetOneShot(true); + exe_ctx.GetProcessRef().Resume(); + } + } else if (m_selected_line < GetNumDisassemblyLines()) { + const Instruction *inst = m_disassembly_sp->GetInstructionList() + .GetInstructionAtIndex(m_selected_line) + .get(); + ExecutionContext exe_ctx = + m_debugger.GetCommandInterpreter().GetExecutionContext(); + if (exe_ctx.HasTargetScope()) { + Address addr = inst->GetAddress(); + BreakpointSP bp_sp = exe_ctx.GetTargetRef().CreateBreakpoint( + addr, // lldb_private::Address + false, // internal + false); // request_hardware + // Make breakpoint one shot + bp_sp->GetOptions()->SetOneShot(true); + exe_ctx.GetProcessRef().Resume(); + } + } + return eKeyHandled; + + case 'b': // 'b' == toggle breakpoint on currently selected line + if (m_selected_line < GetNumSourceLines()) { + ExecutionContext exe_ctx = + m_debugger.GetCommandInterpreter().GetExecutionContext(); + if (exe_ctx.HasTargetScope()) { + BreakpointSP bp_sp = exe_ctx.GetTargetRef().CreateBreakpoint( + nullptr, // Don't limit the breakpoint to certain modules + m_file_sp->GetFileSpec(), // Source file + m_selected_line + + 1, // Source line number (m_selected_line is zero based) + 0, // No column specified. + 0, // No offset + eLazyBoolCalculate, // Check inlines using global setting + eLazyBoolCalculate, // Skip prologue using global setting, + false, // internal + false, // request_hardware + eLazyBoolCalculate); // move_to_nearest_code + } + } else if (m_selected_line < GetNumDisassemblyLines()) { + const Instruction *inst = m_disassembly_sp->GetInstructionList() + .GetInstructionAtIndex(m_selected_line) + .get(); + ExecutionContext exe_ctx = + m_debugger.GetCommandInterpreter().GetExecutionContext(); + if (exe_ctx.HasTargetScope()) { + Address addr = inst->GetAddress(); + BreakpointSP bp_sp = exe_ctx.GetTargetRef().CreateBreakpoint( + addr, // lldb_private::Address + false, // internal + false); // request_hardware + } + } + return eKeyHandled; + + case 'd': // 'd' == detach and let run + case 'D': // 'D' == detach and keep stopped + { + ExecutionContext exe_ctx = + m_debugger.GetCommandInterpreter().GetExecutionContext(); + if (exe_ctx.HasProcessScope()) + exe_ctx.GetProcessRef().Detach(c == 'D'); + } + return eKeyHandled; + + case 'k': + // 'k' == kill + { + ExecutionContext exe_ctx = + m_debugger.GetCommandInterpreter().GetExecutionContext(); + if (exe_ctx.HasProcessScope()) + exe_ctx.GetProcessRef().Destroy(false); + } + return eKeyHandled; + + case 'c': + // 'c' == continue + { + ExecutionContext exe_ctx = + m_debugger.GetCommandInterpreter().GetExecutionContext(); + if (exe_ctx.HasProcessScope()) + exe_ctx.GetProcessRef().Resume(); + } + return eKeyHandled; + + case 'o': + // 'o' == step out + { + ExecutionContext exe_ctx = + m_debugger.GetCommandInterpreter().GetExecutionContext(); + if (exe_ctx.HasThreadScope() && + StateIsStoppedState(exe_ctx.GetProcessRef().GetState(), true)) { + exe_ctx.GetThreadRef().StepOut(); + } + } + return eKeyHandled; + + case 'n': // 'n' == step over + case 'N': // 'N' == step over instruction + { + ExecutionContext exe_ctx = + m_debugger.GetCommandInterpreter().GetExecutionContext(); + if (exe_ctx.HasThreadScope() && + StateIsStoppedState(exe_ctx.GetProcessRef().GetState(), true)) { + bool source_step = (c == 'n'); + exe_ctx.GetThreadRef().StepOver(source_step); + } + } + return eKeyHandled; + + case 's': // 's' == step into + case 'S': // 'S' == step into instruction + { + ExecutionContext exe_ctx = + m_debugger.GetCommandInterpreter().GetExecutionContext(); + if (exe_ctx.HasThreadScope() && + StateIsStoppedState(exe_ctx.GetProcessRef().GetState(), true)) { + bool source_step = (c == 's'); + exe_ctx.GetThreadRef().StepIn(source_step); + } + } + return eKeyHandled; + + case 'h': + window.CreateHelpSubwindow(); + return eKeyHandled; + + default: + break; + } + return eKeyNotHandled; + } + +protected: + typedef std::set<uint32_t> BreakpointLines; + typedef std::set<lldb::addr_t> BreakpointAddrs; + + Debugger &m_debugger; + SymbolContext m_sc; + SourceManager::FileSP m_file_sp; + SymbolContextScope *m_disassembly_scope; + lldb::DisassemblerSP m_disassembly_sp; + AddressRange m_disassembly_range; + StreamString m_title; + lldb::user_id_t m_tid; + int m_line_width; + uint32_t m_selected_line; // The selected line + uint32_t m_pc_line; // The line with the PC + uint32_t m_stop_id; + uint32_t m_frame_idx; + int m_first_visible_line; + int m_min_x; + int m_min_y; + int m_max_x; + int m_max_y; +}; + +DisplayOptions ValueObjectListDelegate::g_options = {true}; + +IOHandlerCursesGUI::IOHandlerCursesGUI(Debugger &debugger) + : IOHandler(debugger, IOHandler::Type::Curses) {} + +void IOHandlerCursesGUI::Activate() { + IOHandler::Activate(); + if (!m_app_ap) { + m_app_ap.reset(new Application(GetInputFILE(), GetOutputFILE())); + + // This is both a window and a menu delegate + std::shared_ptr<ApplicationDelegate> app_delegate_sp( + new ApplicationDelegate(*m_app_ap, m_debugger)); + + MenuDelegateSP app_menu_delegate_sp = + std::static_pointer_cast<MenuDelegate>(app_delegate_sp); + MenuSP lldb_menu_sp( + new Menu("LLDB", "F1", KEY_F(1), ApplicationDelegate::eMenuID_LLDB)); + MenuSP exit_menuitem_sp( + new Menu("Exit", nullptr, 'x', ApplicationDelegate::eMenuID_LLDBExit)); + exit_menuitem_sp->SetCannedResult(MenuActionResult::Quit); + lldb_menu_sp->AddSubmenu(MenuSP(new Menu( + "About LLDB", nullptr, 'a', ApplicationDelegate::eMenuID_LLDBAbout))); + lldb_menu_sp->AddSubmenu(MenuSP(new Menu(Menu::Type::Separator))); + lldb_menu_sp->AddSubmenu(exit_menuitem_sp); + + MenuSP target_menu_sp(new Menu("Target", "F2", KEY_F(2), + ApplicationDelegate::eMenuID_Target)); + target_menu_sp->AddSubmenu(MenuSP(new Menu( + "Create", nullptr, 'c', ApplicationDelegate::eMenuID_TargetCreate))); + target_menu_sp->AddSubmenu(MenuSP(new Menu( + "Delete", nullptr, 'd', ApplicationDelegate::eMenuID_TargetDelete))); + + MenuSP process_menu_sp(new Menu("Process", "F3", KEY_F(3), + ApplicationDelegate::eMenuID_Process)); + process_menu_sp->AddSubmenu(MenuSP(new Menu( + "Attach", nullptr, 'a', ApplicationDelegate::eMenuID_ProcessAttach))); + process_menu_sp->AddSubmenu(MenuSP(new Menu( + "Detach", nullptr, 'd', ApplicationDelegate::eMenuID_ProcessDetach))); + process_menu_sp->AddSubmenu(MenuSP(new Menu( + "Launch", nullptr, 'l', ApplicationDelegate::eMenuID_ProcessLaunch))); + process_menu_sp->AddSubmenu(MenuSP(new Menu(Menu::Type::Separator))); + process_menu_sp->AddSubmenu( + MenuSP(new Menu("Continue", nullptr, 'c', + ApplicationDelegate::eMenuID_ProcessContinue))); + process_menu_sp->AddSubmenu(MenuSP(new Menu( + "Halt", nullptr, 'h', ApplicationDelegate::eMenuID_ProcessHalt))); + process_menu_sp->AddSubmenu(MenuSP(new Menu( + "Kill", nullptr, 'k', ApplicationDelegate::eMenuID_ProcessKill))); + + MenuSP thread_menu_sp(new Menu("Thread", "F4", KEY_F(4), + ApplicationDelegate::eMenuID_Thread)); + thread_menu_sp->AddSubmenu(MenuSP(new Menu( + "Step In", nullptr, 'i', ApplicationDelegate::eMenuID_ThreadStepIn))); + thread_menu_sp->AddSubmenu( + MenuSP(new Menu("Step Over", nullptr, 'v', + ApplicationDelegate::eMenuID_ThreadStepOver))); + thread_menu_sp->AddSubmenu(MenuSP(new Menu( + "Step Out", nullptr, 'o', ApplicationDelegate::eMenuID_ThreadStepOut))); + + MenuSP view_menu_sp( + new Menu("View", "F5", KEY_F(5), ApplicationDelegate::eMenuID_View)); + view_menu_sp->AddSubmenu( + MenuSP(new Menu("Backtrace", nullptr, 'b', + ApplicationDelegate::eMenuID_ViewBacktrace))); + view_menu_sp->AddSubmenu( + MenuSP(new Menu("Registers", nullptr, 'r', + ApplicationDelegate::eMenuID_ViewRegisters))); + view_menu_sp->AddSubmenu(MenuSP(new Menu( + "Source", nullptr, 's', ApplicationDelegate::eMenuID_ViewSource))); + view_menu_sp->AddSubmenu( + MenuSP(new Menu("Variables", nullptr, 'v', + ApplicationDelegate::eMenuID_ViewVariables))); + + MenuSP help_menu_sp( + new Menu("Help", "F6", KEY_F(6), ApplicationDelegate::eMenuID_Help)); + help_menu_sp->AddSubmenu(MenuSP(new Menu( + "GUI Help", nullptr, 'g', ApplicationDelegate::eMenuID_HelpGUIHelp))); + + m_app_ap->Initialize(); + WindowSP &main_window_sp = m_app_ap->GetMainWindow(); + + MenuSP menubar_sp(new Menu(Menu::Type::Bar)); + menubar_sp->AddSubmenu(lldb_menu_sp); + menubar_sp->AddSubmenu(target_menu_sp); + menubar_sp->AddSubmenu(process_menu_sp); + menubar_sp->AddSubmenu(thread_menu_sp); + menubar_sp->AddSubmenu(view_menu_sp); + menubar_sp->AddSubmenu(help_menu_sp); + menubar_sp->SetDelegate(app_menu_delegate_sp); + + Rect content_bounds = main_window_sp->GetFrame(); + Rect menubar_bounds = content_bounds.MakeMenuBar(); + Rect status_bounds = content_bounds.MakeStatusBar(); + Rect source_bounds; + Rect variables_bounds; + Rect threads_bounds; + Rect source_variables_bounds; + content_bounds.VerticalSplitPercentage(0.80, source_variables_bounds, + threads_bounds); + source_variables_bounds.HorizontalSplitPercentage(0.70, source_bounds, + variables_bounds); + + WindowSP menubar_window_sp = + main_window_sp->CreateSubWindow("Menubar", menubar_bounds, false); + // Let the menubar get keys if the active window doesn't handle the keys + // that are typed so it can respond to menubar key presses. + menubar_window_sp->SetCanBeActive( + false); // Don't let the menubar become the active window + menubar_window_sp->SetDelegate(menubar_sp); + + WindowSP source_window_sp( + main_window_sp->CreateSubWindow("Source", source_bounds, true)); + WindowSP variables_window_sp( + main_window_sp->CreateSubWindow("Variables", variables_bounds, false)); + WindowSP threads_window_sp( + main_window_sp->CreateSubWindow("Threads", threads_bounds, false)); + WindowSP status_window_sp( + main_window_sp->CreateSubWindow("Status", status_bounds, false)); + status_window_sp->SetCanBeActive( + false); // Don't let the status bar become the active window + main_window_sp->SetDelegate( + std::static_pointer_cast<WindowDelegate>(app_delegate_sp)); + source_window_sp->SetDelegate( + WindowDelegateSP(new SourceFileWindowDelegate(m_debugger))); + variables_window_sp->SetDelegate( + WindowDelegateSP(new FrameVariablesWindowDelegate(m_debugger))); + TreeDelegateSP thread_delegate_sp(new ThreadsTreeDelegate(m_debugger)); + threads_window_sp->SetDelegate(WindowDelegateSP( + new TreeWindowDelegate(m_debugger, thread_delegate_sp))); + status_window_sp->SetDelegate( + WindowDelegateSP(new StatusBarWindowDelegate(m_debugger))); + + // Show the main help window once the first time the curses GUI is launched + static bool g_showed_help = false; + if (!g_showed_help) { + g_showed_help = true; + main_window_sp->CreateHelpSubwindow(); + } + + init_pair(1, COLOR_WHITE, COLOR_BLUE); + init_pair(2, COLOR_BLACK, COLOR_WHITE); + init_pair(3, COLOR_MAGENTA, COLOR_WHITE); + init_pair(4, COLOR_MAGENTA, COLOR_BLACK); + init_pair(5, COLOR_RED, COLOR_BLACK); + } +} + +void IOHandlerCursesGUI::Deactivate() { m_app_ap->Terminate(); } + +void IOHandlerCursesGUI::Run() { + m_app_ap->Run(m_debugger); + SetIsDone(true); +} + +IOHandlerCursesGUI::~IOHandlerCursesGUI() = default; + +void IOHandlerCursesGUI::Cancel() {} + +bool IOHandlerCursesGUI::Interrupt() { return false; } + +void IOHandlerCursesGUI::GotEOF() {} + +#endif // LLDB_ENABLE_CURSES diff --git a/lldb/source/Core/Mangled.cpp b/lldb/source/Core/Mangled.cpp index b06656aa3fb7..15baffd212af 100644 --- a/lldb/source/Core/Mangled.cpp +++ b/lldb/source/Core/Mangled.cpp @@ -32,18 +32,8 @@ #include <string.h> using namespace lldb_private; -static inline Mangled::ManglingScheme cstring_mangling_scheme(const char *s) { - if (s) { - if (s[0] == '?') - return Mangled::eManglingSchemeMSVC; - if (s[0] == '_' && s[1] == 'Z') - return Mangled::eManglingSchemeItanium; - } - return Mangled::eManglingSchemeNone; -} - -static inline bool cstring_is_mangled(const char *s) { - return cstring_mangling_scheme(s) != Mangled::eManglingSchemeNone; +static inline bool cstring_is_mangled(llvm::StringRef s) { + return Mangled::GetManglingScheme(s) != Mangled::eManglingSchemeNone; } static ConstString @@ -99,6 +89,23 @@ get_demangled_name_without_arguments(ConstString mangled, #pragma mark Mangled +Mangled::ManglingScheme Mangled::GetManglingScheme(llvm::StringRef const name) { + if (name.empty()) + return Mangled::eManglingSchemeNone; + + if (name.startswith("?")) + return Mangled::eManglingSchemeMSVC; + + if (name.startswith("_Z")) + return Mangled::eManglingSchemeItanium; + + // ___Z is a clang extension of block invocations + if (name.startswith("___Z")) + return Mangled::eManglingSchemeItanium; + + return Mangled::eManglingSchemeNone; +} + Mangled::Mangled(ConstString s) : m_mangled(), m_demangled() { if (s) SetValue(s); @@ -159,7 +166,7 @@ void Mangled::SetValue(ConstString s, bool mangled) { void Mangled::SetValue(ConstString name) { if (name) { - if (cstring_is_mangled(name.GetCString())) { + if (cstring_is_mangled(name.GetStringRef())) { m_demangled.Clear(); m_mangled = name; } else { @@ -232,7 +239,7 @@ bool Mangled::DemangleWithRichManglingInfo( assert(m_mangled); // Check whether or not we are interested in this name at all. - ManglingScheme scheme = cstring_mangling_scheme(m_mangled.GetCString()); + ManglingScheme scheme = GetManglingScheme(m_mangled.GetStringRef()); if (skip_mangled_name && skip_mangled_name(m_mangled.GetStringRef(), scheme)) return false; @@ -300,7 +307,7 @@ Mangled::GetDemangledName(lldb::LanguageType language) const { // Don't bother running anything that isn't mangled const char *mangled_name = m_mangled.GetCString(); - ManglingScheme mangling_scheme{cstring_mangling_scheme(mangled_name)}; + ManglingScheme mangling_scheme = GetManglingScheme(m_mangled.GetStringRef()); if (mangling_scheme != eManglingSchemeNone && !m_mangled.GetMangledCounterpart(m_demangled)) { // We didn't already mangle this name, demangle it and if all goes well @@ -405,6 +412,7 @@ size_t Mangled::MemorySize() const { // within those targets. lldb::LanguageType Mangled::GuessLanguage() const { ConstString mangled = GetMangledName(); + if (mangled) { const char *mangled_name = mangled.GetCString(); if (CPlusPlusLanguage::IsCPPMangledName(mangled_name)) diff --git a/lldb/source/Core/Module.cpp b/lldb/source/Core/Module.cpp index aef3f3e3b4b0..031892abdd24 100644 --- a/lldb/source/Core/Module.cpp +++ b/lldb/source/Core/Module.cpp @@ -613,11 +613,10 @@ void Module::FindCompileUnits(const FileSpec &path, const size_t num_compile_units = GetNumCompileUnits(); SymbolContext sc; sc.module_sp = shared_from_this(); - const bool compare_directory = (bool)path.GetDirectory(); for (size_t i = 0; i < num_compile_units; ++i) { sc.comp_unit = GetCompileUnitAtIndex(i).get(); if (sc.comp_unit) { - if (FileSpec::Equal(*sc.comp_unit, path, compare_directory)) + if (FileSpec::Match(path, sc.comp_unit->GetPrimaryFile())) sc_list.Append(sc); } } @@ -1006,12 +1005,14 @@ void Module::FindTypes( } } -void Module::FindTypes(llvm::ArrayRef<CompilerContext> pattern, - LanguageSet languages, TypeMap &types) { +void Module::FindTypes( + llvm::ArrayRef<CompilerContext> pattern, LanguageSet languages, + llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files, + TypeMap &types) { static Timer::Category func_cat(LLVM_PRETTY_FUNCTION); Timer scoped_timer(func_cat, LLVM_PRETTY_FUNCTION); if (SymbolFile *symbols = GetSymbolFile()) - symbols->FindTypes(pattern, languages, types); + symbols->FindTypes(pattern, languages, searched_symbol_files, types); } SymbolFile *Module::GetSymbolFile(bool can_create, Stream *feedback_strm) { @@ -1058,34 +1059,35 @@ std::string Module::GetSpecificationDescription() const { return spec; } -void Module::GetDescription(Stream *s, lldb::DescriptionLevel level) { +void Module::GetDescription(llvm::raw_ostream &s, + lldb::DescriptionLevel level) { std::lock_guard<std::recursive_mutex> guard(m_mutex); if (level >= eDescriptionLevelFull) { if (m_arch.IsValid()) - s->Printf("(%s) ", m_arch.GetArchitectureName()); + s << llvm::formatv("({0}) ", m_arch.GetArchitectureName()); } if (level == eDescriptionLevelBrief) { const char *filename = m_file.GetFilename().GetCString(); if (filename) - s->PutCString(filename); + s << filename; } else { char path[PATH_MAX]; if (m_file.GetPath(path, sizeof(path))) - s->PutCString(path); + s << path; } const char *object_name = m_object_name.GetCString(); if (object_name) - s->Printf("(%s)", object_name); + s << llvm::formatv("({0})", object_name); } void Module::ReportError(const char *format, ...) { if (format && format[0]) { StreamString strm; strm.PutCString("error: "); - GetDescription(&strm, lldb::eDescriptionLevelBrief); + GetDescription(strm.AsRawOstream(), lldb::eDescriptionLevelBrief); strm.PutChar(' '); va_list args; va_start(args, format); @@ -1116,7 +1118,7 @@ void Module::ReportErrorIfModifyDetected(const char *format, ...) { if (format) { StreamString strm; strm.PutCString("error: the object file "); - GetDescription(&strm, lldb::eDescriptionLevelFull); + GetDescription(strm.AsRawOstream(), lldb::eDescriptionLevelFull); strm.PutCString(" has been modified\n"); va_list args; @@ -1142,7 +1144,7 @@ void Module::ReportWarning(const char *format, ...) { if (format && format[0]) { StreamString strm; strm.PutCString("warning: "); - GetDescription(&strm, lldb::eDescriptionLevelFull); + GetDescription(strm.AsRawOstream(), lldb::eDescriptionLevelFull); strm.PutChar(' '); va_list args; @@ -1163,7 +1165,7 @@ void Module::ReportWarning(const char *format, ...) { void Module::LogMessage(Log *log, const char *format, ...) { if (log != nullptr) { StreamString log_message; - GetDescription(&log_message, lldb::eDescriptionLevelFull); + GetDescription(log_message.AsRawOstream(), lldb::eDescriptionLevelFull); log_message.PutCString(": "); va_list args; va_start(args, format); @@ -1176,7 +1178,7 @@ void Module::LogMessage(Log *log, const char *format, ...) { void Module::LogMessageVerboseBacktrace(Log *log, const char *format, ...) { if (log != nullptr) { StreamString log_message; - GetDescription(&log_message, lldb::eDescriptionLevelFull); + GetDescription(log_message.AsRawOstream(), lldb::eDescriptionLevelFull); log_message.PutCString(": "); va_list args; va_start(args, format); @@ -1509,12 +1511,10 @@ bool Module::LoadScriptingResourceInTarget(Target *target, Status &error, return false; } StreamString scripting_stream; - scripting_fspec.Dump(&scripting_stream); - const bool can_reload = true; + scripting_fspec.Dump(scripting_stream.AsRawOstream()); const bool init_lldb_globals = false; bool did_load = script_interpreter->LoadScriptingModule( - scripting_stream.GetData(), can_reload, init_lldb_globals, - error); + scripting_stream.GetData(), init_lldb_globals, error); if (!did_load) return false; } @@ -1557,19 +1557,13 @@ bool Module::MatchesModuleSpec(const ModuleSpec &module_ref) { } const FileSpec &file_spec = module_ref.GetFileSpec(); - if (file_spec) { - if (!FileSpec::Equal(file_spec, m_file, (bool)file_spec.GetDirectory()) && - !FileSpec::Equal(file_spec, m_platform_file, - (bool)file_spec.GetDirectory())) - return false; - } + if (!FileSpec::Match(file_spec, m_file) && + !FileSpec::Match(file_spec, m_platform_file)) + return false; const FileSpec &platform_file_spec = module_ref.GetPlatformFileSpec(); - if (platform_file_spec) { - if (!FileSpec::Equal(platform_file_spec, GetPlatformFileSpec(), - (bool)platform_file_spec.GetDirectory())) - return false; - } + if (!FileSpec::Match(platform_file_spec, GetPlatformFileSpec())) + return false; const ArchSpec &arch = module_ref.GetArchitecture(); if (arch.IsValid()) { diff --git a/lldb/source/Core/ModuleList.cpp b/lldb/source/Core/ModuleList.cpp index b0567a902fd7..07100bb81dca 100644 --- a/lldb/source/Core/ModuleList.cpp +++ b/lldb/source/Core/ModuleList.cpp @@ -752,9 +752,10 @@ Status ModuleList::GetSharedModule(const ModuleSpec &module_spec, Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_MODULES)); if (log != nullptr) - LLDB_LOGF(log, - "module changed: %p, removing from global module list", - static_cast<void *>(module_sp.get())); + LLDB_LOGF( + log, "%p '%s' module changed: removing from global module list", + static_cast<void *>(module_sp.get()), + module_sp->GetFileSpec().GetFilename().GetCString()); shared_module_list.Remove(module_sp); module_sp.reset(); diff --git a/lldb/source/Core/PluginManager.cpp b/lldb/source/Core/PluginManager.cpp index 80b64fb832fa..e8bfef724378 100644 --- a/lldb/source/Core/PluginManager.cpp +++ b/lldb/source/Core/PluginManager.cpp @@ -86,7 +86,7 @@ static void SetPluginInfo(const FileSpec &plugin_file_spec, } template <typename FPtrTy> static FPtrTy CastToFPtr(void *VPtr) { - return reinterpret_cast<FPtrTy>(reinterpret_cast<intptr_t>(VPtr)); + return reinterpret_cast<FPtrTy>(VPtr); } static FileSystem::EnumerateDirectoryResult diff --git a/lldb/source/Core/SearchFilter.cpp b/lldb/source/Core/SearchFilter.cpp index e02b4f66b58c..9902166be522 100644 --- a/lldb/source/Core/SearchFilter.cpp +++ b/lldb/source/Core/SearchFilter.cpp @@ -208,10 +208,12 @@ void SearchFilter::Search(Searcher &searcher) { return; empty_sc.target_sp = m_target_sp; - if (searcher.GetDepth() == lldb::eSearchDepthTarget) + if (searcher.GetDepth() == lldb::eSearchDepthTarget) { searcher.SearchCallback(*this, empty_sc, nullptr); - else - DoModuleIteration(empty_sc, searcher); + return; + } + + DoModuleIteration(empty_sc, searcher); } void SearchFilter::SearchInModuleList(Searcher &searcher, ModuleList &modules) { @@ -221,20 +223,20 @@ void SearchFilter::SearchInModuleList(Searcher &searcher, ModuleList &modules) { return; empty_sc.target_sp = m_target_sp; - if (searcher.GetDepth() == lldb::eSearchDepthTarget) + if (searcher.GetDepth() == lldb::eSearchDepthTarget) { searcher.SearchCallback(*this, empty_sc, nullptr); - else { - std::lock_guard<std::recursive_mutex> guard(modules.GetMutex()); - const size_t numModules = modules.GetSize(); + return; + } - for (size_t i = 0; i < numModules; i++) { - ModuleSP module_sp(modules.GetModuleAtIndexUnlocked(i)); - if (ModulePasses(module_sp)) { - if (DoModuleIteration(module_sp, searcher) == - Searcher::eCallbackReturnStop) - return; - } - } + std::lock_guard<std::recursive_mutex> guard(modules.GetMutex()); + const size_t numModules = modules.GetSize(); + + for (size_t i = 0; i < numModules; i++) { + ModuleSP module_sp(modules.GetModuleAtIndexUnlocked(i)); + if (!ModulePasses(module_sp)) + continue; + if (DoModuleIteration(module_sp, searcher) == Searcher::eCallbackReturnStop) + return; } } @@ -248,45 +250,47 @@ SearchFilter::DoModuleIteration(const lldb::ModuleSP &module_sp, Searcher::CallbackReturn SearchFilter::DoModuleIteration(const SymbolContext &context, Searcher &searcher) { - if (searcher.GetDepth() >= lldb::eSearchDepthModule) { - if (context.module_sp) { - if (searcher.GetDepth() == lldb::eSearchDepthModule) { - SymbolContext matchingContext(context.module_sp.get()); - searcher.SearchCallback(*this, matchingContext, nullptr); - } else { - return DoCUIteration(context.module_sp, context, searcher); - } - } else { - const ModuleList &target_images = m_target_sp->GetImages(); - std::lock_guard<std::recursive_mutex> guard(target_images.GetMutex()); + if (searcher.GetDepth() < lldb::eSearchDepthModule) + return Searcher::eCallbackReturnContinue; - size_t n_modules = target_images.GetSize(); - for (size_t i = 0; i < n_modules; i++) { - // If this is the last level supplied, then call the callback directly, - // otherwise descend. - ModuleSP module_sp(target_images.GetModuleAtIndexUnlocked(i)); - if (!ModulePasses(module_sp)) - continue; + if (context.module_sp) { + if (searcher.GetDepth() != lldb::eSearchDepthModule) + return DoCUIteration(context.module_sp, context, searcher); - if (searcher.GetDepth() == lldb::eSearchDepthModule) { - SymbolContext matchingContext(m_target_sp, module_sp); + SymbolContext matchingContext(context.module_sp.get()); + searcher.SearchCallback(*this, matchingContext, nullptr); + return Searcher::eCallbackReturnContinue; + } - Searcher::CallbackReturn shouldContinue = - searcher.SearchCallback(*this, matchingContext, nullptr); - if (shouldContinue == Searcher::eCallbackReturnStop || - shouldContinue == Searcher::eCallbackReturnPop) - return shouldContinue; - } else { - Searcher::CallbackReturn shouldContinue = - DoCUIteration(module_sp, context, searcher); - if (shouldContinue == Searcher::eCallbackReturnStop) - return shouldContinue; - else if (shouldContinue == Searcher::eCallbackReturnPop) - continue; - } - } + const ModuleList &target_images = m_target_sp->GetImages(); + std::lock_guard<std::recursive_mutex> guard(target_images.GetMutex()); + + size_t n_modules = target_images.GetSize(); + for (size_t i = 0; i < n_modules; i++) { + // If this is the last level supplied, then call the callback directly, + // otherwise descend. + ModuleSP module_sp(target_images.GetModuleAtIndexUnlocked(i)); + if (!ModulePasses(module_sp)) + continue; + + if (searcher.GetDepth() == lldb::eSearchDepthModule) { + SymbolContext matchingContext(m_target_sp, module_sp); + + Searcher::CallbackReturn shouldContinue = + searcher.SearchCallback(*this, matchingContext, nullptr); + if (shouldContinue == Searcher::eCallbackReturnStop || + shouldContinue == Searcher::eCallbackReturnPop) + return shouldContinue; + } else { + Searcher::CallbackReturn shouldContinue = + DoCUIteration(module_sp, context, searcher); + if (shouldContinue == Searcher::eCallbackReturnStop) + return shouldContinue; + else if (shouldContinue == Searcher::eCallbackReturnPop) + continue; } } + return Searcher::eCallbackReturnContinue; } @@ -294,56 +298,56 @@ Searcher::CallbackReturn SearchFilter::DoCUIteration(const ModuleSP &module_sp, const SymbolContext &context, Searcher &searcher) { Searcher::CallbackReturn shouldContinue; - if (context.comp_unit == nullptr) { - const size_t num_comp_units = module_sp->GetNumCompileUnits(); - for (size_t i = 0; i < num_comp_units; i++) { - CompUnitSP cu_sp(module_sp->GetCompileUnitAtIndex(i)); - if (cu_sp) { - if (!CompUnitPasses(*(cu_sp.get()))) - continue; - - if (searcher.GetDepth() == lldb::eSearchDepthCompUnit) { - SymbolContext matchingContext(m_target_sp, module_sp, cu_sp.get()); - - shouldContinue = - searcher.SearchCallback(*this, matchingContext, nullptr); - - if (shouldContinue == Searcher::eCallbackReturnPop) - return Searcher::eCallbackReturnContinue; - else if (shouldContinue == Searcher::eCallbackReturnStop) - return shouldContinue; - } else { - // First make sure this compile unit's functions are parsed - // since CompUnit::ForeachFunction only iterates over already - // parsed functions. - SymbolFile *sym_file = module_sp->GetSymbolFile(); - if (!sym_file) - continue; - if (!sym_file->ParseFunctions(*cu_sp)) - continue; - // If we got any functions, use ForeachFunction to do the iteration. - cu_sp->ForeachFunction([&](const FunctionSP &func_sp) { - if (!FunctionPasses(*func_sp.get())) - return false; // Didn't pass the filter, just keep going. - if (searcher.GetDepth() == lldb::eSearchDepthFunction) { - SymbolContext matchingContext(m_target_sp, module_sp, - cu_sp.get(), func_sp.get()); - shouldContinue = - searcher.SearchCallback(*this, matchingContext, nullptr); - } else { - shouldContinue = DoFunctionIteration(func_sp.get(), context, - searcher); - } - return shouldContinue != Searcher::eCallbackReturnContinue; - }); - } - } - } - } else { + if (context.comp_unit != nullptr) { if (CompUnitPasses(*context.comp_unit)) { SymbolContext matchingContext(m_target_sp, module_sp, context.comp_unit); return searcher.SearchCallback(*this, matchingContext, nullptr); } + return Searcher::eCallbackReturnContinue; + } + + const size_t num_comp_units = module_sp->GetNumCompileUnits(); + for (size_t i = 0; i < num_comp_units; i++) { + CompUnitSP cu_sp(module_sp->GetCompileUnitAtIndex(i)); + if (!cu_sp) + continue; + if (!CompUnitPasses(*(cu_sp.get()))) + continue; + + if (searcher.GetDepth() == lldb::eSearchDepthCompUnit) { + SymbolContext matchingContext(m_target_sp, module_sp, cu_sp.get()); + + shouldContinue = searcher.SearchCallback(*this, matchingContext, nullptr); + + if (shouldContinue == Searcher::eCallbackReturnPop) + return Searcher::eCallbackReturnContinue; + else if (shouldContinue == Searcher::eCallbackReturnStop) + return shouldContinue; + continue; + } + + // First make sure this compile unit's functions are parsed + // since CompUnit::ForeachFunction only iterates over already + // parsed functions. + SymbolFile *sym_file = module_sp->GetSymbolFile(); + if (!sym_file) + continue; + if (!sym_file->ParseFunctions(*cu_sp)) + continue; + // If we got any functions, use ForeachFunction to do the iteration. + cu_sp->ForeachFunction([&](const FunctionSP &func_sp) { + if (!FunctionPasses(*func_sp.get())) + return false; // Didn't pass the filter, just keep going. + if (searcher.GetDepth() == lldb::eSearchDepthFunction) { + SymbolContext matchingContext(m_target_sp, module_sp, cu_sp.get(), + func_sp.get()); + shouldContinue = + searcher.SearchCallback(*this, matchingContext, nullptr); + } else { + shouldContinue = DoFunctionIteration(func_sp.get(), context, searcher); + } + return shouldContinue != Searcher::eCallbackReturnContinue; + }); } return Searcher::eCallbackReturnContinue; } @@ -383,8 +387,7 @@ bool SearchFilterForUnconstrainedSearches::ModulePasses( return true; else if (m_target_sp->ModuleIsExcludedForUnconstrainedSearches(module_sp)) return false; - else - return true; + return true; } lldb::SearchFilterSP SearchFilterForUnconstrainedSearches::DoCopyForBreakpoint( @@ -403,13 +406,11 @@ SearchFilterByModule::~SearchFilterByModule() = default; bool SearchFilterByModule::ModulePasses(const ModuleSP &module_sp) { return (module_sp && - FileSpec::Equal(module_sp->GetFileSpec(), m_module_spec, false)); + FileSpec::Match(m_module_spec, module_sp->GetFileSpec())); } bool SearchFilterByModule::ModulePasses(const FileSpec &spec) { - // Do a full match only if "spec" has a directory - const bool full_match = (bool)spec.GetDirectory(); - return FileSpec::Equal(spec, m_module_spec, full_match); + return FileSpec::Match(m_module_spec, spec); } bool SearchFilterByModule::AddressPasses(Address &address) { @@ -443,8 +444,7 @@ void SearchFilterByModule::Search(Searcher &searcher) { const size_t num_modules = target_modules.GetSize(); for (size_t i = 0; i < num_modules; i++) { Module *module = target_modules.GetModulePointerAtIndexUnlocked(i); - const bool full_match = (bool)m_module_spec.GetDirectory(); - if (FileSpec::Equal(m_module_spec, module->GetFileSpec(), full_match)) { + if (FileSpec::Match(m_module_spec, module->GetFileSpec())) { SymbolContext matchingContext(m_target_sp, module->shared_from_this()); Searcher::CallbackReturn shouldContinue; @@ -523,13 +523,6 @@ SearchFilterByModuleList::SearchFilterByModuleList( enum FilterTy filter_ty) : SearchFilter(target_sp, filter_ty), m_module_spec_list(module_list) {} -SearchFilterByModuleList &SearchFilterByModuleList:: -operator=(const SearchFilterByModuleList &rhs) { - m_target_sp = rhs.m_target_sp; - m_module_spec_list = rhs.m_module_spec_list; - return *this; -} - SearchFilterByModuleList::~SearchFilterByModuleList() = default; bool SearchFilterByModuleList::ModulePasses(const ModuleSP &module_sp) { @@ -580,15 +573,15 @@ void SearchFilterByModuleList::Search(Searcher &searcher) { const size_t num_modules = target_modules.GetSize(); for (size_t i = 0; i < num_modules; i++) { Module *module = target_modules.GetModulePointerAtIndexUnlocked(i); - if (m_module_spec_list.FindFileIndex(0, module->GetFileSpec(), false) != - UINT32_MAX) { - SymbolContext matchingContext(m_target_sp, module->shared_from_this()); - Searcher::CallbackReturn shouldContinue; + if (m_module_spec_list.FindFileIndex(0, module->GetFileSpec(), false) == + UINT32_MAX) + continue; + SymbolContext matchingContext(m_target_sp, module->shared_from_this()); + Searcher::CallbackReturn shouldContinue; - shouldContinue = DoModuleIteration(matchingContext, searcher); - if (shouldContinue == Searcher::eCallbackReturnStop) - return; - } + shouldContinue = DoModuleIteration(matchingContext, searcher); + if (shouldContinue == Searcher::eCallbackReturnStop) + return; } } @@ -599,15 +592,16 @@ void SearchFilterByModuleList::GetDescription(Stream *s) { s->PutCString( m_module_spec_list.GetFileSpecAtIndex(0).GetFilename().AsCString( "<Unknown>")); - } else { - s->Printf(", modules(%" PRIu64 ") = ", (uint64_t)num_modules); - for (size_t i = 0; i < num_modules; i++) { - s->PutCString( - m_module_spec_list.GetFileSpecAtIndex(i).GetFilename().AsCString( - "<Unknown>")); - if (i != num_modules - 1) - s->PutCString(", "); - } + return; + } + + s->Printf(", modules(%" PRIu64 ") = ", (uint64_t)num_modules); + for (size_t i = 0; i < num_modules; i++) { + s->PutCString( + m_module_spec_list.GetFileSpecAtIndex(i).GetFilename().AsCString( + "<Unknown>")); + if (i != num_modules - 1) + s->PutCString(", "); } } @@ -628,21 +622,22 @@ SearchFilterSP SearchFilterByModuleList::CreateFromStructuredData( StructuredData::Array *modules_array; bool success = data_dict.GetValueForKeyAsArray(GetKey(OptionNames::ModList), modules_array); + + if (!success) + return std::make_shared<SearchFilterByModuleList>(target.shared_from_this(), + FileSpecList{}); FileSpecList modules; - if (success) { - size_t num_modules = modules_array->GetSize(); - for (size_t i = 0; i < num_modules; i++) { - llvm::StringRef module; - success = modules_array->GetItemAtIndexAsString(i, module); - if (!success) { - error.SetErrorStringWithFormat( - "SFBM::CFSD: filter module item %zu not a string.", i); - return nullptr; - } - modules.EmplaceBack(module); + size_t num_modules = modules_array->GetSize(); + for (size_t i = 0; i < num_modules; i++) { + llvm::StringRef module; + success = modules_array->GetItemAtIndexAsString(i, module); + if (!success) { + error.SetErrorStringWithFormat( + "SFBM::CFSD: filter module item %zu not a string.", i); + return nullptr; } + modules.EmplaceBack(module); } - return std::make_shared<SearchFilterByModuleList>(target.shared_from_this(), modules); } @@ -669,19 +664,6 @@ SearchFilterByModuleListAndCU::SearchFilterByModuleListAndCU( FilterTy::ByModulesAndCU), m_cu_spec_list(cu_list) {} -SearchFilterByModuleListAndCU::SearchFilterByModuleListAndCU( - const SearchFilterByModuleListAndCU &rhs) = default; - -SearchFilterByModuleListAndCU &SearchFilterByModuleListAndCU:: -operator=(const SearchFilterByModuleListAndCU &rhs) { - if (&rhs != this) { - m_target_sp = rhs.m_target_sp; - m_module_spec_list = rhs.m_module_spec_list; - m_cu_spec_list = rhs.m_cu_spec_list; - } - return *this; -} - SearchFilterByModuleListAndCU::~SearchFilterByModuleListAndCU() = default; lldb::SearchFilterSP SearchFilterByModuleListAndCU::CreateFromStructuredData( @@ -721,7 +703,7 @@ lldb::SearchFilterSP SearchFilterByModuleListAndCU::CreateFromStructuredData( success = cus_array->GetItemAtIndexAsString(i, cu); if (!success) { error.SetErrorStringWithFormat( - "SFBM::CFSD: filter cu item %zu not a string.", i); + "SFBM::CFSD: filter CU item %zu not a string.", i); return nullptr; } cus.EmplaceBack(cu); @@ -746,8 +728,11 @@ bool SearchFilterByModuleListAndCU::AddressPasses(Address &address) { if (m_cu_spec_list.GetSize() != 0) return false; // Has no comp_unit so can't pass the file check. } - if (m_cu_spec_list.FindFileIndex(0, sym_ctx.comp_unit, false) == UINT32_MAX) - return false; // Fails the file check + FileSpec cu_spec; + if (sym_ctx.comp_unit) + cu_spec = sym_ctx.comp_unit->GetPrimaryFile(); + if (m_cu_spec_list.FindFileIndex(0, cu_spec, false) == UINT32_MAX) + return false; // Fails the file check return SearchFilterByModuleList::ModulePasses(sym_ctx.module_sp); } @@ -756,17 +741,16 @@ bool SearchFilterByModuleListAndCU::CompUnitPasses(FileSpec &fileSpec) { } bool SearchFilterByModuleListAndCU::CompUnitPasses(CompileUnit &compUnit) { - bool in_cu_list = - m_cu_spec_list.FindFileIndex(0, compUnit, false) != UINT32_MAX; - if (in_cu_list) { - ModuleSP module_sp(compUnit.GetModule()); - if (module_sp) { - bool module_passes = SearchFilterByModuleList::ModulePasses(module_sp); - return module_passes; - } else - return true; - } else + bool in_cu_list = m_cu_spec_list.FindFileIndex(0, compUnit.GetPrimaryFile(), + false) != UINT32_MAX; + if (!in_cu_list) return false; + + ModuleSP module_sp(compUnit.GetModule()); + if (!module_sp) + return true; + + return SearchFilterByModuleList::ModulePasses(module_sp); } void SearchFilterByModuleListAndCU::Search(Searcher &searcher) { @@ -791,32 +775,34 @@ void SearchFilterByModuleListAndCU::Search(Searcher &searcher) { bool no_modules_in_filter = m_module_spec_list.GetSize() == 0; for (size_t i = 0; i < num_modules; i++) { lldb::ModuleSP module_sp = target_images.GetModuleAtIndexUnlocked(i); - if (no_modules_in_filter || - m_module_spec_list.FindFileIndex(0, module_sp->GetFileSpec(), false) != - UINT32_MAX) { - SymbolContext matchingContext(m_target_sp, module_sp); - Searcher::CallbackReturn shouldContinue; + if (!no_modules_in_filter && + m_module_spec_list.FindFileIndex(0, module_sp->GetFileSpec(), false) == + UINT32_MAX) + continue; - if (searcher.GetDepth() == lldb::eSearchDepthModule) { - shouldContinue = DoModuleIteration(matchingContext, searcher); - if (shouldContinue == Searcher::eCallbackReturnStop) - return; - } else { - const size_t num_cu = module_sp->GetNumCompileUnits(); - for (size_t cu_idx = 0; cu_idx < num_cu; cu_idx++) { - CompUnitSP cu_sp = module_sp->GetCompileUnitAtIndex(cu_idx); - matchingContext.comp_unit = cu_sp.get(); - if (matchingContext.comp_unit) { - if (m_cu_spec_list.FindFileIndex(0, *matchingContext.comp_unit, - false) != UINT32_MAX) { - shouldContinue = - DoCUIteration(module_sp, matchingContext, searcher); - if (shouldContinue == Searcher::eCallbackReturnStop) - return; - } - } - } - } + SymbolContext matchingContext(m_target_sp, module_sp); + Searcher::CallbackReturn shouldContinue; + + if (searcher.GetDepth() == lldb::eSearchDepthModule) { + shouldContinue = DoModuleIteration(matchingContext, searcher); + if (shouldContinue == Searcher::eCallbackReturnStop) + return; + continue; + } + + const size_t num_cu = module_sp->GetNumCompileUnits(); + for (size_t cu_idx = 0; cu_idx < num_cu; cu_idx++) { + CompUnitSP cu_sp = module_sp->GetCompileUnitAtIndex(cu_idx); + matchingContext.comp_unit = cu_sp.get(); + if (!matchingContext.comp_unit) + continue; + if (m_cu_spec_list.FindFileIndex( + 0, matchingContext.comp_unit->GetPrimaryFile(), false) == + UINT32_MAX) + continue; + shouldContinue = DoCUIteration(module_sp, matchingContext, searcher); + if (shouldContinue == Searcher::eCallbackReturnStop) + return; } } } diff --git a/lldb/source/Core/Section.cpp b/lldb/source/Core/Section.cpp index 7615dc1d65c7..1697f1f7a5d4 100644 --- a/lldb/source/Core/Section.cpp +++ b/lldb/source/Core/Section.cpp @@ -80,8 +80,12 @@ const char *Section::GetTypeAsCString() const { return "dwarf-line-str"; case eSectionTypeDWARFDebugLoc: return "dwarf-loc"; + case eSectionTypeDWARFDebugLocDwo: + return "dwarf-loc-dwo"; case eSectionTypeDWARFDebugLocLists: return "dwarf-loclists"; + case eSectionTypeDWARFDebugLocListsDwo: + return "dwarf-loclists-dwo"; case eSectionTypeDWARFDebugMacInfo: return "dwarf-macinfo"; case eSectionTypeDWARFDebugMacro: @@ -94,6 +98,8 @@ const char *Section::GetTypeAsCString() const { return "dwarf-ranges"; case eSectionTypeDWARFDebugRngLists: return "dwarf-rnglists"; + case eSectionTypeDWARFDebugRngListsDwo: + return "dwarf-rnglists-dwo"; case eSectionTypeDWARFDebugStr: return "dwarf-str"; case eSectionTypeDWARFDebugStrDwo: @@ -278,29 +284,6 @@ bool Section::ContainsFileAddress(addr_t vm_addr) const { return false; } -int Section::Compare(const Section &a, const Section &b) { - if (&a == &b) - return 0; - - const ModuleSP a_module_sp = a.GetModule(); - const ModuleSP b_module_sp = b.GetModule(); - if (a_module_sp == b_module_sp) { - user_id_t a_sect_uid = a.GetID(); - user_id_t b_sect_uid = b.GetID(); - if (a_sect_uid < b_sect_uid) - return -1; - if (a_sect_uid > b_sect_uid) - return 1; - return 0; - } else { - // The modules are different, just compare the module pointers - if (a_module_sp.get() < b_module_sp.get()) - return -1; - else - return 1; // We already know the modules aren't equal - } -} - void Section::Dump(Stream *s, Target *target, uint32_t depth) const { // s->Printf("%.*p: ", (int)sizeof(void*) * 2, this); s->Indent(); @@ -321,7 +304,7 @@ void Section::Dump(Stream *s, Target *target, uint32_t depth) const { } VMRange range(addr, addr + m_byte_size); - range.Dump(s, 0); + range.Dump(s->AsRawOstream(), 0); } s->Printf("%c %c%c%c 0x%8.8" PRIx64 " 0x%8.8" PRIx64 " 0x%8.8x ", diff --git a/lldb/source/Core/SourceManager.cpp b/lldb/source/Core/SourceManager.cpp index 42741e4ba4fe..8e0cc57f80c1 100644 --- a/lldb/source/Core/SourceManager.cpp +++ b/lldb/source/Core/SourceManager.cpp @@ -64,7 +64,8 @@ SourceManager::~SourceManager() {} SourceManager::FileSP SourceManager::GetFile(const FileSpec &file_spec) { bool same_as_previous = - m_last_file_sp && m_last_file_sp->FileSpecMatches(file_spec); + m_last_file_sp && + FileSpec::Match(file_spec, m_last_file_sp->GetFileSpec()); DebuggerSP debugger_sp(m_debugger_wp.lock()); FileSP file_sp; @@ -399,24 +400,25 @@ void SourceManager::File::CommonInitializer(const FileSpec &file_spec, if (num_matches != 0) { if (num_matches > 1) { SymbolContext sc; - FileSpec *test_cu_spec = nullptr; + CompileUnit *test_cu = nullptr; for (unsigned i = 0; i < num_matches; i++) { sc_list.GetContextAtIndex(i, sc); if (sc.comp_unit) { - if (test_cu_spec) { - if (test_cu_spec != static_cast<FileSpec *>(sc.comp_unit)) + if (test_cu) { + if (test_cu != sc.comp_unit) got_multiple = true; break; } else - test_cu_spec = sc.comp_unit; + test_cu = sc.comp_unit; } } } if (!got_multiple) { SymbolContext sc; sc_list.GetContextAtIndex(0, sc); - m_file_spec = sc.comp_unit; + if (sc.comp_unit) + m_file_spec = sc.comp_unit->GetPrimaryFile(); m_mod_time = FileSystem::Instance().GetModificationTime(m_file_spec); } } @@ -601,10 +603,6 @@ void SourceManager::File::FindLinesMatchingRegex( } } -bool SourceManager::File::FileSpecMatches(const FileSpec &file_spec) { - return FileSpec::Equal(m_file_spec, file_spec, false); -} - bool lldb_private::operator==(const SourceManager::File &lhs, const SourceManager::File &rhs) { if (lhs.m_file_spec != rhs.m_file_spec) diff --git a/lldb/source/Core/StreamFile.cpp b/lldb/source/Core/StreamFile.cpp index 2ddb39659d66..475c27ec4117 100644 --- a/lldb/source/Core/StreamFile.cpp +++ b/lldb/source/Core/StreamFile.cpp @@ -15,9 +15,6 @@ using namespace lldb; using namespace lldb_private; -// StreamFile constructor -StreamFile::StreamFile() : Stream() { m_file_sp = std::make_shared<File>(); } - StreamFile::StreamFile(uint32_t flags, uint32_t addr_size, ByteOrder byte_order) : Stream(flags, addr_size, byte_order) { m_file_sp = std::make_shared<File>(); @@ -32,20 +29,6 @@ StreamFile::StreamFile(FILE *fh, bool transfer_ownership) : Stream() { m_file_sp = std::make_shared<NativeFile>(fh, transfer_ownership); } -StreamFile::StreamFile(const char *path) : Stream() { - auto file = FileSystem::Instance().Open( - FileSpec(path), File::eOpenOptionWrite | File::eOpenOptionCanCreate | - File::eOpenOptionCloseOnExec); - if (file) - m_file_sp = std::move(file.get()); - else { - // TODO refactor this so the error gets popagated up instead of logged here. - LLDB_LOG_ERROR(GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST), file.takeError(), - "Cannot open {1}: {0}", path); - m_file_sp = std::make_shared<File>(); - } -} - StreamFile::StreamFile(const char *path, File::OpenOptions options, uint32_t permissions) : Stream() { diff --git a/lldb/source/Core/Value.cpp b/lldb/source/Core/Value.cpp index 3124b9338b36..c70ab98dcdfa 100644 --- a/lldb/source/Core/Value.cpp +++ b/lldb/source/Core/Value.cpp @@ -322,6 +322,12 @@ Status Value::GetValueAsData(ExecutionContext *exe_ctx, DataExtractor &data, AddressType address_type = eAddressTypeFile; Address file_so_addr; const CompilerType &ast_type = GetCompilerType(); + llvm::Optional<uint64_t> type_size = ast_type.GetByteSize( + exe_ctx ? exe_ctx->GetBestExecutionContextScope() : nullptr); + // Nothing to be done for a zero-sized type. + if (type_size && *type_size == 0) + return error; + switch (m_value_type) { case eValueTypeVector: if (ast_type.IsValid()) @@ -340,9 +346,8 @@ Status Value::GetValueAsData(ExecutionContext *exe_ctx, DataExtractor &data, uint32_t limit_byte_size = UINT32_MAX; - if (llvm::Optional<uint64_t> size = ast_type.GetByteSize( - exe_ctx ? exe_ctx->GetBestExecutionContextScope() : nullptr)) - limit_byte_size = *size; + if (type_size) + limit_byte_size = *type_size; if (limit_byte_size <= m_value.GetByteSize()) { if (m_value.GetData(data, limit_byte_size)) @@ -507,10 +512,10 @@ Status Value::GetValueAsData(ExecutionContext *exe_ctx, DataExtractor &data, return error; } - // If we got here, we need to read the value from memory + // If we got here, we need to read the value from memory. size_t byte_size = GetValueByteSize(&error, exe_ctx); - // Bail if we encountered any errors getting the byte size + // Bail if we encountered any errors getting the byte size. if (error.Fail()) return error; diff --git a/lldb/source/Core/ValueObject.cpp b/lldb/source/Core/ValueObject.cpp index 74176eeace3a..1dd9a6cf62c3 100644 --- a/lldb/source/Core/ValueObject.cpp +++ b/lldb/source/Core/ValueObject.cpp @@ -22,9 +22,9 @@ #include "lldb/DataFormatters/StringPrinter.h" #include "lldb/DataFormatters/TypeFormat.h" #include "lldb/DataFormatters/TypeSummary.h" -#include "lldb/DataFormatters/TypeValidator.h" #include "lldb/DataFormatters/ValueObjectPrinter.h" #include "lldb/Expression/ExpressionVariable.h" +#include "lldb/Host/Config.h" #include "lldb/Symbol/ClangASTContext.h" #include "lldb/Symbol/CompileUnit.h" #include "lldb/Symbol/CompilerType.h" @@ -82,13 +82,12 @@ ValueObject::ValueObject(ValueObject &parent) m_parent(&parent), m_root(nullptr), m_update_point(parent.GetUpdatePoint()), m_name(), m_data(), m_value(), m_error(), m_value_str(), m_old_value_str(), m_location_str(), - m_summary_str(), m_object_desc_str(), m_validation_result(), - m_manager(parent.GetManager()), m_children(), m_synthetic_children(), - m_dynamic_value(nullptr), m_synthetic_value(nullptr), - m_deref_valobj(nullptr), m_format(eFormatDefault), - m_last_format(eFormatDefault), m_last_format_mgr_revision(0), - m_type_summary_sp(), m_type_format_sp(), m_synthetic_children_sp(), - m_type_validator_sp(), m_user_id_of_forced_summary(), + m_summary_str(), m_object_desc_str(), m_manager(parent.GetManager()), + m_children(), m_synthetic_children(), m_dynamic_value(nullptr), + m_synthetic_value(nullptr), m_deref_valobj(nullptr), + m_format(eFormatDefault), m_last_format(eFormatDefault), + m_last_format_mgr_revision(0), m_type_summary_sp(), m_type_format_sp(), + m_synthetic_children_sp(), m_user_id_of_forced_summary(), m_address_type_of_ptr_or_ref_children(eAddressTypeInvalid), m_value_checksum(), m_preferred_display_language(lldb::eLanguageTypeUnknown), @@ -100,6 +99,8 @@ ValueObject::ValueObject(ValueObject &parent) m_did_calculate_complete_objc_class_type(false), m_is_synthetic_children_generated( parent.m_is_synthetic_children_generated) { + m_data.SetByteOrder(parent.GetDataExtractor().GetByteOrder()); + m_data.SetAddressByteSize(parent.GetDataExtractor().GetAddressByteSize()); m_manager->ManageObject(this); } @@ -110,12 +111,12 @@ ValueObject::ValueObject(ExecutionContextScope *exe_scope, m_parent(nullptr), m_root(nullptr), m_update_point(exe_scope), m_name(), m_data(), m_value(), m_error(), m_value_str(), m_old_value_str(), m_location_str(), m_summary_str(), m_object_desc_str(), - m_validation_result(), m_manager(), m_children(), m_synthetic_children(), + m_manager(), m_children(), m_synthetic_children(), m_dynamic_value(nullptr), m_synthetic_value(nullptr), m_deref_valobj(nullptr), m_format(eFormatDefault), m_last_format(eFormatDefault), m_last_format_mgr_revision(0), m_type_summary_sp(), m_type_format_sp(), m_synthetic_children_sp(), - m_type_validator_sp(), m_user_id_of_forced_summary(), + m_user_id_of_forced_summary(), m_address_type_of_ptr_or_ref_children(child_ptr_or_ref_addr_type), m_value_checksum(), m_preferred_display_language(lldb::eLanguageTypeUnknown), @@ -126,6 +127,14 @@ ValueObject::ValueObject(ExecutionContextScope *exe_scope, m_is_getting_summary(false), m_did_calculate_complete_objc_class_type(false), m_is_synthetic_children_generated(false) { + if (exe_scope) { + TargetSP target_sp(exe_scope->CalculateTarget()); + if (target_sp) { + const ArchSpec &arch = target_sp->GetArchitecture(); + m_data.SetByteOrder(arch.GetByteOrder()); + m_data.SetAddressByteSize(arch.GetAddressByteSize()); + } + } m_manager = new ValueObjectManager(); m_manager->ManageObject(this); } @@ -133,6 +142,58 @@ ValueObject::ValueObject(ExecutionContextScope *exe_scope, // Destructor ValueObject::~ValueObject() {} +void ValueObject::UpdateChildrenAddressType() { + Value::ValueType value_type = m_value.GetValueType(); + ExecutionContext exe_ctx(GetExecutionContextRef()); + Process *process = exe_ctx.GetProcessPtr(); + const bool process_is_alive = process && process->IsAlive(); + const uint32_t type_info = GetCompilerType().GetTypeInfo(); + const bool is_pointer_or_ref = + (type_info & (lldb::eTypeIsPointer | lldb::eTypeIsReference)) != 0; + + switch (value_type) { + case Value::eValueTypeFileAddress: + // If this type is a pointer, then its children will be considered load + // addresses if the pointer or reference is dereferenced, but only if + // the process is alive. + // + // There could be global variables like in the following code: + // struct LinkedListNode { Foo* foo; LinkedListNode* next; }; + // Foo g_foo1; + // Foo g_foo2; + // LinkedListNode g_second_node = { &g_foo2, NULL }; + // LinkedListNode g_first_node = { &g_foo1, &g_second_node }; + // + // When we aren't running, we should be able to look at these variables + // using the "target variable" command. Children of the "g_first_node" + // always will be of the same address type as the parent. But children + // of the "next" member of LinkedListNode will become load addresses if + // we have a live process, or remain a file address if it was a file + // address. + if (process_is_alive && is_pointer_or_ref) + SetAddressTypeOfChildren(eAddressTypeLoad); + else + SetAddressTypeOfChildren(eAddressTypeFile); + break; + case Value::eValueTypeHostAddress: + // Same as above for load addresses, except children of pointer or refs + // are always load addresses. Host addresses are used to store freeze + // dried variables. If this type is a struct, the entire struct + // contents will be copied into the heap of the + // LLDB process, but we do not currently follow any pointers. + if (is_pointer_or_ref) + SetAddressTypeOfChildren(eAddressTypeLoad); + else + SetAddressTypeOfChildren(eAddressTypeHost); + break; + case Value::eValueTypeLoadAddress: + case Value::eValueTypeScalar: + case Value::eValueTypeVector: + SetAddressTypeOfChildren(eAddressTypeLoad); + break; + } +} + bool ValueObject::UpdateValueIfNeeded(bool update_format) { bool did_change_formats = false; @@ -195,6 +256,7 @@ bool ValueObject::UpdateValueIfNeeded(bool update_format) { SetValueIsValid(success); if (success) { + UpdateChildrenAddressType(); const uint64_t max_checksum_size = 128; m_data.Checksum(m_value_checksum, max_checksum_size); } else { @@ -241,11 +303,10 @@ bool ValueObject::UpdateFormatsIfNeeded() { SetValueFormat(DataVisualization::GetFormat(*this, eNoDynamicValues)); SetSummaryFormat( DataVisualization::GetSummaryFormat(*this, GetDynamicValueType())); -#ifndef LLDB_DISABLE_PYTHON +#if LLDB_ENABLE_PYTHON SetSyntheticChildren( DataVisualization::GetSyntheticChildren(*this, GetDynamicValueType())); #endif - SetValidator(DataVisualization::GetValidator(*this, GetDynamicValueType())); } return any_change; @@ -526,6 +587,10 @@ ValueObjectSP ValueObject::GetChildMemberWithName(ConstString name, std::vector<uint32_t> child_indexes; bool omit_empty_base_classes = true; + + if (!GetCompilerType().IsValid()) + return ValueObjectSP(); + const size_t num_child_indexes = GetCompilerType().GetIndexOfChildMemberWithName( name.GetCString(), omit_empty_base_classes, child_indexes); @@ -1037,23 +1102,6 @@ ValueObject::ReadPointedString(lldb::DataBufferSP &buffer_sp, Status &error, return {total_bytes_read, was_capped}; } -std::pair<TypeValidatorResult, std::string> ValueObject::GetValidationStatus() { - if (!UpdateValueIfNeeded(true)) - return {TypeValidatorResult::Success, - ""}; // not the validator's job to discuss update problems - - if (m_validation_result.hasValue()) - return m_validation_result.getValue(); - - if (!m_type_validator_sp) - return {TypeValidatorResult::Success, ""}; // no validator no failure - - auto outcome = m_type_validator_sp->FormatObject(this); - - return (m_validation_result = {outcome.m_result, outcome.m_message}) - .getValue(); -} - const char *ValueObject::GetObjectDescription() { if (!UpdateValueIfNeeded(true)) return nullptr; @@ -1970,15 +2018,14 @@ bool ValueObject::GetBaseClassPath(Stream &s) { bool parent_had_base_class = GetParent() && GetParent()->GetBaseClassPath(s); CompilerType compiler_type = GetCompilerType(); - std::string cxx_class_name; - bool this_had_base_class = - ClangASTContext::GetCXXClassName(compiler_type, cxx_class_name); - if (this_had_base_class) { + llvm::Optional<std::string> cxx_class_name = + ClangASTContext::GetCXXClassName(compiler_type); + if (cxx_class_name) { if (parent_had_base_class) s.PutCString("::"); - s.PutCString(cxx_class_name); + s.PutCString(cxx_class_name.getValue()); } - return parent_had_base_class || this_had_base_class; + return parent_had_base_class || cxx_class_name; } return false; } @@ -3063,10 +3110,6 @@ void ValueObject::ClearUserVisibleData(uint32_t clear_mask) { if (m_synthetic_value) m_synthetic_value = nullptr; } - - if ((clear_mask & eClearUserVisibleDataItemsValidator) == - eClearUserVisibleDataItemsValidator) - m_validation_result.reset(); } SymbolContextScope *ValueObject::GetSymbolContextScope() { diff --git a/lldb/source/Core/ValueObjectSyntheticFilter.cpp b/lldb/source/Core/ValueObjectSyntheticFilter.cpp index a6bf35eac70a..a30be1b08338 100644 --- a/lldb/source/Core/ValueObjectSyntheticFilter.cpp +++ b/lldb/source/Core/ValueObjectSyntheticFilter.cpp @@ -48,8 +48,9 @@ public: ValueObjectSynthetic::ValueObjectSynthetic(ValueObject &parent, lldb::SyntheticChildrenSP filter) : ValueObject(parent), m_synth_sp(filter), m_children_byindex(), - m_name_toindex(), m_synthetic_children_count(UINT32_MAX), - m_synthetic_children_cache(), m_parent_type_name(parent.GetTypeName()), + m_name_toindex(), m_synthetic_children_cache(), + m_synthetic_children_count(UINT32_MAX), + m_parent_type_name(parent.GetTypeName()), m_might_have_children(eLazyBoolCalculate), m_provides_value(eLazyBoolCalculate) { SetName(parent.GetName()); @@ -177,14 +178,20 @@ bool ValueObjectSynthetic::UpdateValue() { "filter said caches are stale - clearing", GetName().AsCString()); // filter said that cached values are stale - m_children_byindex.Clear(); - m_name_toindex.Clear(); + { + std::lock_guard<std::mutex> guard(m_child_mutex); + m_children_byindex.clear(); + m_name_toindex.clear(); + } // usually, an object's value can change but this does not alter its // children count for a synthetic VO that might indeed happen, so we need // to tell the upper echelons that they need to come back to us asking for // children m_children_count_valid = false; - m_synthetic_children_cache.Clear(); + { + std::lock_guard<std::mutex> guard(m_child_mutex); + m_synthetic_children_cache.clear(); + } m_synthetic_children_count = UINT32_MAX; m_might_have_children = eLazyBoolCalculate; } else { @@ -232,7 +239,16 @@ lldb::ValueObjectSP ValueObjectSynthetic::GetChildAtIndex(size_t idx, UpdateValueIfNeeded(); ValueObject *valobj; - if (!m_children_byindex.GetValueForKey(idx, valobj)) { + bool child_is_cached; + { + std::lock_guard<std::mutex> guard(m_child_mutex); + auto cached_child_it = m_children_byindex.find(idx); + child_is_cached = cached_child_it != m_children_byindex.end(); + if (child_is_cached) + valobj = cached_child_it->second; + } + + if (!child_is_cached) { if (can_create && m_synth_filter_up != nullptr) { LLDB_LOGF(log, "[ValueObjectSynthetic::GetChildAtIndex] name=%s, child at " @@ -254,9 +270,12 @@ lldb::ValueObjectSP ValueObjectSynthetic::GetChildAtIndex(size_t idx, if (!synth_guy) return synth_guy; - if (synth_guy->IsSyntheticChildrenGenerated()) - m_synthetic_children_cache.AppendObject(synth_guy); - m_children_byindex.SetValueForKey(idx, synth_guy.get()); + { + std::lock_guard<std::mutex> guard(m_child_mutex); + if (synth_guy->IsSyntheticChildrenGenerated()) + m_synthetic_children_cache.push_back(synth_guy); + m_children_byindex[idx] = synth_guy.get(); + } synth_guy->SetPreferredDisplayLanguageIfNeeded( GetPreferredDisplayLanguage()); return synth_guy; @@ -297,13 +316,21 @@ size_t ValueObjectSynthetic::GetIndexOfChildWithName(ConstString name) { UpdateValueIfNeeded(); uint32_t found_index = UINT32_MAX; - bool did_find = m_name_toindex.GetValueForKey(name.GetCString(), found_index); + bool did_find; + { + std::lock_guard<std::mutex> guard(m_child_mutex); + auto name_to_index = m_name_toindex.find(name.GetCString()); + did_find = name_to_index != m_name_toindex.end(); + if (did_find) + found_index = name_to_index->second; + } if (!did_find && m_synth_filter_up != nullptr) { uint32_t index = m_synth_filter_up->GetIndexOfChildWithName(name); if (index == UINT32_MAX) return index; - m_name_toindex.SetValueForKey(name.GetCString(), index); + std::lock_guard<std::mutex> guard(m_child_mutex); + m_name_toindex[name.GetCString()] = index; return index; } else if (!did_find && m_synth_filter_up == nullptr) return UINT32_MAX; diff --git a/lldb/source/Core/ValueObjectVariable.cpp b/lldb/source/Core/ValueObjectVariable.cpp index 33f9d5410843..240ebe1fdff3 100644 --- a/lldb/source/Core/ValueObjectVariable.cpp +++ b/lldb/source/Core/ValueObjectVariable.cpp @@ -168,51 +168,6 @@ bool ValueObjectVariable::UpdateValue() { Process *process = exe_ctx.GetProcessPtr(); const bool process_is_alive = process && process->IsAlive(); - const uint32_t type_info = compiler_type.GetTypeInfo(); - const bool is_pointer_or_ref = - (type_info & (lldb::eTypeIsPointer | lldb::eTypeIsReference)) != 0; - - switch (value_type) { - case Value::eValueTypeFileAddress: - // If this type is a pointer, then its children will be considered load - // addresses if the pointer or reference is dereferenced, but only if - // the process is alive. - // - // There could be global variables like in the following code: - // struct LinkedListNode { Foo* foo; LinkedListNode* next; }; - // Foo g_foo1; - // Foo g_foo2; - // LinkedListNode g_second_node = { &g_foo2, NULL }; - // LinkedListNode g_first_node = { &g_foo1, &g_second_node }; - // - // When we aren't running, we should be able to look at these variables - // using the "target variable" command. Children of the "g_first_node" - // always will be of the same address type as the parent. But children - // of the "next" member of LinkedListNode will become load addresses if - // we have a live process, or remain what a file address if it what a - // file address. - if (process_is_alive && is_pointer_or_ref) - SetAddressTypeOfChildren(eAddressTypeLoad); - else - SetAddressTypeOfChildren(eAddressTypeFile); - break; - case Value::eValueTypeHostAddress: - // Same as above for load addresses, except children of pointer or refs - // are always load addresses. Host addresses are used to store freeze - // dried variables. If this type is a struct, the entire struct - // contents will be copied into the heap of the - // LLDB process, but we do not currently follow any pointers. - if (is_pointer_or_ref) - SetAddressTypeOfChildren(eAddressTypeLoad); - else - SetAddressTypeOfChildren(eAddressTypeHost); - break; - case Value::eValueTypeLoadAddress: - case Value::eValueTypeScalar: - case Value::eValueTypeVector: - SetAddressTypeOfChildren(eAddressTypeLoad); - break; - } switch (value_type) { case Value::eValueTypeVector: |
