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_ProcessDetachResume, 2833 eMenuID_ProcessDetachSuspended, 2834 eMenuID_ProcessLaunch, 2835 eMenuID_ProcessContinue, 2836 eMenuID_ProcessHalt, 2837 eMenuID_ProcessKill, 2838 2839 eMenuID_Thread, 2840 eMenuID_ThreadStepIn, 2841 eMenuID_ThreadStepOver, 2842 eMenuID_ThreadStepOut, 2843 2844 eMenuID_View, 2845 eMenuID_ViewBacktrace, 2846 eMenuID_ViewRegisters, 2847 eMenuID_ViewSource, 2848 eMenuID_ViewVariables, 2849 2850 eMenuID_Help, 2851 eMenuID_HelpGUIHelp 2852 }; 2853 2854 ApplicationDelegate(Application &app, Debugger &debugger) 2855 : WindowDelegate(), MenuDelegate(), m_app(app), m_debugger(debugger) {} 2856 2857 ~ApplicationDelegate() override = default; 2858 2859 bool WindowDelegateDraw(Window &window, bool force) override { 2860 return false; // Drawing not handled, let standard window drawing happen 2861 } 2862 2863 HandleCharResult WindowDelegateHandleChar(Window &window, int key) override { 2864 switch (key) { 2865 case '\t': 2866 window.SelectNextWindowAsActive(); 2867 return eKeyHandled; 2868 2869 case 'h': 2870 window.CreateHelpSubwindow(); 2871 return eKeyHandled; 2872 2873 case KEY_ESCAPE: 2874 return eQuitApplication; 2875 2876 default: 2877 break; 2878 } 2879 return eKeyNotHandled; 2880 } 2881 2882 const char *WindowDelegateGetHelpText() override { 2883 return "Welcome to the LLDB curses GUI.\n\n" 2884 "Press the TAB key to change the selected view.\n" 2885 "Each view has its own keyboard shortcuts, press 'h' to open a " 2886 "dialog to display them.\n\n" 2887 "Common key bindings for all views:"; 2888 } 2889 2890 KeyHelp *WindowDelegateGetKeyHelp() override { 2891 static curses::KeyHelp g_source_view_key_help[] = { 2892 {'\t', "Select next view"}, 2893 {'h', "Show help dialog with view specific key bindings"}, 2894 {',', "Page up"}, 2895 {'.', "Page down"}, 2896 {KEY_UP, "Select previous"}, 2897 {KEY_DOWN, "Select next"}, 2898 {KEY_LEFT, "Unexpand or select parent"}, 2899 {KEY_RIGHT, "Expand"}, 2900 {KEY_PPAGE, "Page up"}, 2901 {KEY_NPAGE, "Page down"}, 2902 {'\0', nullptr}}; 2903 return g_source_view_key_help; 2904 } 2905 2906 MenuActionResult MenuDelegateAction(Menu &menu) override { 2907 switch (menu.GetIdentifier()) { 2908 case eMenuID_ThreadStepIn: { 2909 ExecutionContext exe_ctx = 2910 m_debugger.GetCommandInterpreter().GetExecutionContext(); 2911 if (exe_ctx.HasThreadScope()) { 2912 Process *process = exe_ctx.GetProcessPtr(); 2913 if (process && process->IsAlive() && 2914 StateIsStoppedState(process->GetState(), true)) 2915 exe_ctx.GetThreadRef().StepIn(true); 2916 } 2917 } 2918 return MenuActionResult::Handled; 2919 2920 case eMenuID_ThreadStepOut: { 2921 ExecutionContext exe_ctx = 2922 m_debugger.GetCommandInterpreter().GetExecutionContext(); 2923 if (exe_ctx.HasThreadScope()) { 2924 Process *process = exe_ctx.GetProcessPtr(); 2925 if (process && process->IsAlive() && 2926 StateIsStoppedState(process->GetState(), true)) 2927 exe_ctx.GetThreadRef().StepOut(); 2928 } 2929 } 2930 return MenuActionResult::Handled; 2931 2932 case eMenuID_ThreadStepOver: { 2933 ExecutionContext exe_ctx = 2934 m_debugger.GetCommandInterpreter().GetExecutionContext(); 2935 if (exe_ctx.HasThreadScope()) { 2936 Process *process = exe_ctx.GetProcessPtr(); 2937 if (process && process->IsAlive() && 2938 StateIsStoppedState(process->GetState(), true)) 2939 exe_ctx.GetThreadRef().StepOver(true); 2940 } 2941 } 2942 return MenuActionResult::Handled; 2943 2944 case eMenuID_ProcessContinue: { 2945 ExecutionContext exe_ctx = 2946 m_debugger.GetCommandInterpreter().GetExecutionContext(); 2947 if (exe_ctx.HasProcessScope()) { 2948 Process *process = exe_ctx.GetProcessPtr(); 2949 if (process && process->IsAlive() && 2950 StateIsStoppedState(process->GetState(), true)) 2951 process->Resume(); 2952 } 2953 } 2954 return MenuActionResult::Handled; 2955 2956 case eMenuID_ProcessKill: { 2957 ExecutionContext exe_ctx = 2958 m_debugger.GetCommandInterpreter().GetExecutionContext(); 2959 if (exe_ctx.HasProcessScope()) { 2960 Process *process = exe_ctx.GetProcessPtr(); 2961 if (process && process->IsAlive()) 2962 process->Destroy(false); 2963 } 2964 } 2965 return MenuActionResult::Handled; 2966 2967 case eMenuID_ProcessHalt: { 2968 ExecutionContext exe_ctx = 2969 m_debugger.GetCommandInterpreter().GetExecutionContext(); 2970 if (exe_ctx.HasProcessScope()) { 2971 Process *process = exe_ctx.GetProcessPtr(); 2972 if (process && process->IsAlive()) 2973 process->Halt(); 2974 } 2975 } 2976 return MenuActionResult::Handled; 2977 2978 case eMenuID_ProcessDetachResume: 2979 case eMenuID_ProcessDetachSuspended: { 2980 ExecutionContext exe_ctx = 2981 m_debugger.GetCommandInterpreter().GetExecutionContext(); 2982 if (exe_ctx.HasProcessScope()) { 2983 Process *process = exe_ctx.GetProcessPtr(); 2984 if (process && process->IsAlive()) 2985 process->Detach(menu.GetIdentifier() == 2986 eMenuID_ProcessDetachSuspended); 2987 } 2988 } 2989 return MenuActionResult::Handled; 2990 2991 case eMenuID_Process: { 2992 // Populate the menu with all of the threads if the process is stopped 2993 // when the Process menu gets selected and is about to display its 2994 // submenu. 2995 Menus &submenus = menu.GetSubmenus(); 2996 ExecutionContext exe_ctx = 2997 m_debugger.GetCommandInterpreter().GetExecutionContext(); 2998 Process *process = exe_ctx.GetProcessPtr(); 2999 if (process && process->IsAlive() && 3000 StateIsStoppedState(process->GetState(), true)) { 3001 if (submenus.size() == 7) 3002 menu.AddSubmenu(MenuSP(new Menu(Menu::Type::Separator))); 3003 else if (submenus.size() > 8) 3004 submenus.erase(submenus.begin() + 8, submenus.end()); 3005 3006 ThreadList &threads = process->GetThreadList(); 3007 std::lock_guard<std::recursive_mutex> guard(threads.GetMutex()); 3008 size_t num_threads = threads.GetSize(); 3009 for (size_t i = 0; i < num_threads; ++i) { 3010 ThreadSP thread_sp = threads.GetThreadAtIndex(i); 3011 char menu_char = '\0'; 3012 if (i < 9) 3013 menu_char = '1' + i; 3014 StreamString thread_menu_title; 3015 thread_menu_title.Printf("Thread %u", thread_sp->GetIndexID()); 3016 const char *thread_name = thread_sp->GetName(); 3017 if (thread_name && thread_name[0]) 3018 thread_menu_title.Printf(" %s", thread_name); 3019 else { 3020 const char *queue_name = thread_sp->GetQueueName(); 3021 if (queue_name && queue_name[0]) 3022 thread_menu_title.Printf(" %s", queue_name); 3023 } 3024 menu.AddSubmenu( 3025 MenuSP(new Menu(thread_menu_title.GetString().str().c_str(), 3026 nullptr, menu_char, thread_sp->GetID()))); 3027 } 3028 } else if (submenus.size() > 7) { 3029 // Remove the separator and any other thread submenu items that were 3030 // previously added 3031 submenus.erase(submenus.begin() + 7, submenus.end()); 3032 } 3033 // Since we are adding and removing items we need to recalculate the name 3034 // lengths 3035 menu.RecalculateNameLengths(); 3036 } 3037 return MenuActionResult::Handled; 3038 3039 case eMenuID_ViewVariables: { 3040 WindowSP main_window_sp = m_app.GetMainWindow(); 3041 WindowSP source_window_sp = main_window_sp->FindSubWindow("Source"); 3042 WindowSP variables_window_sp = main_window_sp->FindSubWindow("Variables"); 3043 WindowSP registers_window_sp = main_window_sp->FindSubWindow("Registers"); 3044 const Rect source_bounds = source_window_sp->GetBounds(); 3045 3046 if (variables_window_sp) { 3047 const Rect variables_bounds = variables_window_sp->GetBounds(); 3048 3049 main_window_sp->RemoveSubWindow(variables_window_sp.get()); 3050 3051 if (registers_window_sp) { 3052 // We have a registers window, so give all the area back to the 3053 // registers window 3054 Rect registers_bounds = variables_bounds; 3055 registers_bounds.size.width = source_bounds.size.width; 3056 registers_window_sp->SetBounds(registers_bounds); 3057 } else { 3058 // We have no registers window showing so give the bottom area back 3059 // to the source view 3060 source_window_sp->Resize(source_bounds.size.width, 3061 source_bounds.size.height + 3062 variables_bounds.size.height); 3063 } 3064 } else { 3065 Rect new_variables_rect; 3066 if (registers_window_sp) { 3067 // We have a registers window so split the area of the registers 3068 // window into two columns where the left hand side will be the 3069 // variables and the right hand side will be the registers 3070 const Rect variables_bounds = registers_window_sp->GetBounds(); 3071 Rect new_registers_rect; 3072 variables_bounds.VerticalSplitPercentage(0.50, new_variables_rect, 3073 new_registers_rect); 3074 registers_window_sp->SetBounds(new_registers_rect); 3075 } else { 3076 // No variables window, grab the bottom part of the source window 3077 Rect new_source_rect; 3078 source_bounds.HorizontalSplitPercentage(0.70, new_source_rect, 3079 new_variables_rect); 3080 source_window_sp->SetBounds(new_source_rect); 3081 } 3082 WindowSP new_window_sp = main_window_sp->CreateSubWindow( 3083 "Variables", new_variables_rect, false); 3084 new_window_sp->SetDelegate( 3085 WindowDelegateSP(new FrameVariablesWindowDelegate(m_debugger))); 3086 } 3087 touchwin(stdscr); 3088 } 3089 return MenuActionResult::Handled; 3090 3091 case eMenuID_ViewRegisters: { 3092 WindowSP main_window_sp = m_app.GetMainWindow(); 3093 WindowSP source_window_sp = main_window_sp->FindSubWindow("Source"); 3094 WindowSP variables_window_sp = main_window_sp->FindSubWindow("Variables"); 3095 WindowSP registers_window_sp = main_window_sp->FindSubWindow("Registers"); 3096 const Rect source_bounds = source_window_sp->GetBounds(); 3097 3098 if (registers_window_sp) { 3099 if (variables_window_sp) { 3100 const Rect variables_bounds = variables_window_sp->GetBounds(); 3101 3102 // We have a variables window, so give all the area back to the 3103 // variables window 3104 variables_window_sp->Resize(variables_bounds.size.width + 3105 registers_window_sp->GetWidth(), 3106 variables_bounds.size.height); 3107 } else { 3108 // We have no variables window showing so give the bottom area back 3109 // to the source view 3110 source_window_sp->Resize(source_bounds.size.width, 3111 source_bounds.size.height + 3112 registers_window_sp->GetHeight()); 3113 } 3114 main_window_sp->RemoveSubWindow(registers_window_sp.get()); 3115 } else { 3116 Rect new_regs_rect; 3117 if (variables_window_sp) { 3118 // We have a variables window, split it into two columns where the 3119 // left hand side will be the variables and the right hand side will 3120 // be the registers 3121 const Rect variables_bounds = variables_window_sp->GetBounds(); 3122 Rect new_vars_rect; 3123 variables_bounds.VerticalSplitPercentage(0.50, new_vars_rect, 3124 new_regs_rect); 3125 variables_window_sp->SetBounds(new_vars_rect); 3126 } else { 3127 // No registers window, grab the bottom part of the source window 3128 Rect new_source_rect; 3129 source_bounds.HorizontalSplitPercentage(0.70, new_source_rect, 3130 new_regs_rect); 3131 source_window_sp->SetBounds(new_source_rect); 3132 } 3133 WindowSP new_window_sp = 3134 main_window_sp->CreateSubWindow("Registers", new_regs_rect, false); 3135 new_window_sp->SetDelegate( 3136 WindowDelegateSP(new RegistersWindowDelegate(m_debugger))); 3137 } 3138 touchwin(stdscr); 3139 } 3140 return MenuActionResult::Handled; 3141 3142 case eMenuID_HelpGUIHelp: 3143 m_app.GetMainWindow()->CreateHelpSubwindow(); 3144 return MenuActionResult::Handled; 3145 3146 default: 3147 break; 3148 } 3149 3150 return MenuActionResult::NotHandled; 3151 } 3152 3153 protected: 3154 Application &m_app; 3155 Debugger &m_debugger; 3156 }; 3157 3158 class StatusBarWindowDelegate : public WindowDelegate { 3159 public: 3160 StatusBarWindowDelegate(Debugger &debugger) : m_debugger(debugger) { 3161 FormatEntity::Parse("Thread: ${thread.id%tid}", m_format); 3162 } 3163 3164 ~StatusBarWindowDelegate() override = default; 3165 3166 bool WindowDelegateDraw(Window &window, bool force) override { 3167 ExecutionContext exe_ctx = 3168 m_debugger.GetCommandInterpreter().GetExecutionContext(); 3169 Process *process = exe_ctx.GetProcessPtr(); 3170 Thread *thread = exe_ctx.GetThreadPtr(); 3171 StackFrame *frame = exe_ctx.GetFramePtr(); 3172 window.Erase(); 3173 window.SetBackground(2); 3174 window.MoveCursor(0, 0); 3175 if (process) { 3176 const StateType state = process->GetState(); 3177 window.Printf("Process: %5" PRIu64 " %10s", process->GetID(), 3178 StateAsCString(state)); 3179 3180 if (StateIsStoppedState(state, true)) { 3181 StreamString strm; 3182 if (thread && FormatEntity::Format(m_format, strm, nullptr, &exe_ctx, 3183 nullptr, nullptr, false, false)) { 3184 window.MoveCursor(40, 0); 3185 window.PutCStringTruncated(strm.GetString().str().c_str(), 1); 3186 } 3187 3188 window.MoveCursor(60, 0); 3189 if (frame) 3190 window.Printf("Frame: %3u PC = 0x%16.16" PRIx64, 3191 frame->GetFrameIndex(), 3192 frame->GetFrameCodeAddress().GetOpcodeLoadAddress( 3193 exe_ctx.GetTargetPtr())); 3194 } else if (state == eStateExited) { 3195 const char *exit_desc = process->GetExitDescription(); 3196 const int exit_status = process->GetExitStatus(); 3197 if (exit_desc && exit_desc[0]) 3198 window.Printf(" with status = %i (%s)", exit_status, exit_desc); 3199 else 3200 window.Printf(" with status = %i", exit_status); 3201 } 3202 } 3203 return true; 3204 } 3205 3206 protected: 3207 Debugger &m_debugger; 3208 FormatEntity::Entry m_format; 3209 }; 3210 3211 class SourceFileWindowDelegate : public WindowDelegate { 3212 public: 3213 SourceFileWindowDelegate(Debugger &debugger) 3214 : WindowDelegate(), m_debugger(debugger), m_sc(), m_file_sp(), 3215 m_disassembly_scope(nullptr), m_disassembly_sp(), m_disassembly_range(), 3216 m_title(), m_line_width(4), m_selected_line(0), m_pc_line(0), 3217 m_stop_id(0), m_frame_idx(UINT32_MAX), m_first_visible_line(0), 3218 m_min_x(0), m_min_y(0), m_max_x(0), m_max_y(0) {} 3219 3220 ~SourceFileWindowDelegate() override = default; 3221 3222 void Update(const SymbolContext &sc) { m_sc = sc; } 3223 3224 uint32_t NumVisibleLines() const { return m_max_y - m_min_y; } 3225 3226 const char *WindowDelegateGetHelpText() override { 3227 return "Source/Disassembly window keyboard shortcuts:"; 3228 } 3229 3230 KeyHelp *WindowDelegateGetKeyHelp() override { 3231 static curses::KeyHelp g_source_view_key_help[] = { 3232 {KEY_RETURN, "Run to selected line with one shot breakpoint"}, 3233 {KEY_UP, "Select previous source line"}, 3234 {KEY_DOWN, "Select next source line"}, 3235 {KEY_PPAGE, "Page up"}, 3236 {KEY_NPAGE, "Page down"}, 3237 {'b', "Set breakpoint on selected source/disassembly line"}, 3238 {'c', "Continue process"}, 3239 {'D', "Detach with process suspended"}, 3240 {'h', "Show help dialog"}, 3241 {'n', "Step over (source line)"}, 3242 {'N', "Step over (single instruction)"}, 3243 {'f', "Step out (finish)"}, 3244 {'s', "Step in (source line)"}, 3245 {'S', "Step in (single instruction)"}, 3246 {'u', "Frame up"}, 3247 {'d', "Frame down"}, 3248 {',', "Page up"}, 3249 {'.', "Page down"}, 3250 {'\0', nullptr}}; 3251 return g_source_view_key_help; 3252 } 3253 3254 bool WindowDelegateDraw(Window &window, bool force) override { 3255 ExecutionContext exe_ctx = 3256 m_debugger.GetCommandInterpreter().GetExecutionContext(); 3257 Process *process = exe_ctx.GetProcessPtr(); 3258 Thread *thread = nullptr; 3259 3260 bool update_location = false; 3261 if (process) { 3262 StateType state = process->GetState(); 3263 if (StateIsStoppedState(state, true)) { 3264 // We are stopped, so it is ok to 3265 update_location = true; 3266 } 3267 } 3268 3269 m_min_x = 1; 3270 m_min_y = 2; 3271 m_max_x = window.GetMaxX() - 1; 3272 m_max_y = window.GetMaxY() - 1; 3273 3274 const uint32_t num_visible_lines = NumVisibleLines(); 3275 StackFrameSP frame_sp; 3276 bool set_selected_line_to_pc = false; 3277 3278 if (update_location) { 3279 const bool process_alive = process ? process->IsAlive() : false; 3280 bool thread_changed = false; 3281 if (process_alive) { 3282 thread = exe_ctx.GetThreadPtr(); 3283 if (thread) { 3284 frame_sp = thread->GetSelectedFrame(); 3285 auto tid = thread->GetID(); 3286 thread_changed = tid != m_tid; 3287 m_tid = tid; 3288 } else { 3289 if (m_tid != LLDB_INVALID_THREAD_ID) { 3290 thread_changed = true; 3291 m_tid = LLDB_INVALID_THREAD_ID; 3292 } 3293 } 3294 } 3295 const uint32_t stop_id = process ? process->GetStopID() : 0; 3296 const bool stop_id_changed = stop_id != m_stop_id; 3297 bool frame_changed = false; 3298 m_stop_id = stop_id; 3299 m_title.Clear(); 3300 if (frame_sp) { 3301 m_sc = frame_sp->GetSymbolContext(eSymbolContextEverything); 3302 if (m_sc.module_sp) { 3303 m_title.Printf( 3304 "%s", m_sc.module_sp->GetFileSpec().GetFilename().GetCString()); 3305 ConstString func_name = m_sc.GetFunctionName(); 3306 if (func_name) 3307 m_title.Printf("`%s", func_name.GetCString()); 3308 } 3309 const uint32_t frame_idx = frame_sp->GetFrameIndex(); 3310 frame_changed = frame_idx != m_frame_idx; 3311 m_frame_idx = frame_idx; 3312 } else { 3313 m_sc.Clear(true); 3314 frame_changed = m_frame_idx != UINT32_MAX; 3315 m_frame_idx = UINT32_MAX; 3316 } 3317 3318 const bool context_changed = 3319 thread_changed || frame_changed || stop_id_changed; 3320 3321 if (process_alive) { 3322 if (m_sc.line_entry.IsValid()) { 3323 m_pc_line = m_sc.line_entry.line; 3324 if (m_pc_line != UINT32_MAX) 3325 --m_pc_line; // Convert to zero based line number... 3326 // Update the selected line if the stop ID changed... 3327 if (context_changed) 3328 m_selected_line = m_pc_line; 3329 3330 if (m_file_sp && m_file_sp->GetFileSpec() == m_sc.line_entry.file) { 3331 // Same file, nothing to do, we should either have the lines or not 3332 // (source file missing) 3333 if (m_selected_line >= static_cast<size_t>(m_first_visible_line)) { 3334 if (m_selected_line >= m_first_visible_line + num_visible_lines) 3335 m_first_visible_line = m_selected_line - 10; 3336 } else { 3337 if (m_selected_line > 10) 3338 m_first_visible_line = m_selected_line - 10; 3339 else 3340 m_first_visible_line = 0; 3341 } 3342 } else { 3343 // File changed, set selected line to the line with the PC 3344 m_selected_line = m_pc_line; 3345 m_file_sp = 3346 m_debugger.GetSourceManager().GetFile(m_sc.line_entry.file); 3347 if (m_file_sp) { 3348 const size_t num_lines = m_file_sp->GetNumLines(); 3349 m_line_width = 1; 3350 for (size_t n = num_lines; n >= 10; n = n / 10) 3351 ++m_line_width; 3352 3353 if (num_lines < num_visible_lines || 3354 m_selected_line < num_visible_lines) 3355 m_first_visible_line = 0; 3356 else 3357 m_first_visible_line = m_selected_line - 10; 3358 } 3359 } 3360 } else { 3361 m_file_sp.reset(); 3362 } 3363 3364 if (!m_file_sp || m_file_sp->GetNumLines() == 0) { 3365 // Show disassembly 3366 bool prefer_file_cache = false; 3367 if (m_sc.function) { 3368 if (m_disassembly_scope != m_sc.function) { 3369 m_disassembly_scope = m_sc.function; 3370 m_disassembly_sp = m_sc.function->GetInstructions( 3371 exe_ctx, nullptr, prefer_file_cache); 3372 if (m_disassembly_sp) { 3373 set_selected_line_to_pc = true; 3374 m_disassembly_range = m_sc.function->GetAddressRange(); 3375 } else { 3376 m_disassembly_range.Clear(); 3377 } 3378 } else { 3379 set_selected_line_to_pc = context_changed; 3380 } 3381 } else if (m_sc.symbol) { 3382 if (m_disassembly_scope != m_sc.symbol) { 3383 m_disassembly_scope = m_sc.symbol; 3384 m_disassembly_sp = m_sc.symbol->GetInstructions( 3385 exe_ctx, nullptr, prefer_file_cache); 3386 if (m_disassembly_sp) { 3387 set_selected_line_to_pc = true; 3388 m_disassembly_range.GetBaseAddress() = 3389 m_sc.symbol->GetAddress(); 3390 m_disassembly_range.SetByteSize(m_sc.symbol->GetByteSize()); 3391 } else { 3392 m_disassembly_range.Clear(); 3393 } 3394 } else { 3395 set_selected_line_to_pc = context_changed; 3396 } 3397 } 3398 } 3399 } else { 3400 m_pc_line = UINT32_MAX; 3401 } 3402 } 3403 3404 const int window_width = window.GetWidth(); 3405 window.Erase(); 3406 window.DrawTitleBox("Sources"); 3407 if (!m_title.GetString().empty()) { 3408 window.AttributeOn(A_REVERSE); 3409 window.MoveCursor(1, 1); 3410 window.PutChar(' '); 3411 window.PutCStringTruncated(m_title.GetString().str().c_str(), 1); 3412 int x = window.GetCursorX(); 3413 if (x < window_width - 1) { 3414 window.Printf("%*s", window_width - x - 1, ""); 3415 } 3416 window.AttributeOff(A_REVERSE); 3417 } 3418 3419 Target *target = exe_ctx.GetTargetPtr(); 3420 const size_t num_source_lines = GetNumSourceLines(); 3421 if (num_source_lines > 0) { 3422 // Display source 3423 BreakpointLines bp_lines; 3424 if (target) { 3425 BreakpointList &bp_list = target->GetBreakpointList(); 3426 const size_t num_bps = bp_list.GetSize(); 3427 for (size_t bp_idx = 0; bp_idx < num_bps; ++bp_idx) { 3428 BreakpointSP bp_sp = bp_list.GetBreakpointAtIndex(bp_idx); 3429 const size_t num_bps_locs = bp_sp->GetNumLocations(); 3430 for (size_t bp_loc_idx = 0; bp_loc_idx < num_bps_locs; ++bp_loc_idx) { 3431 BreakpointLocationSP bp_loc_sp = 3432 bp_sp->GetLocationAtIndex(bp_loc_idx); 3433 LineEntry bp_loc_line_entry; 3434 if (bp_loc_sp->GetAddress().CalculateSymbolContextLineEntry( 3435 bp_loc_line_entry)) { 3436 if (m_file_sp->GetFileSpec() == bp_loc_line_entry.file) { 3437 bp_lines.insert(bp_loc_line_entry.line); 3438 } 3439 } 3440 } 3441 } 3442 } 3443 3444 const attr_t selected_highlight_attr = A_REVERSE; 3445 const attr_t pc_highlight_attr = COLOR_PAIR(1); 3446 3447 for (size_t i = 0; i < num_visible_lines; ++i) { 3448 const uint32_t curr_line = m_first_visible_line + i; 3449 if (curr_line < num_source_lines) { 3450 const int line_y = m_min_y + i; 3451 window.MoveCursor(1, line_y); 3452 const bool is_pc_line = curr_line == m_pc_line; 3453 const bool line_is_selected = m_selected_line == curr_line; 3454 // Highlight the line as the PC line first, then if the selected line 3455 // isn't the same as the PC line, highlight it differently 3456 attr_t highlight_attr = 0; 3457 attr_t bp_attr = 0; 3458 if (is_pc_line) 3459 highlight_attr = pc_highlight_attr; 3460 else if (line_is_selected) 3461 highlight_attr = selected_highlight_attr; 3462 3463 if (bp_lines.find(curr_line + 1) != bp_lines.end()) 3464 bp_attr = COLOR_PAIR(2); 3465 3466 if (bp_attr) 3467 window.AttributeOn(bp_attr); 3468 3469 window.Printf(" %*u ", m_line_width, curr_line + 1); 3470 3471 if (bp_attr) 3472 window.AttributeOff(bp_attr); 3473 3474 window.PutChar(ACS_VLINE); 3475 // Mark the line with the PC with a diamond 3476 if (is_pc_line) 3477 window.PutChar(ACS_DIAMOND); 3478 else 3479 window.PutChar(' '); 3480 3481 if (highlight_attr) 3482 window.AttributeOn(highlight_attr); 3483 const uint32_t line_len = 3484 m_file_sp->GetLineLength(curr_line + 1, false); 3485 if (line_len > 0) 3486 window.PutCString(m_file_sp->PeekLineData(curr_line + 1), line_len); 3487 3488 if (is_pc_line && frame_sp && 3489 frame_sp->GetConcreteFrameIndex() == 0) { 3490 StopInfoSP stop_info_sp; 3491 if (thread) 3492 stop_info_sp = thread->GetStopInfo(); 3493 if (stop_info_sp) { 3494 const char *stop_description = stop_info_sp->GetDescription(); 3495 if (stop_description && stop_description[0]) { 3496 size_t stop_description_len = strlen(stop_description); 3497 int desc_x = window_width - stop_description_len - 16; 3498 window.Printf("%*s", desc_x - window.GetCursorX(), ""); 3499 // window.MoveCursor(window_width - stop_description_len - 15, 3500 // line_y); 3501 window.Printf("<<< Thread %u: %s ", thread->GetIndexID(), 3502 stop_description); 3503 } 3504 } else { 3505 window.Printf("%*s", window_width - window.GetCursorX() - 1, ""); 3506 } 3507 } 3508 if (highlight_attr) 3509 window.AttributeOff(highlight_attr); 3510 } else { 3511 break; 3512 } 3513 } 3514 } else { 3515 size_t num_disassembly_lines = GetNumDisassemblyLines(); 3516 if (num_disassembly_lines > 0) { 3517 // Display disassembly 3518 BreakpointAddrs bp_file_addrs; 3519 Target *target = exe_ctx.GetTargetPtr(); 3520 if (target) { 3521 BreakpointList &bp_list = target->GetBreakpointList(); 3522 const size_t num_bps = bp_list.GetSize(); 3523 for (size_t bp_idx = 0; bp_idx < num_bps; ++bp_idx) { 3524 BreakpointSP bp_sp = bp_list.GetBreakpointAtIndex(bp_idx); 3525 const size_t num_bps_locs = bp_sp->GetNumLocations(); 3526 for (size_t bp_loc_idx = 0; bp_loc_idx < num_bps_locs; 3527 ++bp_loc_idx) { 3528 BreakpointLocationSP bp_loc_sp = 3529 bp_sp->GetLocationAtIndex(bp_loc_idx); 3530 LineEntry bp_loc_line_entry; 3531 const lldb::addr_t file_addr = 3532 bp_loc_sp->GetAddress().GetFileAddress(); 3533 if (file_addr != LLDB_INVALID_ADDRESS) { 3534 if (m_disassembly_range.ContainsFileAddress(file_addr)) 3535 bp_file_addrs.insert(file_addr); 3536 } 3537 } 3538 } 3539 } 3540 3541 const attr_t selected_highlight_attr = A_REVERSE; 3542 const attr_t pc_highlight_attr = COLOR_PAIR(1); 3543 3544 StreamString strm; 3545 3546 InstructionList &insts = m_disassembly_sp->GetInstructionList(); 3547 Address pc_address; 3548 3549 if (frame_sp) 3550 pc_address = frame_sp->GetFrameCodeAddress(); 3551 const uint32_t pc_idx = 3552 pc_address.IsValid() 3553 ? insts.GetIndexOfInstructionAtAddress(pc_address) 3554 : UINT32_MAX; 3555 if (set_selected_line_to_pc) { 3556 m_selected_line = pc_idx; 3557 } 3558 3559 const uint32_t non_visible_pc_offset = (num_visible_lines / 5); 3560 if (static_cast<size_t>(m_first_visible_line) >= num_disassembly_lines) 3561 m_first_visible_line = 0; 3562 3563 if (pc_idx < num_disassembly_lines) { 3564 if (pc_idx < static_cast<uint32_t>(m_first_visible_line) || 3565 pc_idx >= m_first_visible_line + num_visible_lines) 3566 m_first_visible_line = pc_idx - non_visible_pc_offset; 3567 } 3568 3569 for (size_t i = 0; i < num_visible_lines; ++i) { 3570 const uint32_t inst_idx = m_first_visible_line + i; 3571 Instruction *inst = insts.GetInstructionAtIndex(inst_idx).get(); 3572 if (!inst) 3573 break; 3574 3575 const int line_y = m_min_y + i; 3576 window.MoveCursor(1, line_y); 3577 const bool is_pc_line = frame_sp && inst_idx == pc_idx; 3578 const bool line_is_selected = m_selected_line == inst_idx; 3579 // Highlight the line as the PC line first, then if the selected line 3580 // isn't the same as the PC line, highlight it differently 3581 attr_t highlight_attr = 0; 3582 attr_t bp_attr = 0; 3583 if (is_pc_line) 3584 highlight_attr = pc_highlight_attr; 3585 else if (line_is_selected) 3586 highlight_attr = selected_highlight_attr; 3587 3588 if (bp_file_addrs.find(inst->GetAddress().GetFileAddress()) != 3589 bp_file_addrs.end()) 3590 bp_attr = COLOR_PAIR(2); 3591 3592 if (bp_attr) 3593 window.AttributeOn(bp_attr); 3594 3595 window.Printf(" 0x%16.16llx ", 3596 static_cast<unsigned long long>( 3597 inst->GetAddress().GetLoadAddress(target))); 3598 3599 if (bp_attr) 3600 window.AttributeOff(bp_attr); 3601 3602 window.PutChar(ACS_VLINE); 3603 // Mark the line with the PC with a diamond 3604 if (is_pc_line) 3605 window.PutChar(ACS_DIAMOND); 3606 else 3607 window.PutChar(' '); 3608 3609 if (highlight_attr) 3610 window.AttributeOn(highlight_attr); 3611 3612 const char *mnemonic = inst->GetMnemonic(&exe_ctx); 3613 const char *operands = inst->GetOperands(&exe_ctx); 3614 const char *comment = inst->GetComment(&exe_ctx); 3615 3616 if (mnemonic != nullptr && mnemonic[0] == '\0') 3617 mnemonic = nullptr; 3618 if (operands != nullptr && operands[0] == '\0') 3619 operands = nullptr; 3620 if (comment != nullptr && comment[0] == '\0') 3621 comment = nullptr; 3622 3623 strm.Clear(); 3624 3625 if (mnemonic != nullptr && operands != nullptr && comment != nullptr) 3626 strm.Printf("%-8s %-25s ; %s", mnemonic, operands, comment); 3627 else if (mnemonic != nullptr && operands != nullptr) 3628 strm.Printf("%-8s %s", mnemonic, operands); 3629 else if (mnemonic != nullptr) 3630 strm.Printf("%s", mnemonic); 3631 3632 int right_pad = 1; 3633 window.PutCStringTruncated(strm.GetData(), right_pad); 3634 3635 if (is_pc_line && frame_sp && 3636 frame_sp->GetConcreteFrameIndex() == 0) { 3637 StopInfoSP stop_info_sp; 3638 if (thread) 3639 stop_info_sp = thread->GetStopInfo(); 3640 if (stop_info_sp) { 3641 const char *stop_description = stop_info_sp->GetDescription(); 3642 if (stop_description && stop_description[0]) { 3643 size_t stop_description_len = strlen(stop_description); 3644 int desc_x = window_width - stop_description_len - 16; 3645 window.Printf("%*s", desc_x - window.GetCursorX(), ""); 3646 // window.MoveCursor(window_width - stop_description_len - 15, 3647 // line_y); 3648 window.Printf("<<< Thread %u: %s ", thread->GetIndexID(), 3649 stop_description); 3650 } 3651 } else { 3652 window.Printf("%*s", window_width - window.GetCursorX() - 1, ""); 3653 } 3654 } 3655 if (highlight_attr) 3656 window.AttributeOff(highlight_attr); 3657 } 3658 } 3659 } 3660 return true; // Drawing handled 3661 } 3662 3663 size_t GetNumLines() { 3664 size_t num_lines = GetNumSourceLines(); 3665 if (num_lines == 0) 3666 num_lines = GetNumDisassemblyLines(); 3667 return num_lines; 3668 } 3669 3670 size_t GetNumSourceLines() const { 3671 if (m_file_sp) 3672 return m_file_sp->GetNumLines(); 3673 return 0; 3674 } 3675 3676 size_t GetNumDisassemblyLines() const { 3677 if (m_disassembly_sp) 3678 return m_disassembly_sp->GetInstructionList().GetSize(); 3679 return 0; 3680 } 3681 3682 HandleCharResult WindowDelegateHandleChar(Window &window, int c) override { 3683 const uint32_t num_visible_lines = NumVisibleLines(); 3684 const size_t num_lines = GetNumLines(); 3685 3686 switch (c) { 3687 case ',': 3688 case KEY_PPAGE: 3689 // Page up key 3690 if (static_cast<uint32_t>(m_first_visible_line) > num_visible_lines) 3691 m_first_visible_line -= num_visible_lines; 3692 else 3693 m_first_visible_line = 0; 3694 m_selected_line = m_first_visible_line; 3695 return eKeyHandled; 3696 3697 case '.': 3698 case KEY_NPAGE: 3699 // Page down key 3700 { 3701 if (m_first_visible_line + num_visible_lines < num_lines) 3702 m_first_visible_line += num_visible_lines; 3703 else if (num_lines < num_visible_lines) 3704 m_first_visible_line = 0; 3705 else 3706 m_first_visible_line = num_lines - num_visible_lines; 3707 m_selected_line = m_first_visible_line; 3708 } 3709 return eKeyHandled; 3710 3711 case KEY_UP: 3712 if (m_selected_line > 0) { 3713 m_selected_line--; 3714 if (static_cast<size_t>(m_first_visible_line) > m_selected_line) 3715 m_first_visible_line = m_selected_line; 3716 } 3717 return eKeyHandled; 3718 3719 case KEY_DOWN: 3720 if (m_selected_line + 1 < num_lines) { 3721 m_selected_line++; 3722 if (m_first_visible_line + num_visible_lines < m_selected_line) 3723 m_first_visible_line++; 3724 } 3725 return eKeyHandled; 3726 3727 case '\r': 3728 case '\n': 3729 case KEY_ENTER: 3730 // Set a breakpoint and run to the line using a one shot breakpoint 3731 if (GetNumSourceLines() > 0) { 3732 ExecutionContext exe_ctx = 3733 m_debugger.GetCommandInterpreter().GetExecutionContext(); 3734 if (exe_ctx.HasProcessScope() && exe_ctx.GetProcessRef().IsAlive()) { 3735 BreakpointSP bp_sp = exe_ctx.GetTargetRef().CreateBreakpoint( 3736 nullptr, // Don't limit the breakpoint to certain modules 3737 m_file_sp->GetFileSpec(), // Source file 3738 m_selected_line + 3739 1, // Source line number (m_selected_line is zero based) 3740 0, // Unspecified column. 3741 0, // No offset 3742 eLazyBoolCalculate, // Check inlines using global setting 3743 eLazyBoolCalculate, // Skip prologue using global setting, 3744 false, // internal 3745 false, // request_hardware 3746 eLazyBoolCalculate); // move_to_nearest_code 3747 // Make breakpoint one shot 3748 bp_sp->GetOptions()->SetOneShot(true); 3749 exe_ctx.GetProcessRef().Resume(); 3750 } 3751 } else if (m_selected_line < GetNumDisassemblyLines()) { 3752 const Instruction *inst = m_disassembly_sp->GetInstructionList() 3753 .GetInstructionAtIndex(m_selected_line) 3754 .get(); 3755 ExecutionContext exe_ctx = 3756 m_debugger.GetCommandInterpreter().GetExecutionContext(); 3757 if (exe_ctx.HasTargetScope()) { 3758 Address addr = inst->GetAddress(); 3759 BreakpointSP bp_sp = exe_ctx.GetTargetRef().CreateBreakpoint( 3760 addr, // lldb_private::Address 3761 false, // internal 3762 false); // request_hardware 3763 // Make breakpoint one shot 3764 bp_sp->GetOptions()->SetOneShot(true); 3765 exe_ctx.GetProcessRef().Resume(); 3766 } 3767 } 3768 return eKeyHandled; 3769 3770 case 'b': // 'b' == toggle breakpoint on currently selected line 3771 if (m_selected_line < GetNumSourceLines()) { 3772 ExecutionContext exe_ctx = 3773 m_debugger.GetCommandInterpreter().GetExecutionContext(); 3774 if (exe_ctx.HasTargetScope()) { 3775 BreakpointSP bp_sp = exe_ctx.GetTargetRef().CreateBreakpoint( 3776 nullptr, // Don't limit the breakpoint to certain modules 3777 m_file_sp->GetFileSpec(), // Source file 3778 m_selected_line + 3779 1, // Source line number (m_selected_line is zero based) 3780 0, // No column specified. 3781 0, // No offset 3782 eLazyBoolCalculate, // Check inlines using global setting 3783 eLazyBoolCalculate, // Skip prologue using global setting, 3784 false, // internal 3785 false, // request_hardware 3786 eLazyBoolCalculate); // move_to_nearest_code 3787 } 3788 } else if (m_selected_line < GetNumDisassemblyLines()) { 3789 const Instruction *inst = m_disassembly_sp->GetInstructionList() 3790 .GetInstructionAtIndex(m_selected_line) 3791 .get(); 3792 ExecutionContext exe_ctx = 3793 m_debugger.GetCommandInterpreter().GetExecutionContext(); 3794 if (exe_ctx.HasTargetScope()) { 3795 Address addr = inst->GetAddress(); 3796 BreakpointSP bp_sp = exe_ctx.GetTargetRef().CreateBreakpoint( 3797 addr, // lldb_private::Address 3798 false, // internal 3799 false); // request_hardware 3800 } 3801 } 3802 return eKeyHandled; 3803 3804 case 'D': // 'D' == detach and keep stopped 3805 { 3806 ExecutionContext exe_ctx = 3807 m_debugger.GetCommandInterpreter().GetExecutionContext(); 3808 if (exe_ctx.HasProcessScope()) 3809 exe_ctx.GetProcessRef().Detach(true); 3810 } 3811 return eKeyHandled; 3812 3813 case 'c': 3814 // 'c' == continue 3815 { 3816 ExecutionContext exe_ctx = 3817 m_debugger.GetCommandInterpreter().GetExecutionContext(); 3818 if (exe_ctx.HasProcessScope()) 3819 exe_ctx.GetProcessRef().Resume(); 3820 } 3821 return eKeyHandled; 3822 3823 case 'f': 3824 // 'f' == step out (finish) 3825 { 3826 ExecutionContext exe_ctx = 3827 m_debugger.GetCommandInterpreter().GetExecutionContext(); 3828 if (exe_ctx.HasThreadScope() && 3829 StateIsStoppedState(exe_ctx.GetProcessRef().GetState(), true)) { 3830 exe_ctx.GetThreadRef().StepOut(); 3831 } 3832 } 3833 return eKeyHandled; 3834 3835 case 'n': // 'n' == step over 3836 case 'N': // 'N' == step over instruction 3837 { 3838 ExecutionContext exe_ctx = 3839 m_debugger.GetCommandInterpreter().GetExecutionContext(); 3840 if (exe_ctx.HasThreadScope() && 3841 StateIsStoppedState(exe_ctx.GetProcessRef().GetState(), true)) { 3842 bool source_step = (c == 'n'); 3843 exe_ctx.GetThreadRef().StepOver(source_step); 3844 } 3845 } 3846 return eKeyHandled; 3847 3848 case 's': // 's' == step into 3849 case 'S': // 'S' == step into instruction 3850 { 3851 ExecutionContext exe_ctx = 3852 m_debugger.GetCommandInterpreter().GetExecutionContext(); 3853 if (exe_ctx.HasThreadScope() && 3854 StateIsStoppedState(exe_ctx.GetProcessRef().GetState(), true)) { 3855 bool source_step = (c == 's'); 3856 exe_ctx.GetThreadRef().StepIn(source_step); 3857 } 3858 } 3859 return eKeyHandled; 3860 3861 case 'u': // 'u' == frame up 3862 case 'd': // 'd' == frame down 3863 { 3864 ExecutionContext exe_ctx = 3865 m_debugger.GetCommandInterpreter().GetExecutionContext(); 3866 if (exe_ctx.HasThreadScope()) { 3867 Thread *thread = exe_ctx.GetThreadPtr(); 3868 uint32_t frame_idx = thread->GetSelectedFrameIndex(); 3869 if (frame_idx == UINT32_MAX) 3870 frame_idx = 0; 3871 if (c == 'u' && frame_idx + 1 < thread->GetStackFrameCount()) 3872 ++frame_idx; 3873 else if (c == 'd' && frame_idx > 0) 3874 --frame_idx; 3875 if (thread->SetSelectedFrameByIndex(frame_idx, true)) 3876 exe_ctx.SetFrameSP(thread->GetSelectedFrame()); 3877 } 3878 } 3879 return eKeyHandled; 3880 3881 case 'h': 3882 window.CreateHelpSubwindow(); 3883 return eKeyHandled; 3884 3885 default: 3886 break; 3887 } 3888 return eKeyNotHandled; 3889 } 3890 3891 protected: 3892 typedef std::set<uint32_t> BreakpointLines; 3893 typedef std::set<lldb::addr_t> BreakpointAddrs; 3894 3895 Debugger &m_debugger; 3896 SymbolContext m_sc; 3897 SourceManager::FileSP m_file_sp; 3898 SymbolContextScope *m_disassembly_scope; 3899 lldb::DisassemblerSP m_disassembly_sp; 3900 AddressRange m_disassembly_range; 3901 StreamString m_title; 3902 lldb::user_id_t m_tid; 3903 int m_line_width; 3904 uint32_t m_selected_line; // The selected line 3905 uint32_t m_pc_line; // The line with the PC 3906 uint32_t m_stop_id; 3907 uint32_t m_frame_idx; 3908 int m_first_visible_line; 3909 int m_min_x; 3910 int m_min_y; 3911 int m_max_x; 3912 int m_max_y; 3913 }; 3914 3915 DisplayOptions ValueObjectListDelegate::g_options = {true}; 3916 3917 IOHandlerCursesGUI::IOHandlerCursesGUI(Debugger &debugger) 3918 : IOHandler(debugger, IOHandler::Type::Curses) {} 3919 3920 void IOHandlerCursesGUI::Activate() { 3921 IOHandler::Activate(); 3922 if (!m_app_ap) { 3923 m_app_ap = std::make_unique<Application>(GetInputFILE(), GetOutputFILE()); 3924 3925 // This is both a window and a menu delegate 3926 std::shared_ptr<ApplicationDelegate> app_delegate_sp( 3927 new ApplicationDelegate(*m_app_ap, m_debugger)); 3928 3929 MenuDelegateSP app_menu_delegate_sp = 3930 std::static_pointer_cast<MenuDelegate>(app_delegate_sp); 3931 MenuSP lldb_menu_sp( 3932 new Menu("LLDB", "F1", KEY_F(1), ApplicationDelegate::eMenuID_LLDB)); 3933 MenuSP exit_menuitem_sp( 3934 new Menu("Exit", nullptr, 'x', ApplicationDelegate::eMenuID_LLDBExit)); 3935 exit_menuitem_sp->SetCannedResult(MenuActionResult::Quit); 3936 lldb_menu_sp->AddSubmenu(MenuSP(new Menu( 3937 "About LLDB", nullptr, 'a', ApplicationDelegate::eMenuID_LLDBAbout))); 3938 lldb_menu_sp->AddSubmenu(MenuSP(new Menu(Menu::Type::Separator))); 3939 lldb_menu_sp->AddSubmenu(exit_menuitem_sp); 3940 3941 MenuSP target_menu_sp(new Menu("Target", "F2", KEY_F(2), 3942 ApplicationDelegate::eMenuID_Target)); 3943 target_menu_sp->AddSubmenu(MenuSP(new Menu( 3944 "Create", nullptr, 'c', ApplicationDelegate::eMenuID_TargetCreate))); 3945 target_menu_sp->AddSubmenu(MenuSP(new Menu( 3946 "Delete", nullptr, 'd', ApplicationDelegate::eMenuID_TargetDelete))); 3947 3948 MenuSP process_menu_sp(new Menu("Process", "F3", KEY_F(3), 3949 ApplicationDelegate::eMenuID_Process)); 3950 process_menu_sp->AddSubmenu(MenuSP(new Menu( 3951 "Attach", nullptr, 'a', ApplicationDelegate::eMenuID_ProcessAttach))); 3952 process_menu_sp->AddSubmenu( 3953 MenuSP(new Menu("Detach and resume", nullptr, 'd', 3954 ApplicationDelegate::eMenuID_ProcessDetachResume))); 3955 process_menu_sp->AddSubmenu( 3956 MenuSP(new Menu("Detach suspended", nullptr, 's', 3957 ApplicationDelegate::eMenuID_ProcessDetachSuspended))); 3958 process_menu_sp->AddSubmenu(MenuSP(new Menu( 3959 "Launch", nullptr, 'l', ApplicationDelegate::eMenuID_ProcessLaunch))); 3960 process_menu_sp->AddSubmenu(MenuSP(new Menu(Menu::Type::Separator))); 3961 process_menu_sp->AddSubmenu( 3962 MenuSP(new Menu("Continue", nullptr, 'c', 3963 ApplicationDelegate::eMenuID_ProcessContinue))); 3964 process_menu_sp->AddSubmenu(MenuSP(new Menu( 3965 "Halt", nullptr, 'h', ApplicationDelegate::eMenuID_ProcessHalt))); 3966 process_menu_sp->AddSubmenu(MenuSP(new Menu( 3967 "Kill", nullptr, 'k', ApplicationDelegate::eMenuID_ProcessKill))); 3968 3969 MenuSP thread_menu_sp(new Menu("Thread", "F4", KEY_F(4), 3970 ApplicationDelegate::eMenuID_Thread)); 3971 thread_menu_sp->AddSubmenu(MenuSP(new Menu( 3972 "Step In", nullptr, 'i', ApplicationDelegate::eMenuID_ThreadStepIn))); 3973 thread_menu_sp->AddSubmenu( 3974 MenuSP(new Menu("Step Over", nullptr, 'v', 3975 ApplicationDelegate::eMenuID_ThreadStepOver))); 3976 thread_menu_sp->AddSubmenu(MenuSP(new Menu( 3977 "Step Out", nullptr, 'o', ApplicationDelegate::eMenuID_ThreadStepOut))); 3978 3979 MenuSP view_menu_sp( 3980 new Menu("View", "F5", KEY_F(5), ApplicationDelegate::eMenuID_View)); 3981 view_menu_sp->AddSubmenu( 3982 MenuSP(new Menu("Backtrace", nullptr, 'b', 3983 ApplicationDelegate::eMenuID_ViewBacktrace))); 3984 view_menu_sp->AddSubmenu( 3985 MenuSP(new Menu("Registers", nullptr, 'r', 3986 ApplicationDelegate::eMenuID_ViewRegisters))); 3987 view_menu_sp->AddSubmenu(MenuSP(new Menu( 3988 "Source", nullptr, 's', ApplicationDelegate::eMenuID_ViewSource))); 3989 view_menu_sp->AddSubmenu( 3990 MenuSP(new Menu("Variables", nullptr, 'v', 3991 ApplicationDelegate::eMenuID_ViewVariables))); 3992 3993 MenuSP help_menu_sp( 3994 new Menu("Help", "F6", KEY_F(6), ApplicationDelegate::eMenuID_Help)); 3995 help_menu_sp->AddSubmenu(MenuSP(new Menu( 3996 "GUI Help", nullptr, 'g', ApplicationDelegate::eMenuID_HelpGUIHelp))); 3997 3998 m_app_ap->Initialize(); 3999 WindowSP &main_window_sp = m_app_ap->GetMainWindow(); 4000 4001 MenuSP menubar_sp(new Menu(Menu::Type::Bar)); 4002 menubar_sp->AddSubmenu(lldb_menu_sp); 4003 menubar_sp->AddSubmenu(target_menu_sp); 4004 menubar_sp->AddSubmenu(process_menu_sp); 4005 menubar_sp->AddSubmenu(thread_menu_sp); 4006 menubar_sp->AddSubmenu(view_menu_sp); 4007 menubar_sp->AddSubmenu(help_menu_sp); 4008 menubar_sp->SetDelegate(app_menu_delegate_sp); 4009 4010 Rect content_bounds = main_window_sp->GetFrame(); 4011 Rect menubar_bounds = content_bounds.MakeMenuBar(); 4012 Rect status_bounds = content_bounds.MakeStatusBar(); 4013 Rect source_bounds; 4014 Rect variables_bounds; 4015 Rect threads_bounds; 4016 Rect source_variables_bounds; 4017 content_bounds.VerticalSplitPercentage(0.80, source_variables_bounds, 4018 threads_bounds); 4019 source_variables_bounds.HorizontalSplitPercentage(0.70, source_bounds, 4020 variables_bounds); 4021 4022 WindowSP menubar_window_sp = 4023 main_window_sp->CreateSubWindow("Menubar", menubar_bounds, false); 4024 // Let the menubar get keys if the active window doesn't handle the keys 4025 // that are typed so it can respond to menubar key presses. 4026 menubar_window_sp->SetCanBeActive( 4027 false); // Don't let the menubar become the active window 4028 menubar_window_sp->SetDelegate(menubar_sp); 4029 4030 WindowSP source_window_sp( 4031 main_window_sp->CreateSubWindow("Source", source_bounds, true)); 4032 WindowSP variables_window_sp( 4033 main_window_sp->CreateSubWindow("Variables", variables_bounds, false)); 4034 WindowSP threads_window_sp( 4035 main_window_sp->CreateSubWindow("Threads", threads_bounds, false)); 4036 WindowSP status_window_sp( 4037 main_window_sp->CreateSubWindow("Status", status_bounds, false)); 4038 status_window_sp->SetCanBeActive( 4039 false); // Don't let the status bar become the active window 4040 main_window_sp->SetDelegate( 4041 std::static_pointer_cast<WindowDelegate>(app_delegate_sp)); 4042 source_window_sp->SetDelegate( 4043 WindowDelegateSP(new SourceFileWindowDelegate(m_debugger))); 4044 variables_window_sp->SetDelegate( 4045 WindowDelegateSP(new FrameVariablesWindowDelegate(m_debugger))); 4046 TreeDelegateSP thread_delegate_sp(new ThreadsTreeDelegate(m_debugger)); 4047 threads_window_sp->SetDelegate(WindowDelegateSP( 4048 new TreeWindowDelegate(m_debugger, thread_delegate_sp))); 4049 status_window_sp->SetDelegate( 4050 WindowDelegateSP(new StatusBarWindowDelegate(m_debugger))); 4051 4052 // Show the main help window once the first time the curses GUI is launched 4053 static bool g_showed_help = false; 4054 if (!g_showed_help) { 4055 g_showed_help = true; 4056 main_window_sp->CreateHelpSubwindow(); 4057 } 4058 4059 init_pair(1, COLOR_WHITE, COLOR_BLUE); 4060 init_pair(2, COLOR_BLACK, COLOR_WHITE); 4061 init_pair(3, COLOR_MAGENTA, COLOR_WHITE); 4062 init_pair(4, COLOR_MAGENTA, COLOR_BLACK); 4063 init_pair(5, COLOR_RED, COLOR_BLACK); 4064 } 4065 } 4066 4067 void IOHandlerCursesGUI::Deactivate() { m_app_ap->Terminate(); } 4068 4069 void IOHandlerCursesGUI::Run() { 4070 m_app_ap->Run(m_debugger); 4071 SetIsDone(true); 4072 } 4073 4074 IOHandlerCursesGUI::~IOHandlerCursesGUI() = default; 4075 4076 void IOHandlerCursesGUI::Cancel() {} 4077 4078 bool IOHandlerCursesGUI::Interrupt() { return false; } 4079 4080 void IOHandlerCursesGUI::GotEOF() {} 4081 4082 #endif // LLDB_ENABLE_CURSES 4083