1 //===-- IOHandlerCursesGUI.cpp --------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "lldb/Core/IOHandlerCursesGUI.h" 10 #include "lldb/Host/Config.h" 11 12 #if LLDB_ENABLE_CURSES 13 #include <curses.h> 14 #include <panel.h> 15 #endif 16 17 #if defined(__APPLE__) 18 #include <deque> 19 #endif 20 #include <string> 21 22 #include "lldb/Core/Debugger.h" 23 #include "lldb/Core/StreamFile.h" 24 #include "lldb/Host/File.h" 25 #include "lldb/Utility/Predicate.h" 26 #include "lldb/Utility/Status.h" 27 #include "lldb/Utility/StreamString.h" 28 #include "lldb/Utility/StringList.h" 29 #include "lldb/lldb-forward.h" 30 31 #include "lldb/Interpreter/CommandCompletions.h" 32 #include "lldb/Interpreter/CommandInterpreter.h" 33 34 #if LLDB_ENABLE_CURSES 35 #include "lldb/Breakpoint/BreakpointLocation.h" 36 #include "lldb/Core/Module.h" 37 #include "lldb/Core/ValueObject.h" 38 #include "lldb/Core/ValueObjectRegister.h" 39 #include "lldb/Symbol/Block.h" 40 #include "lldb/Symbol/Function.h" 41 #include "lldb/Symbol/Symbol.h" 42 #include "lldb/Symbol/VariableList.h" 43 #include "lldb/Target/Process.h" 44 #include "lldb/Target/RegisterContext.h" 45 #include "lldb/Target/StackFrame.h" 46 #include "lldb/Target/StopInfo.h" 47 #include "lldb/Target/Target.h" 48 #include "lldb/Target/Thread.h" 49 #include "lldb/Utility/State.h" 50 #endif 51 52 #include "llvm/ADT/StringRef.h" 53 54 #ifdef _WIN32 55 #include "lldb/Host/windows/windows.h" 56 #endif 57 58 #include <memory> 59 #include <mutex> 60 61 #include <assert.h> 62 #include <ctype.h> 63 #include <errno.h> 64 #include <locale.h> 65 #include <stdint.h> 66 #include <stdio.h> 67 #include <string.h> 68 #include <type_traits> 69 70 using namespace lldb; 71 using namespace lldb_private; 72 using llvm::None; 73 using llvm::Optional; 74 using llvm::StringRef; 75 76 // we may want curses to be disabled for some builds for instance, windows 77 #if LLDB_ENABLE_CURSES 78 79 #define KEY_RETURN 10 80 #define KEY_ESCAPE 27 81 82 namespace curses { 83 class Menu; 84 class MenuDelegate; 85 class Window; 86 class WindowDelegate; 87 typedef std::shared_ptr<Menu> MenuSP; 88 typedef std::shared_ptr<MenuDelegate> MenuDelegateSP; 89 typedef std::shared_ptr<Window> WindowSP; 90 typedef std::shared_ptr<WindowDelegate> WindowDelegateSP; 91 typedef std::vector<MenuSP> Menus; 92 typedef std::vector<WindowSP> Windows; 93 typedef std::vector<WindowDelegateSP> WindowDelegates; 94 95 #if 0 96 type summary add -s "x=${var.x}, y=${var.y}" curses::Point 97 type summary add -s "w=${var.width}, h=${var.height}" curses::Size 98 type summary add -s "${var.origin%S} ${var.size%S}" curses::Rect 99 #endif 100 101 struct Point { 102 int x; 103 int y; 104 105 Point(int _x = 0, int _y = 0) : x(_x), y(_y) {} 106 107 void Clear() { 108 x = 0; 109 y = 0; 110 } 111 112 Point &operator+=(const Point &rhs) { 113 x += rhs.x; 114 y += rhs.y; 115 return *this; 116 } 117 118 void Dump() { printf("(x=%i, y=%i)\n", x, y); } 119 }; 120 121 bool operator==(const Point &lhs, const Point &rhs) { 122 return lhs.x == rhs.x && lhs.y == rhs.y; 123 } 124 125 bool operator!=(const Point &lhs, const Point &rhs) { 126 return lhs.x != rhs.x || lhs.y != rhs.y; 127 } 128 129 struct Size { 130 int width; 131 int height; 132 Size(int w = 0, int h = 0) : width(w), height(h) {} 133 134 void Clear() { 135 width = 0; 136 height = 0; 137 } 138 139 void Dump() { printf("(w=%i, h=%i)\n", width, height); } 140 }; 141 142 bool operator==(const Size &lhs, const Size &rhs) { 143 return lhs.width == rhs.width && lhs.height == rhs.height; 144 } 145 146 bool operator!=(const Size &lhs, const Size &rhs) { 147 return lhs.width != rhs.width || lhs.height != rhs.height; 148 } 149 150 struct Rect { 151 Point origin; 152 Size size; 153 154 Rect() : origin(), size() {} 155 156 Rect(const Point &p, const Size &s) : origin(p), size(s) {} 157 158 void Clear() { 159 origin.Clear(); 160 size.Clear(); 161 } 162 163 void Dump() { 164 printf("(x=%i, y=%i), w=%i, h=%i)\n", origin.x, origin.y, size.width, 165 size.height); 166 } 167 168 void Inset(int w, int h) { 169 if (size.width > w * 2) 170 size.width -= w * 2; 171 origin.x += w; 172 173 if (size.height > h * 2) 174 size.height -= h * 2; 175 origin.y += h; 176 } 177 178 // Return a status bar rectangle which is the last line of this rectangle. 179 // This rectangle will be modified to not include the status bar area. 180 Rect MakeStatusBar() { 181 Rect status_bar; 182 if (size.height > 1) { 183 status_bar.origin.x = origin.x; 184 status_bar.origin.y = size.height; 185 status_bar.size.width = size.width; 186 status_bar.size.height = 1; 187 --size.height; 188 } 189 return status_bar; 190 } 191 192 // Return a menubar rectangle which is the first line of this rectangle. This 193 // rectangle will be modified to not include the menubar area. 194 Rect MakeMenuBar() { 195 Rect menubar; 196 if (size.height > 1) { 197 menubar.origin.x = origin.x; 198 menubar.origin.y = origin.y; 199 menubar.size.width = size.width; 200 menubar.size.height = 1; 201 ++origin.y; 202 --size.height; 203 } 204 return menubar; 205 } 206 207 void HorizontalSplitPercentage(float top_percentage, Rect &top, 208 Rect &bottom) const { 209 float top_height = top_percentage * size.height; 210 HorizontalSplit(top_height, top, bottom); 211 } 212 213 void HorizontalSplit(int top_height, Rect &top, Rect &bottom) const { 214 top = *this; 215 if (top_height < size.height) { 216 top.size.height = top_height; 217 bottom.origin.x = origin.x; 218 bottom.origin.y = origin.y + top.size.height; 219 bottom.size.width = size.width; 220 bottom.size.height = size.height - top.size.height; 221 } else { 222 bottom.Clear(); 223 } 224 } 225 226 void VerticalSplitPercentage(float left_percentage, Rect &left, 227 Rect &right) const { 228 float left_width = left_percentage * size.width; 229 VerticalSplit(left_width, left, right); 230 } 231 232 void VerticalSplit(int left_width, Rect &left, Rect &right) const { 233 left = *this; 234 if (left_width < size.width) { 235 left.size.width = left_width; 236 right.origin.x = origin.x + left.size.width; 237 right.origin.y = origin.y; 238 right.size.width = size.width - left.size.width; 239 right.size.height = size.height; 240 } else { 241 right.Clear(); 242 } 243 } 244 }; 245 246 bool operator==(const Rect &lhs, const Rect &rhs) { 247 return lhs.origin == rhs.origin && lhs.size == rhs.size; 248 } 249 250 bool operator!=(const Rect &lhs, const Rect &rhs) { 251 return lhs.origin != rhs.origin || lhs.size != rhs.size; 252 } 253 254 enum HandleCharResult { 255 eKeyNotHandled = 0, 256 eKeyHandled = 1, 257 eQuitApplication = 2 258 }; 259 260 enum class MenuActionResult { 261 Handled, 262 NotHandled, 263 Quit // Exit all menus and quit 264 }; 265 266 struct KeyHelp { 267 int ch; 268 const char *description; 269 }; 270 271 class WindowDelegate { 272 public: 273 virtual ~WindowDelegate() = default; 274 275 virtual bool WindowDelegateDraw(Window &window, bool force) { 276 return false; // Drawing not handled 277 } 278 279 virtual HandleCharResult WindowDelegateHandleChar(Window &window, int key) { 280 return eKeyNotHandled; 281 } 282 283 virtual const char *WindowDelegateGetHelpText() { return nullptr; } 284 285 virtual KeyHelp *WindowDelegateGetKeyHelp() { return nullptr; } 286 }; 287 288 class HelpDialogDelegate : public WindowDelegate { 289 public: 290 HelpDialogDelegate(const char *text, KeyHelp *key_help_array); 291 292 ~HelpDialogDelegate() override; 293 294 bool WindowDelegateDraw(Window &window, bool force) override; 295 296 HandleCharResult WindowDelegateHandleChar(Window &window, int key) override; 297 298 size_t GetNumLines() const { return m_text.GetSize(); } 299 300 size_t GetMaxLineLength() const { return m_text.GetMaxStringLength(); } 301 302 protected: 303 StringList m_text; 304 int m_first_visible_line; 305 }; 306 307 class Window { 308 public: 309 Window(const char *name) 310 : m_name(name), m_window(nullptr), m_panel(nullptr), m_parent(nullptr), 311 m_subwindows(), m_delegate_sp(), m_curr_active_window_idx(UINT32_MAX), 312 m_prev_active_window_idx(UINT32_MAX), m_delete(false), 313 m_needs_update(true), m_can_activate(true), m_is_subwin(false) {} 314 315 Window(const char *name, WINDOW *w, bool del = true) 316 : m_name(name), m_window(nullptr), m_panel(nullptr), m_parent(nullptr), 317 m_subwindows(), m_delegate_sp(), m_curr_active_window_idx(UINT32_MAX), 318 m_prev_active_window_idx(UINT32_MAX), m_delete(del), 319 m_needs_update(true), m_can_activate(true), m_is_subwin(false) { 320 if (w) 321 Reset(w); 322 } 323 324 Window(const char *name, const Rect &bounds) 325 : m_name(name), m_window(nullptr), m_parent(nullptr), m_subwindows(), 326 m_delegate_sp(), m_curr_active_window_idx(UINT32_MAX), 327 m_prev_active_window_idx(UINT32_MAX), m_delete(true), 328 m_needs_update(true), m_can_activate(true), m_is_subwin(false) { 329 Reset(::newwin(bounds.size.height, bounds.size.width, bounds.origin.y, 330 bounds.origin.y)); 331 } 332 333 virtual ~Window() { 334 RemoveSubWindows(); 335 Reset(); 336 } 337 338 void Reset(WINDOW *w = nullptr, bool del = true) { 339 if (m_window == w) 340 return; 341 342 if (m_panel) { 343 ::del_panel(m_panel); 344 m_panel = nullptr; 345 } 346 if (m_window && m_delete) { 347 ::delwin(m_window); 348 m_window = nullptr; 349 m_delete = false; 350 } 351 if (w) { 352 m_window = w; 353 m_panel = ::new_panel(m_window); 354 m_delete = del; 355 } 356 } 357 358 void AttributeOn(attr_t attr) { ::wattron(m_window, attr); } 359 void AttributeOff(attr_t attr) { ::wattroff(m_window, attr); } 360 void Box(chtype v_char = ACS_VLINE, chtype h_char = ACS_HLINE) { 361 ::box(m_window, v_char, h_char); 362 } 363 void Clear() { ::wclear(m_window); } 364 void Erase() { ::werase(m_window); } 365 Rect GetBounds() { 366 return Rect(GetParentOrigin(), GetSize()); 367 } // Get the rectangle in our parent window 368 int GetChar() { return ::wgetch(m_window); } 369 int GetCursorX() { return getcurx(m_window); } 370 int GetCursorY() { return getcury(m_window); } 371 Rect GetFrame() { 372 return Rect(Point(), GetSize()); 373 } // Get our rectangle in our own coordinate system 374 Point GetParentOrigin() { return Point(GetParentX(), GetParentY()); } 375 Size GetSize() { return Size(GetWidth(), GetHeight()); } 376 int GetParentX() { return getparx(m_window); } 377 int GetParentY() { return getpary(m_window); } 378 int GetMaxX() { return getmaxx(m_window); } 379 int GetMaxY() { return getmaxy(m_window); } 380 int GetWidth() { return GetMaxX(); } 381 int GetHeight() { return GetMaxY(); } 382 void MoveCursor(int x, int y) { ::wmove(m_window, y, x); } 383 void MoveWindow(int x, int y) { MoveWindow(Point(x, y)); } 384 void Resize(int w, int h) { ::wresize(m_window, h, w); } 385 void Resize(const Size &size) { 386 ::wresize(m_window, size.height, size.width); 387 } 388 void PutChar(int ch) { ::waddch(m_window, ch); } 389 void PutCString(const char *s, int len = -1) { ::waddnstr(m_window, s, len); } 390 void SetBackground(int color_pair_idx) { 391 ::wbkgd(m_window, COLOR_PAIR(color_pair_idx)); 392 } 393 394 void PutCStringTruncated(const char *s, int right_pad) { 395 int bytes_left = GetWidth() - GetCursorX(); 396 if (bytes_left > right_pad) { 397 bytes_left -= right_pad; 398 ::waddnstr(m_window, s, bytes_left); 399 } 400 } 401 402 void MoveWindow(const Point &origin) { 403 const bool moving_window = origin != GetParentOrigin(); 404 if (m_is_subwin && moving_window) { 405 // Can't move subwindows, must delete and re-create 406 Size size = GetSize(); 407 Reset(::subwin(m_parent->m_window, size.height, size.width, origin.y, 408 origin.x), 409 true); 410 } else { 411 ::mvwin(m_window, origin.y, origin.x); 412 } 413 } 414 415 void SetBounds(const Rect &bounds) { 416 const bool moving_window = bounds.origin != GetParentOrigin(); 417 if (m_is_subwin && moving_window) { 418 // Can't move subwindows, must delete and re-create 419 Reset(::subwin(m_parent->m_window, bounds.size.height, bounds.size.width, 420 bounds.origin.y, bounds.origin.x), 421 true); 422 } else { 423 if (moving_window) 424 MoveWindow(bounds.origin); 425 Resize(bounds.size); 426 } 427 } 428 429 void Printf(const char *format, ...) __attribute__((format(printf, 2, 3))) { 430 va_list args; 431 va_start(args, format); 432 vwprintw(m_window, format, args); 433 va_end(args); 434 } 435 436 void Touch() { 437 ::touchwin(m_window); 438 if (m_parent) 439 m_parent->Touch(); 440 } 441 442 WindowSP CreateSubWindow(const char *name, const Rect &bounds, 443 bool make_active) { 444 auto get_window = [this, &bounds]() { 445 return m_window 446 ? ::subwin(m_window, bounds.size.height, bounds.size.width, 447 bounds.origin.y, bounds.origin.x) 448 : ::newwin(bounds.size.height, bounds.size.width, 449 bounds.origin.y, bounds.origin.x); 450 }; 451 WindowSP subwindow_sp = std::make_shared<Window>(name, get_window(), true); 452 subwindow_sp->m_is_subwin = subwindow_sp.operator bool(); 453 subwindow_sp->m_parent = this; 454 if (make_active) { 455 m_prev_active_window_idx = m_curr_active_window_idx; 456 m_curr_active_window_idx = m_subwindows.size(); 457 } 458 m_subwindows.push_back(subwindow_sp); 459 ::top_panel(subwindow_sp->m_panel); 460 m_needs_update = true; 461 return subwindow_sp; 462 } 463 464 bool RemoveSubWindow(Window *window) { 465 Windows::iterator pos, end = m_subwindows.end(); 466 size_t i = 0; 467 for (pos = m_subwindows.begin(); pos != end; ++pos, ++i) { 468 if ((*pos).get() == window) { 469 if (m_prev_active_window_idx == i) 470 m_prev_active_window_idx = UINT32_MAX; 471 else if (m_prev_active_window_idx != UINT32_MAX && 472 m_prev_active_window_idx > i) 473 --m_prev_active_window_idx; 474 475 if (m_curr_active_window_idx == i) 476 m_curr_active_window_idx = UINT32_MAX; 477 else if (m_curr_active_window_idx != UINT32_MAX && 478 m_curr_active_window_idx > i) 479 --m_curr_active_window_idx; 480 window->Erase(); 481 m_subwindows.erase(pos); 482 m_needs_update = true; 483 if (m_parent) 484 m_parent->Touch(); 485 else 486 ::touchwin(stdscr); 487 return true; 488 } 489 } 490 return false; 491 } 492 493 WindowSP FindSubWindow(const char *name) { 494 Windows::iterator pos, end = m_subwindows.end(); 495 size_t i = 0; 496 for (pos = m_subwindows.begin(); pos != end; ++pos, ++i) { 497 if ((*pos)->m_name == name) 498 return *pos; 499 } 500 return WindowSP(); 501 } 502 503 void RemoveSubWindows() { 504 m_curr_active_window_idx = UINT32_MAX; 505 m_prev_active_window_idx = UINT32_MAX; 506 for (Windows::iterator pos = m_subwindows.begin(); 507 pos != m_subwindows.end(); pos = m_subwindows.erase(pos)) { 508 (*pos)->Erase(); 509 } 510 if (m_parent) 511 m_parent->Touch(); 512 else 513 ::touchwin(stdscr); 514 } 515 516 WINDOW *get() { return m_window; } 517 518 operator WINDOW *() { return m_window; } 519 520 // Window drawing utilities 521 void DrawTitleBox(const char *title, const char *bottom_message = nullptr) { 522 attr_t attr = 0; 523 if (IsActive()) 524 attr = A_BOLD | COLOR_PAIR(2); 525 else 526 attr = 0; 527 if (attr) 528 AttributeOn(attr); 529 530 Box(); 531 MoveCursor(3, 0); 532 533 if (title && title[0]) { 534 PutChar('<'); 535 PutCString(title); 536 PutChar('>'); 537 } 538 539 if (bottom_message && bottom_message[0]) { 540 int bottom_message_length = strlen(bottom_message); 541 int x = GetWidth() - 3 - (bottom_message_length + 2); 542 543 if (x > 0) { 544 MoveCursor(x, GetHeight() - 1); 545 PutChar('['); 546 PutCString(bottom_message); 547 PutChar(']'); 548 } else { 549 MoveCursor(1, GetHeight() - 1); 550 PutChar('['); 551 PutCStringTruncated(bottom_message, 1); 552 } 553 } 554 if (attr) 555 AttributeOff(attr); 556 } 557 558 virtual void Draw(bool force) { 559 if (m_delegate_sp && m_delegate_sp->WindowDelegateDraw(*this, force)) 560 return; 561 562 for (auto &subwindow_sp : m_subwindows) 563 subwindow_sp->Draw(force); 564 } 565 566 bool CreateHelpSubwindow() { 567 if (m_delegate_sp) { 568 const char *text = m_delegate_sp->WindowDelegateGetHelpText(); 569 KeyHelp *key_help = m_delegate_sp->WindowDelegateGetKeyHelp(); 570 if ((text && text[0]) || key_help) { 571 std::unique_ptr<HelpDialogDelegate> help_delegate_up( 572 new HelpDialogDelegate(text, key_help)); 573 const size_t num_lines = help_delegate_up->GetNumLines(); 574 const size_t max_length = help_delegate_up->GetMaxLineLength(); 575 Rect bounds = GetBounds(); 576 bounds.Inset(1, 1); 577 if (max_length + 4 < static_cast<size_t>(bounds.size.width)) { 578 bounds.origin.x += (bounds.size.width - max_length + 4) / 2; 579 bounds.size.width = max_length + 4; 580 } else { 581 if (bounds.size.width > 100) { 582 const int inset_w = bounds.size.width / 4; 583 bounds.origin.x += inset_w; 584 bounds.size.width -= 2 * inset_w; 585 } 586 } 587 588 if (num_lines + 2 < static_cast<size_t>(bounds.size.height)) { 589 bounds.origin.y += (bounds.size.height - num_lines + 2) / 2; 590 bounds.size.height = num_lines + 2; 591 } else { 592 if (bounds.size.height > 100) { 593 const int inset_h = bounds.size.height / 4; 594 bounds.origin.y += inset_h; 595 bounds.size.height -= 2 * inset_h; 596 } 597 } 598 WindowSP help_window_sp; 599 Window *parent_window = GetParent(); 600 if (parent_window) 601 help_window_sp = parent_window->CreateSubWindow("Help", bounds, true); 602 else 603 help_window_sp = CreateSubWindow("Help", bounds, true); 604 help_window_sp->SetDelegate( 605 WindowDelegateSP(help_delegate_up.release())); 606 return true; 607 } 608 } 609 return false; 610 } 611 612 virtual HandleCharResult HandleChar(int key) { 613 // Always check the active window first 614 HandleCharResult result = eKeyNotHandled; 615 WindowSP active_window_sp = GetActiveWindow(); 616 if (active_window_sp) { 617 result = active_window_sp->HandleChar(key); 618 if (result != eKeyNotHandled) 619 return result; 620 } 621 622 if (m_delegate_sp) { 623 result = m_delegate_sp->WindowDelegateHandleChar(*this, key); 624 if (result != eKeyNotHandled) 625 return result; 626 } 627 628 // Then check for any windows that want any keys that weren't handled. This 629 // is typically only for a menubar. Make a copy of the subwindows in case 630 // any HandleChar() functions muck with the subwindows. If we don't do 631 // this, we can crash when iterating over the subwindows. 632 Windows subwindows(m_subwindows); 633 for (auto subwindow_sp : subwindows) { 634 if (!subwindow_sp->m_can_activate) { 635 HandleCharResult result = subwindow_sp->HandleChar(key); 636 if (result != eKeyNotHandled) 637 return result; 638 } 639 } 640 641 return eKeyNotHandled; 642 } 643 644 WindowSP GetActiveWindow() { 645 if (!m_subwindows.empty()) { 646 if (m_curr_active_window_idx >= m_subwindows.size()) { 647 if (m_prev_active_window_idx < m_subwindows.size()) { 648 m_curr_active_window_idx = m_prev_active_window_idx; 649 m_prev_active_window_idx = UINT32_MAX; 650 } else if (IsActive()) { 651 m_prev_active_window_idx = UINT32_MAX; 652 m_curr_active_window_idx = UINT32_MAX; 653 654 // Find first window that wants to be active if this window is active 655 const size_t num_subwindows = m_subwindows.size(); 656 for (size_t i = 0; i < num_subwindows; ++i) { 657 if (m_subwindows[i]->GetCanBeActive()) { 658 m_curr_active_window_idx = i; 659 break; 660 } 661 } 662 } 663 } 664 665 if (m_curr_active_window_idx < m_subwindows.size()) 666 return m_subwindows[m_curr_active_window_idx]; 667 } 668 return WindowSP(); 669 } 670 671 bool GetCanBeActive() const { return m_can_activate; } 672 673 void SetCanBeActive(bool b) { m_can_activate = b; } 674 675 void SetDelegate(const WindowDelegateSP &delegate_sp) { 676 m_delegate_sp = delegate_sp; 677 } 678 679 Window *GetParent() const { return m_parent; } 680 681 bool IsActive() const { 682 if (m_parent) 683 return m_parent->GetActiveWindow().get() == this; 684 else 685 return true; // Top level window is always active 686 } 687 688 void SelectNextWindowAsActive() { 689 // Move active focus to next window 690 const size_t num_subwindows = m_subwindows.size(); 691 if (m_curr_active_window_idx == UINT32_MAX) { 692 uint32_t idx = 0; 693 for (auto subwindow_sp : m_subwindows) { 694 if (subwindow_sp->GetCanBeActive()) { 695 m_curr_active_window_idx = idx; 696 break; 697 } 698 ++idx; 699 } 700 } else if (m_curr_active_window_idx + 1 < num_subwindows) { 701 bool handled = false; 702 m_prev_active_window_idx = m_curr_active_window_idx; 703 for (size_t idx = m_curr_active_window_idx + 1; idx < num_subwindows; 704 ++idx) { 705 if (m_subwindows[idx]->GetCanBeActive()) { 706 m_curr_active_window_idx = idx; 707 handled = true; 708 break; 709 } 710 } 711 if (!handled) { 712 for (size_t idx = 0; idx <= m_prev_active_window_idx; ++idx) { 713 if (m_subwindows[idx]->GetCanBeActive()) { 714 m_curr_active_window_idx = idx; 715 break; 716 } 717 } 718 } 719 } else { 720 m_prev_active_window_idx = m_curr_active_window_idx; 721 for (size_t idx = 0; idx < num_subwindows; ++idx) { 722 if (m_subwindows[idx]->GetCanBeActive()) { 723 m_curr_active_window_idx = idx; 724 break; 725 } 726 } 727 } 728 } 729 730 const char *GetName() const { return m_name.c_str(); } 731 732 protected: 733 std::string m_name; 734 WINDOW *m_window; 735 PANEL *m_panel; 736 Window *m_parent; 737 Windows m_subwindows; 738 WindowDelegateSP m_delegate_sp; 739 uint32_t m_curr_active_window_idx; 740 uint32_t m_prev_active_window_idx; 741 bool m_delete; 742 bool m_needs_update; 743 bool m_can_activate; 744 bool m_is_subwin; 745 746 private: 747 Window(const Window &) = delete; 748 const Window &operator=(const Window &) = delete; 749 }; 750 751 class MenuDelegate { 752 public: 753 virtual ~MenuDelegate() = default; 754 755 virtual MenuActionResult MenuDelegateAction(Menu &menu) = 0; 756 }; 757 758 class Menu : public WindowDelegate { 759 public: 760 enum class Type { Invalid, Bar, Item, Separator }; 761 762 // Menubar or separator constructor 763 Menu(Type type); 764 765 // Menuitem constructor 766 Menu(const char *name, const char *key_name, int key_value, 767 uint64_t identifier); 768 769 ~Menu() override = default; 770 771 const MenuDelegateSP &GetDelegate() const { return m_delegate_sp; } 772 773 void SetDelegate(const MenuDelegateSP &delegate_sp) { 774 m_delegate_sp = delegate_sp; 775 } 776 777 void RecalculateNameLengths(); 778 779 void AddSubmenu(const MenuSP &menu_sp); 780 781 int DrawAndRunMenu(Window &window); 782 783 void DrawMenuTitle(Window &window, bool highlight); 784 785 bool WindowDelegateDraw(Window &window, bool force) override; 786 787 HandleCharResult WindowDelegateHandleChar(Window &window, int key) override; 788 789 MenuActionResult ActionPrivate(Menu &menu) { 790 MenuActionResult result = MenuActionResult::NotHandled; 791 if (m_delegate_sp) { 792 result = m_delegate_sp->MenuDelegateAction(menu); 793 if (result != MenuActionResult::NotHandled) 794 return result; 795 } else if (m_parent) { 796 result = m_parent->ActionPrivate(menu); 797 if (result != MenuActionResult::NotHandled) 798 return result; 799 } 800 return m_canned_result; 801 } 802 803 MenuActionResult Action() { 804 // Call the recursive action so it can try to handle it with the menu 805 // delegate, and if not, try our parent menu 806 return ActionPrivate(*this); 807 } 808 809 void SetCannedResult(MenuActionResult result) { m_canned_result = result; } 810 811 Menus &GetSubmenus() { return m_submenus; } 812 813 const Menus &GetSubmenus() const { return m_submenus; } 814 815 int GetSelectedSubmenuIndex() const { return m_selected; } 816 817 void SetSelectedSubmenuIndex(int idx) { m_selected = idx; } 818 819 Type GetType() const { return m_type; } 820 821 int GetStartingColumn() const { return m_start_col; } 822 823 void SetStartingColumn(int col) { m_start_col = col; } 824 825 int GetKeyValue() const { return m_key_value; } 826 827 std::string &GetName() { return m_name; } 828 829 int GetDrawWidth() const { 830 return m_max_submenu_name_length + m_max_submenu_key_name_length + 8; 831 } 832 833 uint64_t GetIdentifier() const { return m_identifier; } 834 835 void SetIdentifier(uint64_t identifier) { m_identifier = identifier; } 836 837 protected: 838 std::string m_name; 839 std::string m_key_name; 840 uint64_t m_identifier; 841 Type m_type; 842 int m_key_value; 843 int m_start_col; 844 int m_max_submenu_name_length; 845 int m_max_submenu_key_name_length; 846 int m_selected; 847 Menu *m_parent; 848 Menus m_submenus; 849 WindowSP m_menu_window_sp; 850 MenuActionResult m_canned_result; 851 MenuDelegateSP m_delegate_sp; 852 }; 853 854 // Menubar or separator constructor 855 Menu::Menu(Type type) 856 : m_name(), m_key_name(), m_identifier(0), m_type(type), m_key_value(0), 857 m_start_col(0), m_max_submenu_name_length(0), 858 m_max_submenu_key_name_length(0), m_selected(0), m_parent(nullptr), 859 m_submenus(), m_canned_result(MenuActionResult::NotHandled), 860 m_delegate_sp() {} 861 862 // Menuitem constructor 863 Menu::Menu(const char *name, const char *key_name, int key_value, 864 uint64_t identifier) 865 : m_name(), m_key_name(), m_identifier(identifier), m_type(Type::Invalid), 866 m_key_value(key_value), m_start_col(0), m_max_submenu_name_length(0), 867 m_max_submenu_key_name_length(0), m_selected(0), m_parent(nullptr), 868 m_submenus(), m_canned_result(MenuActionResult::NotHandled), 869 m_delegate_sp() { 870 if (name && name[0]) { 871 m_name = name; 872 m_type = Type::Item; 873 if (key_name && key_name[0]) 874 m_key_name = key_name; 875 } else { 876 m_type = Type::Separator; 877 } 878 } 879 880 void Menu::RecalculateNameLengths() { 881 m_max_submenu_name_length = 0; 882 m_max_submenu_key_name_length = 0; 883 Menus &submenus = GetSubmenus(); 884 const size_t num_submenus = submenus.size(); 885 for (size_t i = 0; i < num_submenus; ++i) { 886 Menu *submenu = submenus[i].get(); 887 if (static_cast<size_t>(m_max_submenu_name_length) < submenu->m_name.size()) 888 m_max_submenu_name_length = submenu->m_name.size(); 889 if (static_cast<size_t>(m_max_submenu_key_name_length) < 890 submenu->m_key_name.size()) 891 m_max_submenu_key_name_length = submenu->m_key_name.size(); 892 } 893 } 894 895 void Menu::AddSubmenu(const MenuSP &menu_sp) { 896 menu_sp->m_parent = this; 897 if (static_cast<size_t>(m_max_submenu_name_length) < menu_sp->m_name.size()) 898 m_max_submenu_name_length = menu_sp->m_name.size(); 899 if (static_cast<size_t>(m_max_submenu_key_name_length) < 900 menu_sp->m_key_name.size()) 901 m_max_submenu_key_name_length = menu_sp->m_key_name.size(); 902 m_submenus.push_back(menu_sp); 903 } 904 905 void Menu::DrawMenuTitle(Window &window, bool highlight) { 906 if (m_type == Type::Separator) { 907 window.MoveCursor(0, window.GetCursorY()); 908 window.PutChar(ACS_LTEE); 909 int width = window.GetWidth(); 910 if (width > 2) { 911 width -= 2; 912 for (int i = 0; i < width; ++i) 913 window.PutChar(ACS_HLINE); 914 } 915 window.PutChar(ACS_RTEE); 916 } else { 917 const int shortcut_key = m_key_value; 918 bool underlined_shortcut = false; 919 const attr_t hilgight_attr = A_REVERSE; 920 if (highlight) 921 window.AttributeOn(hilgight_attr); 922 if (llvm::isPrint(shortcut_key)) { 923 size_t lower_pos = m_name.find(tolower(shortcut_key)); 924 size_t upper_pos = m_name.find(toupper(shortcut_key)); 925 const char *name = m_name.c_str(); 926 size_t pos = std::min<size_t>(lower_pos, upper_pos); 927 if (pos != std::string::npos) { 928 underlined_shortcut = true; 929 if (pos > 0) { 930 window.PutCString(name, pos); 931 name += pos; 932 } 933 const attr_t shortcut_attr = A_UNDERLINE | A_BOLD; 934 window.AttributeOn(shortcut_attr); 935 window.PutChar(name[0]); 936 window.AttributeOff(shortcut_attr); 937 name++; 938 if (name[0]) 939 window.PutCString(name); 940 } 941 } 942 943 if (!underlined_shortcut) { 944 window.PutCString(m_name.c_str()); 945 } 946 947 if (highlight) 948 window.AttributeOff(hilgight_attr); 949 950 if (m_key_name.empty()) { 951 if (!underlined_shortcut && llvm::isPrint(m_key_value)) { 952 window.AttributeOn(COLOR_PAIR(3)); 953 window.Printf(" (%c)", m_key_value); 954 window.AttributeOff(COLOR_PAIR(3)); 955 } 956 } else { 957 window.AttributeOn(COLOR_PAIR(3)); 958 window.Printf(" (%s)", m_key_name.c_str()); 959 window.AttributeOff(COLOR_PAIR(3)); 960 } 961 } 962 } 963 964 bool Menu::WindowDelegateDraw(Window &window, bool force) { 965 Menus &submenus = GetSubmenus(); 966 const size_t num_submenus = submenus.size(); 967 const int selected_idx = GetSelectedSubmenuIndex(); 968 Menu::Type menu_type = GetType(); 969 switch (menu_type) { 970 case Menu::Type::Bar: { 971 window.SetBackground(2); 972 window.MoveCursor(0, 0); 973 for (size_t i = 0; i < num_submenus; ++i) { 974 Menu *menu = submenus[i].get(); 975 if (i > 0) 976 window.PutChar(' '); 977 menu->SetStartingColumn(window.GetCursorX()); 978 window.PutCString("| "); 979 menu->DrawMenuTitle(window, false); 980 } 981 window.PutCString(" |"); 982 } break; 983 984 case Menu::Type::Item: { 985 int y = 1; 986 int x = 3; 987 // Draw the menu 988 int cursor_x = 0; 989 int cursor_y = 0; 990 window.Erase(); 991 window.SetBackground(2); 992 window.Box(); 993 for (size_t i = 0; i < num_submenus; ++i) { 994 const bool is_selected = (i == static_cast<size_t>(selected_idx)); 995 window.MoveCursor(x, y + i); 996 if (is_selected) { 997 // Remember where we want the cursor to be 998 cursor_x = x - 1; 999 cursor_y = y + i; 1000 } 1001 submenus[i]->DrawMenuTitle(window, is_selected); 1002 } 1003 window.MoveCursor(cursor_x, cursor_y); 1004 } break; 1005 1006 default: 1007 case Menu::Type::Separator: 1008 break; 1009 } 1010 return true; // Drawing handled... 1011 } 1012 1013 HandleCharResult Menu::WindowDelegateHandleChar(Window &window, int key) { 1014 HandleCharResult result = eKeyNotHandled; 1015 1016 Menus &submenus = GetSubmenus(); 1017 const size_t num_submenus = submenus.size(); 1018 const int selected_idx = GetSelectedSubmenuIndex(); 1019 Menu::Type menu_type = GetType(); 1020 if (menu_type == Menu::Type::Bar) { 1021 MenuSP run_menu_sp; 1022 switch (key) { 1023 case KEY_DOWN: 1024 case KEY_UP: 1025 // Show last menu or first menu 1026 if (selected_idx < static_cast<int>(num_submenus)) 1027 run_menu_sp = submenus[selected_idx]; 1028 else if (!submenus.empty()) 1029 run_menu_sp = submenus.front(); 1030 result = eKeyHandled; 1031 break; 1032 1033 case KEY_RIGHT: 1034 ++m_selected; 1035 if (m_selected >= static_cast<int>(num_submenus)) 1036 m_selected = 0; 1037 if (m_selected < static_cast<int>(num_submenus)) 1038 run_menu_sp = submenus[m_selected]; 1039 else if (!submenus.empty()) 1040 run_menu_sp = submenus.front(); 1041 result = eKeyHandled; 1042 break; 1043 1044 case KEY_LEFT: 1045 --m_selected; 1046 if (m_selected < 0) 1047 m_selected = num_submenus - 1; 1048 if (m_selected < static_cast<int>(num_submenus)) 1049 run_menu_sp = submenus[m_selected]; 1050 else if (!submenus.empty()) 1051 run_menu_sp = submenus.front(); 1052 result = eKeyHandled; 1053 break; 1054 1055 default: 1056 for (size_t i = 0; i < num_submenus; ++i) { 1057 if (submenus[i]->GetKeyValue() == key) { 1058 SetSelectedSubmenuIndex(i); 1059 run_menu_sp = submenus[i]; 1060 result = eKeyHandled; 1061 break; 1062 } 1063 } 1064 break; 1065 } 1066 1067 if (run_menu_sp) { 1068 // Run the action on this menu in case we need to populate the menu with 1069 // dynamic content and also in case check marks, and any other menu 1070 // decorations need to be calculated 1071 if (run_menu_sp->Action() == MenuActionResult::Quit) 1072 return eQuitApplication; 1073 1074 Rect menu_bounds; 1075 menu_bounds.origin.x = run_menu_sp->GetStartingColumn(); 1076 menu_bounds.origin.y = 1; 1077 menu_bounds.size.width = run_menu_sp->GetDrawWidth(); 1078 menu_bounds.size.height = run_menu_sp->GetSubmenus().size() + 2; 1079 if (m_menu_window_sp) 1080 window.GetParent()->RemoveSubWindow(m_menu_window_sp.get()); 1081 1082 m_menu_window_sp = window.GetParent()->CreateSubWindow( 1083 run_menu_sp->GetName().c_str(), menu_bounds, true); 1084 m_menu_window_sp->SetDelegate(run_menu_sp); 1085 } 1086 } else if (menu_type == Menu::Type::Item) { 1087 switch (key) { 1088 case KEY_DOWN: 1089 if (m_submenus.size() > 1) { 1090 const int start_select = m_selected; 1091 while (++m_selected != start_select) { 1092 if (static_cast<size_t>(m_selected) >= num_submenus) 1093 m_selected = 0; 1094 if (m_submenus[m_selected]->GetType() == Type::Separator) 1095 continue; 1096 else 1097 break; 1098 } 1099 return eKeyHandled; 1100 } 1101 break; 1102 1103 case KEY_UP: 1104 if (m_submenus.size() > 1) { 1105 const int start_select = m_selected; 1106 while (--m_selected != start_select) { 1107 if (m_selected < static_cast<int>(0)) 1108 m_selected = num_submenus - 1; 1109 if (m_submenus[m_selected]->GetType() == Type::Separator) 1110 continue; 1111 else 1112 break; 1113 } 1114 return eKeyHandled; 1115 } 1116 break; 1117 1118 case KEY_RETURN: 1119 if (static_cast<size_t>(selected_idx) < num_submenus) { 1120 if (submenus[selected_idx]->Action() == MenuActionResult::Quit) 1121 return eQuitApplication; 1122 window.GetParent()->RemoveSubWindow(&window); 1123 return eKeyHandled; 1124 } 1125 break; 1126 1127 case KEY_ESCAPE: // Beware: pressing escape key has 1 to 2 second delay in 1128 // case other chars are entered for escaped sequences 1129 window.GetParent()->RemoveSubWindow(&window); 1130 return eKeyHandled; 1131 1132 default: 1133 for (size_t i = 0; i < num_submenus; ++i) { 1134 Menu *menu = submenus[i].get(); 1135 if (menu->GetKeyValue() == key) { 1136 SetSelectedSubmenuIndex(i); 1137 window.GetParent()->RemoveSubWindow(&window); 1138 if (menu->Action() == MenuActionResult::Quit) 1139 return eQuitApplication; 1140 return eKeyHandled; 1141 } 1142 } 1143 break; 1144 } 1145 } else if (menu_type == Menu::Type::Separator) { 1146 } 1147 return result; 1148 } 1149 1150 class Application { 1151 public: 1152 Application(FILE *in, FILE *out) 1153 : m_window_sp(), m_screen(nullptr), m_in(in), m_out(out) {} 1154 1155 ~Application() { 1156 m_window_delegates.clear(); 1157 m_window_sp.reset(); 1158 if (m_screen) { 1159 ::delscreen(m_screen); 1160 m_screen = nullptr; 1161 } 1162 } 1163 1164 void Initialize() { 1165 ::setlocale(LC_ALL, ""); 1166 ::setlocale(LC_CTYPE, ""); 1167 m_screen = ::newterm(nullptr, m_out, m_in); 1168 ::start_color(); 1169 ::curs_set(0); 1170 ::noecho(); 1171 ::keypad(stdscr, TRUE); 1172 } 1173 1174 void Terminate() { ::endwin(); } 1175 1176 void Run(Debugger &debugger) { 1177 bool done = false; 1178 int delay_in_tenths_of_a_second = 1; 1179 1180 // Alas the threading model in curses is a bit lame so we need to resort to 1181 // polling every 0.5 seconds. We could poll for stdin ourselves and then 1182 // pass the keys down but then we need to translate all of the escape 1183 // sequences ourselves. So we resort to polling for input because we need 1184 // to receive async process events while in this loop. 1185 1186 halfdelay(delay_in_tenths_of_a_second); // Poll using some number of tenths 1187 // of seconds seconds when calling 1188 // Window::GetChar() 1189 1190 ListenerSP listener_sp( 1191 Listener::MakeListener("lldb.IOHandler.curses.Application")); 1192 ConstString broadcaster_class_process(Process::GetStaticBroadcasterClass()); 1193 debugger.EnableForwardEvents(listener_sp); 1194 1195 bool update = true; 1196 #if defined(__APPLE__) 1197 std::deque<int> escape_chars; 1198 #endif 1199 1200 while (!done) { 1201 if (update) { 1202 m_window_sp->Draw(false); 1203 // All windows should be calling Window::DeferredRefresh() instead of 1204 // Window::Refresh() so we can do a single update and avoid any screen 1205 // blinking 1206 update_panels(); 1207 1208 // Cursor hiding isn't working on MacOSX, so hide it in the top left 1209 // corner 1210 m_window_sp->MoveCursor(0, 0); 1211 1212 doupdate(); 1213 update = false; 1214 } 1215 1216 #if defined(__APPLE__) 1217 // Terminal.app doesn't map its function keys correctly, F1-F4 default 1218 // to: \033OP, \033OQ, \033OR, \033OS, so lets take care of this here if 1219 // possible 1220 int ch; 1221 if (escape_chars.empty()) 1222 ch = m_window_sp->GetChar(); 1223 else { 1224 ch = escape_chars.front(); 1225 escape_chars.pop_front(); 1226 } 1227 if (ch == KEY_ESCAPE) { 1228 int ch2 = m_window_sp->GetChar(); 1229 if (ch2 == 'O') { 1230 int ch3 = m_window_sp->GetChar(); 1231 switch (ch3) { 1232 case 'P': 1233 ch = KEY_F(1); 1234 break; 1235 case 'Q': 1236 ch = KEY_F(2); 1237 break; 1238 case 'R': 1239 ch = KEY_F(3); 1240 break; 1241 case 'S': 1242 ch = KEY_F(4); 1243 break; 1244 default: 1245 escape_chars.push_back(ch2); 1246 if (ch3 != -1) 1247 escape_chars.push_back(ch3); 1248 break; 1249 } 1250 } else if (ch2 != -1) 1251 escape_chars.push_back(ch2); 1252 } 1253 #else 1254 int ch = m_window_sp->GetChar(); 1255 1256 #endif 1257 if (ch == -1) { 1258 if (feof(m_in) || ferror(m_in)) { 1259 done = true; 1260 } else { 1261 // Just a timeout from using halfdelay(), check for events 1262 EventSP event_sp; 1263 while (listener_sp->PeekAtNextEvent()) { 1264 listener_sp->GetEvent(event_sp, std::chrono::seconds(0)); 1265 1266 if (event_sp) { 1267 Broadcaster *broadcaster = event_sp->GetBroadcaster(); 1268 if (broadcaster) { 1269 // uint32_t event_type = event_sp->GetType(); 1270 ConstString broadcaster_class( 1271 broadcaster->GetBroadcasterClass()); 1272 if (broadcaster_class == broadcaster_class_process) { 1273 debugger.GetCommandInterpreter().UpdateExecutionContext( 1274 nullptr); 1275 update = true; 1276 continue; // Don't get any key, just update our view 1277 } 1278 } 1279 } 1280 } 1281 } 1282 } else { 1283 HandleCharResult key_result = m_window_sp->HandleChar(ch); 1284 switch (key_result) { 1285 case eKeyHandled: 1286 debugger.GetCommandInterpreter().UpdateExecutionContext(nullptr); 1287 update = true; 1288 break; 1289 case eKeyNotHandled: 1290 break; 1291 case eQuitApplication: 1292 done = true; 1293 break; 1294 } 1295 } 1296 } 1297 1298 debugger.CancelForwardEvents(listener_sp); 1299 } 1300 1301 WindowSP &GetMainWindow() { 1302 if (!m_window_sp) 1303 m_window_sp = std::make_shared<Window>("main", stdscr, false); 1304 return m_window_sp; 1305 } 1306 1307 protected: 1308 WindowSP m_window_sp; 1309 WindowDelegates m_window_delegates; 1310 SCREEN *m_screen; 1311 FILE *m_in; 1312 FILE *m_out; 1313 }; 1314 1315 } // namespace curses 1316 1317 using namespace curses; 1318 1319 struct Row { 1320 ValueObjectManager value; 1321 Row *parent; 1322 // The process stop ID when the children were calculated. 1323 uint32_t children_stop_id; 1324 int row_idx; 1325 int x; 1326 int y; 1327 bool might_have_children; 1328 bool expanded; 1329 bool calculated_children; 1330 std::vector<Row> children; 1331 1332 Row(const ValueObjectSP &v, Row *p) 1333 : value(v, lldb::eDynamicDontRunTarget, true), parent(p), row_idx(0), 1334 x(1), y(1), might_have_children(v ? v->MightHaveChildren() : false), 1335 expanded(false), calculated_children(false), children() {} 1336 1337 size_t GetDepth() const { 1338 if (parent) 1339 return 1 + parent->GetDepth(); 1340 return 0; 1341 } 1342 1343 void Expand() { expanded = true; } 1344 1345 std::vector<Row> &GetChildren() { 1346 ProcessSP process_sp = value.GetProcessSP(); 1347 auto stop_id = process_sp->GetStopID(); 1348 if (process_sp && stop_id != children_stop_id) { 1349 children_stop_id = stop_id; 1350 calculated_children = false; 1351 } 1352 if (!calculated_children) { 1353 children.clear(); 1354 calculated_children = true; 1355 ValueObjectSP valobj = value.GetSP(); 1356 if (valobj) { 1357 const size_t num_children = valobj->GetNumChildren(); 1358 for (size_t i = 0; i < num_children; ++i) { 1359 children.push_back(Row(valobj->GetChildAtIndex(i, true), this)); 1360 } 1361 } 1362 } 1363 return children; 1364 } 1365 1366 void Unexpand() { 1367 expanded = false; 1368 calculated_children = false; 1369 children.clear(); 1370 } 1371 1372 void DrawTree(Window &window) { 1373 if (parent) 1374 parent->DrawTreeForChild(window, this, 0); 1375 1376 if (might_have_children) { 1377 // It we can get UTF8 characters to work we should try to use the 1378 // "symbol" UTF8 string below 1379 // const char *symbol = ""; 1380 // if (row.expanded) 1381 // symbol = "\xe2\x96\xbd "; 1382 // else 1383 // symbol = "\xe2\x96\xb7 "; 1384 // window.PutCString (symbol); 1385 1386 // The ACS_DARROW and ACS_RARROW don't look very nice they are just a 'v' 1387 // or '>' character... 1388 // if (expanded) 1389 // window.PutChar (ACS_DARROW); 1390 // else 1391 // window.PutChar (ACS_RARROW); 1392 // Since we can't find any good looking right arrow/down arrow symbols, 1393 // just use a diamond... 1394 window.PutChar(ACS_DIAMOND); 1395 window.PutChar(ACS_HLINE); 1396 } 1397 } 1398 1399 void DrawTreeForChild(Window &window, Row *child, uint32_t reverse_depth) { 1400 if (parent) 1401 parent->DrawTreeForChild(window, this, reverse_depth + 1); 1402 1403 if (&GetChildren().back() == child) { 1404 // Last child 1405 if (reverse_depth == 0) { 1406 window.PutChar(ACS_LLCORNER); 1407 window.PutChar(ACS_HLINE); 1408 } else { 1409 window.PutChar(' '); 1410 window.PutChar(' '); 1411 } 1412 } else { 1413 if (reverse_depth == 0) { 1414 window.PutChar(ACS_LTEE); 1415 window.PutChar(ACS_HLINE); 1416 } else { 1417 window.PutChar(ACS_VLINE); 1418 window.PutChar(' '); 1419 } 1420 } 1421 } 1422 }; 1423 1424 struct DisplayOptions { 1425 bool show_types; 1426 }; 1427 1428 class TreeItem; 1429 1430 class TreeDelegate { 1431 public: 1432 TreeDelegate() = default; 1433 virtual ~TreeDelegate() = default; 1434 1435 virtual void TreeDelegateDrawTreeItem(TreeItem &item, Window &window) = 0; 1436 virtual void TreeDelegateGenerateChildren(TreeItem &item) = 0; 1437 virtual bool TreeDelegateItemSelected( 1438 TreeItem &item) = 0; // Return true if we need to update views 1439 }; 1440 1441 typedef std::shared_ptr<TreeDelegate> TreeDelegateSP; 1442 1443 class TreeItem { 1444 public: 1445 TreeItem(TreeItem *parent, TreeDelegate &delegate, bool might_have_children) 1446 : m_parent(parent), m_delegate(delegate), m_user_data(nullptr), 1447 m_identifier(0), m_row_idx(-1), m_children(), 1448 m_might_have_children(might_have_children), m_is_expanded(false) {} 1449 1450 TreeItem &operator=(const TreeItem &rhs) { 1451 if (this != &rhs) { 1452 m_parent = rhs.m_parent; 1453 m_delegate = rhs.m_delegate; 1454 m_user_data = rhs.m_user_data; 1455 m_identifier = rhs.m_identifier; 1456 m_row_idx = rhs.m_row_idx; 1457 m_children = rhs.m_children; 1458 m_might_have_children = rhs.m_might_have_children; 1459 m_is_expanded = rhs.m_is_expanded; 1460 } 1461 return *this; 1462 } 1463 1464 TreeItem(const TreeItem &) = default; 1465 1466 size_t GetDepth() const { 1467 if (m_parent) 1468 return 1 + m_parent->GetDepth(); 1469 return 0; 1470 } 1471 1472 int GetRowIndex() const { return m_row_idx; } 1473 1474 void ClearChildren() { m_children.clear(); } 1475 1476 void Resize(size_t n, const TreeItem &t) { m_children.resize(n, t); } 1477 1478 TreeItem &operator[](size_t i) { return m_children[i]; } 1479 1480 void SetRowIndex(int row_idx) { m_row_idx = row_idx; } 1481 1482 size_t GetNumChildren() { 1483 m_delegate.TreeDelegateGenerateChildren(*this); 1484 return m_children.size(); 1485 } 1486 1487 void ItemWasSelected() { m_delegate.TreeDelegateItemSelected(*this); } 1488 1489 void CalculateRowIndexes(int &row_idx) { 1490 SetRowIndex(row_idx); 1491 ++row_idx; 1492 1493 const bool expanded = IsExpanded(); 1494 1495 // The root item must calculate its children, or we must calculate the 1496 // number of children if the item is expanded 1497 if (m_parent == nullptr || expanded) 1498 GetNumChildren(); 1499 1500 for (auto &item : m_children) { 1501 if (expanded) 1502 item.CalculateRowIndexes(row_idx); 1503 else 1504 item.SetRowIndex(-1); 1505 } 1506 } 1507 1508 TreeItem *GetParent() { return m_parent; } 1509 1510 bool IsExpanded() const { return m_is_expanded; } 1511 1512 void Expand() { m_is_expanded = true; } 1513 1514 void Unexpand() { m_is_expanded = false; } 1515 1516 bool Draw(Window &window, const int first_visible_row, 1517 const uint32_t selected_row_idx, int &row_idx, int &num_rows_left) { 1518 if (num_rows_left <= 0) 1519 return false; 1520 1521 if (m_row_idx >= first_visible_row) { 1522 window.MoveCursor(2, row_idx + 1); 1523 1524 if (m_parent) 1525 m_parent->DrawTreeForChild(window, this, 0); 1526 1527 if (m_might_have_children) { 1528 // It we can get UTF8 characters to work we should try to use the 1529 // "symbol" UTF8 string below 1530 // const char *symbol = ""; 1531 // if (row.expanded) 1532 // symbol = "\xe2\x96\xbd "; 1533 // else 1534 // symbol = "\xe2\x96\xb7 "; 1535 // window.PutCString (symbol); 1536 1537 // The ACS_DARROW and ACS_RARROW don't look very nice they are just a 1538 // 'v' or '>' character... 1539 // if (expanded) 1540 // window.PutChar (ACS_DARROW); 1541 // else 1542 // window.PutChar (ACS_RARROW); 1543 // Since we can't find any good looking right arrow/down arrow symbols, 1544 // just use a diamond... 1545 window.PutChar(ACS_DIAMOND); 1546 window.PutChar(ACS_HLINE); 1547 } 1548 bool highlight = (selected_row_idx == static_cast<size_t>(m_row_idx)) && 1549 window.IsActive(); 1550 1551 if (highlight) 1552 window.AttributeOn(A_REVERSE); 1553 1554 m_delegate.TreeDelegateDrawTreeItem(*this, window); 1555 1556 if (highlight) 1557 window.AttributeOff(A_REVERSE); 1558 ++row_idx; 1559 --num_rows_left; 1560 } 1561 1562 if (num_rows_left <= 0) 1563 return false; // We are done drawing... 1564 1565 if (IsExpanded()) { 1566 for (auto &item : m_children) { 1567 // If we displayed all the rows and item.Draw() returns false we are 1568 // done drawing and can exit this for loop 1569 if (!item.Draw(window, first_visible_row, selected_row_idx, row_idx, 1570 num_rows_left)) 1571 break; 1572 } 1573 } 1574 return num_rows_left >= 0; // Return true if not done drawing yet 1575 } 1576 1577 void DrawTreeForChild(Window &window, TreeItem *child, 1578 uint32_t reverse_depth) { 1579 if (m_parent) 1580 m_parent->DrawTreeForChild(window, this, reverse_depth + 1); 1581 1582 if (&m_children.back() == child) { 1583 // Last child 1584 if (reverse_depth == 0) { 1585 window.PutChar(ACS_LLCORNER); 1586 window.PutChar(ACS_HLINE); 1587 } else { 1588 window.PutChar(' '); 1589 window.PutChar(' '); 1590 } 1591 } else { 1592 if (reverse_depth == 0) { 1593 window.PutChar(ACS_LTEE); 1594 window.PutChar(ACS_HLINE); 1595 } else { 1596 window.PutChar(ACS_VLINE); 1597 window.PutChar(' '); 1598 } 1599 } 1600 } 1601 1602 TreeItem *GetItemForRowIndex(uint32_t row_idx) { 1603 if (static_cast<uint32_t>(m_row_idx) == row_idx) 1604 return this; 1605 if (m_children.empty()) 1606 return nullptr; 1607 if (IsExpanded()) { 1608 for (auto &item : m_children) { 1609 TreeItem *selected_item_ptr = item.GetItemForRowIndex(row_idx); 1610 if (selected_item_ptr) 1611 return selected_item_ptr; 1612 } 1613 } 1614 return nullptr; 1615 } 1616 1617 void *GetUserData() const { return m_user_data; } 1618 1619 void SetUserData(void *user_data) { m_user_data = user_data; } 1620 1621 uint64_t GetIdentifier() const { return m_identifier; } 1622 1623 void SetIdentifier(uint64_t identifier) { m_identifier = identifier; } 1624 1625 void SetMightHaveChildren(bool b) { m_might_have_children = b; } 1626 1627 protected: 1628 TreeItem *m_parent; 1629 TreeDelegate &m_delegate; 1630 void *m_user_data; 1631 uint64_t m_identifier; 1632 int m_row_idx; // Zero based visible row index, -1 if not visible or for the 1633 // root item 1634 std::vector<TreeItem> m_children; 1635 bool m_might_have_children; 1636 bool m_is_expanded; 1637 }; 1638 1639 class TreeWindowDelegate : public WindowDelegate { 1640 public: 1641 TreeWindowDelegate(Debugger &debugger, const TreeDelegateSP &delegate_sp) 1642 : m_debugger(debugger), m_delegate_sp(delegate_sp), 1643 m_root(nullptr, *delegate_sp, true), m_selected_item(nullptr), 1644 m_num_rows(0), m_selected_row_idx(0), m_first_visible_row(0), 1645 m_min_x(0), m_min_y(0), m_max_x(0), m_max_y(0) {} 1646 1647 int NumVisibleRows() const { return m_max_y - m_min_y; } 1648 1649 bool WindowDelegateDraw(Window &window, bool force) override { 1650 ExecutionContext exe_ctx( 1651 m_debugger.GetCommandInterpreter().GetExecutionContext()); 1652 Process *process = exe_ctx.GetProcessPtr(); 1653 1654 bool display_content = false; 1655 if (process) { 1656 StateType state = process->GetState(); 1657 if (StateIsStoppedState(state, true)) { 1658 // We are stopped, so it is ok to 1659 display_content = true; 1660 } else if (StateIsRunningState(state)) { 1661 return true; // Don't do any updating when we are running 1662 } 1663 } 1664 1665 m_min_x = 2; 1666 m_min_y = 1; 1667 m_max_x = window.GetWidth() - 1; 1668 m_max_y = window.GetHeight() - 1; 1669 1670 window.Erase(); 1671 window.DrawTitleBox(window.GetName()); 1672 1673 if (display_content) { 1674 const int num_visible_rows = NumVisibleRows(); 1675 m_num_rows = 0; 1676 m_root.CalculateRowIndexes(m_num_rows); 1677 1678 // If we unexpanded while having something selected our total number of 1679 // rows is less than the num visible rows, then make sure we show all the 1680 // rows by setting the first visible row accordingly. 1681 if (m_first_visible_row > 0 && m_num_rows < num_visible_rows) 1682 m_first_visible_row = 0; 1683 1684 // Make sure the selected row is always visible 1685 if (m_selected_row_idx < m_first_visible_row) 1686 m_first_visible_row = m_selected_row_idx; 1687 else if (m_first_visible_row + num_visible_rows <= m_selected_row_idx) 1688 m_first_visible_row = m_selected_row_idx - num_visible_rows + 1; 1689 1690 int row_idx = 0; 1691 int num_rows_left = num_visible_rows; 1692 m_root.Draw(window, m_first_visible_row, m_selected_row_idx, row_idx, 1693 num_rows_left); 1694 // Get the selected row 1695 m_selected_item = m_root.GetItemForRowIndex(m_selected_row_idx); 1696 } else { 1697 m_selected_item = nullptr; 1698 } 1699 1700 return true; // Drawing handled 1701 } 1702 1703 const char *WindowDelegateGetHelpText() override { 1704 return "Thread window keyboard shortcuts:"; 1705 } 1706 1707 KeyHelp *WindowDelegateGetKeyHelp() override { 1708 static curses::KeyHelp g_source_view_key_help[] = { 1709 {KEY_UP, "Select previous item"}, 1710 {KEY_DOWN, "Select next item"}, 1711 {KEY_RIGHT, "Expand the selected item"}, 1712 {KEY_LEFT, 1713 "Unexpand the selected item or select parent if not expanded"}, 1714 {KEY_PPAGE, "Page up"}, 1715 {KEY_NPAGE, "Page down"}, 1716 {'h', "Show help dialog"}, 1717 {' ', "Toggle item expansion"}, 1718 {',', "Page up"}, 1719 {'.', "Page down"}, 1720 {'\0', nullptr}}; 1721 return g_source_view_key_help; 1722 } 1723 1724 HandleCharResult WindowDelegateHandleChar(Window &window, int c) override { 1725 switch (c) { 1726 case ',': 1727 case KEY_PPAGE: 1728 // Page up key 1729 if (m_first_visible_row > 0) { 1730 if (m_first_visible_row > m_max_y) 1731 m_first_visible_row -= m_max_y; 1732 else 1733 m_first_visible_row = 0; 1734 m_selected_row_idx = m_first_visible_row; 1735 m_selected_item = m_root.GetItemForRowIndex(m_selected_row_idx); 1736 if (m_selected_item) 1737 m_selected_item->ItemWasSelected(); 1738 } 1739 return eKeyHandled; 1740 1741 case '.': 1742 case KEY_NPAGE: 1743 // Page down key 1744 if (m_num_rows > m_max_y) { 1745 if (m_first_visible_row + m_max_y < m_num_rows) { 1746 m_first_visible_row += m_max_y; 1747 m_selected_row_idx = m_first_visible_row; 1748 m_selected_item = m_root.GetItemForRowIndex(m_selected_row_idx); 1749 if (m_selected_item) 1750 m_selected_item->ItemWasSelected(); 1751 } 1752 } 1753 return eKeyHandled; 1754 1755 case KEY_UP: 1756 if (m_selected_row_idx > 0) { 1757 --m_selected_row_idx; 1758 m_selected_item = m_root.GetItemForRowIndex(m_selected_row_idx); 1759 if (m_selected_item) 1760 m_selected_item->ItemWasSelected(); 1761 } 1762 return eKeyHandled; 1763 1764 case KEY_DOWN: 1765 if (m_selected_row_idx + 1 < m_num_rows) { 1766 ++m_selected_row_idx; 1767 m_selected_item = m_root.GetItemForRowIndex(m_selected_row_idx); 1768 if (m_selected_item) 1769 m_selected_item->ItemWasSelected(); 1770 } 1771 return eKeyHandled; 1772 1773 case KEY_RIGHT: 1774 if (m_selected_item) { 1775 if (!m_selected_item->IsExpanded()) 1776 m_selected_item->Expand(); 1777 } 1778 return eKeyHandled; 1779 1780 case KEY_LEFT: 1781 if (m_selected_item) { 1782 if (m_selected_item->IsExpanded()) 1783 m_selected_item->Unexpand(); 1784 else if (m_selected_item->GetParent()) { 1785 m_selected_row_idx = m_selected_item->GetParent()->GetRowIndex(); 1786 m_selected_item = m_root.GetItemForRowIndex(m_selected_row_idx); 1787 if (m_selected_item) 1788 m_selected_item->ItemWasSelected(); 1789 } 1790 } 1791 return eKeyHandled; 1792 1793 case ' ': 1794 // Toggle expansion state when SPACE is pressed 1795 if (m_selected_item) { 1796 if (m_selected_item->IsExpanded()) 1797 m_selected_item->Unexpand(); 1798 else 1799 m_selected_item->Expand(); 1800 } 1801 return eKeyHandled; 1802 1803 case 'h': 1804 window.CreateHelpSubwindow(); 1805 return eKeyHandled; 1806 1807 default: 1808 break; 1809 } 1810 return eKeyNotHandled; 1811 } 1812 1813 protected: 1814 Debugger &m_debugger; 1815 TreeDelegateSP m_delegate_sp; 1816 TreeItem m_root; 1817 TreeItem *m_selected_item; 1818 int m_num_rows; 1819 int m_selected_row_idx; 1820 int m_first_visible_row; 1821 int m_min_x; 1822 int m_min_y; 1823 int m_max_x; 1824 int m_max_y; 1825 }; 1826 1827 class FrameTreeDelegate : public TreeDelegate { 1828 public: 1829 FrameTreeDelegate() : TreeDelegate() { 1830 FormatEntity::Parse( 1831 "frame #${frame.index}: {${function.name}${function.pc-offset}}}", 1832 m_format); 1833 } 1834 1835 ~FrameTreeDelegate() override = default; 1836 1837 void TreeDelegateDrawTreeItem(TreeItem &item, Window &window) override { 1838 Thread *thread = (Thread *)item.GetUserData(); 1839 if (thread) { 1840 const uint64_t frame_idx = item.GetIdentifier(); 1841 StackFrameSP frame_sp = thread->GetStackFrameAtIndex(frame_idx); 1842 if (frame_sp) { 1843 StreamString strm; 1844 const SymbolContext &sc = 1845 frame_sp->GetSymbolContext(eSymbolContextEverything); 1846 ExecutionContext exe_ctx(frame_sp); 1847 if (FormatEntity::Format(m_format, strm, &sc, &exe_ctx, nullptr, 1848 nullptr, false, false)) { 1849 int right_pad = 1; 1850 window.PutCStringTruncated(strm.GetString().str().c_str(), right_pad); 1851 } 1852 } 1853 } 1854 } 1855 1856 void TreeDelegateGenerateChildren(TreeItem &item) override { 1857 // No children for frames yet... 1858 } 1859 1860 bool TreeDelegateItemSelected(TreeItem &item) override { 1861 Thread *thread = (Thread *)item.GetUserData(); 1862 if (thread) { 1863 thread->GetProcess()->GetThreadList().SetSelectedThreadByID( 1864 thread->GetID()); 1865 const uint64_t frame_idx = item.GetIdentifier(); 1866 thread->SetSelectedFrameByIndex(frame_idx); 1867 return true; 1868 } 1869 return false; 1870 } 1871 1872 protected: 1873 FormatEntity::Entry m_format; 1874 }; 1875 1876 class ThreadTreeDelegate : public TreeDelegate { 1877 public: 1878 ThreadTreeDelegate(Debugger &debugger) 1879 : TreeDelegate(), m_debugger(debugger), m_tid(LLDB_INVALID_THREAD_ID), 1880 m_stop_id(UINT32_MAX) { 1881 FormatEntity::Parse("thread #${thread.index}: tid = ${thread.id}{, stop " 1882 "reason = ${thread.stop-reason}}", 1883 m_format); 1884 } 1885 1886 ~ThreadTreeDelegate() override = default; 1887 1888 ProcessSP GetProcess() { 1889 return m_debugger.GetCommandInterpreter() 1890 .GetExecutionContext() 1891 .GetProcessSP(); 1892 } 1893 1894 ThreadSP GetThread(const TreeItem &item) { 1895 ProcessSP process_sp = GetProcess(); 1896 if (process_sp) 1897 return process_sp->GetThreadList().FindThreadByID(item.GetIdentifier()); 1898 return ThreadSP(); 1899 } 1900 1901 void TreeDelegateDrawTreeItem(TreeItem &item, Window &window) override { 1902 ThreadSP thread_sp = GetThread(item); 1903 if (thread_sp) { 1904 StreamString strm; 1905 ExecutionContext exe_ctx(thread_sp); 1906 if (FormatEntity::Format(m_format, strm, nullptr, &exe_ctx, nullptr, 1907 nullptr, false, false)) { 1908 int right_pad = 1; 1909 window.PutCStringTruncated(strm.GetString().str().c_str(), right_pad); 1910 } 1911 } 1912 } 1913 1914 void TreeDelegateGenerateChildren(TreeItem &item) override { 1915 ProcessSP process_sp = GetProcess(); 1916 if (process_sp && process_sp->IsAlive()) { 1917 StateType state = process_sp->GetState(); 1918 if (StateIsStoppedState(state, true)) { 1919 ThreadSP thread_sp = GetThread(item); 1920 if (thread_sp) { 1921 if (m_stop_id == process_sp->GetStopID() && 1922 thread_sp->GetID() == m_tid) 1923 return; // Children are already up to date 1924 if (!m_frame_delegate_sp) { 1925 // Always expand the thread item the first time we show it 1926 m_frame_delegate_sp = std::make_shared<FrameTreeDelegate>(); 1927 } 1928 1929 m_stop_id = process_sp->GetStopID(); 1930 m_tid = thread_sp->GetID(); 1931 1932 TreeItem t(&item, *m_frame_delegate_sp, false); 1933 size_t num_frames = thread_sp->GetStackFrameCount(); 1934 item.Resize(num_frames, t); 1935 for (size_t i = 0; i < num_frames; ++i) { 1936 item[i].SetUserData(thread_sp.get()); 1937 item[i].SetIdentifier(i); 1938 } 1939 } 1940 return; 1941 } 1942 } 1943 item.ClearChildren(); 1944 } 1945 1946 bool TreeDelegateItemSelected(TreeItem &item) override { 1947 ProcessSP process_sp = GetProcess(); 1948 if (process_sp && process_sp->IsAlive()) { 1949 StateType state = process_sp->GetState(); 1950 if (StateIsStoppedState(state, true)) { 1951 ThreadSP thread_sp = GetThread(item); 1952 if (thread_sp) { 1953 ThreadList &thread_list = thread_sp->GetProcess()->GetThreadList(); 1954 std::lock_guard<std::recursive_mutex> guard(thread_list.GetMutex()); 1955 ThreadSP selected_thread_sp = thread_list.GetSelectedThread(); 1956 if (selected_thread_sp->GetID() != thread_sp->GetID()) { 1957 thread_list.SetSelectedThreadByID(thread_sp->GetID()); 1958 return true; 1959 } 1960 } 1961 } 1962 } 1963 return false; 1964 } 1965 1966 protected: 1967 Debugger &m_debugger; 1968 std::shared_ptr<FrameTreeDelegate> m_frame_delegate_sp; 1969 lldb::user_id_t m_tid; 1970 uint32_t m_stop_id; 1971 FormatEntity::Entry m_format; 1972 }; 1973 1974 class ThreadsTreeDelegate : public TreeDelegate { 1975 public: 1976 ThreadsTreeDelegate(Debugger &debugger) 1977 : TreeDelegate(), m_thread_delegate_sp(), m_debugger(debugger), 1978 m_stop_id(UINT32_MAX) { 1979 FormatEntity::Parse("process ${process.id}{, name = ${process.name}}", 1980 m_format); 1981 } 1982 1983 ~ThreadsTreeDelegate() override = default; 1984 1985 ProcessSP GetProcess() { 1986 return m_debugger.GetCommandInterpreter() 1987 .GetExecutionContext() 1988 .GetProcessSP(); 1989 } 1990 1991 void TreeDelegateDrawTreeItem(TreeItem &item, Window &window) override { 1992 ProcessSP process_sp = GetProcess(); 1993 if (process_sp && process_sp->IsAlive()) { 1994 StreamString strm; 1995 ExecutionContext exe_ctx(process_sp); 1996 if (FormatEntity::Format(m_format, strm, nullptr, &exe_ctx, nullptr, 1997 nullptr, false, false)) { 1998 int right_pad = 1; 1999 window.PutCStringTruncated(strm.GetString().str().c_str(), right_pad); 2000 } 2001 } 2002 } 2003 2004 void TreeDelegateGenerateChildren(TreeItem &item) override { 2005 ProcessSP process_sp = GetProcess(); 2006 if (process_sp && process_sp->IsAlive()) { 2007 StateType state = process_sp->GetState(); 2008 if (StateIsStoppedState(state, true)) { 2009 const uint32_t stop_id = process_sp->GetStopID(); 2010 if (m_stop_id == stop_id) 2011 return; // Children are already up to date 2012 2013 m_stop_id = stop_id; 2014 2015 if (!m_thread_delegate_sp) { 2016 // Always expand the thread item the first time we show it 2017 // item.Expand(); 2018 m_thread_delegate_sp = 2019 std::make_shared<ThreadTreeDelegate>(m_debugger); 2020 } 2021 2022 TreeItem t(&item, *m_thread_delegate_sp, false); 2023 ThreadList &threads = process_sp->GetThreadList(); 2024 std::lock_guard<std::recursive_mutex> guard(threads.GetMutex()); 2025 size_t num_threads = threads.GetSize(); 2026 item.Resize(num_threads, t); 2027 for (size_t i = 0; i < num_threads; ++i) { 2028 item[i].SetIdentifier(threads.GetThreadAtIndex(i)->GetID()); 2029 item[i].SetMightHaveChildren(true); 2030 } 2031 return; 2032 } 2033 } 2034 item.ClearChildren(); 2035 } 2036 2037 bool TreeDelegateItemSelected(TreeItem &item) override { return false; } 2038 2039 protected: 2040 std::shared_ptr<ThreadTreeDelegate> m_thread_delegate_sp; 2041 Debugger &m_debugger; 2042 uint32_t m_stop_id; 2043 FormatEntity::Entry m_format; 2044 }; 2045 2046 class ValueObjectListDelegate : public WindowDelegate { 2047 public: 2048 ValueObjectListDelegate() 2049 : m_rows(), m_selected_row(nullptr), m_selected_row_idx(0), 2050 m_first_visible_row(0), m_num_rows(0), m_max_x(0), m_max_y(0) {} 2051 2052 ValueObjectListDelegate(ValueObjectList &valobj_list) 2053 : m_rows(), m_selected_row(nullptr), m_selected_row_idx(0), 2054 m_first_visible_row(0), m_num_rows(0), m_max_x(0), m_max_y(0) { 2055 SetValues(valobj_list); 2056 } 2057 2058 ~ValueObjectListDelegate() override = default; 2059 2060 void SetValues(ValueObjectList &valobj_list) { 2061 m_selected_row = nullptr; 2062 m_selected_row_idx = 0; 2063 m_first_visible_row = 0; 2064 m_num_rows = 0; 2065 m_rows.clear(); 2066 for (auto &valobj_sp : valobj_list.GetObjects()) 2067 m_rows.push_back(Row(valobj_sp, nullptr)); 2068 } 2069 2070 bool WindowDelegateDraw(Window &window, bool force) override { 2071 m_num_rows = 0; 2072 m_min_x = 2; 2073 m_min_y = 1; 2074 m_max_x = window.GetWidth() - 1; 2075 m_max_y = window.GetHeight() - 1; 2076 2077 window.Erase(); 2078 window.DrawTitleBox(window.GetName()); 2079 2080 const int num_visible_rows = NumVisibleRows(); 2081 const int num_rows = CalculateTotalNumberRows(m_rows); 2082 2083 // If we unexpanded while having something selected our total number of 2084 // rows is less than the num visible rows, then make sure we show all the 2085 // rows by setting the first visible row accordingly. 2086 if (m_first_visible_row > 0 && num_rows < num_visible_rows) 2087 m_first_visible_row = 0; 2088 2089 // Make sure the selected row is always visible 2090 if (m_selected_row_idx < m_first_visible_row) 2091 m_first_visible_row = m_selected_row_idx; 2092 else if (m_first_visible_row + num_visible_rows <= m_selected_row_idx) 2093 m_first_visible_row = m_selected_row_idx - num_visible_rows + 1; 2094 2095 DisplayRows(window, m_rows, g_options); 2096 2097 // Get the selected row 2098 m_selected_row = GetRowForRowIndex(m_selected_row_idx); 2099 // Keep the cursor on the selected row so the highlight and the cursor are 2100 // always on the same line 2101 if (m_selected_row) 2102 window.MoveCursor(m_selected_row->x, m_selected_row->y); 2103 2104 return true; // Drawing handled 2105 } 2106 2107 KeyHelp *WindowDelegateGetKeyHelp() override { 2108 static curses::KeyHelp g_source_view_key_help[] = { 2109 {KEY_UP, "Select previous item"}, 2110 {KEY_DOWN, "Select next item"}, 2111 {KEY_RIGHT, "Expand selected item"}, 2112 {KEY_LEFT, "Unexpand selected item or select parent if not expanded"}, 2113 {KEY_PPAGE, "Page up"}, 2114 {KEY_NPAGE, "Page down"}, 2115 {'A', "Format as annotated address"}, 2116 {'b', "Format as binary"}, 2117 {'B', "Format as hex bytes with ASCII"}, 2118 {'c', "Format as character"}, 2119 {'d', "Format as a signed integer"}, 2120 {'D', "Format selected value using the default format for the type"}, 2121 {'f', "Format as float"}, 2122 {'h', "Show help dialog"}, 2123 {'i', "Format as instructions"}, 2124 {'o', "Format as octal"}, 2125 {'p', "Format as pointer"}, 2126 {'s', "Format as C string"}, 2127 {'t', "Toggle showing/hiding type names"}, 2128 {'u', "Format as an unsigned integer"}, 2129 {'x', "Format as hex"}, 2130 {'X', "Format as uppercase hex"}, 2131 {' ', "Toggle item expansion"}, 2132 {',', "Page up"}, 2133 {'.', "Page down"}, 2134 {'\0', nullptr}}; 2135 return g_source_view_key_help; 2136 } 2137 2138 HandleCharResult WindowDelegateHandleChar(Window &window, int c) override { 2139 switch (c) { 2140 case 'x': 2141 case 'X': 2142 case 'o': 2143 case 's': 2144 case 'u': 2145 case 'd': 2146 case 'D': 2147 case 'i': 2148 case 'A': 2149 case 'p': 2150 case 'c': 2151 case 'b': 2152 case 'B': 2153 case 'f': 2154 // Change the format for the currently selected item 2155 if (m_selected_row) { 2156 auto valobj_sp = m_selected_row->value.GetSP(); 2157 if (valobj_sp) 2158 valobj_sp->SetFormat(FormatForChar(c)); 2159 } 2160 return eKeyHandled; 2161 2162 case 't': 2163 // Toggle showing type names 2164 g_options.show_types = !g_options.show_types; 2165 return eKeyHandled; 2166 2167 case ',': 2168 case KEY_PPAGE: 2169 // Page up key 2170 if (m_first_visible_row > 0) { 2171 if (static_cast<int>(m_first_visible_row) > m_max_y) 2172 m_first_visible_row -= m_max_y; 2173 else 2174 m_first_visible_row = 0; 2175 m_selected_row_idx = m_first_visible_row; 2176 } 2177 return eKeyHandled; 2178 2179 case '.': 2180 case KEY_NPAGE: 2181 // Page down key 2182 if (m_num_rows > static_cast<size_t>(m_max_y)) { 2183 if (m_first_visible_row + m_max_y < m_num_rows) { 2184 m_first_visible_row += m_max_y; 2185 m_selected_row_idx = m_first_visible_row; 2186 } 2187 } 2188 return eKeyHandled; 2189 2190 case KEY_UP: 2191 if (m_selected_row_idx > 0) 2192 --m_selected_row_idx; 2193 return eKeyHandled; 2194 2195 case KEY_DOWN: 2196 if (m_selected_row_idx + 1 < m_num_rows) 2197 ++m_selected_row_idx; 2198 return eKeyHandled; 2199 2200 case KEY_RIGHT: 2201 if (m_selected_row) { 2202 if (!m_selected_row->expanded) 2203 m_selected_row->Expand(); 2204 } 2205 return eKeyHandled; 2206 2207 case KEY_LEFT: 2208 if (m_selected_row) { 2209 if (m_selected_row->expanded) 2210 m_selected_row->Unexpand(); 2211 else if (m_selected_row->parent) 2212 m_selected_row_idx = m_selected_row->parent->row_idx; 2213 } 2214 return eKeyHandled; 2215 2216 case ' ': 2217 // Toggle expansion state when SPACE is pressed 2218 if (m_selected_row) { 2219 if (m_selected_row->expanded) 2220 m_selected_row->Unexpand(); 2221 else 2222 m_selected_row->Expand(); 2223 } 2224 return eKeyHandled; 2225 2226 case 'h': 2227 window.CreateHelpSubwindow(); 2228 return eKeyHandled; 2229 2230 default: 2231 break; 2232 } 2233 return eKeyNotHandled; 2234 } 2235 2236 protected: 2237 std::vector<Row> m_rows; 2238 Row *m_selected_row; 2239 uint32_t m_selected_row_idx; 2240 uint32_t m_first_visible_row; 2241 uint32_t m_num_rows; 2242 int m_min_x; 2243 int m_min_y; 2244 int m_max_x; 2245 int m_max_y; 2246 2247 static Format FormatForChar(int c) { 2248 switch (c) { 2249 case 'x': 2250 return eFormatHex; 2251 case 'X': 2252 return eFormatHexUppercase; 2253 case 'o': 2254 return eFormatOctal; 2255 case 's': 2256 return eFormatCString; 2257 case 'u': 2258 return eFormatUnsigned; 2259 case 'd': 2260 return eFormatDecimal; 2261 case 'D': 2262 return eFormatDefault; 2263 case 'i': 2264 return eFormatInstruction; 2265 case 'A': 2266 return eFormatAddressInfo; 2267 case 'p': 2268 return eFormatPointer; 2269 case 'c': 2270 return eFormatChar; 2271 case 'b': 2272 return eFormatBinary; 2273 case 'B': 2274 return eFormatBytesWithASCII; 2275 case 'f': 2276 return eFormatFloat; 2277 } 2278 return eFormatDefault; 2279 } 2280 2281 bool DisplayRowObject(Window &window, Row &row, DisplayOptions &options, 2282 bool highlight, bool last_child) { 2283 ValueObject *valobj = row.value.GetSP().get(); 2284 2285 if (valobj == nullptr) 2286 return false; 2287 2288 const char *type_name = 2289 options.show_types ? valobj->GetTypeName().GetCString() : nullptr; 2290 const char *name = valobj->GetName().GetCString(); 2291 const char *value = valobj->GetValueAsCString(); 2292 const char *summary = valobj->GetSummaryAsCString(); 2293 2294 window.MoveCursor(row.x, row.y); 2295 2296 row.DrawTree(window); 2297 2298 if (highlight) 2299 window.AttributeOn(A_REVERSE); 2300 2301 if (type_name && type_name[0]) 2302 window.Printf("(%s) ", type_name); 2303 2304 if (name && name[0]) 2305 window.PutCString(name); 2306 2307 attr_t changd_attr = 0; 2308 if (valobj->GetValueDidChange()) 2309 changd_attr = COLOR_PAIR(5) | A_BOLD; 2310 2311 if (value && value[0]) { 2312 window.PutCString(" = "); 2313 if (changd_attr) 2314 window.AttributeOn(changd_attr); 2315 window.PutCString(value); 2316 if (changd_attr) 2317 window.AttributeOff(changd_attr); 2318 } 2319 2320 if (summary && summary[0]) { 2321 window.PutChar(' '); 2322 if (changd_attr) 2323 window.AttributeOn(changd_attr); 2324 window.PutCString(summary); 2325 if (changd_attr) 2326 window.AttributeOff(changd_attr); 2327 } 2328 2329 if (highlight) 2330 window.AttributeOff(A_REVERSE); 2331 2332 return true; 2333 } 2334 2335 void DisplayRows(Window &window, std::vector<Row> &rows, 2336 DisplayOptions &options) { 2337 // > 0x25B7 2338 // \/ 0x25BD 2339 2340 bool window_is_active = window.IsActive(); 2341 for (auto &row : rows) { 2342 const bool last_child = row.parent && &rows[rows.size() - 1] == &row; 2343 // Save the row index in each Row structure 2344 row.row_idx = m_num_rows; 2345 if ((m_num_rows >= m_first_visible_row) && 2346 ((m_num_rows - m_first_visible_row) < 2347 static_cast<size_t>(NumVisibleRows()))) { 2348 row.x = m_min_x; 2349 row.y = m_num_rows - m_first_visible_row + 1; 2350 if (DisplayRowObject(window, row, options, 2351 window_is_active && 2352 m_num_rows == m_selected_row_idx, 2353 last_child)) { 2354 ++m_num_rows; 2355 } else { 2356 row.x = 0; 2357 row.y = 0; 2358 } 2359 } else { 2360 row.x = 0; 2361 row.y = 0; 2362 ++m_num_rows; 2363 } 2364 2365 auto &children = row.GetChildren(); 2366 if (row.expanded && !children.empty()) { 2367 DisplayRows(window, children, options); 2368 } 2369 } 2370 } 2371 2372 int CalculateTotalNumberRows(std::vector<Row> &rows) { 2373 int row_count = 0; 2374 for (auto &row : rows) { 2375 ++row_count; 2376 if (row.expanded) 2377 row_count += CalculateTotalNumberRows(row.GetChildren()); 2378 } 2379 return row_count; 2380 } 2381 2382 static Row *GetRowForRowIndexImpl(std::vector<Row> &rows, size_t &row_index) { 2383 for (auto &row : rows) { 2384 if (row_index == 0) 2385 return &row; 2386 else { 2387 --row_index; 2388 auto &children = row.GetChildren(); 2389 if (row.expanded && !children.empty()) { 2390 Row *result = GetRowForRowIndexImpl(children, row_index); 2391 if (result) 2392 return result; 2393 } 2394 } 2395 } 2396 return nullptr; 2397 } 2398 2399 Row *GetRowForRowIndex(size_t row_index) { 2400 return GetRowForRowIndexImpl(m_rows, row_index); 2401 } 2402 2403 int NumVisibleRows() const { return m_max_y - m_min_y; } 2404 2405 static DisplayOptions g_options; 2406 }; 2407 2408 class FrameVariablesWindowDelegate : public ValueObjectListDelegate { 2409 public: 2410 FrameVariablesWindowDelegate(Debugger &debugger) 2411 : ValueObjectListDelegate(), m_debugger(debugger), 2412 m_frame_block(nullptr) {} 2413 2414 ~FrameVariablesWindowDelegate() override = default; 2415 2416 const char *WindowDelegateGetHelpText() override { 2417 return "Frame variable window keyboard shortcuts:"; 2418 } 2419 2420 bool WindowDelegateDraw(Window &window, bool force) override { 2421 ExecutionContext exe_ctx( 2422 m_debugger.GetCommandInterpreter().GetExecutionContext()); 2423 Process *process = exe_ctx.GetProcessPtr(); 2424 Block *frame_block = nullptr; 2425 StackFrame *frame = nullptr; 2426 2427 if (process) { 2428 StateType state = process->GetState(); 2429 if (StateIsStoppedState(state, true)) { 2430 frame = exe_ctx.GetFramePtr(); 2431 if (frame) 2432 frame_block = frame->GetFrameBlock(); 2433 } else if (StateIsRunningState(state)) { 2434 return true; // Don't do any updating when we are running 2435 } 2436 } 2437 2438 ValueObjectList local_values; 2439 if (frame_block) { 2440 // Only update the variables if they have changed 2441 if (m_frame_block != frame_block) { 2442 m_frame_block = frame_block; 2443 2444 VariableList *locals = frame->GetVariableList(true); 2445 if (locals) { 2446 const DynamicValueType use_dynamic = eDynamicDontRunTarget; 2447 for (const VariableSP &local_sp : *locals) { 2448 ValueObjectSP value_sp = 2449 frame->GetValueObjectForFrameVariable(local_sp, use_dynamic); 2450 if (value_sp) { 2451 ValueObjectSP synthetic_value_sp = value_sp->GetSyntheticValue(); 2452 if (synthetic_value_sp) 2453 local_values.Append(synthetic_value_sp); 2454 else 2455 local_values.Append(value_sp); 2456 } 2457 } 2458 // Update the values 2459 SetValues(local_values); 2460 } 2461 } 2462 } else { 2463 m_frame_block = nullptr; 2464 // Update the values with an empty list if there is no frame 2465 SetValues(local_values); 2466 } 2467 2468 return ValueObjectListDelegate::WindowDelegateDraw(window, force); 2469 } 2470 2471 protected: 2472 Debugger &m_debugger; 2473 Block *m_frame_block; 2474 }; 2475 2476 class RegistersWindowDelegate : public ValueObjectListDelegate { 2477 public: 2478 RegistersWindowDelegate(Debugger &debugger) 2479 : ValueObjectListDelegate(), m_debugger(debugger) {} 2480 2481 ~RegistersWindowDelegate() override = default; 2482 2483 const char *WindowDelegateGetHelpText() override { 2484 return "Register window keyboard shortcuts:"; 2485 } 2486 2487 bool WindowDelegateDraw(Window &window, bool force) override { 2488 ExecutionContext exe_ctx( 2489 m_debugger.GetCommandInterpreter().GetExecutionContext()); 2490 StackFrame *frame = exe_ctx.GetFramePtr(); 2491 2492 ValueObjectList value_list; 2493 if (frame) { 2494 if (frame->GetStackID() != m_stack_id) { 2495 m_stack_id = frame->GetStackID(); 2496 RegisterContextSP reg_ctx(frame->GetRegisterContext()); 2497 if (reg_ctx) { 2498 const uint32_t num_sets = reg_ctx->GetRegisterSetCount(); 2499 for (uint32_t set_idx = 0; set_idx < num_sets; ++set_idx) { 2500 value_list.Append( 2501 ValueObjectRegisterSet::Create(frame, reg_ctx, set_idx)); 2502 } 2503 } 2504 SetValues(value_list); 2505 } 2506 } else { 2507 Process *process = exe_ctx.GetProcessPtr(); 2508 if (process && process->IsAlive()) 2509 return true; // Don't do any updating if we are running 2510 else { 2511 // Update the values with an empty list if there is no process or the 2512 // process isn't alive anymore 2513 SetValues(value_list); 2514 } 2515 } 2516 return ValueObjectListDelegate::WindowDelegateDraw(window, force); 2517 } 2518 2519 protected: 2520 Debugger &m_debugger; 2521 StackID m_stack_id; 2522 }; 2523 2524 static const char *CursesKeyToCString(int ch) { 2525 static char g_desc[32]; 2526 if (ch >= KEY_F0 && ch < KEY_F0 + 64) { 2527 snprintf(g_desc, sizeof(g_desc), "F%u", ch - KEY_F0); 2528 return g_desc; 2529 } 2530 switch (ch) { 2531 case KEY_DOWN: 2532 return "down"; 2533 case KEY_UP: 2534 return "up"; 2535 case KEY_LEFT: 2536 return "left"; 2537 case KEY_RIGHT: 2538 return "right"; 2539 case KEY_HOME: 2540 return "home"; 2541 case KEY_BACKSPACE: 2542 return "backspace"; 2543 case KEY_DL: 2544 return "delete-line"; 2545 case KEY_IL: 2546 return "insert-line"; 2547 case KEY_DC: 2548 return "delete-char"; 2549 case KEY_IC: 2550 return "insert-char"; 2551 case KEY_CLEAR: 2552 return "clear"; 2553 case KEY_EOS: 2554 return "clear-to-eos"; 2555 case KEY_EOL: 2556 return "clear-to-eol"; 2557 case KEY_SF: 2558 return "scroll-forward"; 2559 case KEY_SR: 2560 return "scroll-backward"; 2561 case KEY_NPAGE: 2562 return "page-down"; 2563 case KEY_PPAGE: 2564 return "page-up"; 2565 case KEY_STAB: 2566 return "set-tab"; 2567 case KEY_CTAB: 2568 return "clear-tab"; 2569 case KEY_CATAB: 2570 return "clear-all-tabs"; 2571 case KEY_ENTER: 2572 return "enter"; 2573 case KEY_PRINT: 2574 return "print"; 2575 case KEY_LL: 2576 return "lower-left key"; 2577 case KEY_A1: 2578 return "upper left of keypad"; 2579 case KEY_A3: 2580 return "upper right of keypad"; 2581 case KEY_B2: 2582 return "center of keypad"; 2583 case KEY_C1: 2584 return "lower left of keypad"; 2585 case KEY_C3: 2586 return "lower right of keypad"; 2587 case KEY_BTAB: 2588 return "back-tab key"; 2589 case KEY_BEG: 2590 return "begin key"; 2591 case KEY_CANCEL: 2592 return "cancel key"; 2593 case KEY_CLOSE: 2594 return "close key"; 2595 case KEY_COMMAND: 2596 return "command key"; 2597 case KEY_COPY: 2598 return "copy key"; 2599 case KEY_CREATE: 2600 return "create key"; 2601 case KEY_END: 2602 return "end key"; 2603 case KEY_EXIT: 2604 return "exit key"; 2605 case KEY_FIND: 2606 return "find key"; 2607 case KEY_HELP: 2608 return "help key"; 2609 case KEY_MARK: 2610 return "mark key"; 2611 case KEY_MESSAGE: 2612 return "message key"; 2613 case KEY_MOVE: 2614 return "move key"; 2615 case KEY_NEXT: 2616 return "next key"; 2617 case KEY_OPEN: 2618 return "open key"; 2619 case KEY_OPTIONS: 2620 return "options key"; 2621 case KEY_PREVIOUS: 2622 return "previous key"; 2623 case KEY_REDO: 2624 return "redo key"; 2625 case KEY_REFERENCE: 2626 return "reference key"; 2627 case KEY_REFRESH: 2628 return "refresh key"; 2629 case KEY_REPLACE: 2630 return "replace key"; 2631 case KEY_RESTART: 2632 return "restart key"; 2633 case KEY_RESUME: 2634 return "resume key"; 2635 case KEY_SAVE: 2636 return "save key"; 2637 case KEY_SBEG: 2638 return "shifted begin key"; 2639 case KEY_SCANCEL: 2640 return "shifted cancel key"; 2641 case KEY_SCOMMAND: 2642 return "shifted command key"; 2643 case KEY_SCOPY: 2644 return "shifted copy key"; 2645 case KEY_SCREATE: 2646 return "shifted create key"; 2647 case KEY_SDC: 2648 return "shifted delete-character key"; 2649 case KEY_SDL: 2650 return "shifted delete-line key"; 2651 case KEY_SELECT: 2652 return "select key"; 2653 case KEY_SEND: 2654 return "shifted end key"; 2655 case KEY_SEOL: 2656 return "shifted clear-to-end-of-line key"; 2657 case KEY_SEXIT: 2658 return "shifted exit key"; 2659 case KEY_SFIND: 2660 return "shifted find key"; 2661 case KEY_SHELP: 2662 return "shifted help key"; 2663 case KEY_SHOME: 2664 return "shifted home key"; 2665 case KEY_SIC: 2666 return "shifted insert-character key"; 2667 case KEY_SLEFT: 2668 return "shifted left-arrow key"; 2669 case KEY_SMESSAGE: 2670 return "shifted message key"; 2671 case KEY_SMOVE: 2672 return "shifted move key"; 2673 case KEY_SNEXT: 2674 return "shifted next key"; 2675 case KEY_SOPTIONS: 2676 return "shifted options key"; 2677 case KEY_SPREVIOUS: 2678 return "shifted previous key"; 2679 case KEY_SPRINT: 2680 return "shifted print key"; 2681 case KEY_SREDO: 2682 return "shifted redo key"; 2683 case KEY_SREPLACE: 2684 return "shifted replace key"; 2685 case KEY_SRIGHT: 2686 return "shifted right-arrow key"; 2687 case KEY_SRSUME: 2688 return "shifted resume key"; 2689 case KEY_SSAVE: 2690 return "shifted save key"; 2691 case KEY_SSUSPEND: 2692 return "shifted suspend key"; 2693 case KEY_SUNDO: 2694 return "shifted undo key"; 2695 case KEY_SUSPEND: 2696 return "suspend key"; 2697 case KEY_UNDO: 2698 return "undo key"; 2699 case KEY_MOUSE: 2700 return "Mouse event has occurred"; 2701 case KEY_RESIZE: 2702 return "Terminal resize event"; 2703 #ifdef KEY_EVENT 2704 case KEY_EVENT: 2705 return "We were interrupted by an event"; 2706 #endif 2707 case KEY_RETURN: 2708 return "return"; 2709 case ' ': 2710 return "space"; 2711 case '\t': 2712 return "tab"; 2713 case KEY_ESCAPE: 2714 return "escape"; 2715 default: 2716 if (llvm::isPrint(ch)) 2717 snprintf(g_desc, sizeof(g_desc), "%c", ch); 2718 else 2719 snprintf(g_desc, sizeof(g_desc), "\\x%2.2x", ch); 2720 return g_desc; 2721 } 2722 return nullptr; 2723 } 2724 2725 HelpDialogDelegate::HelpDialogDelegate(const char *text, 2726 KeyHelp *key_help_array) 2727 : m_text(), m_first_visible_line(0) { 2728 if (text && text[0]) { 2729 m_text.SplitIntoLines(text); 2730 m_text.AppendString(""); 2731 } 2732 if (key_help_array) { 2733 for (KeyHelp *key = key_help_array; key->ch; ++key) { 2734 StreamString key_description; 2735 key_description.Printf("%10s - %s", CursesKeyToCString(key->ch), 2736 key->description); 2737 m_text.AppendString(key_description.GetString()); 2738 } 2739 } 2740 } 2741 2742 HelpDialogDelegate::~HelpDialogDelegate() = default; 2743 2744 bool HelpDialogDelegate::WindowDelegateDraw(Window &window, bool force) { 2745 window.Erase(); 2746 const int window_height = window.GetHeight(); 2747 int x = 2; 2748 int y = 1; 2749 const int min_y = y; 2750 const int max_y = window_height - 1 - y; 2751 const size_t num_visible_lines = max_y - min_y + 1; 2752 const size_t num_lines = m_text.GetSize(); 2753 const char *bottom_message; 2754 if (num_lines <= num_visible_lines) 2755 bottom_message = "Press any key to exit"; 2756 else 2757 bottom_message = "Use arrows to scroll, any other key to exit"; 2758 window.DrawTitleBox(window.GetName(), bottom_message); 2759 while (y <= max_y) { 2760 window.MoveCursor(x, y); 2761 window.PutCStringTruncated( 2762 m_text.GetStringAtIndex(m_first_visible_line + y - min_y), 1); 2763 ++y; 2764 } 2765 return true; 2766 } 2767 2768 HandleCharResult HelpDialogDelegate::WindowDelegateHandleChar(Window &window, 2769 int key) { 2770 bool done = false; 2771 const size_t num_lines = m_text.GetSize(); 2772 const size_t num_visible_lines = window.GetHeight() - 2; 2773 2774 if (num_lines <= num_visible_lines) { 2775 done = true; 2776 // If we have all lines visible and don't need scrolling, then any key 2777 // press will cause us to exit 2778 } else { 2779 switch (key) { 2780 case KEY_UP: 2781 if (m_first_visible_line > 0) 2782 --m_first_visible_line; 2783 break; 2784 2785 case KEY_DOWN: 2786 if (m_first_visible_line + num_visible_lines < num_lines) 2787 ++m_first_visible_line; 2788 break; 2789 2790 case KEY_PPAGE: 2791 case ',': 2792 if (m_first_visible_line > 0) { 2793 if (static_cast<size_t>(m_first_visible_line) >= num_visible_lines) 2794 m_first_visible_line -= num_visible_lines; 2795 else 2796 m_first_visible_line = 0; 2797 } 2798 break; 2799 2800 case KEY_NPAGE: 2801 case '.': 2802 if (m_first_visible_line + num_visible_lines < num_lines) { 2803 m_first_visible_line += num_visible_lines; 2804 if (static_cast<size_t>(m_first_visible_line) > num_lines) 2805 m_first_visible_line = num_lines - num_visible_lines; 2806 } 2807 break; 2808 2809 default: 2810 done = true; 2811 break; 2812 } 2813 } 2814 if (done) 2815 window.GetParent()->RemoveSubWindow(&window); 2816 return eKeyHandled; 2817 } 2818 2819 class ApplicationDelegate : public WindowDelegate, public MenuDelegate { 2820 public: 2821 enum { 2822 eMenuID_LLDB = 1, 2823 eMenuID_LLDBAbout, 2824 eMenuID_LLDBExit, 2825 2826 eMenuID_Target, 2827 eMenuID_TargetCreate, 2828 eMenuID_TargetDelete, 2829 2830 eMenuID_Process, 2831 eMenuID_ProcessAttach, 2832 eMenuID_ProcessDetach, 2833 eMenuID_ProcessLaunch, 2834 eMenuID_ProcessContinue, 2835 eMenuID_ProcessHalt, 2836 eMenuID_ProcessKill, 2837 2838 eMenuID_Thread, 2839 eMenuID_ThreadStepIn, 2840 eMenuID_ThreadStepOver, 2841 eMenuID_ThreadStepOut, 2842 2843 eMenuID_View, 2844 eMenuID_ViewBacktrace, 2845 eMenuID_ViewRegisters, 2846 eMenuID_ViewSource, 2847 eMenuID_ViewVariables, 2848 2849 eMenuID_Help, 2850 eMenuID_HelpGUIHelp 2851 }; 2852 2853 ApplicationDelegate(Application &app, Debugger &debugger) 2854 : WindowDelegate(), MenuDelegate(), m_app(app), m_debugger(debugger) {} 2855 2856 ~ApplicationDelegate() override = default; 2857 2858 bool WindowDelegateDraw(Window &window, bool force) override { 2859 return false; // Drawing not handled, let standard window drawing happen 2860 } 2861 2862 HandleCharResult WindowDelegateHandleChar(Window &window, int key) override { 2863 switch (key) { 2864 case '\t': 2865 window.SelectNextWindowAsActive(); 2866 return eKeyHandled; 2867 2868 case 'h': 2869 window.CreateHelpSubwindow(); 2870 return eKeyHandled; 2871 2872 case KEY_ESCAPE: 2873 return eQuitApplication; 2874 2875 default: 2876 break; 2877 } 2878 return eKeyNotHandled; 2879 } 2880 2881 const char *WindowDelegateGetHelpText() override { 2882 return "Welcome to the LLDB curses GUI.\n\n" 2883 "Press the TAB key to change the selected view.\n" 2884 "Each view has its own keyboard shortcuts, press 'h' to open a " 2885 "dialog to display them.\n\n" 2886 "Common key bindings for all views:"; 2887 } 2888 2889 KeyHelp *WindowDelegateGetKeyHelp() override { 2890 static curses::KeyHelp g_source_view_key_help[] = { 2891 {'\t', "Select next view"}, 2892 {'h', "Show help dialog with view specific key bindings"}, 2893 {',', "Page up"}, 2894 {'.', "Page down"}, 2895 {KEY_UP, "Select previous"}, 2896 {KEY_DOWN, "Select next"}, 2897 {KEY_LEFT, "Unexpand or select parent"}, 2898 {KEY_RIGHT, "Expand"}, 2899 {KEY_PPAGE, "Page up"}, 2900 {KEY_NPAGE, "Page down"}, 2901 {'\0', nullptr}}; 2902 return g_source_view_key_help; 2903 } 2904 2905 MenuActionResult MenuDelegateAction(Menu &menu) override { 2906 switch (menu.GetIdentifier()) { 2907 case eMenuID_ThreadStepIn: { 2908 ExecutionContext exe_ctx = 2909 m_debugger.GetCommandInterpreter().GetExecutionContext(); 2910 if (exe_ctx.HasThreadScope()) { 2911 Process *process = exe_ctx.GetProcessPtr(); 2912 if (process && process->IsAlive() && 2913 StateIsStoppedState(process->GetState(), true)) 2914 exe_ctx.GetThreadRef().StepIn(true); 2915 } 2916 } 2917 return MenuActionResult::Handled; 2918 2919 case eMenuID_ThreadStepOut: { 2920 ExecutionContext exe_ctx = 2921 m_debugger.GetCommandInterpreter().GetExecutionContext(); 2922 if (exe_ctx.HasThreadScope()) { 2923 Process *process = exe_ctx.GetProcessPtr(); 2924 if (process && process->IsAlive() && 2925 StateIsStoppedState(process->GetState(), true)) 2926 exe_ctx.GetThreadRef().StepOut(); 2927 } 2928 } 2929 return MenuActionResult::Handled; 2930 2931 case eMenuID_ThreadStepOver: { 2932 ExecutionContext exe_ctx = 2933 m_debugger.GetCommandInterpreter().GetExecutionContext(); 2934 if (exe_ctx.HasThreadScope()) { 2935 Process *process = exe_ctx.GetProcessPtr(); 2936 if (process && process->IsAlive() && 2937 StateIsStoppedState(process->GetState(), true)) 2938 exe_ctx.GetThreadRef().StepOver(true); 2939 } 2940 } 2941 return MenuActionResult::Handled; 2942 2943 case eMenuID_ProcessContinue: { 2944 ExecutionContext exe_ctx = 2945 m_debugger.GetCommandInterpreter().GetExecutionContext(); 2946 if (exe_ctx.HasProcessScope()) { 2947 Process *process = exe_ctx.GetProcessPtr(); 2948 if (process && process->IsAlive() && 2949 StateIsStoppedState(process->GetState(), true)) 2950 process->Resume(); 2951 } 2952 } 2953 return MenuActionResult::Handled; 2954 2955 case eMenuID_ProcessKill: { 2956 ExecutionContext exe_ctx = 2957 m_debugger.GetCommandInterpreter().GetExecutionContext(); 2958 if (exe_ctx.HasProcessScope()) { 2959 Process *process = exe_ctx.GetProcessPtr(); 2960 if (process && process->IsAlive()) 2961 process->Destroy(false); 2962 } 2963 } 2964 return MenuActionResult::Handled; 2965 2966 case eMenuID_ProcessHalt: { 2967 ExecutionContext exe_ctx = 2968 m_debugger.GetCommandInterpreter().GetExecutionContext(); 2969 if (exe_ctx.HasProcessScope()) { 2970 Process *process = exe_ctx.GetProcessPtr(); 2971 if (process && process->IsAlive()) 2972 process->Halt(); 2973 } 2974 } 2975 return MenuActionResult::Handled; 2976 2977 case eMenuID_ProcessDetach: { 2978 ExecutionContext exe_ctx = 2979 m_debugger.GetCommandInterpreter().GetExecutionContext(); 2980 if (exe_ctx.HasProcessScope()) { 2981 Process *process = exe_ctx.GetProcessPtr(); 2982 if (process && process->IsAlive()) 2983 process->Detach(false); 2984 } 2985 } 2986 return MenuActionResult::Handled; 2987 2988 case eMenuID_Process: { 2989 // Populate the menu with all of the threads if the process is stopped 2990 // when the Process menu gets selected and is about to display its 2991 // submenu. 2992 Menus &submenus = menu.GetSubmenus(); 2993 ExecutionContext exe_ctx = 2994 m_debugger.GetCommandInterpreter().GetExecutionContext(); 2995 Process *process = exe_ctx.GetProcessPtr(); 2996 if (process && process->IsAlive() && 2997 StateIsStoppedState(process->GetState(), true)) { 2998 if (submenus.size() == 7) 2999 menu.AddSubmenu(MenuSP(new Menu(Menu::Type::Separator))); 3000 else if (submenus.size() > 8) 3001 submenus.erase(submenus.begin() + 8, submenus.end()); 3002 3003 ThreadList &threads = process->GetThreadList(); 3004 std::lock_guard<std::recursive_mutex> guard(threads.GetMutex()); 3005 size_t num_threads = threads.GetSize(); 3006 for (size_t i = 0; i < num_threads; ++i) { 3007 ThreadSP thread_sp = threads.GetThreadAtIndex(i); 3008 char menu_char = '\0'; 3009 if (i < 9) 3010 menu_char = '1' + i; 3011 StreamString thread_menu_title; 3012 thread_menu_title.Printf("Thread %u", thread_sp->GetIndexID()); 3013 const char *thread_name = thread_sp->GetName(); 3014 if (thread_name && thread_name[0]) 3015 thread_menu_title.Printf(" %s", thread_name); 3016 else { 3017 const char *queue_name = thread_sp->GetQueueName(); 3018 if (queue_name && queue_name[0]) 3019 thread_menu_title.Printf(" %s", queue_name); 3020 } 3021 menu.AddSubmenu( 3022 MenuSP(new Menu(thread_menu_title.GetString().str().c_str(), 3023 nullptr, menu_char, thread_sp->GetID()))); 3024 } 3025 } else if (submenus.size() > 7) { 3026 // Remove the separator and any other thread submenu items that were 3027 // previously added 3028 submenus.erase(submenus.begin() + 7, submenus.end()); 3029 } 3030 // Since we are adding and removing items we need to recalculate the name 3031 // lengths 3032 menu.RecalculateNameLengths(); 3033 } 3034 return MenuActionResult::Handled; 3035 3036 case eMenuID_ViewVariables: { 3037 WindowSP main_window_sp = m_app.GetMainWindow(); 3038 WindowSP source_window_sp = main_window_sp->FindSubWindow("Source"); 3039 WindowSP variables_window_sp = main_window_sp->FindSubWindow("Variables"); 3040 WindowSP registers_window_sp = main_window_sp->FindSubWindow("Registers"); 3041 const Rect source_bounds = source_window_sp->GetBounds(); 3042 3043 if (variables_window_sp) { 3044 const Rect variables_bounds = variables_window_sp->GetBounds(); 3045 3046 main_window_sp->RemoveSubWindow(variables_window_sp.get()); 3047 3048 if (registers_window_sp) { 3049 // We have a registers window, so give all the area back to the 3050 // registers window 3051 Rect registers_bounds = variables_bounds; 3052 registers_bounds.size.width = source_bounds.size.width; 3053 registers_window_sp->SetBounds(registers_bounds); 3054 } else { 3055 // We have no registers window showing so give the bottom area back 3056 // to the source view 3057 source_window_sp->Resize(source_bounds.size.width, 3058 source_bounds.size.height + 3059 variables_bounds.size.height); 3060 } 3061 } else { 3062 Rect new_variables_rect; 3063 if (registers_window_sp) { 3064 // We have a registers window so split the area of the registers 3065 // window into two columns where the left hand side will be the 3066 // variables and the right hand side will be the registers 3067 const Rect variables_bounds = registers_window_sp->GetBounds(); 3068 Rect new_registers_rect; 3069 variables_bounds.VerticalSplitPercentage(0.50, new_variables_rect, 3070 new_registers_rect); 3071 registers_window_sp->SetBounds(new_registers_rect); 3072 } else { 3073 // No variables window, grab the bottom part of the source window 3074 Rect new_source_rect; 3075 source_bounds.HorizontalSplitPercentage(0.70, new_source_rect, 3076 new_variables_rect); 3077 source_window_sp->SetBounds(new_source_rect); 3078 } 3079 WindowSP new_window_sp = main_window_sp->CreateSubWindow( 3080 "Variables", new_variables_rect, false); 3081 new_window_sp->SetDelegate( 3082 WindowDelegateSP(new FrameVariablesWindowDelegate(m_debugger))); 3083 } 3084 touchwin(stdscr); 3085 } 3086 return MenuActionResult::Handled; 3087 3088 case eMenuID_ViewRegisters: { 3089 WindowSP main_window_sp = m_app.GetMainWindow(); 3090 WindowSP source_window_sp = main_window_sp->FindSubWindow("Source"); 3091 WindowSP variables_window_sp = main_window_sp->FindSubWindow("Variables"); 3092 WindowSP registers_window_sp = main_window_sp->FindSubWindow("Registers"); 3093 const Rect source_bounds = source_window_sp->GetBounds(); 3094 3095 if (registers_window_sp) { 3096 if (variables_window_sp) { 3097 const Rect variables_bounds = variables_window_sp->GetBounds(); 3098 3099 // We have a variables window, so give all the area back to the 3100 // variables window 3101 variables_window_sp->Resize(variables_bounds.size.width + 3102 registers_window_sp->GetWidth(), 3103 variables_bounds.size.height); 3104 } else { 3105 // We have no variables window showing so give the bottom area back 3106 // to the source view 3107 source_window_sp->Resize(source_bounds.size.width, 3108 source_bounds.size.height + 3109 registers_window_sp->GetHeight()); 3110 } 3111 main_window_sp->RemoveSubWindow(registers_window_sp.get()); 3112 } else { 3113 Rect new_regs_rect; 3114 if (variables_window_sp) { 3115 // We have a variables window, split it into two columns where the 3116 // left hand side will be the variables and the right hand side will 3117 // be the registers 3118 const Rect variables_bounds = variables_window_sp->GetBounds(); 3119 Rect new_vars_rect; 3120 variables_bounds.VerticalSplitPercentage(0.50, new_vars_rect, 3121 new_regs_rect); 3122 variables_window_sp->SetBounds(new_vars_rect); 3123 } else { 3124 // No registers window, grab the bottom part of the source window 3125 Rect new_source_rect; 3126 source_bounds.HorizontalSplitPercentage(0.70, new_source_rect, 3127 new_regs_rect); 3128 source_window_sp->SetBounds(new_source_rect); 3129 } 3130 WindowSP new_window_sp = 3131 main_window_sp->CreateSubWindow("Registers", new_regs_rect, false); 3132 new_window_sp->SetDelegate( 3133 WindowDelegateSP(new RegistersWindowDelegate(m_debugger))); 3134 } 3135 touchwin(stdscr); 3136 } 3137 return MenuActionResult::Handled; 3138 3139 case eMenuID_HelpGUIHelp: 3140 m_app.GetMainWindow()->CreateHelpSubwindow(); 3141 return MenuActionResult::Handled; 3142 3143 default: 3144 break; 3145 } 3146 3147 return MenuActionResult::NotHandled; 3148 } 3149 3150 protected: 3151 Application &m_app; 3152 Debugger &m_debugger; 3153 }; 3154 3155 class StatusBarWindowDelegate : public WindowDelegate { 3156 public: 3157 StatusBarWindowDelegate(Debugger &debugger) : m_debugger(debugger) { 3158 FormatEntity::Parse("Thread: ${thread.id%tid}", m_format); 3159 } 3160 3161 ~StatusBarWindowDelegate() override = default; 3162 3163 bool WindowDelegateDraw(Window &window, bool force) override { 3164 ExecutionContext exe_ctx = 3165 m_debugger.GetCommandInterpreter().GetExecutionContext(); 3166 Process *process = exe_ctx.GetProcessPtr(); 3167 Thread *thread = exe_ctx.GetThreadPtr(); 3168 StackFrame *frame = exe_ctx.GetFramePtr(); 3169 window.Erase(); 3170 window.SetBackground(2); 3171 window.MoveCursor(0, 0); 3172 if (process) { 3173 const StateType state = process->GetState(); 3174 window.Printf("Process: %5" PRIu64 " %10s", process->GetID(), 3175 StateAsCString(state)); 3176 3177 if (StateIsStoppedState(state, true)) { 3178 StreamString strm; 3179 if (thread && FormatEntity::Format(m_format, strm, nullptr, &exe_ctx, 3180 nullptr, nullptr, false, false)) { 3181 window.MoveCursor(40, 0); 3182 window.PutCStringTruncated(strm.GetString().str().c_str(), 1); 3183 } 3184 3185 window.MoveCursor(60, 0); 3186 if (frame) 3187 window.Printf("Frame: %3u PC = 0x%16.16" PRIx64, 3188 frame->GetFrameIndex(), 3189 frame->GetFrameCodeAddress().GetOpcodeLoadAddress( 3190 exe_ctx.GetTargetPtr())); 3191 } else if (state == eStateExited) { 3192 const char *exit_desc = process->GetExitDescription(); 3193 const int exit_status = process->GetExitStatus(); 3194 if (exit_desc && exit_desc[0]) 3195 window.Printf(" with status = %i (%s)", exit_status, exit_desc); 3196 else 3197 window.Printf(" with status = %i", exit_status); 3198 } 3199 } 3200 return true; 3201 } 3202 3203 protected: 3204 Debugger &m_debugger; 3205 FormatEntity::Entry m_format; 3206 }; 3207 3208 class SourceFileWindowDelegate : public WindowDelegate { 3209 public: 3210 SourceFileWindowDelegate(Debugger &debugger) 3211 : WindowDelegate(), m_debugger(debugger), m_sc(), m_file_sp(), 3212 m_disassembly_scope(nullptr), m_disassembly_sp(), m_disassembly_range(), 3213 m_title(), m_line_width(4), m_selected_line(0), m_pc_line(0), 3214 m_stop_id(0), m_frame_idx(UINT32_MAX), m_first_visible_line(0), 3215 m_min_x(0), m_min_y(0), m_max_x(0), m_max_y(0) {} 3216 3217 ~SourceFileWindowDelegate() override = default; 3218 3219 void Update(const SymbolContext &sc) { m_sc = sc; } 3220 3221 uint32_t NumVisibleLines() const { return m_max_y - m_min_y; } 3222 3223 const char *WindowDelegateGetHelpText() override { 3224 return "Source/Disassembly window keyboard shortcuts:"; 3225 } 3226 3227 KeyHelp *WindowDelegateGetKeyHelp() override { 3228 static curses::KeyHelp g_source_view_key_help[] = { 3229 {KEY_RETURN, "Run to selected line with one shot breakpoint"}, 3230 {KEY_UP, "Select previous source line"}, 3231 {KEY_DOWN, "Select next source line"}, 3232 {KEY_PPAGE, "Page up"}, 3233 {KEY_NPAGE, "Page down"}, 3234 {'b', "Set breakpoint on selected source/disassembly line"}, 3235 {'c', "Continue process"}, 3236 {'d', "Detach and resume process"}, 3237 {'D', "Detach with process suspended"}, 3238 {'h', "Show help dialog"}, 3239 {'k', "Kill process"}, 3240 {'n', "Step over (source line)"}, 3241 {'N', "Step over (single instruction)"}, 3242 {'o', "Step out"}, 3243 {'s', "Step in (source line)"}, 3244 {'S', "Step in (single instruction)"}, 3245 {',', "Page up"}, 3246 {'.', "Page down"}, 3247 {'\0', nullptr}}; 3248 return g_source_view_key_help; 3249 } 3250 3251 bool WindowDelegateDraw(Window &window, bool force) override { 3252 ExecutionContext exe_ctx = 3253 m_debugger.GetCommandInterpreter().GetExecutionContext(); 3254 Process *process = exe_ctx.GetProcessPtr(); 3255 Thread *thread = nullptr; 3256 3257 bool update_location = false; 3258 if (process) { 3259 StateType state = process->GetState(); 3260 if (StateIsStoppedState(state, true)) { 3261 // We are stopped, so it is ok to 3262 update_location = true; 3263 } 3264 } 3265 3266 m_min_x = 1; 3267 m_min_y = 2; 3268 m_max_x = window.GetMaxX() - 1; 3269 m_max_y = window.GetMaxY() - 1; 3270 3271 const uint32_t num_visible_lines = NumVisibleLines(); 3272 StackFrameSP frame_sp; 3273 bool set_selected_line_to_pc = false; 3274 3275 if (update_location) { 3276 const bool process_alive = process ? process->IsAlive() : false; 3277 bool thread_changed = false; 3278 if (process_alive) { 3279 thread = exe_ctx.GetThreadPtr(); 3280 if (thread) { 3281 frame_sp = thread->GetSelectedFrame(); 3282 auto tid = thread->GetID(); 3283 thread_changed = tid != m_tid; 3284 m_tid = tid; 3285 } else { 3286 if (m_tid != LLDB_INVALID_THREAD_ID) { 3287 thread_changed = true; 3288 m_tid = LLDB_INVALID_THREAD_ID; 3289 } 3290 } 3291 } 3292 const uint32_t stop_id = process ? process->GetStopID() : 0; 3293 const bool stop_id_changed = stop_id != m_stop_id; 3294 bool frame_changed = false; 3295 m_stop_id = stop_id; 3296 m_title.Clear(); 3297 if (frame_sp) { 3298 m_sc = frame_sp->GetSymbolContext(eSymbolContextEverything); 3299 if (m_sc.module_sp) { 3300 m_title.Printf( 3301 "%s", m_sc.module_sp->GetFileSpec().GetFilename().GetCString()); 3302 ConstString func_name = m_sc.GetFunctionName(); 3303 if (func_name) 3304 m_title.Printf("`%s", func_name.GetCString()); 3305 } 3306 const uint32_t frame_idx = frame_sp->GetFrameIndex(); 3307 frame_changed = frame_idx != m_frame_idx; 3308 m_frame_idx = frame_idx; 3309 } else { 3310 m_sc.Clear(true); 3311 frame_changed = m_frame_idx != UINT32_MAX; 3312 m_frame_idx = UINT32_MAX; 3313 } 3314 3315 const bool context_changed = 3316 thread_changed || frame_changed || stop_id_changed; 3317 3318 if (process_alive) { 3319 if (m_sc.line_entry.IsValid()) { 3320 m_pc_line = m_sc.line_entry.line; 3321 if (m_pc_line != UINT32_MAX) 3322 --m_pc_line; // Convert to zero based line number... 3323 // Update the selected line if the stop ID changed... 3324 if (context_changed) 3325 m_selected_line = m_pc_line; 3326 3327 if (m_file_sp && m_file_sp->GetFileSpec() == m_sc.line_entry.file) { 3328 // Same file, nothing to do, we should either have the lines or not 3329 // (source file missing) 3330 if (m_selected_line >= static_cast<size_t>(m_first_visible_line)) { 3331 if (m_selected_line >= m_first_visible_line + num_visible_lines) 3332 m_first_visible_line = m_selected_line - 10; 3333 } else { 3334 if (m_selected_line > 10) 3335 m_first_visible_line = m_selected_line - 10; 3336 else 3337 m_first_visible_line = 0; 3338 } 3339 } else { 3340 // File changed, set selected line to the line with the PC 3341 m_selected_line = m_pc_line; 3342 m_file_sp = 3343 m_debugger.GetSourceManager().GetFile(m_sc.line_entry.file); 3344 if (m_file_sp) { 3345 const size_t num_lines = m_file_sp->GetNumLines(); 3346 m_line_width = 1; 3347 for (size_t n = num_lines; n >= 10; n = n / 10) 3348 ++m_line_width; 3349 3350 if (num_lines < num_visible_lines || 3351 m_selected_line < num_visible_lines) 3352 m_first_visible_line = 0; 3353 else 3354 m_first_visible_line = m_selected_line - 10; 3355 } 3356 } 3357 } else { 3358 m_file_sp.reset(); 3359 } 3360 3361 if (!m_file_sp || m_file_sp->GetNumLines() == 0) { 3362 // Show disassembly 3363 bool prefer_file_cache = false; 3364 if (m_sc.function) { 3365 if (m_disassembly_scope != m_sc.function) { 3366 m_disassembly_scope = m_sc.function; 3367 m_disassembly_sp = m_sc.function->GetInstructions( 3368 exe_ctx, nullptr, prefer_file_cache); 3369 if (m_disassembly_sp) { 3370 set_selected_line_to_pc = true; 3371 m_disassembly_range = m_sc.function->GetAddressRange(); 3372 } else { 3373 m_disassembly_range.Clear(); 3374 } 3375 } else { 3376 set_selected_line_to_pc = context_changed; 3377 } 3378 } else if (m_sc.symbol) { 3379 if (m_disassembly_scope != m_sc.symbol) { 3380 m_disassembly_scope = m_sc.symbol; 3381 m_disassembly_sp = m_sc.symbol->GetInstructions( 3382 exe_ctx, nullptr, prefer_file_cache); 3383 if (m_disassembly_sp) { 3384 set_selected_line_to_pc = true; 3385 m_disassembly_range.GetBaseAddress() = 3386 m_sc.symbol->GetAddress(); 3387 m_disassembly_range.SetByteSize(m_sc.symbol->GetByteSize()); 3388 } else { 3389 m_disassembly_range.Clear(); 3390 } 3391 } else { 3392 set_selected_line_to_pc = context_changed; 3393 } 3394 } 3395 } 3396 } else { 3397 m_pc_line = UINT32_MAX; 3398 } 3399 } 3400 3401 const int window_width = window.GetWidth(); 3402 window.Erase(); 3403 window.DrawTitleBox("Sources"); 3404 if (!m_title.GetString().empty()) { 3405 window.AttributeOn(A_REVERSE); 3406 window.MoveCursor(1, 1); 3407 window.PutChar(' '); 3408 window.PutCStringTruncated(m_title.GetString().str().c_str(), 1); 3409 int x = window.GetCursorX(); 3410 if (x < window_width - 1) { 3411 window.Printf("%*s", window_width - x - 1, ""); 3412 } 3413 window.AttributeOff(A_REVERSE); 3414 } 3415 3416 Target *target = exe_ctx.GetTargetPtr(); 3417 const size_t num_source_lines = GetNumSourceLines(); 3418 if (num_source_lines > 0) { 3419 // Display source 3420 BreakpointLines bp_lines; 3421 if (target) { 3422 BreakpointList &bp_list = target->GetBreakpointList(); 3423 const size_t num_bps = bp_list.GetSize(); 3424 for (size_t bp_idx = 0; bp_idx < num_bps; ++bp_idx) { 3425 BreakpointSP bp_sp = bp_list.GetBreakpointAtIndex(bp_idx); 3426 const size_t num_bps_locs = bp_sp->GetNumLocations(); 3427 for (size_t bp_loc_idx = 0; bp_loc_idx < num_bps_locs; ++bp_loc_idx) { 3428 BreakpointLocationSP bp_loc_sp = 3429 bp_sp->GetLocationAtIndex(bp_loc_idx); 3430 LineEntry bp_loc_line_entry; 3431 if (bp_loc_sp->GetAddress().CalculateSymbolContextLineEntry( 3432 bp_loc_line_entry)) { 3433 if (m_file_sp->GetFileSpec() == bp_loc_line_entry.file) { 3434 bp_lines.insert(bp_loc_line_entry.line); 3435 } 3436 } 3437 } 3438 } 3439 } 3440 3441 const attr_t selected_highlight_attr = A_REVERSE; 3442 const attr_t pc_highlight_attr = COLOR_PAIR(1); 3443 3444 for (size_t i = 0; i < num_visible_lines; ++i) { 3445 const uint32_t curr_line = m_first_visible_line + i; 3446 if (curr_line < num_source_lines) { 3447 const int line_y = m_min_y + i; 3448 window.MoveCursor(1, line_y); 3449 const bool is_pc_line = curr_line == m_pc_line; 3450 const bool line_is_selected = m_selected_line == curr_line; 3451 // Highlight the line as the PC line first, then if the selected line 3452 // isn't the same as the PC line, highlight it differently 3453 attr_t highlight_attr = 0; 3454 attr_t bp_attr = 0; 3455 if (is_pc_line) 3456 highlight_attr = pc_highlight_attr; 3457 else if (line_is_selected) 3458 highlight_attr = selected_highlight_attr; 3459 3460 if (bp_lines.find(curr_line + 1) != bp_lines.end()) 3461 bp_attr = COLOR_PAIR(2); 3462 3463 if (bp_attr) 3464 window.AttributeOn(bp_attr); 3465 3466 window.Printf(" %*u ", m_line_width, curr_line + 1); 3467 3468 if (bp_attr) 3469 window.AttributeOff(bp_attr); 3470 3471 window.PutChar(ACS_VLINE); 3472 // Mark the line with the PC with a diamond 3473 if (is_pc_line) 3474 window.PutChar(ACS_DIAMOND); 3475 else 3476 window.PutChar(' '); 3477 3478 if (highlight_attr) 3479 window.AttributeOn(highlight_attr); 3480 const uint32_t line_len = 3481 m_file_sp->GetLineLength(curr_line + 1, false); 3482 if (line_len > 0) 3483 window.PutCString(m_file_sp->PeekLineData(curr_line + 1), line_len); 3484 3485 if (is_pc_line && frame_sp && 3486 frame_sp->GetConcreteFrameIndex() == 0) { 3487 StopInfoSP stop_info_sp; 3488 if (thread) 3489 stop_info_sp = thread->GetStopInfo(); 3490 if (stop_info_sp) { 3491 const char *stop_description = stop_info_sp->GetDescription(); 3492 if (stop_description && stop_description[0]) { 3493 size_t stop_description_len = strlen(stop_description); 3494 int desc_x = window_width - stop_description_len - 16; 3495 window.Printf("%*s", desc_x - window.GetCursorX(), ""); 3496 // window.MoveCursor(window_width - stop_description_len - 15, 3497 // line_y); 3498 window.Printf("<<< Thread %u: %s ", thread->GetIndexID(), 3499 stop_description); 3500 } 3501 } else { 3502 window.Printf("%*s", window_width - window.GetCursorX() - 1, ""); 3503 } 3504 } 3505 if (highlight_attr) 3506 window.AttributeOff(highlight_attr); 3507 } else { 3508 break; 3509 } 3510 } 3511 } else { 3512 size_t num_disassembly_lines = GetNumDisassemblyLines(); 3513 if (num_disassembly_lines > 0) { 3514 // Display disassembly 3515 BreakpointAddrs bp_file_addrs; 3516 Target *target = exe_ctx.GetTargetPtr(); 3517 if (target) { 3518 BreakpointList &bp_list = target->GetBreakpointList(); 3519 const size_t num_bps = bp_list.GetSize(); 3520 for (size_t bp_idx = 0; bp_idx < num_bps; ++bp_idx) { 3521 BreakpointSP bp_sp = bp_list.GetBreakpointAtIndex(bp_idx); 3522 const size_t num_bps_locs = bp_sp->GetNumLocations(); 3523 for (size_t bp_loc_idx = 0; bp_loc_idx < num_bps_locs; 3524 ++bp_loc_idx) { 3525 BreakpointLocationSP bp_loc_sp = 3526 bp_sp->GetLocationAtIndex(bp_loc_idx); 3527 LineEntry bp_loc_line_entry; 3528 const lldb::addr_t file_addr = 3529 bp_loc_sp->GetAddress().GetFileAddress(); 3530 if (file_addr != LLDB_INVALID_ADDRESS) { 3531 if (m_disassembly_range.ContainsFileAddress(file_addr)) 3532 bp_file_addrs.insert(file_addr); 3533 } 3534 } 3535 } 3536 } 3537 3538 const attr_t selected_highlight_attr = A_REVERSE; 3539 const attr_t pc_highlight_attr = COLOR_PAIR(1); 3540 3541 StreamString strm; 3542 3543 InstructionList &insts = m_disassembly_sp->GetInstructionList(); 3544 Address pc_address; 3545 3546 if (frame_sp) 3547 pc_address = frame_sp->GetFrameCodeAddress(); 3548 const uint32_t pc_idx = 3549 pc_address.IsValid() 3550 ? insts.GetIndexOfInstructionAtAddress(pc_address) 3551 : UINT32_MAX; 3552 if (set_selected_line_to_pc) { 3553 m_selected_line = pc_idx; 3554 } 3555 3556 const uint32_t non_visible_pc_offset = (num_visible_lines / 5); 3557 if (static_cast<size_t>(m_first_visible_line) >= num_disassembly_lines) 3558 m_first_visible_line = 0; 3559 3560 if (pc_idx < num_disassembly_lines) { 3561 if (pc_idx < static_cast<uint32_t>(m_first_visible_line) || 3562 pc_idx >= m_first_visible_line + num_visible_lines) 3563 m_first_visible_line = pc_idx - non_visible_pc_offset; 3564 } 3565 3566 for (size_t i = 0; i < num_visible_lines; ++i) { 3567 const uint32_t inst_idx = m_first_visible_line + i; 3568 Instruction *inst = insts.GetInstructionAtIndex(inst_idx).get(); 3569 if (!inst) 3570 break; 3571 3572 const int line_y = m_min_y + i; 3573 window.MoveCursor(1, line_y); 3574 const bool is_pc_line = frame_sp && inst_idx == pc_idx; 3575 const bool line_is_selected = m_selected_line == inst_idx; 3576 // Highlight the line as the PC line first, then if the selected line 3577 // isn't the same as the PC line, highlight it differently 3578 attr_t highlight_attr = 0; 3579 attr_t bp_attr = 0; 3580 if (is_pc_line) 3581 highlight_attr = pc_highlight_attr; 3582 else if (line_is_selected) 3583 highlight_attr = selected_highlight_attr; 3584 3585 if (bp_file_addrs.find(inst->GetAddress().GetFileAddress()) != 3586 bp_file_addrs.end()) 3587 bp_attr = COLOR_PAIR(2); 3588 3589 if (bp_attr) 3590 window.AttributeOn(bp_attr); 3591 3592 window.Printf(" 0x%16.16llx ", 3593 static_cast<unsigned long long>( 3594 inst->GetAddress().GetLoadAddress(target))); 3595 3596 if (bp_attr) 3597 window.AttributeOff(bp_attr); 3598 3599 window.PutChar(ACS_VLINE); 3600 // Mark the line with the PC with a diamond 3601 if (is_pc_line) 3602 window.PutChar(ACS_DIAMOND); 3603 else 3604 window.PutChar(' '); 3605 3606 if (highlight_attr) 3607 window.AttributeOn(highlight_attr); 3608 3609 const char *mnemonic = inst->GetMnemonic(&exe_ctx); 3610 const char *operands = inst->GetOperands(&exe_ctx); 3611 const char *comment = inst->GetComment(&exe_ctx); 3612 3613 if (mnemonic != nullptr && mnemonic[0] == '\0') 3614 mnemonic = nullptr; 3615 if (operands != nullptr && operands[0] == '\0') 3616 operands = nullptr; 3617 if (comment != nullptr && comment[0] == '\0') 3618 comment = nullptr; 3619 3620 strm.Clear(); 3621 3622 if (mnemonic != nullptr && operands != nullptr && comment != nullptr) 3623 strm.Printf("%-8s %-25s ; %s", mnemonic, operands, comment); 3624 else if (mnemonic != nullptr && operands != nullptr) 3625 strm.Printf("%-8s %s", mnemonic, operands); 3626 else if (mnemonic != nullptr) 3627 strm.Printf("%s", mnemonic); 3628 3629 int right_pad = 1; 3630 window.PutCStringTruncated(strm.GetData(), right_pad); 3631 3632 if (is_pc_line && frame_sp && 3633 frame_sp->GetConcreteFrameIndex() == 0) { 3634 StopInfoSP stop_info_sp; 3635 if (thread) 3636 stop_info_sp = thread->GetStopInfo(); 3637 if (stop_info_sp) { 3638 const char *stop_description = stop_info_sp->GetDescription(); 3639 if (stop_description && stop_description[0]) { 3640 size_t stop_description_len = strlen(stop_description); 3641 int desc_x = window_width - stop_description_len - 16; 3642 window.Printf("%*s", desc_x - window.GetCursorX(), ""); 3643 // window.MoveCursor(window_width - stop_description_len - 15, 3644 // line_y); 3645 window.Printf("<<< Thread %u: %s ", thread->GetIndexID(), 3646 stop_description); 3647 } 3648 } else { 3649 window.Printf("%*s", window_width - window.GetCursorX() - 1, ""); 3650 } 3651 } 3652 if (highlight_attr) 3653 window.AttributeOff(highlight_attr); 3654 } 3655 } 3656 } 3657 return true; // Drawing handled 3658 } 3659 3660 size_t GetNumLines() { 3661 size_t num_lines = GetNumSourceLines(); 3662 if (num_lines == 0) 3663 num_lines = GetNumDisassemblyLines(); 3664 return num_lines; 3665 } 3666 3667 size_t GetNumSourceLines() const { 3668 if (m_file_sp) 3669 return m_file_sp->GetNumLines(); 3670 return 0; 3671 } 3672 3673 size_t GetNumDisassemblyLines() const { 3674 if (m_disassembly_sp) 3675 return m_disassembly_sp->GetInstructionList().GetSize(); 3676 return 0; 3677 } 3678 3679 HandleCharResult WindowDelegateHandleChar(Window &window, int c) override { 3680 const uint32_t num_visible_lines = NumVisibleLines(); 3681 const size_t num_lines = GetNumLines(); 3682 3683 switch (c) { 3684 case ',': 3685 case KEY_PPAGE: 3686 // Page up key 3687 if (static_cast<uint32_t>(m_first_visible_line) > num_visible_lines) 3688 m_first_visible_line -= num_visible_lines; 3689 else 3690 m_first_visible_line = 0; 3691 m_selected_line = m_first_visible_line; 3692 return eKeyHandled; 3693 3694 case '.': 3695 case KEY_NPAGE: 3696 // Page down key 3697 { 3698 if (m_first_visible_line + num_visible_lines < num_lines) 3699 m_first_visible_line += num_visible_lines; 3700 else if (num_lines < num_visible_lines) 3701 m_first_visible_line = 0; 3702 else 3703 m_first_visible_line = num_lines - num_visible_lines; 3704 m_selected_line = m_first_visible_line; 3705 } 3706 return eKeyHandled; 3707 3708 case KEY_UP: 3709 if (m_selected_line > 0) { 3710 m_selected_line--; 3711 if (static_cast<size_t>(m_first_visible_line) > m_selected_line) 3712 m_first_visible_line = m_selected_line; 3713 } 3714 return eKeyHandled; 3715 3716 case KEY_DOWN: 3717 if (m_selected_line + 1 < num_lines) { 3718 m_selected_line++; 3719 if (m_first_visible_line + num_visible_lines < m_selected_line) 3720 m_first_visible_line++; 3721 } 3722 return eKeyHandled; 3723 3724 case '\r': 3725 case '\n': 3726 case KEY_ENTER: 3727 // Set a breakpoint and run to the line using a one shot breakpoint 3728 if (GetNumSourceLines() > 0) { 3729 ExecutionContext exe_ctx = 3730 m_debugger.GetCommandInterpreter().GetExecutionContext(); 3731 if (exe_ctx.HasProcessScope() && exe_ctx.GetProcessRef().IsAlive()) { 3732 BreakpointSP bp_sp = exe_ctx.GetTargetRef().CreateBreakpoint( 3733 nullptr, // Don't limit the breakpoint to certain modules 3734 m_file_sp->GetFileSpec(), // Source file 3735 m_selected_line + 3736 1, // Source line number (m_selected_line is zero based) 3737 0, // Unspecified column. 3738 0, // No offset 3739 eLazyBoolCalculate, // Check inlines using global setting 3740 eLazyBoolCalculate, // Skip prologue using global setting, 3741 false, // internal 3742 false, // request_hardware 3743 eLazyBoolCalculate); // move_to_nearest_code 3744 // Make breakpoint one shot 3745 bp_sp->GetOptions()->SetOneShot(true); 3746 exe_ctx.GetProcessRef().Resume(); 3747 } 3748 } else if (m_selected_line < GetNumDisassemblyLines()) { 3749 const Instruction *inst = m_disassembly_sp->GetInstructionList() 3750 .GetInstructionAtIndex(m_selected_line) 3751 .get(); 3752 ExecutionContext exe_ctx = 3753 m_debugger.GetCommandInterpreter().GetExecutionContext(); 3754 if (exe_ctx.HasTargetScope()) { 3755 Address addr = inst->GetAddress(); 3756 BreakpointSP bp_sp = exe_ctx.GetTargetRef().CreateBreakpoint( 3757 addr, // lldb_private::Address 3758 false, // internal 3759 false); // request_hardware 3760 // Make breakpoint one shot 3761 bp_sp->GetOptions()->SetOneShot(true); 3762 exe_ctx.GetProcessRef().Resume(); 3763 } 3764 } 3765 return eKeyHandled; 3766 3767 case 'b': // 'b' == toggle breakpoint on currently selected line 3768 if (m_selected_line < GetNumSourceLines()) { 3769 ExecutionContext exe_ctx = 3770 m_debugger.GetCommandInterpreter().GetExecutionContext(); 3771 if (exe_ctx.HasTargetScope()) { 3772 BreakpointSP bp_sp = exe_ctx.GetTargetRef().CreateBreakpoint( 3773 nullptr, // Don't limit the breakpoint to certain modules 3774 m_file_sp->GetFileSpec(), // Source file 3775 m_selected_line + 3776 1, // Source line number (m_selected_line is zero based) 3777 0, // No column specified. 3778 0, // No offset 3779 eLazyBoolCalculate, // Check inlines using global setting 3780 eLazyBoolCalculate, // Skip prologue using global setting, 3781 false, // internal 3782 false, // request_hardware 3783 eLazyBoolCalculate); // move_to_nearest_code 3784 } 3785 } else if (m_selected_line < GetNumDisassemblyLines()) { 3786 const Instruction *inst = m_disassembly_sp->GetInstructionList() 3787 .GetInstructionAtIndex(m_selected_line) 3788 .get(); 3789 ExecutionContext exe_ctx = 3790 m_debugger.GetCommandInterpreter().GetExecutionContext(); 3791 if (exe_ctx.HasTargetScope()) { 3792 Address addr = inst->GetAddress(); 3793 BreakpointSP bp_sp = exe_ctx.GetTargetRef().CreateBreakpoint( 3794 addr, // lldb_private::Address 3795 false, // internal 3796 false); // request_hardware 3797 } 3798 } 3799 return eKeyHandled; 3800 3801 case 'd': // 'd' == detach and let run 3802 case 'D': // 'D' == detach and keep stopped 3803 { 3804 ExecutionContext exe_ctx = 3805 m_debugger.GetCommandInterpreter().GetExecutionContext(); 3806 if (exe_ctx.HasProcessScope()) 3807 exe_ctx.GetProcessRef().Detach(c == 'D'); 3808 } 3809 return eKeyHandled; 3810 3811 case 'k': 3812 // 'k' == kill 3813 { 3814 ExecutionContext exe_ctx = 3815 m_debugger.GetCommandInterpreter().GetExecutionContext(); 3816 if (exe_ctx.HasProcessScope()) 3817 exe_ctx.GetProcessRef().Destroy(false); 3818 } 3819 return eKeyHandled; 3820 3821 case 'c': 3822 // 'c' == continue 3823 { 3824 ExecutionContext exe_ctx = 3825 m_debugger.GetCommandInterpreter().GetExecutionContext(); 3826 if (exe_ctx.HasProcessScope()) 3827 exe_ctx.GetProcessRef().Resume(); 3828 } 3829 return eKeyHandled; 3830 3831 case 'o': 3832 // 'o' == step out 3833 { 3834 ExecutionContext exe_ctx = 3835 m_debugger.GetCommandInterpreter().GetExecutionContext(); 3836 if (exe_ctx.HasThreadScope() && 3837 StateIsStoppedState(exe_ctx.GetProcessRef().GetState(), true)) { 3838 exe_ctx.GetThreadRef().StepOut(); 3839 } 3840 } 3841 return eKeyHandled; 3842 3843 case 'n': // 'n' == step over 3844 case 'N': // 'N' == step over instruction 3845 { 3846 ExecutionContext exe_ctx = 3847 m_debugger.GetCommandInterpreter().GetExecutionContext(); 3848 if (exe_ctx.HasThreadScope() && 3849 StateIsStoppedState(exe_ctx.GetProcessRef().GetState(), true)) { 3850 bool source_step = (c == 'n'); 3851 exe_ctx.GetThreadRef().StepOver(source_step); 3852 } 3853 } 3854 return eKeyHandled; 3855 3856 case 's': // 's' == step into 3857 case 'S': // 'S' == step into instruction 3858 { 3859 ExecutionContext exe_ctx = 3860 m_debugger.GetCommandInterpreter().GetExecutionContext(); 3861 if (exe_ctx.HasThreadScope() && 3862 StateIsStoppedState(exe_ctx.GetProcessRef().GetState(), true)) { 3863 bool source_step = (c == 's'); 3864 exe_ctx.GetThreadRef().StepIn(source_step); 3865 } 3866 } 3867 return eKeyHandled; 3868 3869 case 'h': 3870 window.CreateHelpSubwindow(); 3871 return eKeyHandled; 3872 3873 default: 3874 break; 3875 } 3876 return eKeyNotHandled; 3877 } 3878 3879 protected: 3880 typedef std::set<uint32_t> BreakpointLines; 3881 typedef std::set<lldb::addr_t> BreakpointAddrs; 3882 3883 Debugger &m_debugger; 3884 SymbolContext m_sc; 3885 SourceManager::FileSP m_file_sp; 3886 SymbolContextScope *m_disassembly_scope; 3887 lldb::DisassemblerSP m_disassembly_sp; 3888 AddressRange m_disassembly_range; 3889 StreamString m_title; 3890 lldb::user_id_t m_tid; 3891 int m_line_width; 3892 uint32_t m_selected_line; // The selected line 3893 uint32_t m_pc_line; // The line with the PC 3894 uint32_t m_stop_id; 3895 uint32_t m_frame_idx; 3896 int m_first_visible_line; 3897 int m_min_x; 3898 int m_min_y; 3899 int m_max_x; 3900 int m_max_y; 3901 }; 3902 3903 DisplayOptions ValueObjectListDelegate::g_options = {true}; 3904 3905 IOHandlerCursesGUI::IOHandlerCursesGUI(Debugger &debugger) 3906 : IOHandler(debugger, IOHandler::Type::Curses) {} 3907 3908 void IOHandlerCursesGUI::Activate() { 3909 IOHandler::Activate(); 3910 if (!m_app_ap) { 3911 m_app_ap = std::make_unique<Application>(GetInputFILE(), GetOutputFILE()); 3912 3913 // This is both a window and a menu delegate 3914 std::shared_ptr<ApplicationDelegate> app_delegate_sp( 3915 new ApplicationDelegate(*m_app_ap, m_debugger)); 3916 3917 MenuDelegateSP app_menu_delegate_sp = 3918 std::static_pointer_cast<MenuDelegate>(app_delegate_sp); 3919 MenuSP lldb_menu_sp( 3920 new Menu("LLDB", "F1", KEY_F(1), ApplicationDelegate::eMenuID_LLDB)); 3921 MenuSP exit_menuitem_sp( 3922 new Menu("Exit", nullptr, 'x', ApplicationDelegate::eMenuID_LLDBExit)); 3923 exit_menuitem_sp->SetCannedResult(MenuActionResult::Quit); 3924 lldb_menu_sp->AddSubmenu(MenuSP(new Menu( 3925 "About LLDB", nullptr, 'a', ApplicationDelegate::eMenuID_LLDBAbout))); 3926 lldb_menu_sp->AddSubmenu(MenuSP(new Menu(Menu::Type::Separator))); 3927 lldb_menu_sp->AddSubmenu(exit_menuitem_sp); 3928 3929 MenuSP target_menu_sp(new Menu("Target", "F2", KEY_F(2), 3930 ApplicationDelegate::eMenuID_Target)); 3931 target_menu_sp->AddSubmenu(MenuSP(new Menu( 3932 "Create", nullptr, 'c', ApplicationDelegate::eMenuID_TargetCreate))); 3933 target_menu_sp->AddSubmenu(MenuSP(new Menu( 3934 "Delete", nullptr, 'd', ApplicationDelegate::eMenuID_TargetDelete))); 3935 3936 MenuSP process_menu_sp(new Menu("Process", "F3", KEY_F(3), 3937 ApplicationDelegate::eMenuID_Process)); 3938 process_menu_sp->AddSubmenu(MenuSP(new Menu( 3939 "Attach", nullptr, 'a', ApplicationDelegate::eMenuID_ProcessAttach))); 3940 process_menu_sp->AddSubmenu(MenuSP(new Menu( 3941 "Detach", nullptr, 'd', ApplicationDelegate::eMenuID_ProcessDetach))); 3942 process_menu_sp->AddSubmenu(MenuSP(new Menu( 3943 "Launch", nullptr, 'l', ApplicationDelegate::eMenuID_ProcessLaunch))); 3944 process_menu_sp->AddSubmenu(MenuSP(new Menu(Menu::Type::Separator))); 3945 process_menu_sp->AddSubmenu( 3946 MenuSP(new Menu("Continue", nullptr, 'c', 3947 ApplicationDelegate::eMenuID_ProcessContinue))); 3948 process_menu_sp->AddSubmenu(MenuSP(new Menu( 3949 "Halt", nullptr, 'h', ApplicationDelegate::eMenuID_ProcessHalt))); 3950 process_menu_sp->AddSubmenu(MenuSP(new Menu( 3951 "Kill", nullptr, 'k', ApplicationDelegate::eMenuID_ProcessKill))); 3952 3953 MenuSP thread_menu_sp(new Menu("Thread", "F4", KEY_F(4), 3954 ApplicationDelegate::eMenuID_Thread)); 3955 thread_menu_sp->AddSubmenu(MenuSP(new Menu( 3956 "Step In", nullptr, 'i', ApplicationDelegate::eMenuID_ThreadStepIn))); 3957 thread_menu_sp->AddSubmenu( 3958 MenuSP(new Menu("Step Over", nullptr, 'v', 3959 ApplicationDelegate::eMenuID_ThreadStepOver))); 3960 thread_menu_sp->AddSubmenu(MenuSP(new Menu( 3961 "Step Out", nullptr, 'o', ApplicationDelegate::eMenuID_ThreadStepOut))); 3962 3963 MenuSP view_menu_sp( 3964 new Menu("View", "F5", KEY_F(5), ApplicationDelegate::eMenuID_View)); 3965 view_menu_sp->AddSubmenu( 3966 MenuSP(new Menu("Backtrace", nullptr, 'b', 3967 ApplicationDelegate::eMenuID_ViewBacktrace))); 3968 view_menu_sp->AddSubmenu( 3969 MenuSP(new Menu("Registers", nullptr, 'r', 3970 ApplicationDelegate::eMenuID_ViewRegisters))); 3971 view_menu_sp->AddSubmenu(MenuSP(new Menu( 3972 "Source", nullptr, 's', ApplicationDelegate::eMenuID_ViewSource))); 3973 view_menu_sp->AddSubmenu( 3974 MenuSP(new Menu("Variables", nullptr, 'v', 3975 ApplicationDelegate::eMenuID_ViewVariables))); 3976 3977 MenuSP help_menu_sp( 3978 new Menu("Help", "F6", KEY_F(6), ApplicationDelegate::eMenuID_Help)); 3979 help_menu_sp->AddSubmenu(MenuSP(new Menu( 3980 "GUI Help", nullptr, 'g', ApplicationDelegate::eMenuID_HelpGUIHelp))); 3981 3982 m_app_ap->Initialize(); 3983 WindowSP &main_window_sp = m_app_ap->GetMainWindow(); 3984 3985 MenuSP menubar_sp(new Menu(Menu::Type::Bar)); 3986 menubar_sp->AddSubmenu(lldb_menu_sp); 3987 menubar_sp->AddSubmenu(target_menu_sp); 3988 menubar_sp->AddSubmenu(process_menu_sp); 3989 menubar_sp->AddSubmenu(thread_menu_sp); 3990 menubar_sp->AddSubmenu(view_menu_sp); 3991 menubar_sp->AddSubmenu(help_menu_sp); 3992 menubar_sp->SetDelegate(app_menu_delegate_sp); 3993 3994 Rect content_bounds = main_window_sp->GetFrame(); 3995 Rect menubar_bounds = content_bounds.MakeMenuBar(); 3996 Rect status_bounds = content_bounds.MakeStatusBar(); 3997 Rect source_bounds; 3998 Rect variables_bounds; 3999 Rect threads_bounds; 4000 Rect source_variables_bounds; 4001 content_bounds.VerticalSplitPercentage(0.80, source_variables_bounds, 4002 threads_bounds); 4003 source_variables_bounds.HorizontalSplitPercentage(0.70, source_bounds, 4004 variables_bounds); 4005 4006 WindowSP menubar_window_sp = 4007 main_window_sp->CreateSubWindow("Menubar", menubar_bounds, false); 4008 // Let the menubar get keys if the active window doesn't handle the keys 4009 // that are typed so it can respond to menubar key presses. 4010 menubar_window_sp->SetCanBeActive( 4011 false); // Don't let the menubar become the active window 4012 menubar_window_sp->SetDelegate(menubar_sp); 4013 4014 WindowSP source_window_sp( 4015 main_window_sp->CreateSubWindow("Source", source_bounds, true)); 4016 WindowSP variables_window_sp( 4017 main_window_sp->CreateSubWindow("Variables", variables_bounds, false)); 4018 WindowSP threads_window_sp( 4019 main_window_sp->CreateSubWindow("Threads", threads_bounds, false)); 4020 WindowSP status_window_sp( 4021 main_window_sp->CreateSubWindow("Status", status_bounds, false)); 4022 status_window_sp->SetCanBeActive( 4023 false); // Don't let the status bar become the active window 4024 main_window_sp->SetDelegate( 4025 std::static_pointer_cast<WindowDelegate>(app_delegate_sp)); 4026 source_window_sp->SetDelegate( 4027 WindowDelegateSP(new SourceFileWindowDelegate(m_debugger))); 4028 variables_window_sp->SetDelegate( 4029 WindowDelegateSP(new FrameVariablesWindowDelegate(m_debugger))); 4030 TreeDelegateSP thread_delegate_sp(new ThreadsTreeDelegate(m_debugger)); 4031 threads_window_sp->SetDelegate(WindowDelegateSP( 4032 new TreeWindowDelegate(m_debugger, thread_delegate_sp))); 4033 status_window_sp->SetDelegate( 4034 WindowDelegateSP(new StatusBarWindowDelegate(m_debugger))); 4035 4036 // Show the main help window once the first time the curses GUI is launched 4037 static bool g_showed_help = false; 4038 if (!g_showed_help) { 4039 g_showed_help = true; 4040 main_window_sp->CreateHelpSubwindow(); 4041 } 4042 4043 init_pair(1, COLOR_WHITE, COLOR_BLUE); 4044 init_pair(2, COLOR_BLACK, COLOR_WHITE); 4045 init_pair(3, COLOR_MAGENTA, COLOR_WHITE); 4046 init_pair(4, COLOR_MAGENTA, COLOR_BLACK); 4047 init_pair(5, COLOR_RED, COLOR_BLACK); 4048 } 4049 } 4050 4051 void IOHandlerCursesGUI::Deactivate() { m_app_ap->Terminate(); } 4052 4053 void IOHandlerCursesGUI::Run() { 4054 m_app_ap->Run(m_debugger); 4055 SetIsDone(true); 4056 } 4057 4058 IOHandlerCursesGUI::~IOHandlerCursesGUI() = default; 4059 4060 void IOHandlerCursesGUI::Cancel() {} 4061 4062 bool IOHandlerCursesGUI::Interrupt() { return false; } 4063 4064 void IOHandlerCursesGUI::GotEOF() {} 4065 4066 #endif // LLDB_ENABLE_CURSES 4067