1 //===-- IOHandler.cpp -------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Core/IOHandler.h"
13 #ifndef LLDB_DISABLE_CURSES
19 #if defined(__APPLE__)
24 // Other libraries and framework includes
26 #include "lldb/Core/Debugger.h"
27 #include "lldb/Core/StreamFile.h"
28 #include "lldb/Host/File.h" // for File
29 #include "lldb/Host/Predicate.h" // for Predicate, ::eBroad...
30 #include "lldb/Utility/Status.h" // for Status
31 #include "lldb/Utility/StreamString.h" // for StreamString
32 #include "lldb/Utility/StringList.h" // for StringList
33 #include "lldb/lldb-forward.h" // for StreamFileSP
35 #ifndef LLDB_DISABLE_LIBEDIT
36 #include "lldb/Host/Editline.h"
38 #include "lldb/Interpreter/CommandCompletions.h"
39 #include "lldb/Interpreter/CommandInterpreter.h"
40 #ifndef LLDB_DISABLE_CURSES
41 #include "lldb/Breakpoint/BreakpointLocation.h"
42 #include "lldb/Core/Module.h"
43 #include "lldb/Core/State.h"
44 #include "lldb/Core/ValueObject.h"
45 #include "lldb/Core/ValueObjectRegister.h"
46 #include "lldb/Symbol/Block.h"
47 #include "lldb/Symbol/Function.h"
48 #include "lldb/Symbol/Symbol.h"
49 #include "lldb/Symbol/VariableList.h"
50 #include "lldb/Target/Process.h"
51 #include "lldb/Target/RegisterContext.h"
52 #include "lldb/Target/StackFrame.h"
53 #include "lldb/Target/StopInfo.h"
54 #include "lldb/Target/Target.h"
55 #include "lldb/Target/Thread.h"
58 #include "llvm/ADT/StringRef.h" // for StringRef
61 #include "lldb/Host/windows/windows.h"
64 #include <memory> // for shared_ptr
65 #include <mutex> // for recursive_mutex
67 #include <assert.h> // for assert
68 #include <ctype.h> // for isspace
69 #include <errno.h> // for EINTR, errno
70 #include <locale.h> // for setlocale
71 #include <stdint.h> // for uint32_t, UINT32_MAX
72 #include <stdio.h> // for size_t, fprintf, feof
73 #include <string.h> // for strlen
74 #include <type_traits> // for move
77 using namespace lldb_private;
79 IOHandler::IOHandler(Debugger &debugger, IOHandler::Type type)
80 : IOHandler(debugger, type,
81 StreamFileSP(), // Adopt STDIN from top input reader
82 StreamFileSP(), // Adopt STDOUT from top input reader
83 StreamFileSP(), // Adopt STDERR from top input reader
87 IOHandler::IOHandler(Debugger &debugger, IOHandler::Type type,
88 const lldb::StreamFileSP &input_sp,
89 const lldb::StreamFileSP &output_sp,
90 const lldb::StreamFileSP &error_sp, uint32_t flags)
91 : m_debugger(debugger), m_input_sp(input_sp), m_output_sp(output_sp),
92 m_error_sp(error_sp), m_popped(false), m_flags(flags), m_type(type),
93 m_user_data(nullptr), m_done(false), m_active(false) {
94 // If any files are not specified, then adopt them from the top input reader.
95 if (!m_input_sp || !m_output_sp || !m_error_sp)
96 debugger.AdoptTopIOHandlerFilesIfInvalid(m_input_sp, m_output_sp,
100 IOHandler::~IOHandler() = default;
102 int IOHandler::GetInputFD() {
103 return (m_input_sp ? m_input_sp->GetFile().GetDescriptor() : -1);
106 int IOHandler::GetOutputFD() {
107 return (m_output_sp ? m_output_sp->GetFile().GetDescriptor() : -1);
110 int IOHandler::GetErrorFD() {
111 return (m_error_sp ? m_error_sp->GetFile().GetDescriptor() : -1);
114 FILE *IOHandler::GetInputFILE() {
115 return (m_input_sp ? m_input_sp->GetFile().GetStream() : nullptr);
118 FILE *IOHandler::GetOutputFILE() {
119 return (m_output_sp ? m_output_sp->GetFile().GetStream() : nullptr);
122 FILE *IOHandler::GetErrorFILE() {
123 return (m_error_sp ? m_error_sp->GetFile().GetStream() : nullptr);
126 StreamFileSP &IOHandler::GetInputStreamFile() { return m_input_sp; }
128 StreamFileSP &IOHandler::GetOutputStreamFile() { return m_output_sp; }
130 StreamFileSP &IOHandler::GetErrorStreamFile() { return m_error_sp; }
132 bool IOHandler::GetIsInteractive() {
133 return GetInputStreamFile()->GetFile().GetIsInteractive();
136 bool IOHandler::GetIsRealTerminal() {
137 return GetInputStreamFile()->GetFile().GetIsRealTerminal();
140 void IOHandler::SetPopped(bool b) { m_popped.SetValue(b, eBroadcastOnChange); }
142 void IOHandler::WaitForPop() { m_popped.WaitForValueEqualTo(true); }
144 void IOHandlerStack::PrintAsync(Stream *stream, const char *s, size_t len) {
146 std::lock_guard<std::recursive_mutex> guard(m_mutex);
148 m_top->PrintAsync(stream, s, len);
152 IOHandlerConfirm::IOHandlerConfirm(Debugger &debugger, llvm::StringRef prompt,
153 bool default_response)
155 debugger, IOHandler::Type::Confirm,
156 nullptr, // nullptr editline_name means no history loaded/saved
157 llvm::StringRef(), // No prompt
158 llvm::StringRef(), // No continuation prompt
160 false, // Don't colorize the prompt (i.e. the confirm message.)
162 m_default_response(default_response), m_user_response(default_response) {
163 StreamString prompt_stream;
164 prompt_stream.PutCString(prompt);
165 if (m_default_response)
166 prompt_stream.Printf(": [Y/n] ");
168 prompt_stream.Printf(": [y/N] ");
170 SetPrompt(prompt_stream.GetString());
173 IOHandlerConfirm::~IOHandlerConfirm() = default;
175 int IOHandlerConfirm::IOHandlerComplete(IOHandler &io_handler,
176 const char *current_line,
178 const char *last_char,
179 int skip_first_n_matches,
180 int max_matches, StringList &matches) {
181 if (current_line == cursor) {
182 if (m_default_response) {
183 matches.AppendString("y");
185 matches.AppendString("n");
188 return matches.GetSize();
191 void IOHandlerConfirm::IOHandlerInputComplete(IOHandler &io_handler,
194 // User just hit enter, set the response to the default
195 m_user_response = m_default_response;
196 io_handler.SetIsDone(true);
200 if (line.size() == 1) {
204 m_user_response = true;
205 io_handler.SetIsDone(true);
209 m_user_response = false;
210 io_handler.SetIsDone(true);
217 if (line == "yes" || line == "YES" || line == "Yes") {
218 m_user_response = true;
219 io_handler.SetIsDone(true);
220 } else if (line == "no" || line == "NO" || line == "No") {
221 m_user_response = false;
222 io_handler.SetIsDone(true);
226 int IOHandlerDelegate::IOHandlerComplete(IOHandler &io_handler,
227 const char *current_line,
229 const char *last_char,
230 int skip_first_n_matches,
231 int max_matches, StringList &matches) {
232 switch (m_completion) {
233 case Completion::None:
236 case Completion::LLDBCommand:
237 return io_handler.GetDebugger().GetCommandInterpreter().HandleCompletion(
238 current_line, cursor, last_char, skip_first_n_matches, max_matches,
241 case Completion::Expression: {
242 CompletionRequest request(current_line, current_line - cursor,
243 skip_first_n_matches, max_matches, matches);
244 CommandCompletions::InvokeCommonCompletionCallbacks(
245 io_handler.GetDebugger().GetCommandInterpreter(),
246 CommandCompletions::eVariablePathCompletion, request, nullptr);
248 size_t num_matches = request.GetNumberOfMatches();
249 if (num_matches > 0) {
250 std::string common_prefix;
251 matches.LongestCommonPrefix(common_prefix);
252 const size_t partial_name_len = request.GetCursorArgumentPrefix().size();
254 // If we matched a unique single command, add a space... Only do this if
255 // the completer told us this was a complete word, however...
256 if (num_matches == 1 && request.GetWordComplete()) {
257 common_prefix.push_back(' ');
259 common_prefix.erase(0, partial_name_len);
260 matches.InsertStringAtIndex(0, std::move(common_prefix));
269 IOHandlerEditline::IOHandlerEditline(
270 Debugger &debugger, IOHandler::Type type,
271 const char *editline_name, // Used for saving history files
272 llvm::StringRef prompt, llvm::StringRef continuation_prompt,
273 bool multi_line, bool color_prompts, uint32_t line_number_start,
274 IOHandlerDelegate &delegate)
275 : IOHandlerEditline(debugger, type,
276 StreamFileSP(), // Inherit input from top input reader
277 StreamFileSP(), // Inherit output from top input reader
278 StreamFileSP(), // Inherit error from top input reader
280 editline_name, // Used for saving history files
281 prompt, continuation_prompt, multi_line, color_prompts,
282 line_number_start, delegate) {}
284 IOHandlerEditline::IOHandlerEditline(
285 Debugger &debugger, IOHandler::Type type,
286 const lldb::StreamFileSP &input_sp, const lldb::StreamFileSP &output_sp,
287 const lldb::StreamFileSP &error_sp, uint32_t flags,
288 const char *editline_name, // Used for saving history files
289 llvm::StringRef prompt, llvm::StringRef continuation_prompt,
290 bool multi_line, bool color_prompts, uint32_t line_number_start,
291 IOHandlerDelegate &delegate)
292 : IOHandler(debugger, type, input_sp, output_sp, error_sp, flags),
293 #ifndef LLDB_DISABLE_LIBEDIT
296 m_delegate(delegate), m_prompt(), m_continuation_prompt(),
297 m_current_lines_ptr(nullptr), m_base_line_number(line_number_start),
298 m_curr_line_idx(UINT32_MAX), m_multi_line(multi_line),
299 m_color_prompts(color_prompts), m_interrupt_exits(true),
303 #ifndef LLDB_DISABLE_LIBEDIT
304 bool use_editline = false;
306 use_editline = m_input_sp->GetFile().GetIsRealTerminal();
309 m_editline_ap.reset(new Editline(editline_name, GetInputFILE(),
310 GetOutputFILE(), GetErrorFILE(),
312 m_editline_ap->SetIsInputCompleteCallback(IsInputCompleteCallback, this);
313 m_editline_ap->SetAutoCompleteCallback(AutoCompleteCallback, this);
314 // See if the delegate supports fixing indentation
315 const char *indent_chars = delegate.IOHandlerGetFixIndentationCharacters();
317 // The delegate does support indentation, hook it up so when any
318 // indentation character is typed, the delegate gets a chance to fix it
319 m_editline_ap->SetFixIndentationCallback(FixIndentationCallback, this,
324 SetBaseLineNumber(m_base_line_number);
326 SetContinuationPrompt(continuation_prompt);
329 IOHandlerEditline::~IOHandlerEditline() {
330 #ifndef LLDB_DISABLE_LIBEDIT
331 m_editline_ap.reset();
335 void IOHandlerEditline::Activate() {
336 IOHandler::Activate();
337 m_delegate.IOHandlerActivated(*this);
340 void IOHandlerEditline::Deactivate() {
341 IOHandler::Deactivate();
342 m_delegate.IOHandlerDeactivated(*this);
345 bool IOHandlerEditline::GetLine(std::string &line, bool &interrupted) {
346 #ifndef LLDB_DISABLE_LIBEDIT
348 return m_editline_ap->GetLine(line, interrupted);
353 FILE *in = GetInputFILE();
355 if (GetIsInteractive()) {
356 const char *prompt = nullptr;
358 if (m_multi_line && m_curr_line_idx > 0)
359 prompt = GetContinuationPrompt();
361 if (prompt == nullptr)
362 prompt = GetPrompt();
364 if (prompt && prompt[0]) {
365 FILE *out = GetOutputFILE();
367 ::fprintf(out, "%s", prompt);
374 bool got_line = false;
377 if (fgets(buffer, sizeof(buffer), in) == nullptr) {
378 const int saved_errno = errno;
381 else if (ferror(in)) {
382 if (saved_errno != EINTR)
387 size_t buffer_len = strlen(buffer);
388 assert(buffer[buffer_len] == '\0');
389 char last_char = buffer[buffer_len - 1];
390 if (last_char == '\r' || last_char == '\n') {
392 // Strip trailing newlines
393 while (last_char == '\r' || last_char == '\n') {
397 last_char = buffer[buffer_len - 1];
400 line.append(buffer, buffer_len);
404 // We might have gotten a newline on a line by itself make sure to return
405 // true in this case.
408 // No more input file, we are done...
412 #ifndef LLDB_DISABLE_LIBEDIT
417 #ifndef LLDB_DISABLE_LIBEDIT
418 bool IOHandlerEditline::IsInputCompleteCallback(Editline *editline,
421 IOHandlerEditline *editline_reader = (IOHandlerEditline *)baton;
422 return editline_reader->m_delegate.IOHandlerIsInputComplete(*editline_reader,
426 int IOHandlerEditline::FixIndentationCallback(Editline *editline,
427 const StringList &lines,
430 IOHandlerEditline *editline_reader = (IOHandlerEditline *)baton;
431 return editline_reader->m_delegate.IOHandlerFixIndentation(
432 *editline_reader, lines, cursor_position);
435 int IOHandlerEditline::AutoCompleteCallback(const char *current_line,
437 const char *last_char,
438 int skip_first_n_matches,
440 StringList &matches, void *baton) {
441 IOHandlerEditline *editline_reader = (IOHandlerEditline *)baton;
443 return editline_reader->m_delegate.IOHandlerComplete(
444 *editline_reader, current_line, cursor, last_char, skip_first_n_matches,
445 max_matches, matches);
450 const char *IOHandlerEditline::GetPrompt() {
451 #ifndef LLDB_DISABLE_LIBEDIT
453 return m_editline_ap->GetPrompt();
456 if (m_prompt.empty())
458 #ifndef LLDB_DISABLE_LIBEDIT
461 return m_prompt.c_str();
464 bool IOHandlerEditline::SetPrompt(llvm::StringRef prompt) {
467 #ifndef LLDB_DISABLE_LIBEDIT
469 m_editline_ap->SetPrompt(m_prompt.empty() ? nullptr : m_prompt.c_str());
474 const char *IOHandlerEditline::GetContinuationPrompt() {
475 return (m_continuation_prompt.empty() ? nullptr
476 : m_continuation_prompt.c_str());
479 void IOHandlerEditline::SetContinuationPrompt(llvm::StringRef prompt) {
480 m_continuation_prompt = prompt;
482 #ifndef LLDB_DISABLE_LIBEDIT
484 m_editline_ap->SetContinuationPrompt(m_continuation_prompt.empty()
486 : m_continuation_prompt.c_str());
490 void IOHandlerEditline::SetBaseLineNumber(uint32_t line) {
491 m_base_line_number = line;
494 uint32_t IOHandlerEditline::GetCurrentLineIndex() const {
495 #ifndef LLDB_DISABLE_LIBEDIT
497 return m_editline_ap->GetCurrentLine();
499 return m_curr_line_idx;
502 bool IOHandlerEditline::GetLines(StringList &lines, bool &interrupted) {
503 m_current_lines_ptr = &lines;
505 bool success = false;
506 #ifndef LLDB_DISABLE_LIBEDIT
508 return m_editline_ap->GetLines(m_base_line_number, lines, interrupted);
515 // Show line numbers if we are asked to
517 if (m_base_line_number > 0 && GetIsInteractive()) {
518 FILE *out = GetOutputFILE();
520 ::fprintf(out, "%u%s", m_base_line_number + (uint32_t)lines.GetSize(),
521 GetPrompt() == nullptr ? " " : "");
524 m_curr_line_idx = lines.GetSize();
526 bool interrupted = false;
527 if (GetLine(line, interrupted) && !interrupted) {
528 lines.AppendString(line);
529 done = m_delegate.IOHandlerIsInputComplete(*this, lines);
534 success = lines.GetSize() > 0;
535 #ifndef LLDB_DISABLE_LIBEDIT
541 // Each IOHandler gets to run until it is done. It should read data from the
542 // "in" and place output into "out" and "err and return when done.
543 void IOHandlerEditline::Run() {
546 bool interrupted = false;
549 if (GetLines(lines, interrupted)) {
551 m_done = m_interrupt_exits;
552 m_delegate.IOHandlerInputInterrupted(*this, line);
555 line = lines.CopyList();
556 m_delegate.IOHandlerInputComplete(*this, line);
562 if (GetLine(line, interrupted)) {
564 m_delegate.IOHandlerInputInterrupted(*this, line);
566 m_delegate.IOHandlerInputComplete(*this, line);
574 void IOHandlerEditline::Cancel() {
575 #ifndef LLDB_DISABLE_LIBEDIT
577 m_editline_ap->Cancel();
581 bool IOHandlerEditline::Interrupt() {
582 // Let the delgate handle it first
583 if (m_delegate.IOHandlerInterrupt(*this))
586 #ifndef LLDB_DISABLE_LIBEDIT
588 return m_editline_ap->Interrupt();
593 void IOHandlerEditline::GotEOF() {
594 #ifndef LLDB_DISABLE_LIBEDIT
596 m_editline_ap->Interrupt();
600 void IOHandlerEditline::PrintAsync(Stream *stream, const char *s, size_t len) {
601 #ifndef LLDB_DISABLE_LIBEDIT
603 m_editline_ap->PrintAsync(stream, s, len);
608 const char *prompt = GetPrompt();
610 // Back up over previous prompt using Windows API
611 CONSOLE_SCREEN_BUFFER_INFO screen_buffer_info;
612 HANDLE console_handle = GetStdHandle(STD_OUTPUT_HANDLE);
613 GetConsoleScreenBufferInfo(console_handle, &screen_buffer_info);
614 COORD coord = screen_buffer_info.dwCursorPosition;
615 coord.X -= strlen(prompt);
618 SetConsoleCursorPosition(console_handle, coord);
621 IOHandler::PrintAsync(stream, s, len);
624 IOHandler::PrintAsync(GetOutputStreamFile().get(), prompt,
630 // we may want curses to be disabled for some builds for instance, windows
631 #ifndef LLDB_DISABLE_CURSES
633 #define KEY_RETURN 10
634 #define KEY_ESCAPE 27
640 class WindowDelegate;
641 typedef std::shared_ptr<Menu> MenuSP;
642 typedef std::shared_ptr<MenuDelegate> MenuDelegateSP;
643 typedef std::shared_ptr<Window> WindowSP;
644 typedef std::shared_ptr<WindowDelegate> WindowDelegateSP;
645 typedef std::vector<MenuSP> Menus;
646 typedef std::vector<WindowSP> Windows;
647 typedef std::vector<WindowDelegateSP> WindowDelegates;
650 type summary add -s "x=${var.x}, y=${var.y}" curses::Point
651 type summary add -s "w=${var.width}, h=${var.height}" curses::Size
652 type summary add -s "${var.origin%S} ${var.size%S}" curses::Rect
659 Point(int _x = 0, int _y = 0) : x(_x), y(_y) {}
666 Point &operator+=(const Point &rhs) {
672 void Dump() { printf("(x=%i, y=%i)\n", x, y); }
675 bool operator==(const Point &lhs, const Point &rhs) {
676 return lhs.x == rhs.x && lhs.y == rhs.y;
679 bool operator!=(const Point &lhs, const Point &rhs) {
680 return lhs.x != rhs.x || lhs.y != rhs.y;
686 Size(int w = 0, int h = 0) : width(w), height(h) {}
693 void Dump() { printf("(w=%i, h=%i)\n", width, height); }
696 bool operator==(const Size &lhs, const Size &rhs) {
697 return lhs.width == rhs.width && lhs.height == rhs.height;
700 bool operator!=(const Size &lhs, const Size &rhs) {
701 return lhs.width != rhs.width || lhs.height != rhs.height;
708 Rect() : origin(), size() {}
710 Rect(const Point &p, const Size &s) : origin(p), size(s) {}
718 printf("(x=%i, y=%i), w=%i, h=%i)\n", origin.x, origin.y, size.width,
722 void Inset(int w, int h) {
723 if (size.width > w * 2)
727 if (size.height > h * 2)
728 size.height -= h * 2;
732 // Return a status bar rectangle which is the last line of this rectangle.
733 // This rectangle will be modified to not include the status bar area.
734 Rect MakeStatusBar() {
736 if (size.height > 1) {
737 status_bar.origin.x = origin.x;
738 status_bar.origin.y = size.height;
739 status_bar.size.width = size.width;
740 status_bar.size.height = 1;
746 // Return a menubar rectangle which is the first line of this rectangle. This
747 // rectangle will be modified to not include the menubar area.
750 if (size.height > 1) {
751 menubar.origin.x = origin.x;
752 menubar.origin.y = origin.y;
753 menubar.size.width = size.width;
754 menubar.size.height = 1;
761 void HorizontalSplitPercentage(float top_percentage, Rect &top,
762 Rect &bottom) const {
763 float top_height = top_percentage * size.height;
764 HorizontalSplit(top_height, top, bottom);
767 void HorizontalSplit(int top_height, Rect &top, Rect &bottom) const {
769 if (top_height < size.height) {
770 top.size.height = top_height;
771 bottom.origin.x = origin.x;
772 bottom.origin.y = origin.y + top.size.height;
773 bottom.size.width = size.width;
774 bottom.size.height = size.height - top.size.height;
780 void VerticalSplitPercentage(float left_percentage, Rect &left,
782 float left_width = left_percentage * size.width;
783 VerticalSplit(left_width, left, right);
786 void VerticalSplit(int left_width, Rect &left, Rect &right) const {
788 if (left_width < size.width) {
789 left.size.width = left_width;
790 right.origin.x = origin.x + left.size.width;
791 right.origin.y = origin.y;
792 right.size.width = size.width - left.size.width;
793 right.size.height = size.height;
800 bool operator==(const Rect &lhs, const Rect &rhs) {
801 return lhs.origin == rhs.origin && lhs.size == rhs.size;
804 bool operator!=(const Rect &lhs, const Rect &rhs) {
805 return lhs.origin != rhs.origin || lhs.size != rhs.size;
808 enum HandleCharResult {
814 enum class MenuActionResult {
817 Quit // Exit all menus and quit
822 const char *description;
825 class WindowDelegate {
827 virtual ~WindowDelegate() = default;
829 virtual bool WindowDelegateDraw(Window &window, bool force) {
830 return false; // Drawing not handled
833 virtual HandleCharResult WindowDelegateHandleChar(Window &window, int key) {
834 return eKeyNotHandled;
837 virtual const char *WindowDelegateGetHelpText() { return nullptr; }
839 virtual KeyHelp *WindowDelegateGetKeyHelp() { return nullptr; }
842 class HelpDialogDelegate : public WindowDelegate {
844 HelpDialogDelegate(const char *text, KeyHelp *key_help_array);
846 ~HelpDialogDelegate() override;
848 bool WindowDelegateDraw(Window &window, bool force) override;
850 HandleCharResult WindowDelegateHandleChar(Window &window, int key) override;
852 size_t GetNumLines() const { return m_text.GetSize(); }
854 size_t GetMaxLineLength() const { return m_text.GetMaxStringLength(); }
858 int m_first_visible_line;
863 Window(const char *name)
864 : m_name(name), m_window(nullptr), m_panel(nullptr), m_parent(nullptr),
865 m_subwindows(), m_delegate_sp(), m_curr_active_window_idx(UINT32_MAX),
866 m_prev_active_window_idx(UINT32_MAX), m_delete(false),
867 m_needs_update(true), m_can_activate(true), m_is_subwin(false) {}
869 Window(const char *name, WINDOW *w, bool del = true)
870 : m_name(name), m_window(nullptr), m_panel(nullptr), m_parent(nullptr),
871 m_subwindows(), m_delegate_sp(), m_curr_active_window_idx(UINT32_MAX),
872 m_prev_active_window_idx(UINT32_MAX), m_delete(del),
873 m_needs_update(true), m_can_activate(true), m_is_subwin(false) {
878 Window(const char *name, const Rect &bounds)
879 : m_name(name), m_window(nullptr), m_parent(nullptr), m_subwindows(),
880 m_delegate_sp(), m_curr_active_window_idx(UINT32_MAX),
881 m_prev_active_window_idx(UINT32_MAX), m_delete(true),
882 m_needs_update(true), m_can_activate(true), m_is_subwin(false) {
883 Reset(::newwin(bounds.size.height, bounds.size.width, bounds.origin.y,
892 void Reset(WINDOW *w = nullptr, bool del = true) {
897 ::del_panel(m_panel);
900 if (m_window && m_delete) {
907 m_panel = ::new_panel(m_window);
912 void AttributeOn(attr_t attr) { ::wattron(m_window, attr); }
913 void AttributeOff(attr_t attr) { ::wattroff(m_window, attr); }
914 void Box(chtype v_char = ACS_VLINE, chtype h_char = ACS_HLINE) {
915 ::box(m_window, v_char, h_char);
917 void Clear() { ::wclear(m_window); }
918 void Erase() { ::werase(m_window); }
920 return Rect(GetParentOrigin(), GetSize());
921 } // Get the rectangle in our parent window
922 int GetChar() { return ::wgetch(m_window); }
923 int GetCursorX() { return getcurx(m_window); }
924 int GetCursorY() { return getcury(m_window); }
926 return Rect(Point(), GetSize());
927 } // Get our rectangle in our own coordinate system
928 Point GetParentOrigin() { return Point(GetParentX(), GetParentY()); }
929 Size GetSize() { return Size(GetWidth(), GetHeight()); }
930 int GetParentX() { return getparx(m_window); }
931 int GetParentY() { return getpary(m_window); }
932 int GetMaxX() { return getmaxx(m_window); }
933 int GetMaxY() { return getmaxy(m_window); }
934 int GetWidth() { return GetMaxX(); }
935 int GetHeight() { return GetMaxY(); }
936 void MoveCursor(int x, int y) { ::wmove(m_window, y, x); }
937 void MoveWindow(int x, int y) { MoveWindow(Point(x, y)); }
938 void Resize(int w, int h) { ::wresize(m_window, h, w); }
939 void Resize(const Size &size) {
940 ::wresize(m_window, size.height, size.width);
942 void PutChar(int ch) { ::waddch(m_window, ch); }
943 void PutCString(const char *s, int len = -1) { ::waddnstr(m_window, s, len); }
944 void Refresh() { ::wrefresh(m_window); }
945 void DeferredRefresh() {
946 // We are using panels, so we don't need to call this...
947 //::wnoutrefresh(m_window);
949 void SetBackground(int color_pair_idx) {
950 ::wbkgd(m_window, COLOR_PAIR(color_pair_idx));
952 void UnderlineOn() { AttributeOn(A_UNDERLINE); }
953 void UnderlineOff() { AttributeOff(A_UNDERLINE); }
955 void PutCStringTruncated(const char *s, int right_pad) {
956 int bytes_left = GetWidth() - GetCursorX();
957 if (bytes_left > right_pad) {
958 bytes_left -= right_pad;
959 ::waddnstr(m_window, s, bytes_left);
963 void MoveWindow(const Point &origin) {
964 const bool moving_window = origin != GetParentOrigin();
965 if (m_is_subwin && moving_window) {
966 // Can't move subwindows, must delete and re-create
967 Size size = GetSize();
968 Reset(::subwin(m_parent->m_window, size.height, size.width, origin.y,
972 ::mvwin(m_window, origin.y, origin.x);
976 void SetBounds(const Rect &bounds) {
977 const bool moving_window = bounds.origin != GetParentOrigin();
978 if (m_is_subwin && moving_window) {
979 // Can't move subwindows, must delete and re-create
980 Reset(::subwin(m_parent->m_window, bounds.size.height, bounds.size.width,
981 bounds.origin.y, bounds.origin.x),
985 MoveWindow(bounds.origin);
990 void Printf(const char *format, ...) __attribute__((format(printf, 2, 3))) {
992 va_start(args, format);
993 vwprintw(m_window, format, args);
998 ::touchwin(m_window);
1003 WindowSP CreateSubWindow(const char *name, const Rect &bounds,
1005 WindowSP subwindow_sp;
1007 subwindow_sp.reset(new Window(
1008 name, ::subwin(m_window, bounds.size.height, bounds.size.width,
1009 bounds.origin.y, bounds.origin.x),
1011 subwindow_sp->m_is_subwin = true;
1014 new Window(name, ::newwin(bounds.size.height, bounds.size.width,
1015 bounds.origin.y, bounds.origin.x),
1017 subwindow_sp->m_is_subwin = false;
1019 subwindow_sp->m_parent = this;
1021 m_prev_active_window_idx = m_curr_active_window_idx;
1022 m_curr_active_window_idx = m_subwindows.size();
1024 m_subwindows.push_back(subwindow_sp);
1025 ::top_panel(subwindow_sp->m_panel);
1026 m_needs_update = true;
1027 return subwindow_sp;
1030 bool RemoveSubWindow(Window *window) {
1031 Windows::iterator pos, end = m_subwindows.end();
1033 for (pos = m_subwindows.begin(); pos != end; ++pos, ++i) {
1034 if ((*pos).get() == window) {
1035 if (m_prev_active_window_idx == i)
1036 m_prev_active_window_idx = UINT32_MAX;
1037 else if (m_prev_active_window_idx != UINT32_MAX &&
1038 m_prev_active_window_idx > i)
1039 --m_prev_active_window_idx;
1041 if (m_curr_active_window_idx == i)
1042 m_curr_active_window_idx = UINT32_MAX;
1043 else if (m_curr_active_window_idx != UINT32_MAX &&
1044 m_curr_active_window_idx > i)
1045 --m_curr_active_window_idx;
1047 m_subwindows.erase(pos);
1048 m_needs_update = true;
1059 WindowSP FindSubWindow(const char *name) {
1060 Windows::iterator pos, end = m_subwindows.end();
1062 for (pos = m_subwindows.begin(); pos != end; ++pos, ++i) {
1063 if ((*pos)->m_name.compare(name) == 0)
1069 void RemoveSubWindows() {
1070 m_curr_active_window_idx = UINT32_MAX;
1071 m_prev_active_window_idx = UINT32_MAX;
1072 for (Windows::iterator pos = m_subwindows.begin();
1073 pos != m_subwindows.end(); pos = m_subwindows.erase(pos)) {
1082 WINDOW *get() { return m_window; }
1084 operator WINDOW *() { return m_window; }
1086 //----------------------------------------------------------------------
1087 // Window drawing utilities
1088 //----------------------------------------------------------------------
1089 void DrawTitleBox(const char *title, const char *bottom_message = nullptr) {
1092 attr = A_BOLD | COLOR_PAIR(2);
1101 if (title && title[0]) {
1107 if (bottom_message && bottom_message[0]) {
1108 int bottom_message_length = strlen(bottom_message);
1109 int x = GetWidth() - 3 - (bottom_message_length + 2);
1112 MoveCursor(x, GetHeight() - 1);
1114 PutCString(bottom_message);
1117 MoveCursor(1, GetHeight() - 1);
1119 PutCStringTruncated(bottom_message, 1);
1126 virtual void Draw(bool force) {
1127 if (m_delegate_sp && m_delegate_sp->WindowDelegateDraw(*this, force))
1130 for (auto &subwindow_sp : m_subwindows)
1131 subwindow_sp->Draw(force);
1134 bool CreateHelpSubwindow() {
1135 if (m_delegate_sp) {
1136 const char *text = m_delegate_sp->WindowDelegateGetHelpText();
1137 KeyHelp *key_help = m_delegate_sp->WindowDelegateGetKeyHelp();
1138 if ((text && text[0]) || key_help) {
1139 std::unique_ptr<HelpDialogDelegate> help_delegate_ap(
1140 new HelpDialogDelegate(text, key_help));
1141 const size_t num_lines = help_delegate_ap->GetNumLines();
1142 const size_t max_length = help_delegate_ap->GetMaxLineLength();
1143 Rect bounds = GetBounds();
1145 if (max_length + 4 < static_cast<size_t>(bounds.size.width)) {
1146 bounds.origin.x += (bounds.size.width - max_length + 4) / 2;
1147 bounds.size.width = max_length + 4;
1149 if (bounds.size.width > 100) {
1150 const int inset_w = bounds.size.width / 4;
1151 bounds.origin.x += inset_w;
1152 bounds.size.width -= 2 * inset_w;
1156 if (num_lines + 2 < static_cast<size_t>(bounds.size.height)) {
1157 bounds.origin.y += (bounds.size.height - num_lines + 2) / 2;
1158 bounds.size.height = num_lines + 2;
1160 if (bounds.size.height > 100) {
1161 const int inset_h = bounds.size.height / 4;
1162 bounds.origin.y += inset_h;
1163 bounds.size.height -= 2 * inset_h;
1166 WindowSP help_window_sp;
1167 Window *parent_window = GetParent();
1169 help_window_sp = parent_window->CreateSubWindow("Help", bounds, true);
1171 help_window_sp = CreateSubWindow("Help", bounds, true);
1172 help_window_sp->SetDelegate(
1173 WindowDelegateSP(help_delegate_ap.release()));
1180 virtual HandleCharResult HandleChar(int key) {
1181 // Always check the active window first
1182 HandleCharResult result = eKeyNotHandled;
1183 WindowSP active_window_sp = GetActiveWindow();
1184 if (active_window_sp) {
1185 result = active_window_sp->HandleChar(key);
1186 if (result != eKeyNotHandled)
1190 if (m_delegate_sp) {
1191 result = m_delegate_sp->WindowDelegateHandleChar(*this, key);
1192 if (result != eKeyNotHandled)
1196 // Then check for any windows that want any keys that weren't handled. This
1197 // is typically only for a menubar. Make a copy of the subwindows in case
1198 // any HandleChar() functions muck with the subwindows. If we don't do
1199 // this, we can crash when iterating over the subwindows.
1200 Windows subwindows(m_subwindows);
1201 for (auto subwindow_sp : subwindows) {
1202 if (!subwindow_sp->m_can_activate) {
1203 HandleCharResult result = subwindow_sp->HandleChar(key);
1204 if (result != eKeyNotHandled)
1209 return eKeyNotHandled;
1212 bool SetActiveWindow(Window *window) {
1213 const size_t num_subwindows = m_subwindows.size();
1214 for (size_t i = 0; i < num_subwindows; ++i) {
1215 if (m_subwindows[i].get() == window) {
1216 m_prev_active_window_idx = m_curr_active_window_idx;
1217 ::top_panel(window->m_panel);
1218 m_curr_active_window_idx = i;
1225 WindowSP GetActiveWindow() {
1226 if (!m_subwindows.empty()) {
1227 if (m_curr_active_window_idx >= m_subwindows.size()) {
1228 if (m_prev_active_window_idx < m_subwindows.size()) {
1229 m_curr_active_window_idx = m_prev_active_window_idx;
1230 m_prev_active_window_idx = UINT32_MAX;
1231 } else if (IsActive()) {
1232 m_prev_active_window_idx = UINT32_MAX;
1233 m_curr_active_window_idx = UINT32_MAX;
1235 // Find first window that wants to be active if this window is active
1236 const size_t num_subwindows = m_subwindows.size();
1237 for (size_t i = 0; i < num_subwindows; ++i) {
1238 if (m_subwindows[i]->GetCanBeActive()) {
1239 m_curr_active_window_idx = i;
1246 if (m_curr_active_window_idx < m_subwindows.size())
1247 return m_subwindows[m_curr_active_window_idx];
1252 bool GetCanBeActive() const { return m_can_activate; }
1254 void SetCanBeActive(bool b) { m_can_activate = b; }
1256 const WindowDelegateSP &GetDelegate() const { return m_delegate_sp; }
1258 void SetDelegate(const WindowDelegateSP &delegate_sp) {
1259 m_delegate_sp = delegate_sp;
1262 Window *GetParent() const { return m_parent; }
1264 bool IsActive() const {
1266 return m_parent->GetActiveWindow().get() == this;
1268 return true; // Top level window is always active
1271 void SelectNextWindowAsActive() {
1272 // Move active focus to next window
1273 const size_t num_subwindows = m_subwindows.size();
1274 if (m_curr_active_window_idx == UINT32_MAX) {
1276 for (auto subwindow_sp : m_subwindows) {
1277 if (subwindow_sp->GetCanBeActive()) {
1278 m_curr_active_window_idx = idx;
1283 } else if (m_curr_active_window_idx + 1 < num_subwindows) {
1284 bool handled = false;
1285 m_prev_active_window_idx = m_curr_active_window_idx;
1286 for (size_t idx = m_curr_active_window_idx + 1; idx < num_subwindows;
1288 if (m_subwindows[idx]->GetCanBeActive()) {
1289 m_curr_active_window_idx = idx;
1295 for (size_t idx = 0; idx <= m_prev_active_window_idx; ++idx) {
1296 if (m_subwindows[idx]->GetCanBeActive()) {
1297 m_curr_active_window_idx = idx;
1303 m_prev_active_window_idx = m_curr_active_window_idx;
1304 for (size_t idx = 0; idx < num_subwindows; ++idx) {
1305 if (m_subwindows[idx]->GetCanBeActive()) {
1306 m_curr_active_window_idx = idx;
1313 const char *GetName() const { return m_name.c_str(); }
1320 Windows m_subwindows;
1321 WindowDelegateSP m_delegate_sp;
1322 uint32_t m_curr_active_window_idx;
1323 uint32_t m_prev_active_window_idx;
1325 bool m_needs_update;
1326 bool m_can_activate;
1330 DISALLOW_COPY_AND_ASSIGN(Window);
1333 class MenuDelegate {
1335 virtual ~MenuDelegate() = default;
1337 virtual MenuActionResult MenuDelegateAction(Menu &menu) = 0;
1340 class Menu : public WindowDelegate {
1342 enum class Type { Invalid, Bar, Item, Separator };
1344 // Menubar or separator constructor
1347 // Menuitem constructor
1348 Menu(const char *name, const char *key_name, int key_value,
1349 uint64_t identifier);
1351 ~Menu() override = default;
1353 const MenuDelegateSP &GetDelegate() const { return m_delegate_sp; }
1355 void SetDelegate(const MenuDelegateSP &delegate_sp) {
1356 m_delegate_sp = delegate_sp;
1359 void RecalculateNameLengths();
1361 void AddSubmenu(const MenuSP &menu_sp);
1363 int DrawAndRunMenu(Window &window);
1365 void DrawMenuTitle(Window &window, bool highlight);
1367 bool WindowDelegateDraw(Window &window, bool force) override;
1369 HandleCharResult WindowDelegateHandleChar(Window &window, int key) override;
1371 MenuActionResult ActionPrivate(Menu &menu) {
1372 MenuActionResult result = MenuActionResult::NotHandled;
1373 if (m_delegate_sp) {
1374 result = m_delegate_sp->MenuDelegateAction(menu);
1375 if (result != MenuActionResult::NotHandled)
1377 } else if (m_parent) {
1378 result = m_parent->ActionPrivate(menu);
1379 if (result != MenuActionResult::NotHandled)
1382 return m_canned_result;
1385 MenuActionResult Action() {
1386 // Call the recursive action so it can try to handle it with the menu
1387 // delegate, and if not, try our parent menu
1388 return ActionPrivate(*this);
1391 void SetCannedResult(MenuActionResult result) { m_canned_result = result; }
1393 Menus &GetSubmenus() { return m_submenus; }
1395 const Menus &GetSubmenus() const { return m_submenus; }
1397 int GetSelectedSubmenuIndex() const { return m_selected; }
1399 void SetSelectedSubmenuIndex(int idx) { m_selected = idx; }
1401 Type GetType() const { return m_type; }
1403 int GetStartingColumn() const { return m_start_col; }
1405 void SetStartingColumn(int col) { m_start_col = col; }
1407 int GetKeyValue() const { return m_key_value; }
1409 void SetKeyValue(int key_value) { m_key_value = key_value; }
1411 std::string &GetName() { return m_name; }
1413 std::string &GetKeyName() { return m_key_name; }
1415 int GetDrawWidth() const {
1416 return m_max_submenu_name_length + m_max_submenu_key_name_length + 8;
1419 uint64_t GetIdentifier() const { return m_identifier; }
1421 void SetIdentifier(uint64_t identifier) { m_identifier = identifier; }
1425 std::string m_key_name;
1426 uint64_t m_identifier;
1430 int m_max_submenu_name_length;
1431 int m_max_submenu_key_name_length;
1435 WindowSP m_menu_window_sp;
1436 MenuActionResult m_canned_result;
1437 MenuDelegateSP m_delegate_sp;
1440 // Menubar or separator constructor
1441 Menu::Menu(Type type)
1442 : m_name(), m_key_name(), m_identifier(0), m_type(type), m_key_value(0),
1443 m_start_col(0), m_max_submenu_name_length(0),
1444 m_max_submenu_key_name_length(0), m_selected(0), m_parent(nullptr),
1445 m_submenus(), m_canned_result(MenuActionResult::NotHandled),
1448 // Menuitem constructor
1449 Menu::Menu(const char *name, const char *key_name, int key_value,
1450 uint64_t identifier)
1451 : m_name(), m_key_name(), m_identifier(identifier), m_type(Type::Invalid),
1452 m_key_value(key_value), m_start_col(0), m_max_submenu_name_length(0),
1453 m_max_submenu_key_name_length(0), m_selected(0), m_parent(nullptr),
1454 m_submenus(), m_canned_result(MenuActionResult::NotHandled),
1456 if (name && name[0]) {
1458 m_type = Type::Item;
1459 if (key_name && key_name[0])
1460 m_key_name = key_name;
1462 m_type = Type::Separator;
1466 void Menu::RecalculateNameLengths() {
1467 m_max_submenu_name_length = 0;
1468 m_max_submenu_key_name_length = 0;
1469 Menus &submenus = GetSubmenus();
1470 const size_t num_submenus = submenus.size();
1471 for (size_t i = 0; i < num_submenus; ++i) {
1472 Menu *submenu = submenus[i].get();
1473 if (static_cast<size_t>(m_max_submenu_name_length) < submenu->m_name.size())
1474 m_max_submenu_name_length = submenu->m_name.size();
1475 if (static_cast<size_t>(m_max_submenu_key_name_length) <
1476 submenu->m_key_name.size())
1477 m_max_submenu_key_name_length = submenu->m_key_name.size();
1481 void Menu::AddSubmenu(const MenuSP &menu_sp) {
1482 menu_sp->m_parent = this;
1483 if (static_cast<size_t>(m_max_submenu_name_length) < menu_sp->m_name.size())
1484 m_max_submenu_name_length = menu_sp->m_name.size();
1485 if (static_cast<size_t>(m_max_submenu_key_name_length) <
1486 menu_sp->m_key_name.size())
1487 m_max_submenu_key_name_length = menu_sp->m_key_name.size();
1488 m_submenus.push_back(menu_sp);
1491 void Menu::DrawMenuTitle(Window &window, bool highlight) {
1492 if (m_type == Type::Separator) {
1493 window.MoveCursor(0, window.GetCursorY());
1494 window.PutChar(ACS_LTEE);
1495 int width = window.GetWidth();
1498 for (int i = 0; i < width; ++i)
1499 window.PutChar(ACS_HLINE);
1501 window.PutChar(ACS_RTEE);
1503 const int shortcut_key = m_key_value;
1504 bool underlined_shortcut = false;
1505 const attr_t hilgight_attr = A_REVERSE;
1507 window.AttributeOn(hilgight_attr);
1508 if (isprint(shortcut_key)) {
1509 size_t lower_pos = m_name.find(tolower(shortcut_key));
1510 size_t upper_pos = m_name.find(toupper(shortcut_key));
1511 const char *name = m_name.c_str();
1512 size_t pos = std::min<size_t>(lower_pos, upper_pos);
1513 if (pos != std::string::npos) {
1514 underlined_shortcut = true;
1516 window.PutCString(name, pos);
1519 const attr_t shortcut_attr = A_UNDERLINE | A_BOLD;
1520 window.AttributeOn(shortcut_attr);
1521 window.PutChar(name[0]);
1522 window.AttributeOff(shortcut_attr);
1525 window.PutCString(name);
1529 if (!underlined_shortcut) {
1530 window.PutCString(m_name.c_str());
1534 window.AttributeOff(hilgight_attr);
1536 if (m_key_name.empty()) {
1537 if (!underlined_shortcut && isprint(m_key_value)) {
1538 window.AttributeOn(COLOR_PAIR(3));
1539 window.Printf(" (%c)", m_key_value);
1540 window.AttributeOff(COLOR_PAIR(3));
1543 window.AttributeOn(COLOR_PAIR(3));
1544 window.Printf(" (%s)", m_key_name.c_str());
1545 window.AttributeOff(COLOR_PAIR(3));
1550 bool Menu::WindowDelegateDraw(Window &window, bool force) {
1551 Menus &submenus = GetSubmenus();
1552 const size_t num_submenus = submenus.size();
1553 const int selected_idx = GetSelectedSubmenuIndex();
1554 Menu::Type menu_type = GetType();
1555 switch (menu_type) {
1556 case Menu::Type::Bar: {
1557 window.SetBackground(2);
1558 window.MoveCursor(0, 0);
1559 for (size_t i = 0; i < num_submenus; ++i) {
1560 Menu *menu = submenus[i].get();
1562 window.PutChar(' ');
1563 menu->SetStartingColumn(window.GetCursorX());
1564 window.PutCString("| ");
1565 menu->DrawMenuTitle(window, false);
1567 window.PutCString(" |");
1568 window.DeferredRefresh();
1571 case Menu::Type::Item: {
1578 window.SetBackground(2);
1580 for (size_t i = 0; i < num_submenus; ++i) {
1581 const bool is_selected = (i == static_cast<size_t>(selected_idx));
1582 window.MoveCursor(x, y + i);
1584 // Remember where we want the cursor to be
1588 submenus[i]->DrawMenuTitle(window, is_selected);
1590 window.MoveCursor(cursor_x, cursor_y);
1591 window.DeferredRefresh();
1595 case Menu::Type::Separator:
1598 return true; // Drawing handled...
1601 HandleCharResult Menu::WindowDelegateHandleChar(Window &window, int key) {
1602 HandleCharResult result = eKeyNotHandled;
1604 Menus &submenus = GetSubmenus();
1605 const size_t num_submenus = submenus.size();
1606 const int selected_idx = GetSelectedSubmenuIndex();
1607 Menu::Type menu_type = GetType();
1608 if (menu_type == Menu::Type::Bar) {
1613 // Show last menu or first menu
1614 if (selected_idx < static_cast<int>(num_submenus))
1615 run_menu_sp = submenus[selected_idx];
1616 else if (!submenus.empty())
1617 run_menu_sp = submenus.front();
1618 result = eKeyHandled;
1623 if (m_selected >= static_cast<int>(num_submenus))
1625 if (m_selected < static_cast<int>(num_submenus))
1626 run_menu_sp = submenus[m_selected];
1627 else if (!submenus.empty())
1628 run_menu_sp = submenus.front();
1629 result = eKeyHandled;
1635 m_selected = num_submenus - 1;
1636 if (m_selected < static_cast<int>(num_submenus))
1637 run_menu_sp = submenus[m_selected];
1638 else if (!submenus.empty())
1639 run_menu_sp = submenus.front();
1640 result = eKeyHandled;
1644 for (size_t i = 0; i < num_submenus; ++i) {
1645 if (submenus[i]->GetKeyValue() == key) {
1646 SetSelectedSubmenuIndex(i);
1647 run_menu_sp = submenus[i];
1648 result = eKeyHandled;
1656 // Run the action on this menu in case we need to populate the menu with
1657 // dynamic content and also in case check marks, and any other menu
1658 // decorations need to be calculated
1659 if (run_menu_sp->Action() == MenuActionResult::Quit)
1660 return eQuitApplication;
1663 menu_bounds.origin.x = run_menu_sp->GetStartingColumn();
1664 menu_bounds.origin.y = 1;
1665 menu_bounds.size.width = run_menu_sp->GetDrawWidth();
1666 menu_bounds.size.height = run_menu_sp->GetSubmenus().size() + 2;
1667 if (m_menu_window_sp)
1668 window.GetParent()->RemoveSubWindow(m_menu_window_sp.get());
1670 m_menu_window_sp = window.GetParent()->CreateSubWindow(
1671 run_menu_sp->GetName().c_str(), menu_bounds, true);
1672 m_menu_window_sp->SetDelegate(run_menu_sp);
1674 } else if (menu_type == Menu::Type::Item) {
1677 if (m_submenus.size() > 1) {
1678 const int start_select = m_selected;
1679 while (++m_selected != start_select) {
1680 if (static_cast<size_t>(m_selected) >= num_submenus)
1682 if (m_submenus[m_selected]->GetType() == Type::Separator)
1692 if (m_submenus.size() > 1) {
1693 const int start_select = m_selected;
1694 while (--m_selected != start_select) {
1695 if (m_selected < static_cast<int>(0))
1696 m_selected = num_submenus - 1;
1697 if (m_submenus[m_selected]->GetType() == Type::Separator)
1707 if (static_cast<size_t>(selected_idx) < num_submenus) {
1708 if (submenus[selected_idx]->Action() == MenuActionResult::Quit)
1709 return eQuitApplication;
1710 window.GetParent()->RemoveSubWindow(&window);
1715 case KEY_ESCAPE: // Beware: pressing escape key has 1 to 2 second delay in
1716 // case other chars are entered for escaped sequences
1717 window.GetParent()->RemoveSubWindow(&window);
1721 for (size_t i = 0; i < num_submenus; ++i) {
1722 Menu *menu = submenus[i].get();
1723 if (menu->GetKeyValue() == key) {
1724 SetSelectedSubmenuIndex(i);
1725 window.GetParent()->RemoveSubWindow(&window);
1726 if (menu->Action() == MenuActionResult::Quit)
1727 return eQuitApplication;
1733 } else if (menu_type == Menu::Type::Separator) {
1740 Application(FILE *in, FILE *out)
1741 : m_window_sp(), m_screen(nullptr), m_in(in), m_out(out) {}
1744 m_window_delegates.clear();
1745 m_window_sp.reset();
1747 ::delscreen(m_screen);
1753 ::setlocale(LC_ALL, "");
1754 ::setlocale(LC_CTYPE, "");
1758 m_screen = ::newterm(nullptr, m_out, m_in);
1763 ::keypad(stdscr, TRUE);
1766 void Terminate() { ::endwin(); }
1768 void Run(Debugger &debugger) {
1770 int delay_in_tenths_of_a_second = 1;
1772 // Alas the threading model in curses is a bit lame so we need to resort to
1773 // polling every 0.5 seconds. We could poll for stdin ourselves and then
1774 // pass the keys down but then we need to translate all of the escape
1775 // sequences ourselves. So we resort to polling for input because we need
1776 // to receive async process events while in this loop.
1778 halfdelay(delay_in_tenths_of_a_second); // Poll using some number of tenths
1779 // of seconds seconds when calling
1780 // Window::GetChar()
1782 ListenerSP listener_sp(
1783 Listener::MakeListener("lldb.IOHandler.curses.Application"));
1784 ConstString broadcaster_class_target(Target::GetStaticBroadcasterClass());
1785 ConstString broadcaster_class_process(Process::GetStaticBroadcasterClass());
1786 ConstString broadcaster_class_thread(Thread::GetStaticBroadcasterClass());
1787 debugger.EnableForwardEvents(listener_sp);
1790 #if defined(__APPLE__)
1791 std::deque<int> escape_chars;
1796 m_window_sp->Draw(false);
1797 // All windows should be calling Window::DeferredRefresh() instead of
1798 // Window::Refresh() so we can do a single update and avoid any screen
1802 // Cursor hiding isn't working on MacOSX, so hide it in the top left
1804 m_window_sp->MoveCursor(0, 0);
1810 #if defined(__APPLE__)
1811 // Terminal.app doesn't map its function keys correctly, F1-F4 default
1812 // to: \033OP, \033OQ, \033OR, \033OS, so lets take care of this here if
1815 if (escape_chars.empty())
1816 ch = m_window_sp->GetChar();
1818 ch = escape_chars.front();
1819 escape_chars.pop_front();
1821 if (ch == KEY_ESCAPE) {
1822 int ch2 = m_window_sp->GetChar();
1824 int ch3 = m_window_sp->GetChar();
1839 escape_chars.push_back(ch2);
1841 escape_chars.push_back(ch3);
1844 } else if (ch2 != -1)
1845 escape_chars.push_back(ch2);
1848 int ch = m_window_sp->GetChar();
1852 if (feof(m_in) || ferror(m_in)) {
1855 // Just a timeout from using halfdelay(), check for events
1857 while (listener_sp->PeekAtNextEvent()) {
1858 listener_sp->GetEvent(event_sp, std::chrono::seconds(0));
1861 Broadcaster *broadcaster = event_sp->GetBroadcaster();
1863 // uint32_t event_type = event_sp->GetType();
1864 ConstString broadcaster_class(
1865 broadcaster->GetBroadcasterClass());
1866 if (broadcaster_class == broadcaster_class_process) {
1867 debugger.GetCommandInterpreter().UpdateExecutionContext(
1870 continue; // Don't get any key, just update our view
1877 HandleCharResult key_result = m_window_sp->HandleChar(ch);
1878 switch (key_result) {
1880 debugger.GetCommandInterpreter().UpdateExecutionContext(nullptr);
1883 case eKeyNotHandled:
1885 case eQuitApplication:
1892 debugger.CancelForwardEvents(listener_sp);
1895 WindowSP &GetMainWindow() {
1897 m_window_sp.reset(new Window("main", stdscr, false));
1901 WindowDelegates &GetWindowDelegates() { return m_window_delegates; }
1904 WindowSP m_window_sp;
1905 WindowDelegates m_window_delegates;
1911 } // namespace curses
1913 using namespace curses;
1916 ValueObjectManager value;
1918 // The process stop ID when the children were calculated.
1919 uint32_t children_stop_id;
1923 bool might_have_children;
1925 bool calculated_children;
1926 std::vector<Row> children;
1928 Row(const ValueObjectSP &v, Row *p)
1929 : value(v, lldb::eDynamicDontRunTarget, true), parent(p), row_idx(0),
1930 x(1), y(1), might_have_children(v ? v->MightHaveChildren() : false),
1931 expanded(false), calculated_children(false), children() {}
1933 size_t GetDepth() const {
1935 return 1 + parent->GetDepth();
1943 std::vector<Row> &GetChildren() {
1944 ProcessSP process_sp = value.GetProcessSP();
1945 auto stop_id = process_sp->GetStopID();
1946 if (process_sp && stop_id != children_stop_id) {
1947 children_stop_id = stop_id;
1948 calculated_children = false;
1950 if (!calculated_children) {
1952 calculated_children = true;
1953 ValueObjectSP valobj = value.GetSP();
1955 const size_t num_children = valobj->GetNumChildren();
1956 for (size_t i = 0; i < num_children; ++i) {
1957 children.push_back(Row(valobj->GetChildAtIndex(i, true), this));
1966 calculated_children = false;
1970 void DrawTree(Window &window) {
1972 parent->DrawTreeForChild(window, this, 0);
1974 if (might_have_children) {
1975 // It we can get UTF8 characters to work we should try to use the
1976 // "symbol" UTF8 string below
1977 // const char *symbol = "";
1978 // if (row.expanded)
1979 // symbol = "\xe2\x96\xbd ";
1981 // symbol = "\xe2\x96\xb7 ";
1982 // window.PutCString (symbol);
1984 // The ACS_DARROW and ACS_RARROW don't look very nice they are just a 'v'
1985 // or '>' character...
1987 // window.PutChar (ACS_DARROW);
1989 // window.PutChar (ACS_RARROW);
1990 // Since we can't find any good looking right arrow/down arrow symbols,
1991 // just use a diamond...
1992 window.PutChar(ACS_DIAMOND);
1993 window.PutChar(ACS_HLINE);
1997 void DrawTreeForChild(Window &window, Row *child, uint32_t reverse_depth) {
1999 parent->DrawTreeForChild(window, this, reverse_depth + 1);
2001 if (&GetChildren().back() == child) {
2003 if (reverse_depth == 0) {
2004 window.PutChar(ACS_LLCORNER);
2005 window.PutChar(ACS_HLINE);
2007 window.PutChar(' ');
2008 window.PutChar(' ');
2011 if (reverse_depth == 0) {
2012 window.PutChar(ACS_LTEE);
2013 window.PutChar(ACS_HLINE);
2015 window.PutChar(ACS_VLINE);
2016 window.PutChar(' ');
2022 struct DisplayOptions {
2028 class TreeDelegate {
2030 TreeDelegate() = default;
2031 virtual ~TreeDelegate() = default;
2033 virtual void TreeDelegateDrawTreeItem(TreeItem &item, Window &window) = 0;
2034 virtual void TreeDelegateGenerateChildren(TreeItem &item) = 0;
2035 virtual bool TreeDelegateItemSelected(
2036 TreeItem &item) = 0; // Return true if we need to update views
2039 typedef std::shared_ptr<TreeDelegate> TreeDelegateSP;
2043 TreeItem(TreeItem *parent, TreeDelegate &delegate, bool might_have_children)
2044 : m_parent(parent), m_delegate(delegate), m_user_data(nullptr),
2045 m_identifier(0), m_row_idx(-1), m_children(),
2046 m_might_have_children(might_have_children), m_is_expanded(false) {}
2048 TreeItem &operator=(const TreeItem &rhs) {
2050 m_parent = rhs.m_parent;
2051 m_delegate = rhs.m_delegate;
2052 m_user_data = rhs.m_user_data;
2053 m_identifier = rhs.m_identifier;
2054 m_row_idx = rhs.m_row_idx;
2055 m_children = rhs.m_children;
2056 m_might_have_children = rhs.m_might_have_children;
2057 m_is_expanded = rhs.m_is_expanded;
2062 size_t GetDepth() const {
2064 return 1 + m_parent->GetDepth();
2068 int GetRowIndex() const { return m_row_idx; }
2070 void ClearChildren() { m_children.clear(); }
2072 void Resize(size_t n, const TreeItem &t) { m_children.resize(n, t); }
2074 TreeItem &operator[](size_t i) { return m_children[i]; }
2076 void SetRowIndex(int row_idx) { m_row_idx = row_idx; }
2078 size_t GetNumChildren() {
2079 m_delegate.TreeDelegateGenerateChildren(*this);
2080 return m_children.size();
2083 void ItemWasSelected() { m_delegate.TreeDelegateItemSelected(*this); }
2085 void CalculateRowIndexes(int &row_idx) {
2086 SetRowIndex(row_idx);
2089 const bool expanded = IsExpanded();
2091 // The root item must calculate its children, or we must calculate the
2092 // number of children if the item is expanded
2093 if (m_parent == nullptr || expanded)
2096 for (auto &item : m_children) {
2098 item.CalculateRowIndexes(row_idx);
2100 item.SetRowIndex(-1);
2104 TreeItem *GetParent() { return m_parent; }
2106 bool IsExpanded() const { return m_is_expanded; }
2108 void Expand() { m_is_expanded = true; }
2110 void Unexpand() { m_is_expanded = false; }
2112 bool Draw(Window &window, const int first_visible_row,
2113 const uint32_t selected_row_idx, int &row_idx, int &num_rows_left) {
2114 if (num_rows_left <= 0)
2117 if (m_row_idx >= first_visible_row) {
2118 window.MoveCursor(2, row_idx + 1);
2121 m_parent->DrawTreeForChild(window, this, 0);
2123 if (m_might_have_children) {
2124 // It we can get UTF8 characters to work we should try to use the
2125 // "symbol" UTF8 string below
2126 // const char *symbol = "";
2127 // if (row.expanded)
2128 // symbol = "\xe2\x96\xbd ";
2130 // symbol = "\xe2\x96\xb7 ";
2131 // window.PutCString (symbol);
2133 // The ACS_DARROW and ACS_RARROW don't look very nice they are just a
2134 // 'v' or '>' character...
2136 // window.PutChar (ACS_DARROW);
2138 // window.PutChar (ACS_RARROW);
2139 // Since we can't find any good looking right arrow/down arrow symbols,
2140 // just use a diamond...
2141 window.PutChar(ACS_DIAMOND);
2142 window.PutChar(ACS_HLINE);
2144 bool highlight = (selected_row_idx == static_cast<size_t>(m_row_idx)) &&
2148 window.AttributeOn(A_REVERSE);
2150 m_delegate.TreeDelegateDrawTreeItem(*this, window);
2153 window.AttributeOff(A_REVERSE);
2158 if (num_rows_left <= 0)
2159 return false; // We are done drawing...
2162 for (auto &item : m_children) {
2163 // If we displayed all the rows and item.Draw() returns false we are
2164 // done drawing and can exit this for loop
2165 if (!item.Draw(window, first_visible_row, selected_row_idx, row_idx,
2170 return num_rows_left >= 0; // Return true if not done drawing yet
2173 void DrawTreeForChild(Window &window, TreeItem *child,
2174 uint32_t reverse_depth) {
2176 m_parent->DrawTreeForChild(window, this, reverse_depth + 1);
2178 if (&m_children.back() == child) {
2180 if (reverse_depth == 0) {
2181 window.PutChar(ACS_LLCORNER);
2182 window.PutChar(ACS_HLINE);
2184 window.PutChar(' ');
2185 window.PutChar(' ');
2188 if (reverse_depth == 0) {
2189 window.PutChar(ACS_LTEE);
2190 window.PutChar(ACS_HLINE);
2192 window.PutChar(ACS_VLINE);
2193 window.PutChar(' ');
2198 TreeItem *GetItemForRowIndex(uint32_t row_idx) {
2199 if (static_cast<uint32_t>(m_row_idx) == row_idx)
2201 if (m_children.empty())
2204 for (auto &item : m_children) {
2205 TreeItem *selected_item_ptr = item.GetItemForRowIndex(row_idx);
2206 if (selected_item_ptr)
2207 return selected_item_ptr;
2213 void *GetUserData() const { return m_user_data; }
2215 void SetUserData(void *user_data) { m_user_data = user_data; }
2217 uint64_t GetIdentifier() const { return m_identifier; }
2219 void SetIdentifier(uint64_t identifier) { m_identifier = identifier; }
2221 void SetMightHaveChildren(bool b) { m_might_have_children = b; }
2225 TreeDelegate &m_delegate;
2227 uint64_t m_identifier;
2228 int m_row_idx; // Zero based visible row index, -1 if not visible or for the
2230 std::vector<TreeItem> m_children;
2231 bool m_might_have_children;
2235 class TreeWindowDelegate : public WindowDelegate {
2237 TreeWindowDelegate(Debugger &debugger, const TreeDelegateSP &delegate_sp)
2238 : m_debugger(debugger), m_delegate_sp(delegate_sp),
2239 m_root(nullptr, *delegate_sp, true), m_selected_item(nullptr),
2240 m_num_rows(0), m_selected_row_idx(0), m_first_visible_row(0),
2241 m_min_x(0), m_min_y(0), m_max_x(0), m_max_y(0) {}
2243 int NumVisibleRows() const { return m_max_y - m_min_y; }
2245 bool WindowDelegateDraw(Window &window, bool force) override {
2246 ExecutionContext exe_ctx(
2247 m_debugger.GetCommandInterpreter().GetExecutionContext());
2248 Process *process = exe_ctx.GetProcessPtr();
2250 bool display_content = false;
2252 StateType state = process->GetState();
2253 if (StateIsStoppedState(state, true)) {
2254 // We are stopped, so it is ok to
2255 display_content = true;
2256 } else if (StateIsRunningState(state)) {
2257 return true; // Don't do any updating when we are running
2263 m_max_x = window.GetWidth() - 1;
2264 m_max_y = window.GetHeight() - 1;
2267 window.DrawTitleBox(window.GetName());
2269 if (display_content) {
2270 const int num_visible_rows = NumVisibleRows();
2272 m_root.CalculateRowIndexes(m_num_rows);
2274 // If we unexpanded while having something selected our total number of
2275 // rows is less than the num visible rows, then make sure we show all the
2276 // rows by setting the first visible row accordingly.
2277 if (m_first_visible_row > 0 && m_num_rows < num_visible_rows)
2278 m_first_visible_row = 0;
2280 // Make sure the selected row is always visible
2281 if (m_selected_row_idx < m_first_visible_row)
2282 m_first_visible_row = m_selected_row_idx;
2283 else if (m_first_visible_row + num_visible_rows <= m_selected_row_idx)
2284 m_first_visible_row = m_selected_row_idx - num_visible_rows + 1;
2287 int num_rows_left = num_visible_rows;
2288 m_root.Draw(window, m_first_visible_row, m_selected_row_idx, row_idx,
2290 // Get the selected row
2291 m_selected_item = m_root.GetItemForRowIndex(m_selected_row_idx);
2293 m_selected_item = nullptr;
2296 window.DeferredRefresh();
2298 return true; // Drawing handled
2301 const char *WindowDelegateGetHelpText() override {
2302 return "Thread window keyboard shortcuts:";
2305 KeyHelp *WindowDelegateGetKeyHelp() override {
2306 static curses::KeyHelp g_source_view_key_help[] = {
2307 {KEY_UP, "Select previous item"},
2308 {KEY_DOWN, "Select next item"},
2309 {KEY_RIGHT, "Expand the selected item"},
2311 "Unexpand the selected item or select parent if not expanded"},
2312 {KEY_PPAGE, "Page up"},
2313 {KEY_NPAGE, "Page down"},
2314 {'h', "Show help dialog"},
2315 {' ', "Toggle item expansion"},
2319 return g_source_view_key_help;
2322 HandleCharResult WindowDelegateHandleChar(Window &window, int c) override {
2327 if (m_first_visible_row > 0) {
2328 if (m_first_visible_row > m_max_y)
2329 m_first_visible_row -= m_max_y;
2331 m_first_visible_row = 0;
2332 m_selected_row_idx = m_first_visible_row;
2333 m_selected_item = m_root.GetItemForRowIndex(m_selected_row_idx);
2334 if (m_selected_item)
2335 m_selected_item->ItemWasSelected();
2342 if (m_num_rows > m_max_y) {
2343 if (m_first_visible_row + m_max_y < m_num_rows) {
2344 m_first_visible_row += m_max_y;
2345 m_selected_row_idx = m_first_visible_row;
2346 m_selected_item = m_root.GetItemForRowIndex(m_selected_row_idx);
2347 if (m_selected_item)
2348 m_selected_item->ItemWasSelected();
2354 if (m_selected_row_idx > 0) {
2355 --m_selected_row_idx;
2356 m_selected_item = m_root.GetItemForRowIndex(m_selected_row_idx);
2357 if (m_selected_item)
2358 m_selected_item->ItemWasSelected();
2363 if (m_selected_row_idx + 1 < m_num_rows) {
2364 ++m_selected_row_idx;
2365 m_selected_item = m_root.GetItemForRowIndex(m_selected_row_idx);
2366 if (m_selected_item)
2367 m_selected_item->ItemWasSelected();
2372 if (m_selected_item) {
2373 if (!m_selected_item->IsExpanded())
2374 m_selected_item->Expand();
2379 if (m_selected_item) {
2380 if (m_selected_item->IsExpanded())
2381 m_selected_item->Unexpand();
2382 else if (m_selected_item->GetParent()) {
2383 m_selected_row_idx = m_selected_item->GetParent()->GetRowIndex();
2384 m_selected_item = m_root.GetItemForRowIndex(m_selected_row_idx);
2385 if (m_selected_item)
2386 m_selected_item->ItemWasSelected();
2392 // Toggle expansion state when SPACE is pressed
2393 if (m_selected_item) {
2394 if (m_selected_item->IsExpanded())
2395 m_selected_item->Unexpand();
2397 m_selected_item->Expand();
2402 window.CreateHelpSubwindow();
2408 return eKeyNotHandled;
2412 Debugger &m_debugger;
2413 TreeDelegateSP m_delegate_sp;
2415 TreeItem *m_selected_item;
2417 int m_selected_row_idx;
2418 int m_first_visible_row;
2425 class FrameTreeDelegate : public TreeDelegate {
2427 FrameTreeDelegate() : TreeDelegate() {
2428 FormatEntity::Parse(
2429 "frame #${frame.index}: {${function.name}${function.pc-offset}}}",
2433 ~FrameTreeDelegate() override = default;
2435 void TreeDelegateDrawTreeItem(TreeItem &item, Window &window) override {
2436 Thread *thread = (Thread *)item.GetUserData();
2438 const uint64_t frame_idx = item.GetIdentifier();
2439 StackFrameSP frame_sp = thread->GetStackFrameAtIndex(frame_idx);
2442 const SymbolContext &sc =
2443 frame_sp->GetSymbolContext(eSymbolContextEverything);
2444 ExecutionContext exe_ctx(frame_sp);
2445 if (FormatEntity::Format(m_format, strm, &sc, &exe_ctx, nullptr,
2446 nullptr, false, false)) {
2448 window.PutCStringTruncated(strm.GetString().str().c_str(), right_pad);
2454 void TreeDelegateGenerateChildren(TreeItem &item) override {
2455 // No children for frames yet...
2458 bool TreeDelegateItemSelected(TreeItem &item) override {
2459 Thread *thread = (Thread *)item.GetUserData();
2461 thread->GetProcess()->GetThreadList().SetSelectedThreadByID(
2463 const uint64_t frame_idx = item.GetIdentifier();
2464 thread->SetSelectedFrameByIndex(frame_idx);
2471 FormatEntity::Entry m_format;
2474 class ThreadTreeDelegate : public TreeDelegate {
2476 ThreadTreeDelegate(Debugger &debugger)
2477 : TreeDelegate(), m_debugger(debugger), m_tid(LLDB_INVALID_THREAD_ID),
2478 m_stop_id(UINT32_MAX) {
2479 FormatEntity::Parse("thread #${thread.index}: tid = ${thread.id}{, stop "
2480 "reason = ${thread.stop-reason}}",
2484 ~ThreadTreeDelegate() override = default;
2486 ProcessSP GetProcess() {
2487 return m_debugger.GetCommandInterpreter()
2488 .GetExecutionContext()
2492 ThreadSP GetThread(const TreeItem &item) {
2493 ProcessSP process_sp = GetProcess();
2495 return process_sp->GetThreadList().FindThreadByID(item.GetIdentifier());
2499 void TreeDelegateDrawTreeItem(TreeItem &item, Window &window) override {
2500 ThreadSP thread_sp = GetThread(item);
2503 ExecutionContext exe_ctx(thread_sp);
2504 if (FormatEntity::Format(m_format, strm, nullptr, &exe_ctx, nullptr,
2505 nullptr, false, false)) {
2507 window.PutCStringTruncated(strm.GetString().str().c_str(), right_pad);
2512 void TreeDelegateGenerateChildren(TreeItem &item) override {
2513 ProcessSP process_sp = GetProcess();
2514 if (process_sp && process_sp->IsAlive()) {
2515 StateType state = process_sp->GetState();
2516 if (StateIsStoppedState(state, true)) {
2517 ThreadSP thread_sp = GetThread(item);
2519 if (m_stop_id == process_sp->GetStopID() &&
2520 thread_sp->GetID() == m_tid)
2521 return; // Children are already up to date
2522 if (!m_frame_delegate_sp) {
2523 // Always expand the thread item the first time we show it
2524 m_frame_delegate_sp.reset(new FrameTreeDelegate());
2527 m_stop_id = process_sp->GetStopID();
2528 m_tid = thread_sp->GetID();
2530 TreeItem t(&item, *m_frame_delegate_sp, false);
2531 size_t num_frames = thread_sp->GetStackFrameCount();
2532 item.Resize(num_frames, t);
2533 for (size_t i = 0; i < num_frames; ++i) {
2534 item[i].SetUserData(thread_sp.get());
2535 item[i].SetIdentifier(i);
2541 item.ClearChildren();
2544 bool TreeDelegateItemSelected(TreeItem &item) override {
2545 ProcessSP process_sp = GetProcess();
2546 if (process_sp && process_sp->IsAlive()) {
2547 StateType state = process_sp->GetState();
2548 if (StateIsStoppedState(state, true)) {
2549 ThreadSP thread_sp = GetThread(item);
2551 ThreadList &thread_list = thread_sp->GetProcess()->GetThreadList();
2552 std::lock_guard<std::recursive_mutex> guard(thread_list.GetMutex());
2553 ThreadSP selected_thread_sp = thread_list.GetSelectedThread();
2554 if (selected_thread_sp->GetID() != thread_sp->GetID()) {
2555 thread_list.SetSelectedThreadByID(thread_sp->GetID());
2565 Debugger &m_debugger;
2566 std::shared_ptr<FrameTreeDelegate> m_frame_delegate_sp;
2567 lldb::user_id_t m_tid;
2569 FormatEntity::Entry m_format;
2572 class ThreadsTreeDelegate : public TreeDelegate {
2574 ThreadsTreeDelegate(Debugger &debugger)
2575 : TreeDelegate(), m_thread_delegate_sp(), m_debugger(debugger),
2576 m_stop_id(UINT32_MAX) {
2577 FormatEntity::Parse("process ${process.id}{, name = ${process.name}}",
2581 ~ThreadsTreeDelegate() override = default;
2583 ProcessSP GetProcess() {
2584 return m_debugger.GetCommandInterpreter()
2585 .GetExecutionContext()
2589 void TreeDelegateDrawTreeItem(TreeItem &item, Window &window) override {
2590 ProcessSP process_sp = GetProcess();
2591 if (process_sp && process_sp->IsAlive()) {
2593 ExecutionContext exe_ctx(process_sp);
2594 if (FormatEntity::Format(m_format, strm, nullptr, &exe_ctx, nullptr,
2595 nullptr, false, false)) {
2597 window.PutCStringTruncated(strm.GetString().str().c_str(), right_pad);
2602 void TreeDelegateGenerateChildren(TreeItem &item) override {
2603 ProcessSP process_sp = GetProcess();
2604 if (process_sp && process_sp->IsAlive()) {
2605 StateType state = process_sp->GetState();
2606 if (StateIsStoppedState(state, true)) {
2607 const uint32_t stop_id = process_sp->GetStopID();
2608 if (m_stop_id == stop_id)
2609 return; // Children are already up to date
2611 m_stop_id = stop_id;
2613 if (!m_thread_delegate_sp) {
2614 // Always expand the thread item the first time we show it
2616 m_thread_delegate_sp.reset(new ThreadTreeDelegate(m_debugger));
2619 TreeItem t(&item, *m_thread_delegate_sp, false);
2620 ThreadList &threads = process_sp->GetThreadList();
2621 std::lock_guard<std::recursive_mutex> guard(threads.GetMutex());
2622 size_t num_threads = threads.GetSize();
2623 item.Resize(num_threads, t);
2624 for (size_t i = 0; i < num_threads; ++i) {
2625 item[i].SetIdentifier(threads.GetThreadAtIndex(i)->GetID());
2626 item[i].SetMightHaveChildren(true);
2631 item.ClearChildren();
2634 bool TreeDelegateItemSelected(TreeItem &item) override { return false; }
2637 std::shared_ptr<ThreadTreeDelegate> m_thread_delegate_sp;
2638 Debugger &m_debugger;
2640 FormatEntity::Entry m_format;
2643 class ValueObjectListDelegate : public WindowDelegate {
2645 ValueObjectListDelegate()
2646 : m_rows(), m_selected_row(nullptr),
2647 m_selected_row_idx(0), m_first_visible_row(0), m_num_rows(0),
2648 m_max_x(0), m_max_y(0) {}
2650 ValueObjectListDelegate(ValueObjectList &valobj_list)
2651 : m_rows(), m_selected_row(nullptr),
2652 m_selected_row_idx(0), m_first_visible_row(0), m_num_rows(0),
2653 m_max_x(0), m_max_y(0) {
2654 SetValues(valobj_list);
2657 ~ValueObjectListDelegate() override = default;
2659 void SetValues(ValueObjectList &valobj_list) {
2660 m_selected_row = nullptr;
2661 m_selected_row_idx = 0;
2662 m_first_visible_row = 0;
2665 for (auto &valobj_sp : valobj_list.GetObjects())
2666 m_rows.push_back(Row(valobj_sp, nullptr));
2669 bool WindowDelegateDraw(Window &window, bool force) override {
2673 m_max_x = window.GetWidth() - 1;
2674 m_max_y = window.GetHeight() - 1;
2677 window.DrawTitleBox(window.GetName());
2679 const int num_visible_rows = NumVisibleRows();
2680 const int num_rows = CalculateTotalNumberRows(m_rows);
2682 // If we unexpanded while having something selected our total number of
2683 // rows is less than the num visible rows, then make sure we show all the
2684 // rows by setting the first visible row accordingly.
2685 if (m_first_visible_row > 0 && num_rows < num_visible_rows)
2686 m_first_visible_row = 0;
2688 // Make sure the selected row is always visible
2689 if (m_selected_row_idx < m_first_visible_row)
2690 m_first_visible_row = m_selected_row_idx;
2691 else if (m_first_visible_row + num_visible_rows <= m_selected_row_idx)
2692 m_first_visible_row = m_selected_row_idx - num_visible_rows + 1;
2694 DisplayRows(window, m_rows, g_options);
2696 window.DeferredRefresh();
2698 // Get the selected row
2699 m_selected_row = GetRowForRowIndex(m_selected_row_idx);
2700 // Keep the cursor on the selected row so the highlight and the cursor are
2701 // always on the same line
2703 window.MoveCursor(m_selected_row->x, m_selected_row->y);
2705 return true; // Drawing handled
2708 KeyHelp *WindowDelegateGetKeyHelp() override {
2709 static curses::KeyHelp g_source_view_key_help[] = {
2710 {KEY_UP, "Select previous item"},
2711 {KEY_DOWN, "Select next item"},
2712 {KEY_RIGHT, "Expand selected item"},
2713 {KEY_LEFT, "Unexpand selected item or select parent if not expanded"},
2714 {KEY_PPAGE, "Page up"},
2715 {KEY_NPAGE, "Page down"},
2716 {'A', "Format as annotated address"},
2717 {'b', "Format as binary"},
2718 {'B', "Format as hex bytes with ASCII"},
2719 {'c', "Format as character"},
2720 {'d', "Format as a signed integer"},
2721 {'D', "Format selected value using the default format for the type"},
2722 {'f', "Format as float"},
2723 {'h', "Show help dialog"},
2724 {'i', "Format as instructions"},
2725 {'o', "Format as octal"},
2726 {'p', "Format as pointer"},
2727 {'s', "Format as C string"},
2728 {'t', "Toggle showing/hiding type names"},
2729 {'u', "Format as an unsigned integer"},
2730 {'x', "Format as hex"},
2731 {'X', "Format as uppercase hex"},
2732 {' ', "Toggle item expansion"},
2736 return g_source_view_key_help;
2739 HandleCharResult WindowDelegateHandleChar(Window &window, int c) override {
2755 // Change the format for the currently selected item
2756 if (m_selected_row) {
2757 auto valobj_sp = m_selected_row->value.GetSP();
2759 valobj_sp->SetFormat(FormatForChar(c));
2764 // Toggle showing type names
2765 g_options.show_types = !g_options.show_types;
2771 if (m_first_visible_row > 0) {
2772 if (static_cast<int>(m_first_visible_row) > m_max_y)
2773 m_first_visible_row -= m_max_y;
2775 m_first_visible_row = 0;
2776 m_selected_row_idx = m_first_visible_row;
2783 if (m_num_rows > static_cast<size_t>(m_max_y)) {
2784 if (m_first_visible_row + m_max_y < m_num_rows) {
2785 m_first_visible_row += m_max_y;
2786 m_selected_row_idx = m_first_visible_row;
2792 if (m_selected_row_idx > 0)
2793 --m_selected_row_idx;
2797 if (m_selected_row_idx + 1 < m_num_rows)
2798 ++m_selected_row_idx;
2802 if (m_selected_row) {
2803 if (!m_selected_row->expanded)
2804 m_selected_row->Expand();
2809 if (m_selected_row) {
2810 if (m_selected_row->expanded)
2811 m_selected_row->Unexpand();
2812 else if (m_selected_row->parent)
2813 m_selected_row_idx = m_selected_row->parent->row_idx;
2818 // Toggle expansion state when SPACE is pressed
2819 if (m_selected_row) {
2820 if (m_selected_row->expanded)
2821 m_selected_row->Unexpand();
2823 m_selected_row->Expand();
2828 window.CreateHelpSubwindow();
2834 return eKeyNotHandled;
2838 std::vector<Row> m_rows;
2839 Row *m_selected_row;
2840 uint32_t m_selected_row_idx;
2841 uint32_t m_first_visible_row;
2842 uint32_t m_num_rows;
2848 static Format FormatForChar(int c) {
2853 return eFormatHexUppercase;
2855 return eFormatOctal;
2857 return eFormatCString;
2859 return eFormatUnsigned;
2861 return eFormatDecimal;
2863 return eFormatDefault;
2865 return eFormatInstruction;
2867 return eFormatAddressInfo;
2869 return eFormatPointer;
2873 return eFormatBinary;
2875 return eFormatBytesWithASCII;
2877 return eFormatFloat;
2879 return eFormatDefault;
2882 bool DisplayRowObject(Window &window, Row &row, DisplayOptions &options,
2883 bool highlight, bool last_child) {
2884 ValueObject *valobj = row.value.GetSP().get();
2886 if (valobj == nullptr)
2889 const char *type_name =
2890 options.show_types ? valobj->GetTypeName().GetCString() : nullptr;
2891 const char *name = valobj->GetName().GetCString();
2892 const char *value = valobj->GetValueAsCString();
2893 const char *summary = valobj->GetSummaryAsCString();
2895 window.MoveCursor(row.x, row.y);
2897 row.DrawTree(window);
2900 window.AttributeOn(A_REVERSE);
2902 if (type_name && type_name[0])
2903 window.Printf("(%s) ", type_name);
2905 if (name && name[0])
2906 window.PutCString(name);
2908 attr_t changd_attr = 0;
2909 if (valobj->GetValueDidChange())
2910 changd_attr = COLOR_PAIR(5) | A_BOLD;
2912 if (value && value[0]) {
2913 window.PutCString(" = ");
2915 window.AttributeOn(changd_attr);
2916 window.PutCString(value);
2918 window.AttributeOff(changd_attr);
2921 if (summary && summary[0]) {
2922 window.PutChar(' ');
2924 window.AttributeOn(changd_attr);
2925 window.PutCString(summary);
2927 window.AttributeOff(changd_attr);
2931 window.AttributeOff(A_REVERSE);
2936 void DisplayRows(Window &window, std::vector<Row> &rows,
2937 DisplayOptions &options) {
2941 bool window_is_active = window.IsActive();
2942 for (auto &row : rows) {
2943 const bool last_child = row.parent && &rows[rows.size() - 1] == &row;
2944 // Save the row index in each Row structure
2945 row.row_idx = m_num_rows;
2946 if ((m_num_rows >= m_first_visible_row) &&
2947 ((m_num_rows - m_first_visible_row) <
2948 static_cast<size_t>(NumVisibleRows()))) {
2950 row.y = m_num_rows - m_first_visible_row + 1;
2951 if (DisplayRowObject(window, row, options,
2953 m_num_rows == m_selected_row_idx,
2966 auto &children = row.GetChildren();
2967 if (row.expanded && !children.empty()) {
2968 DisplayRows(window, children, options);
2973 int CalculateTotalNumberRows(std::vector<Row> &rows) {
2975 for (auto &row : rows) {
2978 row_count += CalculateTotalNumberRows(row.GetChildren());
2983 static Row *GetRowForRowIndexImpl(std::vector<Row> &rows, size_t &row_index) {
2984 for (auto &row : rows) {
2989 auto &children = row.GetChildren();
2990 if (row.expanded && !children.empty()) {
2991 Row *result = GetRowForRowIndexImpl(children, row_index);
3000 Row *GetRowForRowIndex(size_t row_index) {
3001 return GetRowForRowIndexImpl(m_rows, row_index);
3004 int NumVisibleRows() const { return m_max_y - m_min_y; }
3006 static DisplayOptions g_options;
3009 class FrameVariablesWindowDelegate : public ValueObjectListDelegate {
3011 FrameVariablesWindowDelegate(Debugger &debugger)
3012 : ValueObjectListDelegate(), m_debugger(debugger),
3013 m_frame_block(nullptr) {}
3015 ~FrameVariablesWindowDelegate() override = default;
3017 const char *WindowDelegateGetHelpText() override {
3018 return "Frame variable window keyboard shortcuts:";
3021 bool WindowDelegateDraw(Window &window, bool force) override {
3022 ExecutionContext exe_ctx(
3023 m_debugger.GetCommandInterpreter().GetExecutionContext());
3024 Process *process = exe_ctx.GetProcessPtr();
3025 Block *frame_block = nullptr;
3026 StackFrame *frame = nullptr;
3029 StateType state = process->GetState();
3030 if (StateIsStoppedState(state, true)) {
3031 frame = exe_ctx.GetFramePtr();
3033 frame_block = frame->GetFrameBlock();
3034 } else if (StateIsRunningState(state)) {
3035 return true; // Don't do any updating when we are running
3039 ValueObjectList local_values;
3041 // Only update the variables if they have changed
3042 if (m_frame_block != frame_block) {
3043 m_frame_block = frame_block;
3045 VariableList *locals = frame->GetVariableList(true);
3047 const DynamicValueType use_dynamic = eDynamicDontRunTarget;
3048 const size_t num_locals = locals->GetSize();
3049 for (size_t i = 0; i < num_locals; ++i) {
3050 ValueObjectSP value_sp = frame->GetValueObjectForFrameVariable(
3051 locals->GetVariableAtIndex(i), use_dynamic);
3053 ValueObjectSP synthetic_value_sp = value_sp->GetSyntheticValue();
3054 if (synthetic_value_sp)
3055 local_values.Append(synthetic_value_sp);
3057 local_values.Append(value_sp);
3060 // Update the values
3061 SetValues(local_values);
3065 m_frame_block = nullptr;
3066 // Update the values with an empty list if there is no frame
3067 SetValues(local_values);
3070 return ValueObjectListDelegate::WindowDelegateDraw(window, force);
3074 Debugger &m_debugger;
3075 Block *m_frame_block;
3078 class RegistersWindowDelegate : public ValueObjectListDelegate {
3080 RegistersWindowDelegate(Debugger &debugger)
3081 : ValueObjectListDelegate(), m_debugger(debugger) {}
3083 ~RegistersWindowDelegate() override = default;
3085 const char *WindowDelegateGetHelpText() override {
3086 return "Register window keyboard shortcuts:";
3089 bool WindowDelegateDraw(Window &window, bool force) override {
3090 ExecutionContext exe_ctx(
3091 m_debugger.GetCommandInterpreter().GetExecutionContext());
3092 StackFrame *frame = exe_ctx.GetFramePtr();
3094 ValueObjectList value_list;
3096 if (frame->GetStackID() != m_stack_id) {
3097 m_stack_id = frame->GetStackID();
3098 RegisterContextSP reg_ctx(frame->GetRegisterContext());
3100 const uint32_t num_sets = reg_ctx->GetRegisterSetCount();
3101 for (uint32_t set_idx = 0; set_idx < num_sets; ++set_idx) {
3103 ValueObjectRegisterSet::Create(frame, reg_ctx, set_idx));
3106 SetValues(value_list);
3109 Process *process = exe_ctx.GetProcessPtr();
3110 if (process && process->IsAlive())
3111 return true; // Don't do any updating if we are running
3113 // Update the values with an empty list if there is no process or the
3114 // process isn't alive anymore
3115 SetValues(value_list);
3118 return ValueObjectListDelegate::WindowDelegateDraw(window, force);
3122 Debugger &m_debugger;
3126 static const char *CursesKeyToCString(int ch) {
3127 static char g_desc[32];
3128 if (ch >= KEY_F0 && ch < KEY_F0 + 64) {
3129 snprintf(g_desc, sizeof(g_desc), "F%u", ch - KEY_F0);
3146 return "delete-line";
3148 return "insert-line";
3150 return "delete-char";
3152 return "insert-char";
3156 return "clear-to-eos";
3158 return "clear-to-eol";
3160 return "scroll-forward";
3162 return "scroll-backward";
3172 return "clear-all-tabs";
3178 return "lower-left key";
3180 return "upper left of keypad";
3182 return "upper right of keypad";
3184 return "center of keypad";
3186 return "lower left of keypad";
3188 return "lower right of keypad";
3190 return "back-tab key";
3194 return "cancel key";
3198 return "command key";
3202 return "create key";
3214 return "message key";
3222 return "options key";
3224 return "previous key";
3228 return "reference key";
3230 return "refresh key";
3232 return "replace key";
3234 return "restart key";
3236 return "resume key";
3240 return "shifted begin key";
3242 return "shifted cancel key";
3244 return "shifted command key";
3246 return "shifted copy key";
3248 return "shifted create key";
3250 return "shifted delete-character key";
3252 return "shifted delete-line key";
3254 return "select key";
3256 return "shifted end key";
3258 return "shifted clear-to-end-of-line key";
3260 return "shifted exit key";
3262 return "shifted find key";
3264 return "shifted help key";
3266 return "shifted home key";
3268 return "shifted insert-character key";
3270 return "shifted left-arrow key";
3272 return "shifted message key";
3274 return "shifted move key";
3276 return "shifted next key";
3278 return "shifted options key";
3280 return "shifted previous key";
3282 return "shifted print key";
3284 return "shifted redo key";
3286 return "shifted replace key";
3288 return "shifted right-arrow key";
3290 return "shifted resume key";
3292 return "shifted save key";
3294 return "shifted suspend key";
3296 return "shifted undo key";
3298 return "suspend key";
3302 return "Mouse event has occurred";
3304 return "Terminal resize event";
3307 return "We were interrupted by an event";
3319 snprintf(g_desc, sizeof(g_desc), "%c", ch);
3321 snprintf(g_desc, sizeof(g_desc), "\\x%2.2x", ch);
3327 HelpDialogDelegate::HelpDialogDelegate(const char *text,
3328 KeyHelp *key_help_array)
3329 : m_text(), m_first_visible_line(0) {
3330 if (text && text[0]) {
3331 m_text.SplitIntoLines(text);
3332 m_text.AppendString("");
3334 if (key_help_array) {
3335 for (KeyHelp *key = key_help_array; key->ch; ++key) {
3336 StreamString key_description;
3337 key_description.Printf("%10s - %s", CursesKeyToCString(key->ch),
3339 m_text.AppendString(key_description.GetString());
3344 HelpDialogDelegate::~HelpDialogDelegate() = default;
3346 bool HelpDialogDelegate::WindowDelegateDraw(Window &window, bool force) {
3348 const int window_height = window.GetHeight();
3351 const int min_y = y;
3352 const int max_y = window_height - 1 - y;
3353 const size_t num_visible_lines = max_y - min_y + 1;
3354 const size_t num_lines = m_text.GetSize();
3355 const char *bottom_message;
3356 if (num_lines <= num_visible_lines)
3357 bottom_message = "Press any key to exit";
3359 bottom_message = "Use arrows to scroll, any other key to exit";
3360 window.DrawTitleBox(window.GetName(), bottom_message);
3361 while (y <= max_y) {
3362 window.MoveCursor(x, y);
3363 window.PutCStringTruncated(
3364 m_text.GetStringAtIndex(m_first_visible_line + y - min_y), 1);
3370 HandleCharResult HelpDialogDelegate::WindowDelegateHandleChar(Window &window,
3373 const size_t num_lines = m_text.GetSize();
3374 const size_t num_visible_lines = window.GetHeight() - 2;
3376 if (num_lines <= num_visible_lines) {
3378 // If we have all lines visible and don't need scrolling, then any key
3379 // press will cause us to exit
3383 if (m_first_visible_line > 0)
3384 --m_first_visible_line;
3388 if (m_first_visible_line + num_visible_lines < num_lines)
3389 ++m_first_visible_line;
3394 if (m_first_visible_line > 0) {
3395 if (static_cast<size_t>(m_first_visible_line) >= num_visible_lines)
3396 m_first_visible_line -= num_visible_lines;
3398 m_first_visible_line = 0;
3404 if (m_first_visible_line + num_visible_lines < num_lines) {
3405 m_first_visible_line += num_visible_lines;
3406 if (static_cast<size_t>(m_first_visible_line) > num_lines)
3407 m_first_visible_line = num_lines - num_visible_lines;
3417 window.GetParent()->RemoveSubWindow(&window);
3421 class ApplicationDelegate : public WindowDelegate, public MenuDelegate {
3429 eMenuID_TargetCreate,
3430 eMenuID_TargetDelete,
3433 eMenuID_ProcessAttach,
3434 eMenuID_ProcessDetach,
3435 eMenuID_ProcessLaunch,
3436 eMenuID_ProcessContinue,
3437 eMenuID_ProcessHalt,
3438 eMenuID_ProcessKill,
3441 eMenuID_ThreadStepIn,
3442 eMenuID_ThreadStepOver,
3443 eMenuID_ThreadStepOut,
3446 eMenuID_ViewBacktrace,
3447 eMenuID_ViewRegisters,
3449 eMenuID_ViewVariables,
3455 ApplicationDelegate(Application &app, Debugger &debugger)
3456 : WindowDelegate(), MenuDelegate(), m_app(app), m_debugger(debugger) {}
3458 ~ApplicationDelegate() override = default;
3460 bool WindowDelegateDraw(Window &window, bool force) override {
3461 return false; // Drawing not handled, let standard window drawing happen
3464 HandleCharResult WindowDelegateHandleChar(Window &window, int key) override {
3467 window.SelectNextWindowAsActive();
3471 window.CreateHelpSubwindow();
3475 return eQuitApplication;
3480 return eKeyNotHandled;
3483 const char *WindowDelegateGetHelpText() override {
3484 return "Welcome to the LLDB curses GUI.\n\n"
3485 "Press the TAB key to change the selected view.\n"
3486 "Each view has its own keyboard shortcuts, press 'h' to open a "
3487 "dialog to display them.\n\n"
3488 "Common key bindings for all views:";
3491 KeyHelp *WindowDelegateGetKeyHelp() override {
3492 static curses::KeyHelp g_source_view_key_help[] = {
3493 {'\t', "Select next view"},
3494 {'h', "Show help dialog with view specific key bindings"},
3497 {KEY_UP, "Select previous"},
3498 {KEY_DOWN, "Select next"},
3499 {KEY_LEFT, "Unexpand or select parent"},
3500 {KEY_RIGHT, "Expand"},
3501 {KEY_PPAGE, "Page up"},
3502 {KEY_NPAGE, "Page down"},
3504 return g_source_view_key_help;
3507 MenuActionResult MenuDelegateAction(Menu &menu) override {
3508 switch (menu.GetIdentifier()) {
3509 case eMenuID_ThreadStepIn: {
3510 ExecutionContext exe_ctx =
3511 m_debugger.GetCommandInterpreter().GetExecutionContext();
3512 if (exe_ctx.HasThreadScope()) {
3513 Process *process = exe_ctx.GetProcessPtr();
3514 if (process && process->IsAlive() &&
3515 StateIsStoppedState(process->GetState(), true))
3516 exe_ctx.GetThreadRef().StepIn(true);
3519 return MenuActionResult::Handled;
3521 case eMenuID_ThreadStepOut: {
3522 ExecutionContext exe_ctx =
3523 m_debugger.GetCommandInterpreter().GetExecutionContext();
3524 if (exe_ctx.HasThreadScope()) {
3525 Process *process = exe_ctx.GetProcessPtr();
3526 if (process && process->IsAlive() &&
3527 StateIsStoppedState(process->GetState(), true))
3528 exe_ctx.GetThreadRef().StepOut();
3531 return MenuActionResult::Handled;
3533 case eMenuID_ThreadStepOver: {
3534 ExecutionContext exe_ctx =
3535 m_debugger.GetCommandInterpreter().GetExecutionContext();
3536 if (exe_ctx.HasThreadScope()) {
3537 Process *process = exe_ctx.GetProcessPtr();
3538 if (process && process->IsAlive() &&
3539 StateIsStoppedState(process->GetState(), true))
3540 exe_ctx.GetThreadRef().StepOver(true);
3543 return MenuActionResult::Handled;
3545 case eMenuID_ProcessContinue: {
3546 ExecutionContext exe_ctx =
3547 m_debugger.GetCommandInterpreter().GetExecutionContext();
3548 if (exe_ctx.HasProcessScope()) {
3549 Process *process = exe_ctx.GetProcessPtr();
3550 if (process && process->IsAlive() &&
3551 StateIsStoppedState(process->GetState(), true))
3555 return MenuActionResult::Handled;
3557 case eMenuID_ProcessKill: {
3558 ExecutionContext exe_ctx =
3559 m_debugger.GetCommandInterpreter().GetExecutionContext();
3560 if (exe_ctx.HasProcessScope()) {
3561 Process *process = exe_ctx.GetProcessPtr();
3562 if (process && process->IsAlive())
3563 process->Destroy(false);
3566 return MenuActionResult::Handled;
3568 case eMenuID_ProcessHalt: {
3569 ExecutionContext exe_ctx =
3570 m_debugger.GetCommandInterpreter().GetExecutionContext();
3571 if (exe_ctx.HasProcessScope()) {
3572 Process *process = exe_ctx.GetProcessPtr();
3573 if (process && process->IsAlive())
3577 return MenuActionResult::Handled;
3579 case eMenuID_ProcessDetach: {
3580 ExecutionContext exe_ctx =
3581 m_debugger.GetCommandInterpreter().GetExecutionContext();
3582 if (exe_ctx.HasProcessScope()) {
3583 Process *process = exe_ctx.GetProcessPtr();
3584 if (process && process->IsAlive())
3585 process->Detach(false);
3588 return MenuActionResult::Handled;
3590 case eMenuID_Process: {
3591 // Populate the menu with all of the threads if the process is stopped
3592 // when the Process menu gets selected and is about to display its
3594 Menus &submenus = menu.GetSubmenus();
3595 ExecutionContext exe_ctx =
3596 m_debugger.GetCommandInterpreter().GetExecutionContext();
3597 Process *process = exe_ctx.GetProcessPtr();
3598 if (process && process->IsAlive() &&
3599 StateIsStoppedState(process->GetState(), true)) {
3600 if (submenus.size() == 7)
3601 menu.AddSubmenu(MenuSP(new Menu(Menu::Type::Separator)));
3602 else if (submenus.size() > 8)
3603 submenus.erase(submenus.begin() + 8, submenus.end());
3605 ThreadList &threads = process->GetThreadList();
3606 std::lock_guard<std::recursive_mutex> guard(threads.GetMutex());
3607 size_t num_threads = threads.GetSize();
3608 for (size_t i = 0; i < num_threads; ++i) {
3609 ThreadSP thread_sp = threads.GetThreadAtIndex(i);
3610 char menu_char = '\0';
3612 menu_char = '1' + i;
3613 StreamString thread_menu_title;
3614 thread_menu_title.Printf("Thread %u", thread_sp->GetIndexID());
3615 const char *thread_name = thread_sp->GetName();
3616 if (thread_name && thread_name[0])
3617 thread_menu_title.Printf(" %s", thread_name);
3619 const char *queue_name = thread_sp->GetQueueName();
3620 if (queue_name && queue_name[0])
3621 thread_menu_title.Printf(" %s", queue_name);
3624 MenuSP(new Menu(thread_menu_title.GetString().str().c_str(),
3625 nullptr, menu_char, thread_sp->GetID())));
3627 } else if (submenus.size() > 7) {
3628 // Remove the separator and any other thread submenu items that were
3630 submenus.erase(submenus.begin() + 7, submenus.end());
3632 // Since we are adding and removing items we need to recalculate the name
3634 menu.RecalculateNameLengths();
3636 return MenuActionResult::Handled;
3638 case eMenuID_ViewVariables: {
3639 WindowSP main_window_sp = m_app.GetMainWindow();
3640 WindowSP source_window_sp = main_window_sp->FindSubWindow("Source");
3641 WindowSP variables_window_sp = main_window_sp->FindSubWindow("Variables");
3642 WindowSP registers_window_sp = main_window_sp->FindSubWindow("Registers");
3643 const Rect source_bounds = source_window_sp->GetBounds();
3645 if (variables_window_sp) {
3646 const Rect variables_bounds = variables_window_sp->GetBounds();
3648 main_window_sp->RemoveSubWindow(variables_window_sp.get());
3650 if (registers_window_sp) {
3651 // We have a registers window, so give all the area back to the
3653 Rect registers_bounds = variables_bounds;
3654 registers_bounds.size.width = source_bounds.size.width;
3655 registers_window_sp->SetBounds(registers_bounds);
3657 // We have no registers window showing so give the bottom area back
3658 // to the source view
3659 source_window_sp->Resize(source_bounds.size.width,
3660 source_bounds.size.height +
3661 variables_bounds.size.height);
3664 Rect new_variables_rect;
3665 if (registers_window_sp) {
3666 // We have a registers window so split the area of the registers
3667 // window into two columns where the left hand side will be the
3668 // variables and the right hand side will be the registers
3669 const Rect variables_bounds = registers_window_sp->GetBounds();
3670 Rect new_registers_rect;
3671 variables_bounds.VerticalSplitPercentage(0.50, new_variables_rect,
3672 new_registers_rect);
3673 registers_window_sp->SetBounds(new_registers_rect);
3675 // No variables window, grab the bottom part of the source window
3676 Rect new_source_rect;
3677 source_bounds.HorizontalSplitPercentage(0.70, new_source_rect,
3678 new_variables_rect);
3679 source_window_sp->SetBounds(new_source_rect);
3681 WindowSP new_window_sp = main_window_sp->CreateSubWindow(
3682 "Variables", new_variables_rect, false);
3683 new_window_sp->SetDelegate(
3684 WindowDelegateSP(new FrameVariablesWindowDelegate(m_debugger)));
3688 return MenuActionResult::Handled;
3690 case eMenuID_ViewRegisters: {
3691 WindowSP main_window_sp = m_app.GetMainWindow();
3692 WindowSP source_window_sp = main_window_sp->FindSubWindow("Source");
3693 WindowSP variables_window_sp = main_window_sp->FindSubWindow("Variables");
3694 WindowSP registers_window_sp = main_window_sp->FindSubWindow("Registers");
3695 const Rect source_bounds = source_window_sp->GetBounds();
3697 if (registers_window_sp) {
3698 if (variables_window_sp) {
3699 const Rect variables_bounds = variables_window_sp->GetBounds();
3701 // We have a variables window, so give all the area back to the
3703 variables_window_sp->Resize(variables_bounds.size.width +
3704 registers_window_sp->GetWidth(),
3705 variables_bounds.size.height);
3707 // We have no variables window showing so give the bottom area back
3708 // to the source view
3709 source_window_sp->Resize(source_bounds.size.width,
3710 source_bounds.size.height +
3711 registers_window_sp->GetHeight());
3713 main_window_sp->RemoveSubWindow(registers_window_sp.get());
3716 if (variables_window_sp) {
3717 // We have a variables window, split it into two columns where the
3718 // left hand side will be the variables and the right hand side will
3720 const Rect variables_bounds = variables_window_sp->GetBounds();
3722 variables_bounds.VerticalSplitPercentage(0.50, new_vars_rect,
3724 variables_window_sp->SetBounds(new_vars_rect);
3726 // No registers window, grab the bottom part of the source window
3727 Rect new_source_rect;
3728 source_bounds.HorizontalSplitPercentage(0.70, new_source_rect,
3730 source_window_sp->SetBounds(new_source_rect);
3732 WindowSP new_window_sp =
3733 main_window_sp->CreateSubWindow("Registers", new_regs_rect, false);
3734 new_window_sp->SetDelegate(
3735 WindowDelegateSP(new RegistersWindowDelegate(m_debugger)));
3739 return MenuActionResult::Handled;
3741 case eMenuID_HelpGUIHelp:
3742 m_app.GetMainWindow()->CreateHelpSubwindow();
3743 return MenuActionResult::Handled;
3749 return MenuActionResult::NotHandled;
3754 Debugger &m_debugger;
3757 class StatusBarWindowDelegate : public WindowDelegate {
3759 StatusBarWindowDelegate(Debugger &debugger) : m_debugger(debugger) {
3760 FormatEntity::Parse("Thread: ${thread.id%tid}", m_format);
3763 ~StatusBarWindowDelegate() override = default;
3765 bool WindowDelegateDraw(Window &window, bool force) override {
3766 ExecutionContext exe_ctx =
3767 m_debugger.GetCommandInterpreter().GetExecutionContext();
3768 Process *process = exe_ctx.GetProcessPtr();
3769 Thread *thread = exe_ctx.GetThreadPtr();
3770 StackFrame *frame = exe_ctx.GetFramePtr();
3772 window.SetBackground(2);
3773 window.MoveCursor(0, 0);
3775 const StateType state = process->GetState();
3776 window.Printf("Process: %5" PRIu64 " %10s", process->GetID(),
3777 StateAsCString(state));
3779 if (StateIsStoppedState(state, true)) {
3781 if (thread && FormatEntity::Format(m_format, strm, nullptr, &exe_ctx,
3782 nullptr, nullptr, false, false)) {
3783 window.MoveCursor(40, 0);
3784 window.PutCStringTruncated(strm.GetString().str().c_str(), 1);
3787 window.MoveCursor(60, 0);
3789 window.Printf("Frame: %3u PC = 0x%16.16" PRIx64,
3790 frame->GetFrameIndex(),
3791 frame->GetFrameCodeAddress().GetOpcodeLoadAddress(
3792 exe_ctx.GetTargetPtr()));
3793 } else if (state == eStateExited) {
3794 const char *exit_desc = process->GetExitDescription();
3795 const int exit_status = process->GetExitStatus();
3796 if (exit_desc && exit_desc[0])
3797 window.Printf(" with status = %i (%s)", exit_status, exit_desc);
3799 window.Printf(" with status = %i", exit_status);
3802 window.DeferredRefresh();
3807 Debugger &m_debugger;
3808 FormatEntity::Entry m_format;
3811 class SourceFileWindowDelegate : public WindowDelegate {
3813 SourceFileWindowDelegate(Debugger &debugger)
3814 : WindowDelegate(), m_debugger(debugger), m_sc(), m_file_sp(),
3815 m_disassembly_scope(nullptr), m_disassembly_sp(), m_disassembly_range(),
3816 m_title(), m_line_width(4), m_selected_line(0), m_pc_line(0),
3817 m_stop_id(0), m_frame_idx(UINT32_MAX), m_first_visible_line(0),
3818 m_min_x(0), m_min_y(0), m_max_x(0), m_max_y(0) {}
3820 ~SourceFileWindowDelegate() override = default;
3822 void Update(const SymbolContext &sc) { m_sc = sc; }
3824 uint32_t NumVisibleLines() const { return m_max_y - m_min_y; }
3826 const char *WindowDelegateGetHelpText() override {
3827 return "Source/Disassembly window keyboard shortcuts:";
3830 KeyHelp *WindowDelegateGetKeyHelp() override {
3831 static curses::KeyHelp g_source_view_key_help[] = {
3832 {KEY_RETURN, "Run to selected line with one shot breakpoint"},
3833 {KEY_UP, "Select previous source line"},
3834 {KEY_DOWN, "Select next source line"},
3835 {KEY_PPAGE, "Page up"},
3836 {KEY_NPAGE, "Page down"},
3837 {'b', "Set breakpoint on selected source/disassembly line"},
3838 {'c', "Continue process"},
3839 {'d', "Detach and resume process"},
3840 {'D', "Detach with process suspended"},
3841 {'h', "Show help dialog"},
3842 {'k', "Kill process"},
3843 {'n', "Step over (source line)"},
3844 {'N', "Step over (single instruction)"},
3846 {'s', "Step in (source line)"},
3847 {'S', "Step in (single instruction)"},
3851 return g_source_view_key_help;
3854 bool WindowDelegateDraw(Window &window, bool force) override {
3855 ExecutionContext exe_ctx =
3856 m_debugger.GetCommandInterpreter().GetExecutionContext();
3857 Process *process = exe_ctx.GetProcessPtr();
3858 Thread *thread = nullptr;
3860 bool update_location = false;
3862 StateType state = process->GetState();
3863 if (StateIsStoppedState(state, true)) {
3864 // We are stopped, so it is ok to
3865 update_location = true;
3871 m_max_x = window.GetMaxX() - 1;
3872 m_max_y = window.GetMaxY() - 1;
3874 const uint32_t num_visible_lines = NumVisibleLines();
3875 StackFrameSP frame_sp;
3876 bool set_selected_line_to_pc = false;
3878 if (update_location) {
3879 const bool process_alive = process ? process->IsAlive() : false;
3880 bool thread_changed = false;
3881 if (process_alive) {
3882 thread = exe_ctx.GetThreadPtr();
3884 frame_sp = thread->GetSelectedFrame();
3885 auto tid = thread->GetID();
3886 thread_changed = tid != m_tid;
3889 if (m_tid != LLDB_INVALID_THREAD_ID) {
3890 thread_changed = true;
3891 m_tid = LLDB_INVALID_THREAD_ID;
3895 const uint32_t stop_id = process ? process->GetStopID() : 0;
3896 const bool stop_id_changed = stop_id != m_stop_id;
3897 bool frame_changed = false;
3898 m_stop_id = stop_id;
3901 m_sc = frame_sp->GetSymbolContext(eSymbolContextEverything);
3902 if (m_sc.module_sp) {
3904 "%s", m_sc.module_sp->GetFileSpec().GetFilename().GetCString());
3905 ConstString func_name = m_sc.GetFunctionName();
3907 m_title.Printf("`%s", func_name.GetCString());
3909 const uint32_t frame_idx = frame_sp->GetFrameIndex();
3910 frame_changed = frame_idx != m_frame_idx;
3911 m_frame_idx = frame_idx;
3914 frame_changed = m_frame_idx != UINT32_MAX;
3915 m_frame_idx = UINT32_MAX;
3918 const bool context_changed =
3919 thread_changed || frame_changed || stop_id_changed;
3921 if (process_alive) {
3922 if (m_sc.line_entry.IsValid()) {
3923 m_pc_line = m_sc.line_entry.line;
3924 if (m_pc_line != UINT32_MAX)
3925 --m_pc_line; // Convert to zero based line number...
3926 // Update the selected line if the stop ID changed...
3927 if (context_changed)
3928 m_selected_line = m_pc_line;
3930 if (m_file_sp && m_file_sp->FileSpecMatches(m_sc.line_entry.file)) {
3931 // Same file, nothing to do, we should either have the lines or not
3932 // (source file missing)
3933 if (m_selected_line >= static_cast<size_t>(m_first_visible_line)) {
3934 if (m_selected_line >= m_first_visible_line + num_visible_lines)
3935 m_first_visible_line = m_selected_line - 10;
3937 if (m_selected_line > 10)
3938 m_first_visible_line = m_selected_line - 10;
3940 m_first_visible_line = 0;
3943 // File changed, set selected line to the line with the PC
3944 m_selected_line = m_pc_line;
3946 m_debugger.GetSourceManager().GetFile(m_sc.line_entry.file);
3948 const size_t num_lines = m_file_sp->GetNumLines();
3949 int m_line_width = 1;
3950 for (size_t n = num_lines; n >= 10; n = n / 10)
3953 snprintf(m_line_format, sizeof(m_line_format), " %%%iu ",
3955 if (num_lines < num_visible_lines ||
3956 m_selected_line < num_visible_lines)
3957 m_first_visible_line = 0;
3959 m_first_visible_line = m_selected_line - 10;
3966 if (!m_file_sp || m_file_sp->GetNumLines() == 0) {
3968 bool prefer_file_cache = false;
3969 if (m_sc.function) {
3970 if (m_disassembly_scope != m_sc.function) {
3971 m_disassembly_scope = m_sc.function;
3972 m_disassembly_sp = m_sc.function->GetInstructions(
3973 exe_ctx, nullptr, prefer_file_cache);
3974 if (m_disassembly_sp) {
3975 set_selected_line_to_pc = true;
3976 m_disassembly_range = m_sc.function->GetAddressRange();
3978 m_disassembly_range.Clear();
3981 set_selected_line_to_pc = context_changed;
3983 } else if (m_sc.symbol) {
3984 if (m_disassembly_scope != m_sc.symbol) {
3985 m_disassembly_scope = m_sc.symbol;
3986 m_disassembly_sp = m_sc.symbol->GetInstructions(
3987 exe_ctx, nullptr, prefer_file_cache);
3988 if (m_disassembly_sp) {
3989 set_selected_line_to_pc = true;
3990 m_disassembly_range.GetBaseAddress() =
3991 m_sc.symbol->GetAddress();
3992 m_disassembly_range.SetByteSize(m_sc.symbol->GetByteSize());
3994 m_disassembly_range.Clear();
3997 set_selected_line_to_pc = context_changed;
4002 m_pc_line = UINT32_MAX;
4006 const int window_width = window.GetWidth();
4008 window.DrawTitleBox("Sources");
4009 if (!m_title.GetString().empty()) {
4010 window.AttributeOn(A_REVERSE);
4011 window.MoveCursor(1, 1);
4012 window.PutChar(' ');
4013 window.PutCStringTruncated(m_title.GetString().str().c_str(), 1);
4014 int x = window.GetCursorX();
4015 if (x < window_width - 1) {
4016 window.Printf("%*s", window_width - x - 1, "");
4018 window.AttributeOff(A_REVERSE);
4021 Target *target = exe_ctx.GetTargetPtr();
4022 const size_t num_source_lines = GetNumSourceLines();
4023 if (num_source_lines > 0) {
4025 BreakpointLines bp_lines;
4027 BreakpointList &bp_list = target->GetBreakpointList();
4028 const size_t num_bps = bp_list.GetSize();
4029 for (size_t bp_idx = 0; bp_idx < num_bps; ++bp_idx) {
4030 BreakpointSP bp_sp = bp_list.GetBreakpointAtIndex(bp_idx);
4031 const size_t num_bps_locs = bp_sp->GetNumLocations();
4032 for (size_t bp_loc_idx = 0; bp_loc_idx < num_bps_locs; ++bp_loc_idx) {
4033 BreakpointLocationSP bp_loc_sp =
4034 bp_sp->GetLocationAtIndex(bp_loc_idx);
4035 LineEntry bp_loc_line_entry;
4036 if (bp_loc_sp->GetAddress().CalculateSymbolContextLineEntry(
4037 bp_loc_line_entry)) {
4038 if (m_file_sp->GetFileSpec() == bp_loc_line_entry.file) {
4039 bp_lines.insert(bp_loc_line_entry.line);
4046 const attr_t selected_highlight_attr = A_REVERSE;
4047 const attr_t pc_highlight_attr = COLOR_PAIR(1);
4049 for (size_t i = 0; i < num_visible_lines; ++i) {
4050 const uint32_t curr_line = m_first_visible_line + i;
4051 if (curr_line < num_source_lines) {
4052 const int line_y = m_min_y + i;
4053 window.MoveCursor(1, line_y);
4054 const bool is_pc_line = curr_line == m_pc_line;
4055 const bool line_is_selected = m_selected_line == curr_line;
4056 // Highlight the line as the PC line first, then if the selected line
4057 // isn't the same as the PC line, highlight it differently
4058 attr_t highlight_attr = 0;
4061 highlight_attr = pc_highlight_attr;
4062 else if (line_is_selected)
4063 highlight_attr = selected_highlight_attr;
4065 if (bp_lines.find(curr_line + 1) != bp_lines.end())
4066 bp_attr = COLOR_PAIR(2);
4069 window.AttributeOn(bp_attr);
4071 window.Printf(m_line_format, curr_line + 1);
4074 window.AttributeOff(bp_attr);
4076 window.PutChar(ACS_VLINE);
4077 // Mark the line with the PC with a diamond
4079 window.PutChar(ACS_DIAMOND);
4081 window.PutChar(' ');
4084 window.AttributeOn(highlight_attr);
4085 const uint32_t line_len =
4086 m_file_sp->GetLineLength(curr_line + 1, false);
4088 window.PutCString(m_file_sp->PeekLineData(curr_line + 1), line_len);
4090 if (is_pc_line && frame_sp &&
4091 frame_sp->GetConcreteFrameIndex() == 0) {
4092 StopInfoSP stop_info_sp;
4094 stop_info_sp = thread->GetStopInfo();
4096 const char *stop_description = stop_info_sp->GetDescription();
4097 if (stop_description && stop_description[0]) {
4098 size_t stop_description_len = strlen(stop_description);
4099 int desc_x = window_width - stop_description_len - 16;
4100 window.Printf("%*s", desc_x - window.GetCursorX(), "");
4101 // window.MoveCursor(window_width - stop_description_len - 15,
4103 window.Printf("<<< Thread %u: %s ", thread->GetIndexID(),
4107 window.Printf("%*s", window_width - window.GetCursorX() - 1, "");
4111 window.AttributeOff(highlight_attr);
4117 size_t num_disassembly_lines = GetNumDisassemblyLines();
4118 if (num_disassembly_lines > 0) {
4119 // Display disassembly
4120 BreakpointAddrs bp_file_addrs;
4121 Target *target = exe_ctx.GetTargetPtr();
4123 BreakpointList &bp_list = target->GetBreakpointList();
4124 const size_t num_bps = bp_list.GetSize();
4125 for (size_t bp_idx = 0; bp_idx < num_bps; ++bp_idx) {
4126 BreakpointSP bp_sp = bp_list.GetBreakpointAtIndex(bp_idx);
4127 const size_t num_bps_locs = bp_sp->GetNumLocations();
4128 for (size_t bp_loc_idx = 0; bp_loc_idx < num_bps_locs;
4130 BreakpointLocationSP bp_loc_sp =
4131 bp_sp->GetLocationAtIndex(bp_loc_idx);
4132 LineEntry bp_loc_line_entry;
4133 const lldb::addr_t file_addr =
4134 bp_loc_sp->GetAddress().GetFileAddress();
4135 if (file_addr != LLDB_INVALID_ADDRESS) {
4136 if (m_disassembly_range.ContainsFileAddress(file_addr))
4137 bp_file_addrs.insert(file_addr);
4143 const attr_t selected_highlight_attr = A_REVERSE;
4144 const attr_t pc_highlight_attr = COLOR_PAIR(1);
4148 InstructionList &insts = m_disassembly_sp->GetInstructionList();
4152 pc_address = frame_sp->GetFrameCodeAddress();
4153 const uint32_t pc_idx =
4154 pc_address.IsValid()
4155 ? insts.GetIndexOfInstructionAtAddress(pc_address)
4157 if (set_selected_line_to_pc) {
4158 m_selected_line = pc_idx;
4161 const uint32_t non_visible_pc_offset = (num_visible_lines / 5);
4162 if (static_cast<size_t>(m_first_visible_line) >= num_disassembly_lines)
4163 m_first_visible_line = 0;
4165 if (pc_idx < num_disassembly_lines) {
4166 if (pc_idx < static_cast<uint32_t>(m_first_visible_line) ||
4167 pc_idx >= m_first_visible_line + num_visible_lines)
4168 m_first_visible_line = pc_idx - non_visible_pc_offset;
4171 for (size_t i = 0; i < num_visible_lines; ++i) {
4172 const uint32_t inst_idx = m_first_visible_line + i;
4173 Instruction *inst = insts.GetInstructionAtIndex(inst_idx).get();
4177 const int line_y = m_min_y + i;
4178 window.MoveCursor(1, line_y);
4179 const bool is_pc_line = frame_sp && inst_idx == pc_idx;
4180 const bool line_is_selected = m_selected_line == inst_idx;
4181 // Highlight the line as the PC line first, then if the selected line
4182 // isn't the same as the PC line, highlight it differently
4183 attr_t highlight_attr = 0;
4186 highlight_attr = pc_highlight_attr;
4187 else if (line_is_selected)
4188 highlight_attr = selected_highlight_attr;
4190 if (bp_file_addrs.find(inst->GetAddress().GetFileAddress()) !=
4191 bp_file_addrs.end())
4192 bp_attr = COLOR_PAIR(2);
4195 window.AttributeOn(bp_attr);
4197 window.Printf(" 0x%16.16llx ",
4198 static_cast<unsigned long long>(
4199 inst->GetAddress().GetLoadAddress(target)));
4202 window.AttributeOff(bp_attr);
4204 window.PutChar(ACS_VLINE);
4205 // Mark the line with the PC with a diamond
4207 window.PutChar(ACS_DIAMOND);
4209 window.PutChar(' ');
4212 window.AttributeOn(highlight_attr);
4214 const char *mnemonic = inst->GetMnemonic(&exe_ctx);
4215 const char *operands = inst->GetOperands(&exe_ctx);
4216 const char *comment = inst->GetComment(&exe_ctx);
4218 if (mnemonic != nullptr && mnemonic[0] == '\0')
4220 if (operands != nullptr && operands[0] == '\0')
4222 if (comment != nullptr && comment[0] == '\0')
4227 if (mnemonic != nullptr && operands != nullptr && comment != nullptr)
4228 strm.Printf("%-8s %-25s ; %s", mnemonic, operands, comment);
4229 else if (mnemonic != nullptr && operands != nullptr)
4230 strm.Printf("%-8s %s", mnemonic, operands);
4231 else if (mnemonic != nullptr)
4232 strm.Printf("%s", mnemonic);
4235 window.PutCStringTruncated(strm.GetData(), right_pad);
4237 if (is_pc_line && frame_sp &&
4238 frame_sp->GetConcreteFrameIndex() == 0) {
4239 StopInfoSP stop_info_sp;
4241 stop_info_sp = thread->GetStopInfo();
4243 const char *stop_description = stop_info_sp->GetDescription();
4244 if (stop_description && stop_description[0]) {
4245 size_t stop_description_len = strlen(stop_description);
4246 int desc_x = window_width - stop_description_len - 16;
4247 window.Printf("%*s", desc_x - window.GetCursorX(), "");
4248 // window.MoveCursor(window_width - stop_description_len - 15,
4250 window.Printf("<<< Thread %u: %s ", thread->GetIndexID(),
4254 window.Printf("%*s", window_width - window.GetCursorX() - 1, "");
4258 window.AttributeOff(highlight_attr);
4262 window.DeferredRefresh();
4263 return true; // Drawing handled
4266 size_t GetNumLines() {
4267 size_t num_lines = GetNumSourceLines();
4269 num_lines = GetNumDisassemblyLines();
4273 size_t GetNumSourceLines() const {
4275 return m_file_sp->GetNumLines();
4279 size_t GetNumDisassemblyLines() const {
4280 if (m_disassembly_sp)
4281 return m_disassembly_sp->GetInstructionList().GetSize();
4285 HandleCharResult WindowDelegateHandleChar(Window &window, int c) override {
4286 const uint32_t num_visible_lines = NumVisibleLines();
4287 const size_t num_lines = GetNumLines();
4293 if (static_cast<uint32_t>(m_first_visible_line) > num_visible_lines)
4294 m_first_visible_line -= num_visible_lines;
4296 m_first_visible_line = 0;
4297 m_selected_line = m_first_visible_line;
4304 if (m_first_visible_line + num_visible_lines < num_lines)
4305 m_first_visible_line += num_visible_lines;
4306 else if (num_lines < num_visible_lines)
4307 m_first_visible_line = 0;
4309 m_first_visible_line = num_lines - num_visible_lines;
4310 m_selected_line = m_first_visible_line;
4315 if (m_selected_line > 0) {
4317 if (static_cast<size_t>(m_first_visible_line) > m_selected_line)
4318 m_first_visible_line = m_selected_line;
4323 if (m_selected_line + 1 < num_lines) {
4325 if (m_first_visible_line + num_visible_lines < m_selected_line)
4326 m_first_visible_line++;
4333 // Set a breakpoint and run to the line using a one shot breakpoint
4334 if (GetNumSourceLines() > 0) {
4335 ExecutionContext exe_ctx =
4336 m_debugger.GetCommandInterpreter().GetExecutionContext();
4337 if (exe_ctx.HasProcessScope() && exe_ctx.GetProcessRef().IsAlive()) {
4338 BreakpointSP bp_sp = exe_ctx.GetTargetRef().CreateBreakpoint(
4339 nullptr, // Don't limit the breakpoint to certain modules
4340 m_file_sp->GetFileSpec(), // Source file
4342 1, // Source line number (m_selected_line is zero based)
4344 eLazyBoolCalculate, // Check inlines using global setting
4345 eLazyBoolCalculate, // Skip prologue using global setting,
4347 false, // request_hardware
4348 eLazyBoolCalculate); // move_to_nearest_code
4349 // Make breakpoint one shot
4350 bp_sp->GetOptions()->SetOneShot(true);
4351 exe_ctx.GetProcessRef().Resume();
4353 } else if (m_selected_line < GetNumDisassemblyLines()) {
4354 const Instruction *inst = m_disassembly_sp->GetInstructionList()
4355 .GetInstructionAtIndex(m_selected_line)
4357 ExecutionContext exe_ctx =
4358 m_debugger.GetCommandInterpreter().GetExecutionContext();
4359 if (exe_ctx.HasTargetScope()) {
4360 Address addr = inst->GetAddress();
4361 BreakpointSP bp_sp = exe_ctx.GetTargetRef().CreateBreakpoint(
4362 addr, // lldb_private::Address
4364 false); // request_hardware
4365 // Make breakpoint one shot
4366 bp_sp->GetOptions()->SetOneShot(true);
4367 exe_ctx.GetProcessRef().Resume();
4372 case 'b': // 'b' == toggle breakpoint on currently selected line
4373 if (m_selected_line < GetNumSourceLines()) {
4374 ExecutionContext exe_ctx =
4375 m_debugger.GetCommandInterpreter().GetExecutionContext();
4376 if (exe_ctx.HasTargetScope()) {
4377 BreakpointSP bp_sp = exe_ctx.GetTargetRef().CreateBreakpoint(
4378 nullptr, // Don't limit the breakpoint to certain modules
4379 m_file_sp->GetFileSpec(), // Source file
4381 1, // Source line number (m_selected_line is zero based)
4383 eLazyBoolCalculate, // Check inlines using global setting
4384 eLazyBoolCalculate, // Skip prologue using global setting,
4386 false, // request_hardware
4387 eLazyBoolCalculate); // move_to_nearest_code
4389 } else if (m_selected_line < GetNumDisassemblyLines()) {
4390 const Instruction *inst = m_disassembly_sp->GetInstructionList()
4391 .GetInstructionAtIndex(m_selected_line)
4393 ExecutionContext exe_ctx =
4394 m_debugger.GetCommandInterpreter().GetExecutionContext();
4395 if (exe_ctx.HasTargetScope()) {
4396 Address addr = inst->GetAddress();
4397 BreakpointSP bp_sp = exe_ctx.GetTargetRef().CreateBreakpoint(
4398 addr, // lldb_private::Address
4400 false); // request_hardware
4405 case 'd': // 'd' == detach and let run
4406 case 'D': // 'D' == detach and keep stopped
4408 ExecutionContext exe_ctx =
4409 m_debugger.GetCommandInterpreter().GetExecutionContext();
4410 if (exe_ctx.HasProcessScope())
4411 exe_ctx.GetProcessRef().Detach(c == 'D');
4418 ExecutionContext exe_ctx =
4419 m_debugger.GetCommandInterpreter().GetExecutionContext();
4420 if (exe_ctx.HasProcessScope())
4421 exe_ctx.GetProcessRef().Destroy(false);
4428 ExecutionContext exe_ctx =
4429 m_debugger.GetCommandInterpreter().GetExecutionContext();
4430 if (exe_ctx.HasProcessScope())
4431 exe_ctx.GetProcessRef().Resume();
4438 ExecutionContext exe_ctx =
4439 m_debugger.GetCommandInterpreter().GetExecutionContext();
4440 if (exe_ctx.HasThreadScope() &&
4441 StateIsStoppedState(exe_ctx.GetProcessRef().GetState(), true)) {
4442 exe_ctx.GetThreadRef().StepOut();
4447 case 'n': // 'n' == step over
4448 case 'N': // 'N' == step over instruction
4450 ExecutionContext exe_ctx =
4451 m_debugger.GetCommandInterpreter().GetExecutionContext();
4452 if (exe_ctx.HasThreadScope() &&
4453 StateIsStoppedState(exe_ctx.GetProcessRef().GetState(), true)) {
4454 bool source_step = (c == 'n');
4455 exe_ctx.GetThreadRef().StepOver(source_step);
4460 case 's': // 's' == step into
4461 case 'S': // 'S' == step into instruction
4463 ExecutionContext exe_ctx =
4464 m_debugger.GetCommandInterpreter().GetExecutionContext();
4465 if (exe_ctx.HasThreadScope() &&
4466 StateIsStoppedState(exe_ctx.GetProcessRef().GetState(), true)) {
4467 bool source_step = (c == 's');
4468 exe_ctx.GetThreadRef().StepIn(source_step);
4474 window.CreateHelpSubwindow();
4480 return eKeyNotHandled;
4484 typedef std::set<uint32_t> BreakpointLines;
4485 typedef std::set<lldb::addr_t> BreakpointAddrs;
4487 Debugger &m_debugger;
4489 SourceManager::FileSP m_file_sp;
4490 SymbolContextScope *m_disassembly_scope;
4491 lldb::DisassemblerSP m_disassembly_sp;
4492 AddressRange m_disassembly_range;
4493 StreamString m_title;
4494 lldb::user_id_t m_tid;
4495 char m_line_format[8];
4497 uint32_t m_selected_line; // The selected line
4498 uint32_t m_pc_line; // The line with the PC
4500 uint32_t m_frame_idx;
4501 int m_first_visible_line;
4508 DisplayOptions ValueObjectListDelegate::g_options = {true};
4510 IOHandlerCursesGUI::IOHandlerCursesGUI(Debugger &debugger)
4511 : IOHandler(debugger, IOHandler::Type::Curses) {}
4513 void IOHandlerCursesGUI::Activate() {
4514 IOHandler::Activate();
4516 m_app_ap.reset(new Application(GetInputFILE(), GetOutputFILE()));
4518 // This is both a window and a menu delegate
4519 std::shared_ptr<ApplicationDelegate> app_delegate_sp(
4520 new ApplicationDelegate(*m_app_ap, m_debugger));
4522 MenuDelegateSP app_menu_delegate_sp =
4523 std::static_pointer_cast<MenuDelegate>(app_delegate_sp);
4524 MenuSP lldb_menu_sp(
4525 new Menu("LLDB", "F1", KEY_F(1), ApplicationDelegate::eMenuID_LLDB));
4526 MenuSP exit_menuitem_sp(
4527 new Menu("Exit", nullptr, 'x', ApplicationDelegate::eMenuID_LLDBExit));
4528 exit_menuitem_sp->SetCannedResult(MenuActionResult::Quit);
4529 lldb_menu_sp->AddSubmenu(MenuSP(new Menu(
4530 "About LLDB", nullptr, 'a', ApplicationDelegate::eMenuID_LLDBAbout)));
4531 lldb_menu_sp->AddSubmenu(MenuSP(new Menu(Menu::Type::Separator)));
4532 lldb_menu_sp->AddSubmenu(exit_menuitem_sp);
4534 MenuSP target_menu_sp(new Menu("Target", "F2", KEY_F(2),
4535 ApplicationDelegate::eMenuID_Target));
4536 target_menu_sp->AddSubmenu(MenuSP(new Menu(
4537 "Create", nullptr, 'c', ApplicationDelegate::eMenuID_TargetCreate)));
4538 target_menu_sp->AddSubmenu(MenuSP(new Menu(
4539 "Delete", nullptr, 'd', ApplicationDelegate::eMenuID_TargetDelete)));
4541 MenuSP process_menu_sp(new Menu("Process", "F3", KEY_F(3),
4542 ApplicationDelegate::eMenuID_Process));
4543 process_menu_sp->AddSubmenu(MenuSP(new Menu(
4544 "Attach", nullptr, 'a', ApplicationDelegate::eMenuID_ProcessAttach)));
4545 process_menu_sp->AddSubmenu(MenuSP(new Menu(
4546 "Detach", nullptr, 'd', ApplicationDelegate::eMenuID_ProcessDetach)));
4547 process_menu_sp->AddSubmenu(MenuSP(new Menu(
4548 "Launch", nullptr, 'l', ApplicationDelegate::eMenuID_ProcessLaunch)));
4549 process_menu_sp->AddSubmenu(MenuSP(new Menu(Menu::Type::Separator)));
4550 process_menu_sp->AddSubmenu(
4551 MenuSP(new Menu("Continue", nullptr, 'c',
4552 ApplicationDelegate::eMenuID_ProcessContinue)));
4553 process_menu_sp->AddSubmenu(MenuSP(new Menu(
4554 "Halt", nullptr, 'h', ApplicationDelegate::eMenuID_ProcessHalt)));
4555 process_menu_sp->AddSubmenu(MenuSP(new Menu(
4556 "Kill", nullptr, 'k', ApplicationDelegate::eMenuID_ProcessKill)));
4558 MenuSP thread_menu_sp(new Menu("Thread", "F4", KEY_F(4),
4559 ApplicationDelegate::eMenuID_Thread));
4560 thread_menu_sp->AddSubmenu(MenuSP(new Menu(
4561 "Step In", nullptr, 'i', ApplicationDelegate::eMenuID_ThreadStepIn)));
4562 thread_menu_sp->AddSubmenu(
4563 MenuSP(new Menu("Step Over", nullptr, 'v',
4564 ApplicationDelegate::eMenuID_ThreadStepOver)));
4565 thread_menu_sp->AddSubmenu(MenuSP(new Menu(
4566 "Step Out", nullptr, 'o', ApplicationDelegate::eMenuID_ThreadStepOut)));
4568 MenuSP view_menu_sp(
4569 new Menu("View", "F5", KEY_F(5), ApplicationDelegate::eMenuID_View));
4570 view_menu_sp->AddSubmenu(
4571 MenuSP(new Menu("Backtrace", nullptr, 'b',
4572 ApplicationDelegate::eMenuID_ViewBacktrace)));
4573 view_menu_sp->AddSubmenu(
4574 MenuSP(new Menu("Registers", nullptr, 'r',
4575 ApplicationDelegate::eMenuID_ViewRegisters)));
4576 view_menu_sp->AddSubmenu(MenuSP(new Menu(
4577 "Source", nullptr, 's', ApplicationDelegate::eMenuID_ViewSource)));
4578 view_menu_sp->AddSubmenu(
4579 MenuSP(new Menu("Variables", nullptr, 'v',
4580 ApplicationDelegate::eMenuID_ViewVariables)));
4582 MenuSP help_menu_sp(
4583 new Menu("Help", "F6", KEY_F(6), ApplicationDelegate::eMenuID_Help));
4584 help_menu_sp->AddSubmenu(MenuSP(new Menu(
4585 "GUI Help", nullptr, 'g', ApplicationDelegate::eMenuID_HelpGUIHelp)));
4587 m_app_ap->Initialize();
4588 WindowSP &main_window_sp = m_app_ap->GetMainWindow();
4590 MenuSP menubar_sp(new Menu(Menu::Type::Bar));
4591 menubar_sp->AddSubmenu(lldb_menu_sp);
4592 menubar_sp->AddSubmenu(target_menu_sp);
4593 menubar_sp->AddSubmenu(process_menu_sp);
4594 menubar_sp->AddSubmenu(thread_menu_sp);
4595 menubar_sp->AddSubmenu(view_menu_sp);
4596 menubar_sp->AddSubmenu(help_menu_sp);
4597 menubar_sp->SetDelegate(app_menu_delegate_sp);
4599 Rect content_bounds = main_window_sp->GetFrame();
4600 Rect menubar_bounds = content_bounds.MakeMenuBar();
4601 Rect status_bounds = content_bounds.MakeStatusBar();
4603 Rect variables_bounds;
4604 Rect threads_bounds;
4605 Rect source_variables_bounds;
4606 content_bounds.VerticalSplitPercentage(0.80, source_variables_bounds,
4608 source_variables_bounds.HorizontalSplitPercentage(0.70, source_bounds,
4611 WindowSP menubar_window_sp =
4612 main_window_sp->CreateSubWindow("Menubar", menubar_bounds, false);
4613 // Let the menubar get keys if the active window doesn't handle the keys
4614 // that are typed so it can respond to menubar key presses.
4615 menubar_window_sp->SetCanBeActive(
4616 false); // Don't let the menubar become the active window
4617 menubar_window_sp->SetDelegate(menubar_sp);
4619 WindowSP source_window_sp(
4620 main_window_sp->CreateSubWindow("Source", source_bounds, true));
4621 WindowSP variables_window_sp(
4622 main_window_sp->CreateSubWindow("Variables", variables_bounds, false));
4623 WindowSP threads_window_sp(
4624 main_window_sp->CreateSubWindow("Threads", threads_bounds, false));
4625 WindowSP status_window_sp(
4626 main_window_sp->CreateSubWindow("Status", status_bounds, false));
4627 status_window_sp->SetCanBeActive(
4628 false); // Don't let the status bar become the active window
4629 main_window_sp->SetDelegate(
4630 std::static_pointer_cast<WindowDelegate>(app_delegate_sp));
4631 source_window_sp->SetDelegate(
4632 WindowDelegateSP(new SourceFileWindowDelegate(m_debugger)));
4633 variables_window_sp->SetDelegate(
4634 WindowDelegateSP(new FrameVariablesWindowDelegate(m_debugger)));
4635 TreeDelegateSP thread_delegate_sp(new ThreadsTreeDelegate(m_debugger));
4636 threads_window_sp->SetDelegate(WindowDelegateSP(
4637 new TreeWindowDelegate(m_debugger, thread_delegate_sp)));
4638 status_window_sp->SetDelegate(
4639 WindowDelegateSP(new StatusBarWindowDelegate(m_debugger)));
4641 // Show the main help window once the first time the curses GUI is launched
4642 static bool g_showed_help = false;
4643 if (!g_showed_help) {
4644 g_showed_help = true;
4645 main_window_sp->CreateHelpSubwindow();
4648 init_pair(1, COLOR_WHITE, COLOR_BLUE);
4649 init_pair(2, COLOR_BLACK, COLOR_WHITE);
4650 init_pair(3, COLOR_MAGENTA, COLOR_WHITE);
4651 init_pair(4, COLOR_MAGENTA, COLOR_BLACK);
4652 init_pair(5, COLOR_RED, COLOR_BLACK);
4656 void IOHandlerCursesGUI::Deactivate() { m_app_ap->Terminate(); }
4658 void IOHandlerCursesGUI::Run() {
4659 m_app_ap->Run(m_debugger);
4663 IOHandlerCursesGUI::~IOHandlerCursesGUI() = default;
4665 void IOHandlerCursesGUI::Cancel() {}
4667 bool IOHandlerCursesGUI::Interrupt() { return false; }
4669 void IOHandlerCursesGUI::GotEOF() {}
4671 #endif // LLDB_DISABLE_CURSES