1 //===-- IOHandler.cpp -------------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 11 #include "lldb/lldb-python.h" 12 13 #include <string> 14 15 #include "lldb/Breakpoint/BreakpointLocation.h" 16 #include "lldb/Core/IOHandler.h" 17 #include "lldb/Core/Debugger.h" 18 #include "lldb/Core/Module.h" 19 #include "lldb/Core/State.h" 20 #include "lldb/Core/StreamFile.h" 21 #include "lldb/Core/ValueObjectRegister.h" 22 #ifndef LLDB_DISABLE_LIBEDIT 23 #include "lldb/Host/Editline.h" 24 #endif 25 #include "lldb/Interpreter/CommandCompletions.h" 26 #include "lldb/Interpreter/CommandInterpreter.h" 27 #include "lldb/Symbol/Block.h" 28 #include "lldb/Symbol/Function.h" 29 #include "lldb/Symbol/Symbol.h" 30 #include "lldb/Target/RegisterContext.h" 31 #include "lldb/Target/ThreadPlan.h" 32 33 #ifndef LLDB_DISABLE_CURSES 34 #include <ncurses.h> 35 #include <panel.h> 36 #endif 37 38 using namespace lldb; 39 using namespace lldb_private; 40 41 IOHandler::IOHandler (Debugger &debugger, IOHandler::Type type) : 42 IOHandler (debugger, 43 type, 44 StreamFileSP(), // Adopt STDIN from top input reader 45 StreamFileSP(), // Adopt STDOUT from top input reader 46 StreamFileSP(), // Adopt STDERR from top input reader 47 0) // Flags 48 { 49 } 50 51 52 IOHandler::IOHandler (Debugger &debugger, 53 IOHandler::Type type, 54 const lldb::StreamFileSP &input_sp, 55 const lldb::StreamFileSP &output_sp, 56 const lldb::StreamFileSP &error_sp, 57 uint32_t flags) : 58 m_debugger (debugger), 59 m_input_sp (input_sp), 60 m_output_sp (output_sp), 61 m_error_sp (error_sp), 62 m_popped (false), 63 m_flags (flags), 64 m_type (type), 65 m_user_data (NULL), 66 m_done (false), 67 m_active (false) 68 { 69 // If any files are not specified, then adopt them from the top input reader. 70 if (!m_input_sp || !m_output_sp || !m_error_sp) 71 debugger.AdoptTopIOHandlerFilesIfInvalid (m_input_sp, 72 m_output_sp, 73 m_error_sp); 74 } 75 76 IOHandler::~IOHandler() 77 { 78 } 79 80 81 int 82 IOHandler::GetInputFD() 83 { 84 if (m_input_sp) 85 return m_input_sp->GetFile().GetDescriptor(); 86 return -1; 87 } 88 89 int 90 IOHandler::GetOutputFD() 91 { 92 if (m_output_sp) 93 return m_output_sp->GetFile().GetDescriptor(); 94 return -1; 95 } 96 97 int 98 IOHandler::GetErrorFD() 99 { 100 if (m_error_sp) 101 return m_error_sp->GetFile().GetDescriptor(); 102 return -1; 103 } 104 105 FILE * 106 IOHandler::GetInputFILE() 107 { 108 if (m_input_sp) 109 return m_input_sp->GetFile().GetStream(); 110 return NULL; 111 } 112 113 FILE * 114 IOHandler::GetOutputFILE() 115 { 116 if (m_output_sp) 117 return m_output_sp->GetFile().GetStream(); 118 return NULL; 119 } 120 121 FILE * 122 IOHandler::GetErrorFILE() 123 { 124 if (m_error_sp) 125 return m_error_sp->GetFile().GetStream(); 126 return NULL; 127 } 128 129 StreamFileSP & 130 IOHandler::GetInputStreamFile() 131 { 132 return m_input_sp; 133 } 134 135 StreamFileSP & 136 IOHandler::GetOutputStreamFile() 137 { 138 return m_output_sp; 139 } 140 141 142 StreamFileSP & 143 IOHandler::GetErrorStreamFile() 144 { 145 return m_error_sp; 146 } 147 148 bool 149 IOHandler::GetIsInteractive () 150 { 151 return GetInputStreamFile()->GetFile().GetIsInteractive (); 152 } 153 154 bool 155 IOHandler::GetIsRealTerminal () 156 { 157 return GetInputStreamFile()->GetFile().GetIsRealTerminal(); 158 } 159 160 void 161 IOHandler::SetPopped (bool b) 162 { 163 m_popped.SetValue(b, eBroadcastOnChange); 164 } 165 166 void 167 IOHandler::WaitForPop () 168 { 169 m_popped.WaitForValueEqualTo(true); 170 } 171 172 IOHandlerConfirm::IOHandlerConfirm (Debugger &debugger, 173 const char *prompt, 174 bool default_response) : 175 IOHandlerEditline(debugger, 176 IOHandler::Type::Confirm, 177 NULL, // NULL editline_name means no history loaded/saved 178 NULL, // No prompt 179 NULL, // No continuation prompt 180 false, // Multi-line 181 false, // Don't colorize the prompt (i.e. the confirm message.) 182 0, 183 *this), 184 m_default_response (default_response), 185 m_user_response (default_response) 186 { 187 StreamString prompt_stream; 188 prompt_stream.PutCString(prompt); 189 if (m_default_response) 190 prompt_stream.Printf(": [Y/n] "); 191 else 192 prompt_stream.Printf(": [y/N] "); 193 194 SetPrompt (prompt_stream.GetString().c_str()); 195 196 } 197 198 199 IOHandlerConfirm::~IOHandlerConfirm () 200 { 201 } 202 203 int 204 IOHandlerConfirm::IOHandlerComplete (IOHandler &io_handler, 205 const char *current_line, 206 const char *cursor, 207 const char *last_char, 208 int skip_first_n_matches, 209 int max_matches, 210 StringList &matches) 211 { 212 if (current_line == cursor) 213 { 214 if (m_default_response) 215 { 216 matches.AppendString("y"); 217 } 218 else 219 { 220 matches.AppendString("n"); 221 } 222 } 223 return matches.GetSize(); 224 } 225 226 void 227 IOHandlerConfirm::IOHandlerInputComplete (IOHandler &io_handler, std::string &line) 228 { 229 if (line.empty()) 230 { 231 // User just hit enter, set the response to the default 232 m_user_response = m_default_response; 233 io_handler.SetIsDone(true); 234 return; 235 } 236 237 if (line.size() == 1) 238 { 239 switch (line[0]) 240 { 241 case 'y': 242 case 'Y': 243 m_user_response = true; 244 io_handler.SetIsDone(true); 245 return; 246 case 'n': 247 case 'N': 248 m_user_response = false; 249 io_handler.SetIsDone(true); 250 return; 251 default: 252 break; 253 } 254 } 255 256 if (line == "yes" || line == "YES" || line == "Yes") 257 { 258 m_user_response = true; 259 io_handler.SetIsDone(true); 260 } 261 else if (line == "no" || line == "NO" || line == "No") 262 { 263 m_user_response = false; 264 io_handler.SetIsDone(true); 265 } 266 } 267 268 int 269 IOHandlerDelegate::IOHandlerComplete (IOHandler &io_handler, 270 const char *current_line, 271 const char *cursor, 272 const char *last_char, 273 int skip_first_n_matches, 274 int max_matches, 275 StringList &matches) 276 { 277 switch (m_completion) 278 { 279 case Completion::None: 280 break; 281 282 case Completion::LLDBCommand: 283 return io_handler.GetDebugger().GetCommandInterpreter().HandleCompletion (current_line, 284 cursor, 285 last_char, 286 skip_first_n_matches, 287 max_matches, 288 matches); 289 290 case Completion::Expression: 291 { 292 bool word_complete = false; 293 const char *word_start = cursor; 294 if (cursor > current_line) 295 --word_start; 296 while (word_start > current_line && !isspace(*word_start)) 297 --word_start; 298 CommandCompletions::InvokeCommonCompletionCallbacks (io_handler.GetDebugger().GetCommandInterpreter(), 299 CommandCompletions::eVariablePathCompletion, 300 word_start, 301 skip_first_n_matches, 302 max_matches, 303 NULL, 304 word_complete, 305 matches); 306 307 size_t num_matches = matches.GetSize(); 308 if (num_matches > 0) 309 { 310 std::string common_prefix; 311 matches.LongestCommonPrefix (common_prefix); 312 const size_t partial_name_len = strlen(word_start); 313 314 // If we matched a unique single command, add a space... 315 // Only do this if the completer told us this was a complete word, however... 316 if (num_matches == 1 && word_complete) 317 { 318 common_prefix.push_back(' '); 319 } 320 common_prefix.erase (0, partial_name_len); 321 matches.InsertStringAtIndex(0, std::move(common_prefix)); 322 } 323 return num_matches; 324 } 325 break; 326 } 327 328 329 return 0; 330 } 331 332 333 IOHandlerEditline::IOHandlerEditline (Debugger &debugger, 334 IOHandler::Type type, 335 const char *editline_name, // Used for saving history files 336 const char *prompt, 337 const char *continuation_prompt, 338 bool multi_line, 339 bool color_prompts, 340 uint32_t line_number_start, 341 IOHandlerDelegate &delegate) : 342 IOHandlerEditline(debugger, 343 type, 344 StreamFileSP(), // Inherit input from top input reader 345 StreamFileSP(), // Inherit output from top input reader 346 StreamFileSP(), // Inherit error from top input reader 347 0, // Flags 348 editline_name, // Used for saving history files 349 prompt, 350 continuation_prompt, 351 multi_line, 352 color_prompts, 353 line_number_start, 354 delegate) 355 { 356 } 357 358 IOHandlerEditline::IOHandlerEditline (Debugger &debugger, 359 IOHandler::Type type, 360 const lldb::StreamFileSP &input_sp, 361 const lldb::StreamFileSP &output_sp, 362 const lldb::StreamFileSP &error_sp, 363 uint32_t flags, 364 const char *editline_name, // Used for saving history files 365 const char *prompt, 366 const char *continuation_prompt, 367 bool multi_line, 368 bool color_prompts, 369 uint32_t line_number_start, 370 IOHandlerDelegate &delegate) : 371 IOHandler (debugger, type, input_sp, output_sp, error_sp, flags), 372 #ifndef LLDB_DISABLE_LIBEDIT 373 m_editline_ap (), 374 #endif 375 m_delegate (delegate), 376 m_prompt (), 377 m_continuation_prompt(), 378 m_current_lines_ptr (NULL), 379 m_base_line_number (line_number_start), 380 m_curr_line_idx (UINT32_MAX), 381 m_multi_line (multi_line), 382 m_color_prompts (color_prompts), 383 m_interrupt_exits (true) 384 { 385 SetPrompt(prompt); 386 387 #ifndef LLDB_DISABLE_LIBEDIT 388 bool use_editline = false; 389 390 use_editline = m_input_sp->GetFile().GetIsRealTerminal(); 391 392 if (use_editline) 393 { 394 m_editline_ap.reset(new Editline (editline_name, 395 GetInputFILE (), 396 GetOutputFILE (), 397 GetErrorFILE (), 398 m_color_prompts)); 399 m_editline_ap->SetIsInputCompleteCallback (IsInputCompleteCallback, this); 400 m_editline_ap->SetAutoCompleteCallback (AutoCompleteCallback, this); 401 // See if the delegate supports fixing indentation 402 const char *indent_chars = delegate.IOHandlerGetFixIndentationCharacters(); 403 if (indent_chars) 404 { 405 // The delegate does support indentation, hook it up so when any indentation 406 // character is typed, the delegate gets a chance to fix it 407 m_editline_ap->SetFixIndentationCallback (FixIndentationCallback, this, indent_chars); 408 } 409 } 410 #endif 411 SetBaseLineNumber (m_base_line_number); 412 SetPrompt(prompt ? prompt : ""); 413 SetContinuationPrompt(continuation_prompt); 414 } 415 416 IOHandlerEditline::~IOHandlerEditline () 417 { 418 #ifndef LLDB_DISABLE_LIBEDIT 419 m_editline_ap.reset(); 420 #endif 421 } 422 423 void 424 IOHandlerEditline::Activate () 425 { 426 IOHandler::Activate(); 427 m_delegate.IOHandlerActivated(*this); 428 } 429 430 void 431 IOHandlerEditline::Deactivate () 432 { 433 IOHandler::Deactivate(); 434 m_delegate.IOHandlerDeactivated(*this); 435 } 436 437 438 bool 439 IOHandlerEditline::GetLine (std::string &line, bool &interrupted) 440 { 441 #ifndef LLDB_DISABLE_LIBEDIT 442 if (m_editline_ap) 443 { 444 return m_editline_ap->GetLine (line, interrupted); 445 } 446 else 447 { 448 #endif 449 line.clear(); 450 451 FILE *in = GetInputFILE(); 452 if (in) 453 { 454 if (GetIsInteractive()) 455 { 456 const char *prompt = NULL; 457 458 if (m_multi_line && m_curr_line_idx > 0) 459 prompt = GetContinuationPrompt(); 460 461 if (prompt == NULL) 462 prompt = GetPrompt(); 463 464 if (prompt && prompt[0]) 465 { 466 FILE *out = GetOutputFILE(); 467 if (out) 468 { 469 ::fprintf(out, "%s", prompt); 470 ::fflush(out); 471 } 472 } 473 } 474 char buffer[256]; 475 bool done = false; 476 bool got_line = false; 477 while (!done) 478 { 479 if (fgets(buffer, sizeof(buffer), in) == NULL) 480 { 481 const int saved_errno = errno; 482 if (feof(in)) 483 done = true; 484 else if (ferror(in)) 485 { 486 if (saved_errno != EINTR) 487 done = true; 488 } 489 } 490 else 491 { 492 got_line = true; 493 size_t buffer_len = strlen(buffer); 494 assert (buffer[buffer_len] == '\0'); 495 char last_char = buffer[buffer_len-1]; 496 if (last_char == '\r' || last_char == '\n') 497 { 498 done = true; 499 // Strip trailing newlines 500 while (last_char == '\r' || last_char == '\n') 501 { 502 --buffer_len; 503 if (buffer_len == 0) 504 break; 505 last_char = buffer[buffer_len-1]; 506 } 507 } 508 line.append(buffer, buffer_len); 509 } 510 } 511 // We might have gotten a newline on a line by itself 512 // make sure to return true in this case. 513 return got_line; 514 } 515 else 516 { 517 // No more input file, we are done... 518 SetIsDone(true); 519 } 520 return false; 521 #ifndef LLDB_DISABLE_LIBEDIT 522 } 523 #endif 524 } 525 526 527 #ifndef LLDB_DISABLE_LIBEDIT 528 bool 529 IOHandlerEditline::IsInputCompleteCallback (Editline *editline, 530 StringList &lines, 531 void *baton) 532 { 533 IOHandlerEditline *editline_reader = (IOHandlerEditline *) baton; 534 return editline_reader->m_delegate.IOHandlerIsInputComplete(*editline_reader, lines); 535 } 536 537 int 538 IOHandlerEditline::FixIndentationCallback (Editline *editline, 539 const StringList &lines, 540 int cursor_position, 541 void *baton) 542 { 543 IOHandlerEditline *editline_reader = (IOHandlerEditline *) baton; 544 return editline_reader->m_delegate.IOHandlerFixIndentation(*editline_reader, lines, cursor_position); 545 } 546 547 int 548 IOHandlerEditline::AutoCompleteCallback (const char *current_line, 549 const char *cursor, 550 const char *last_char, 551 int skip_first_n_matches, 552 int max_matches, 553 StringList &matches, 554 void *baton) 555 { 556 IOHandlerEditline *editline_reader = (IOHandlerEditline *) baton; 557 if (editline_reader) 558 return editline_reader->m_delegate.IOHandlerComplete (*editline_reader, 559 current_line, 560 cursor, 561 last_char, 562 skip_first_n_matches, 563 max_matches, 564 matches); 565 return 0; 566 } 567 #endif 568 569 const char * 570 IOHandlerEditline::GetPrompt () 571 { 572 #ifndef LLDB_DISABLE_LIBEDIT 573 if (m_editline_ap) 574 { 575 return m_editline_ap->GetPrompt (); 576 } 577 else 578 { 579 #endif 580 if (m_prompt.empty()) 581 return NULL; 582 #ifndef LLDB_DISABLE_LIBEDIT 583 } 584 #endif 585 return m_prompt.c_str(); 586 } 587 588 bool 589 IOHandlerEditline::SetPrompt (const char *p) 590 { 591 if (p && p[0]) 592 m_prompt = p; 593 else 594 m_prompt.clear(); 595 #ifndef LLDB_DISABLE_LIBEDIT 596 if (m_editline_ap) 597 m_editline_ap->SetPrompt (m_prompt.empty() ? NULL : m_prompt.c_str()); 598 #endif 599 return true; 600 } 601 602 const char * 603 IOHandlerEditline::GetContinuationPrompt () 604 { 605 if (m_continuation_prompt.empty()) 606 return NULL; 607 return m_continuation_prompt.c_str(); 608 } 609 610 611 void 612 IOHandlerEditline::SetContinuationPrompt (const char *p) 613 { 614 if (p && p[0]) 615 m_continuation_prompt = p; 616 else 617 m_continuation_prompt.clear(); 618 619 #ifndef LLDB_DISABLE_LIBEDIT 620 if (m_editline_ap) 621 m_editline_ap->SetContinuationPrompt (m_continuation_prompt.empty() ? NULL : m_continuation_prompt.c_str()); 622 #endif 623 } 624 625 626 void 627 IOHandlerEditline::SetBaseLineNumber (uint32_t line) 628 { 629 m_base_line_number = line; 630 } 631 632 uint32_t 633 IOHandlerEditline::GetCurrentLineIndex () const 634 { 635 #ifndef LLDB_DISABLE_LIBEDIT 636 if (m_editline_ap) 637 return m_editline_ap->GetCurrentLine(); 638 #endif 639 return m_curr_line_idx; 640 } 641 642 bool 643 IOHandlerEditline::GetLines (StringList &lines, bool &interrupted) 644 { 645 m_current_lines_ptr = &lines; 646 647 bool success = false; 648 #ifndef LLDB_DISABLE_LIBEDIT 649 if (m_editline_ap) 650 { 651 return m_editline_ap->GetLines (m_base_line_number, lines, interrupted); 652 } 653 else 654 { 655 #endif 656 bool done = false; 657 Error error; 658 659 while (!done) 660 { 661 // Show line numbers if we are asked to 662 std::string line; 663 if (m_base_line_number > 0 && GetIsInteractive()) 664 { 665 FILE *out = GetOutputFILE(); 666 if (out) 667 ::fprintf(out, "%u%s", m_base_line_number + (uint32_t)lines.GetSize(), GetPrompt() == NULL ? " " : ""); 668 } 669 670 m_curr_line_idx = lines.GetSize(); 671 672 bool interrupted = false; 673 if (GetLine(line, interrupted) && !interrupted) 674 { 675 lines.AppendString(line); 676 done = m_delegate.IOHandlerIsInputComplete(*this, lines); 677 } 678 else 679 { 680 done = true; 681 } 682 } 683 success = lines.GetSize() > 0; 684 #ifndef LLDB_DISABLE_LIBEDIT 685 } 686 #endif 687 return success; 688 } 689 690 // Each IOHandler gets to run until it is done. It should read data 691 // from the "in" and place output into "out" and "err and return 692 // when done. 693 void 694 IOHandlerEditline::Run () 695 { 696 std::string line; 697 while (IsActive()) 698 { 699 bool interrupted = false; 700 if (m_multi_line) 701 { 702 StringList lines; 703 if (GetLines (lines, interrupted)) 704 { 705 if (interrupted) 706 { 707 m_done = m_interrupt_exits; 708 m_delegate.IOHandlerInputInterrupted (*this, line); 709 710 } 711 else 712 { 713 line = lines.CopyList(); 714 m_delegate.IOHandlerInputComplete (*this, line); 715 } 716 } 717 else 718 { 719 m_done = true; 720 } 721 } 722 else 723 { 724 if (GetLine(line, interrupted)) 725 { 726 if (interrupted) 727 m_delegate.IOHandlerInputInterrupted (*this, line); 728 else 729 m_delegate.IOHandlerInputComplete (*this, line); 730 } 731 else 732 { 733 m_done = true; 734 } 735 } 736 } 737 } 738 739 void 740 IOHandlerEditline::Hide () 741 { 742 #ifndef LLDB_DISABLE_LIBEDIT 743 if (m_editline_ap) 744 m_editline_ap->Hide(); 745 #endif 746 } 747 748 749 void 750 IOHandlerEditline::Refresh () 751 { 752 #ifndef LLDB_DISABLE_LIBEDIT 753 if (m_editline_ap) 754 { 755 m_editline_ap->Refresh(); 756 } 757 else 758 { 759 #endif 760 const char *prompt = GetPrompt(); 761 if (prompt && prompt[0]) 762 { 763 FILE *out = GetOutputFILE(); 764 if (out) 765 { 766 ::fprintf(out, "%s", prompt); 767 ::fflush(out); 768 } 769 } 770 #ifndef LLDB_DISABLE_LIBEDIT 771 } 772 #endif 773 } 774 775 void 776 IOHandlerEditline::Cancel () 777 { 778 #ifndef LLDB_DISABLE_LIBEDIT 779 if (m_editline_ap) 780 m_editline_ap->Interrupt (); 781 #endif 782 } 783 784 bool 785 IOHandlerEditline::Interrupt () 786 { 787 // Let the delgate handle it first 788 if (m_delegate.IOHandlerInterrupt(*this)) 789 return true; 790 791 #ifndef LLDB_DISABLE_LIBEDIT 792 if (m_editline_ap) 793 return m_editline_ap->Interrupt(); 794 #endif 795 return false; 796 } 797 798 void 799 IOHandlerEditline::GotEOF() 800 { 801 #ifndef LLDB_DISABLE_LIBEDIT 802 if (m_editline_ap) 803 m_editline_ap->Interrupt(); 804 #endif 805 } 806 807 // we may want curses to be disabled for some builds 808 // for instance, windows 809 #ifndef LLDB_DISABLE_CURSES 810 811 #include "lldb/Core/ValueObject.h" 812 #include "lldb/Symbol/VariableList.h" 813 #include "lldb/Target/Target.h" 814 #include "lldb/Target/Process.h" 815 #include "lldb/Target/Thread.h" 816 #include "lldb/Target/StackFrame.h" 817 818 #define KEY_RETURN 10 819 #define KEY_ESCAPE 27 820 821 namespace curses 822 { 823 class Menu; 824 class MenuDelegate; 825 class Window; 826 class WindowDelegate; 827 typedef std::shared_ptr<Menu> MenuSP; 828 typedef std::shared_ptr<MenuDelegate> MenuDelegateSP; 829 typedef std::shared_ptr<Window> WindowSP; 830 typedef std::shared_ptr<WindowDelegate> WindowDelegateSP; 831 typedef std::vector<MenuSP> Menus; 832 typedef std::vector<WindowSP> Windows; 833 typedef std::vector<WindowDelegateSP> WindowDelegates; 834 835 #if 0 836 type summary add -s "x=${var.x}, y=${var.y}" curses::Point 837 type summary add -s "w=${var.width}, h=${var.height}" curses::Size 838 type summary add -s "${var.origin%S} ${var.size%S}" curses::Rect 839 #endif 840 struct Point 841 { 842 int x; 843 int y; 844 845 Point (int _x = 0, int _y = 0) : 846 x(_x), 847 y(_y) 848 { 849 } 850 851 void 852 Clear () 853 { 854 x = 0; 855 y = 0; 856 } 857 858 Point & 859 operator += (const Point &rhs) 860 { 861 x += rhs.x; 862 y += rhs.y; 863 return *this; 864 } 865 866 void 867 Dump () 868 { 869 printf ("(x=%i, y=%i)\n", x, y); 870 } 871 872 }; 873 874 bool operator == (const Point &lhs, const Point &rhs) 875 { 876 return lhs.x == rhs.x && lhs.y == rhs.y; 877 } 878 bool operator != (const Point &lhs, const Point &rhs) 879 { 880 return lhs.x != rhs.x || lhs.y != rhs.y; 881 } 882 883 struct Size 884 { 885 int width; 886 int height; 887 Size (int w = 0, int h = 0) : 888 width (w), 889 height (h) 890 { 891 } 892 893 void 894 Clear () 895 { 896 width = 0; 897 height = 0; 898 } 899 900 void 901 Dump () 902 { 903 printf ("(w=%i, h=%i)\n", width, height); 904 } 905 906 }; 907 908 bool operator == (const Size &lhs, const Size &rhs) 909 { 910 return lhs.width == rhs.width && lhs.height == rhs.height; 911 } 912 bool operator != (const Size &lhs, const Size &rhs) 913 { 914 return lhs.width != rhs.width || lhs.height != rhs.height; 915 } 916 917 struct Rect 918 { 919 Point origin; 920 Size size; 921 922 Rect () : 923 origin(), 924 size() 925 { 926 } 927 928 Rect (const Point &p, const Size &s) : 929 origin (p), 930 size (s) 931 { 932 } 933 934 void 935 Clear () 936 { 937 origin.Clear(); 938 size.Clear(); 939 } 940 941 void 942 Dump () 943 { 944 printf ("(x=%i, y=%i), w=%i, h=%i)\n", origin.x, origin.y, size.width, size.height); 945 } 946 947 void 948 Inset (int w, int h) 949 { 950 if (size.width > w*2) 951 size.width -= w*2; 952 origin.x += w; 953 954 if (size.height > h*2) 955 size.height -= h*2; 956 origin.y += h; 957 } 958 // Return a status bar rectangle which is the last line of 959 // this rectangle. This rectangle will be modified to not 960 // include the status bar area. 961 Rect 962 MakeStatusBar () 963 { 964 Rect status_bar; 965 if (size.height > 1) 966 { 967 status_bar.origin.x = origin.x; 968 status_bar.origin.y = size.height; 969 status_bar.size.width = size.width; 970 status_bar.size.height = 1; 971 --size.height; 972 } 973 return status_bar; 974 } 975 976 // Return a menubar rectangle which is the first line of 977 // this rectangle. This rectangle will be modified to not 978 // include the menubar area. 979 Rect 980 MakeMenuBar () 981 { 982 Rect menubar; 983 if (size.height > 1) 984 { 985 menubar.origin.x = origin.x; 986 menubar.origin.y = origin.y; 987 menubar.size.width = size.width; 988 menubar.size.height = 1; 989 ++origin.y; 990 --size.height; 991 } 992 return menubar; 993 } 994 995 void 996 HorizontalSplitPercentage (float top_percentage, Rect &top, Rect &bottom) const 997 { 998 float top_height = top_percentage * size.height; 999 HorizontalSplit (top_height, top, bottom); 1000 } 1001 1002 void 1003 HorizontalSplit (int top_height, Rect &top, Rect &bottom) const 1004 { 1005 top = *this; 1006 if (top_height < size.height) 1007 { 1008 top.size.height = top_height; 1009 bottom.origin.x = origin.x; 1010 bottom.origin.y = origin.y + top.size.height; 1011 bottom.size.width = size.width; 1012 bottom.size.height = size.height - top.size.height; 1013 } 1014 else 1015 { 1016 bottom.Clear(); 1017 } 1018 } 1019 1020 void 1021 VerticalSplitPercentage (float left_percentage, Rect &left, Rect &right) const 1022 { 1023 float left_width = left_percentage * size.width; 1024 VerticalSplit (left_width, left, right); 1025 } 1026 1027 1028 void 1029 VerticalSplit (int left_width, Rect &left, Rect &right) const 1030 { 1031 left = *this; 1032 if (left_width < size.width) 1033 { 1034 left.size.width = left_width; 1035 right.origin.x = origin.x + left.size.width; 1036 right.origin.y = origin.y; 1037 right.size.width = size.width - left.size.width; 1038 right.size.height = size.height; 1039 } 1040 else 1041 { 1042 right.Clear(); 1043 } 1044 } 1045 }; 1046 1047 bool operator == (const Rect &lhs, const Rect &rhs) 1048 { 1049 return lhs.origin == rhs.origin && lhs.size == rhs.size; 1050 } 1051 bool operator != (const Rect &lhs, const Rect &rhs) 1052 { 1053 return lhs.origin != rhs.origin || lhs.size != rhs.size; 1054 } 1055 1056 enum HandleCharResult 1057 { 1058 eKeyNotHandled = 0, 1059 eKeyHandled = 1, 1060 eQuitApplication = 2 1061 }; 1062 1063 enum class MenuActionResult 1064 { 1065 Handled, 1066 NotHandled, 1067 Quit // Exit all menus and quit 1068 }; 1069 1070 struct KeyHelp 1071 { 1072 int ch; 1073 const char *description; 1074 }; 1075 1076 class WindowDelegate 1077 { 1078 public: 1079 virtual 1080 ~WindowDelegate() 1081 { 1082 } 1083 1084 virtual bool 1085 WindowDelegateDraw (Window &window, bool force) 1086 { 1087 return false; // Drawing not handled 1088 } 1089 1090 virtual HandleCharResult 1091 WindowDelegateHandleChar (Window &window, int key) 1092 { 1093 return eKeyNotHandled; 1094 } 1095 1096 virtual const char * 1097 WindowDelegateGetHelpText () 1098 { 1099 return NULL; 1100 } 1101 1102 virtual KeyHelp * 1103 WindowDelegateGetKeyHelp () 1104 { 1105 return NULL; 1106 } 1107 }; 1108 1109 class HelpDialogDelegate : 1110 public WindowDelegate 1111 { 1112 public: 1113 HelpDialogDelegate (const char *text, KeyHelp *key_help_array); 1114 1115 virtual 1116 ~HelpDialogDelegate(); 1117 1118 virtual bool 1119 WindowDelegateDraw (Window &window, bool force); 1120 1121 virtual HandleCharResult 1122 WindowDelegateHandleChar (Window &window, int key); 1123 1124 size_t 1125 GetNumLines() const 1126 { 1127 return m_text.GetSize(); 1128 } 1129 1130 size_t 1131 GetMaxLineLength () const 1132 { 1133 return m_text.GetMaxStringLength(); 1134 } 1135 1136 protected: 1137 StringList m_text; 1138 int m_first_visible_line; 1139 }; 1140 1141 1142 class Window 1143 { 1144 public: 1145 1146 Window (const char *name) : 1147 m_name (name), 1148 m_window (NULL), 1149 m_panel (NULL), 1150 m_parent (NULL), 1151 m_subwindows (), 1152 m_delegate_sp (), 1153 m_curr_active_window_idx (UINT32_MAX), 1154 m_prev_active_window_idx (UINT32_MAX), 1155 m_delete (false), 1156 m_needs_update (true), 1157 m_can_activate (true), 1158 m_is_subwin (false) 1159 { 1160 } 1161 1162 Window (const char *name, WINDOW *w, bool del = true) : 1163 m_name (name), 1164 m_window (NULL), 1165 m_panel (NULL), 1166 m_parent (NULL), 1167 m_subwindows (), 1168 m_delegate_sp (), 1169 m_curr_active_window_idx (UINT32_MAX), 1170 m_prev_active_window_idx (UINT32_MAX), 1171 m_delete (del), 1172 m_needs_update (true), 1173 m_can_activate (true), 1174 m_is_subwin (false) 1175 { 1176 if (w) 1177 Reset(w); 1178 } 1179 1180 Window (const char *name, const Rect &bounds) : 1181 m_name (name), 1182 m_window (NULL), 1183 m_parent (NULL), 1184 m_subwindows (), 1185 m_delegate_sp (), 1186 m_curr_active_window_idx (UINT32_MAX), 1187 m_prev_active_window_idx (UINT32_MAX), 1188 m_delete (true), 1189 m_needs_update (true), 1190 m_can_activate (true), 1191 m_is_subwin (false) 1192 { 1193 Reset (::newwin (bounds.size.height, bounds.size.width, bounds.origin.y, bounds.origin.y)); 1194 } 1195 1196 virtual 1197 ~Window () 1198 { 1199 RemoveSubWindows (); 1200 Reset (); 1201 } 1202 1203 void 1204 Reset (WINDOW *w = NULL, bool del = true) 1205 { 1206 if (m_window == w) 1207 return; 1208 1209 if (m_panel) 1210 { 1211 ::del_panel (m_panel); 1212 m_panel = NULL; 1213 } 1214 if (m_window && m_delete) 1215 { 1216 ::delwin (m_window); 1217 m_window = NULL; 1218 m_delete = false; 1219 } 1220 if (w) 1221 { 1222 m_window = w; 1223 m_panel = ::new_panel (m_window); 1224 m_delete = del; 1225 } 1226 } 1227 1228 void AttributeOn (attr_t attr) { ::wattron (m_window, attr); } 1229 void AttributeOff (attr_t attr) { ::wattroff (m_window, attr); } 1230 void Box (chtype v_char = ACS_VLINE, chtype h_char = ACS_HLINE) { ::box(m_window, v_char, h_char); } 1231 void Clear () { ::wclear (m_window); } 1232 void Erase () { ::werase (m_window); } 1233 Rect GetBounds () { return Rect (GetParentOrigin(), GetSize()); } // Get the rectangle in our parent window 1234 int GetChar () { return ::wgetch (m_window); } 1235 int GetCursorX () { return getcurx (m_window); } 1236 int GetCursorY () { return getcury (m_window); } 1237 Rect GetFrame () { return Rect (Point(), GetSize()); } // Get our rectangle in our own coordinate system 1238 Point GetParentOrigin() { return Point (GetParentX(), GetParentY()); } 1239 Size GetSize() { return Size (GetWidth(), GetHeight()); } 1240 int GetParentX () { return getparx (m_window); } 1241 int GetParentY () { return getpary (m_window); } 1242 int GetMaxX() { return getmaxx (m_window); } 1243 int GetMaxY() { return getmaxy (m_window); } 1244 int GetWidth() { return GetMaxX(); } 1245 int GetHeight() { return GetMaxY(); } 1246 void MoveCursor (int x, int y) { ::wmove (m_window, y, x); } 1247 void MoveWindow (int x, int y) { MoveWindow(Point(x,y)); } 1248 void Resize (int w, int h) { ::wresize(m_window, h, w); } 1249 void Resize (const Size &size) { ::wresize(m_window, size.height, size.width); } 1250 void PutChar (int ch) { ::waddch (m_window, ch); } 1251 void PutCString (const char *s, int len = -1) { ::waddnstr (m_window, s, len); } 1252 void Refresh () { ::wrefresh (m_window); } 1253 void DeferredRefresh () 1254 { 1255 // We are using panels, so we don't need to call this... 1256 //::wnoutrefresh(m_window); 1257 } 1258 void SetBackground (int color_pair_idx) { ::wbkgd (m_window,COLOR_PAIR(color_pair_idx)); } 1259 void UnderlineOn () { AttributeOn(A_UNDERLINE); } 1260 void UnderlineOff () { AttributeOff(A_UNDERLINE); } 1261 1262 void PutCStringTruncated (const char *s, int right_pad) 1263 { 1264 int bytes_left = GetWidth() - GetCursorX(); 1265 if (bytes_left > right_pad) 1266 { 1267 bytes_left -= right_pad; 1268 ::waddnstr (m_window, s, bytes_left); 1269 } 1270 } 1271 1272 void 1273 MoveWindow (const Point &origin) 1274 { 1275 const bool moving_window = origin != GetParentOrigin(); 1276 if (m_is_subwin && moving_window) 1277 { 1278 // Can't move subwindows, must delete and re-create 1279 Size size = GetSize(); 1280 Reset (::subwin (m_parent->m_window, 1281 size.height, 1282 size.width, 1283 origin.y, 1284 origin.x), true); 1285 } 1286 else 1287 { 1288 ::mvwin (m_window, origin.y, origin.x); 1289 } 1290 } 1291 1292 void 1293 SetBounds (const Rect &bounds) 1294 { 1295 const bool moving_window = bounds.origin != GetParentOrigin(); 1296 if (m_is_subwin && moving_window) 1297 { 1298 // Can't move subwindows, must delete and re-create 1299 Reset (::subwin (m_parent->m_window, 1300 bounds.size.height, 1301 bounds.size.width, 1302 bounds.origin.y, 1303 bounds.origin.x), true); 1304 } 1305 else 1306 { 1307 if (moving_window) 1308 MoveWindow(bounds.origin); 1309 Resize (bounds.size); 1310 } 1311 } 1312 1313 void 1314 Printf (const char *format, ...) __attribute__ ((format (printf, 2, 3))) 1315 { 1316 va_list args; 1317 va_start (args, format); 1318 vwprintw(m_window, format, args); 1319 va_end (args); 1320 } 1321 1322 void 1323 Touch () 1324 { 1325 ::touchwin (m_window); 1326 if (m_parent) 1327 m_parent->Touch(); 1328 } 1329 1330 WindowSP 1331 CreateSubWindow (const char *name, const Rect &bounds, bool make_active) 1332 { 1333 WindowSP subwindow_sp; 1334 if (m_window) 1335 { 1336 subwindow_sp.reset(new Window(name, ::subwin (m_window, 1337 bounds.size.height, 1338 bounds.size.width, 1339 bounds.origin.y, 1340 bounds.origin.x), true)); 1341 subwindow_sp->m_is_subwin = true; 1342 } 1343 else 1344 { 1345 subwindow_sp.reset(new Window(name, ::newwin (bounds.size.height, 1346 bounds.size.width, 1347 bounds.origin.y, 1348 bounds.origin.x), true)); 1349 subwindow_sp->m_is_subwin = false; 1350 } 1351 subwindow_sp->m_parent = this; 1352 if (make_active) 1353 { 1354 m_prev_active_window_idx = m_curr_active_window_idx; 1355 m_curr_active_window_idx = m_subwindows.size(); 1356 } 1357 m_subwindows.push_back(subwindow_sp); 1358 ::top_panel (subwindow_sp->m_panel); 1359 m_needs_update = true; 1360 return subwindow_sp; 1361 } 1362 1363 bool 1364 RemoveSubWindow (Window *window) 1365 { 1366 Windows::iterator pos, end = m_subwindows.end(); 1367 size_t i = 0; 1368 for (pos = m_subwindows.begin(); pos != end; ++pos, ++i) 1369 { 1370 if ((*pos).get() == window) 1371 { 1372 if (m_prev_active_window_idx == i) 1373 m_prev_active_window_idx = UINT32_MAX; 1374 else if (m_prev_active_window_idx != UINT32_MAX && m_prev_active_window_idx > i) 1375 --m_prev_active_window_idx; 1376 1377 if (m_curr_active_window_idx == i) 1378 m_curr_active_window_idx = UINT32_MAX; 1379 else if (m_curr_active_window_idx != UINT32_MAX && m_curr_active_window_idx > i) 1380 --m_curr_active_window_idx; 1381 window->Erase(); 1382 m_subwindows.erase(pos); 1383 m_needs_update = true; 1384 if (m_parent) 1385 m_parent->Touch(); 1386 else 1387 ::touchwin (stdscr); 1388 return true; 1389 } 1390 } 1391 return false; 1392 } 1393 1394 WindowSP 1395 FindSubWindow (const char *name) 1396 { 1397 Windows::iterator pos, end = m_subwindows.end(); 1398 size_t i = 0; 1399 for (pos = m_subwindows.begin(); pos != end; ++pos, ++i) 1400 { 1401 if ((*pos)->m_name.compare(name) == 0) 1402 return *pos; 1403 } 1404 return WindowSP(); 1405 } 1406 1407 void 1408 RemoveSubWindows () 1409 { 1410 m_curr_active_window_idx = UINT32_MAX; 1411 m_prev_active_window_idx = UINT32_MAX; 1412 for (Windows::iterator pos = m_subwindows.begin(); 1413 pos != m_subwindows.end(); 1414 pos = m_subwindows.erase(pos)) 1415 { 1416 (*pos)->Erase(); 1417 } 1418 if (m_parent) 1419 m_parent->Touch(); 1420 else 1421 ::touchwin (stdscr); 1422 } 1423 1424 WINDOW * 1425 get() 1426 { 1427 return m_window; 1428 } 1429 1430 operator WINDOW *() 1431 { 1432 return m_window; 1433 } 1434 1435 //---------------------------------------------------------------------- 1436 // Window drawing utilities 1437 //---------------------------------------------------------------------- 1438 void 1439 DrawTitleBox (const char *title, const char *bottom_message = NULL) 1440 { 1441 attr_t attr = 0; 1442 if (IsActive()) 1443 attr = A_BOLD | COLOR_PAIR(2); 1444 else 1445 attr = 0; 1446 if (attr) 1447 AttributeOn(attr); 1448 1449 Box(); 1450 MoveCursor(3, 0); 1451 1452 if (title && title[0]) 1453 { 1454 PutChar ('<'); 1455 PutCString (title); 1456 PutChar ('>'); 1457 } 1458 1459 if (bottom_message && bottom_message[0]) 1460 { 1461 int bottom_message_length = strlen(bottom_message); 1462 int x = GetWidth() - 3 - (bottom_message_length + 2); 1463 1464 if (x > 0) 1465 { 1466 MoveCursor (x, GetHeight() - 1); 1467 PutChar ('['); 1468 PutCString(bottom_message); 1469 PutChar (']'); 1470 } 1471 else 1472 { 1473 MoveCursor (1, GetHeight() - 1); 1474 PutChar ('['); 1475 PutCStringTruncated (bottom_message, 1); 1476 } 1477 } 1478 if (attr) 1479 AttributeOff(attr); 1480 1481 } 1482 1483 virtual void 1484 Draw (bool force) 1485 { 1486 if (m_delegate_sp && m_delegate_sp->WindowDelegateDraw (*this, force)) 1487 return; 1488 1489 for (auto &subwindow_sp : m_subwindows) 1490 subwindow_sp->Draw(force); 1491 } 1492 1493 bool 1494 CreateHelpSubwindow () 1495 { 1496 if (m_delegate_sp) 1497 { 1498 const char *text = m_delegate_sp->WindowDelegateGetHelpText (); 1499 KeyHelp *key_help = m_delegate_sp->WindowDelegateGetKeyHelp (); 1500 if ((text && text[0]) || key_help) 1501 { 1502 std::auto_ptr<HelpDialogDelegate> help_delegate_ap(new HelpDialogDelegate(text, key_help)); 1503 const size_t num_lines = help_delegate_ap->GetNumLines(); 1504 const size_t max_length = help_delegate_ap->GetMaxLineLength(); 1505 Rect bounds = GetBounds(); 1506 bounds.Inset(1, 1); 1507 if (max_length + 4 < static_cast<size_t>(bounds.size.width)) 1508 { 1509 bounds.origin.x += (bounds.size.width - max_length + 4)/2; 1510 bounds.size.width = max_length + 4; 1511 } 1512 else 1513 { 1514 if (bounds.size.width > 100) 1515 { 1516 const int inset_w = bounds.size.width / 4; 1517 bounds.origin.x += inset_w; 1518 bounds.size.width -= 2*inset_w; 1519 } 1520 } 1521 1522 if (num_lines + 2 < static_cast<size_t>(bounds.size.height)) 1523 { 1524 bounds.origin.y += (bounds.size.height - num_lines + 2)/2; 1525 bounds.size.height = num_lines + 2; 1526 } 1527 else 1528 { 1529 if (bounds.size.height > 100) 1530 { 1531 const int inset_h = bounds.size.height / 4; 1532 bounds.origin.y += inset_h; 1533 bounds.size.height -= 2*inset_h; 1534 } 1535 } 1536 WindowSP help_window_sp; 1537 Window *parent_window = GetParent(); 1538 if (parent_window) 1539 help_window_sp = parent_window->CreateSubWindow("Help", bounds, true); 1540 else 1541 help_window_sp = CreateSubWindow("Help", bounds, true); 1542 help_window_sp->SetDelegate(WindowDelegateSP(help_delegate_ap.release())); 1543 return true; 1544 } 1545 } 1546 return false; 1547 } 1548 1549 virtual HandleCharResult 1550 HandleChar (int key) 1551 { 1552 // Always check the active window first 1553 HandleCharResult result = eKeyNotHandled; 1554 WindowSP active_window_sp = GetActiveWindow (); 1555 if (active_window_sp) 1556 { 1557 result = active_window_sp->HandleChar (key); 1558 if (result != eKeyNotHandled) 1559 return result; 1560 } 1561 1562 if (m_delegate_sp) 1563 { 1564 result = m_delegate_sp->WindowDelegateHandleChar (*this, key); 1565 if (result != eKeyNotHandled) 1566 return result; 1567 } 1568 1569 // Then check for any windows that want any keys 1570 // that weren't handled. This is typically only 1571 // for a menubar. 1572 // Make a copy of the subwindows in case any HandleChar() 1573 // functions muck with the subwindows. If we don't do this, 1574 // we can crash when iterating over the subwindows. 1575 Windows subwindows (m_subwindows); 1576 for (auto subwindow_sp : subwindows) 1577 { 1578 if (subwindow_sp->m_can_activate == false) 1579 { 1580 HandleCharResult result = subwindow_sp->HandleChar(key); 1581 if (result != eKeyNotHandled) 1582 return result; 1583 } 1584 } 1585 1586 return eKeyNotHandled; 1587 } 1588 1589 bool 1590 SetActiveWindow (Window *window) 1591 { 1592 const size_t num_subwindows = m_subwindows.size(); 1593 for (size_t i=0; i<num_subwindows; ++i) 1594 { 1595 if (m_subwindows[i].get() == window) 1596 { 1597 m_prev_active_window_idx = m_curr_active_window_idx; 1598 ::top_panel (window->m_panel); 1599 m_curr_active_window_idx = i; 1600 return true; 1601 } 1602 } 1603 return false; 1604 } 1605 1606 WindowSP 1607 GetActiveWindow () 1608 { 1609 if (!m_subwindows.empty()) 1610 { 1611 if (m_curr_active_window_idx >= m_subwindows.size()) 1612 { 1613 if (m_prev_active_window_idx < m_subwindows.size()) 1614 { 1615 m_curr_active_window_idx = m_prev_active_window_idx; 1616 m_prev_active_window_idx = UINT32_MAX; 1617 } 1618 else if (IsActive()) 1619 { 1620 m_prev_active_window_idx = UINT32_MAX; 1621 m_curr_active_window_idx = UINT32_MAX; 1622 1623 // Find first window that wants to be active if this window is active 1624 const size_t num_subwindows = m_subwindows.size(); 1625 for (size_t i=0; i<num_subwindows; ++i) 1626 { 1627 if (m_subwindows[i]->GetCanBeActive()) 1628 { 1629 m_curr_active_window_idx = i; 1630 break; 1631 } 1632 } 1633 } 1634 } 1635 1636 if (m_curr_active_window_idx < m_subwindows.size()) 1637 return m_subwindows[m_curr_active_window_idx]; 1638 } 1639 return WindowSP(); 1640 } 1641 1642 bool 1643 GetCanBeActive () const 1644 { 1645 return m_can_activate; 1646 } 1647 1648 void 1649 SetCanBeActive (bool b) 1650 { 1651 m_can_activate = b; 1652 } 1653 1654 const WindowDelegateSP & 1655 GetDelegate () const 1656 { 1657 return m_delegate_sp; 1658 } 1659 1660 void 1661 SetDelegate (const WindowDelegateSP &delegate_sp) 1662 { 1663 m_delegate_sp = delegate_sp; 1664 } 1665 1666 Window * 1667 GetParent () const 1668 { 1669 return m_parent; 1670 } 1671 1672 bool 1673 IsActive () const 1674 { 1675 if (m_parent) 1676 return m_parent->GetActiveWindow().get() == this; 1677 else 1678 return true; // Top level window is always active 1679 } 1680 1681 void 1682 SelectNextWindowAsActive () 1683 { 1684 // Move active focus to next window 1685 const size_t num_subwindows = m_subwindows.size(); 1686 if (m_curr_active_window_idx == UINT32_MAX) 1687 { 1688 uint32_t idx = 0; 1689 for (auto subwindow_sp : m_subwindows) 1690 { 1691 if (subwindow_sp->GetCanBeActive()) 1692 { 1693 m_curr_active_window_idx = idx; 1694 break; 1695 } 1696 ++idx; 1697 } 1698 } 1699 else if (m_curr_active_window_idx + 1 < num_subwindows) 1700 { 1701 bool handled = false; 1702 m_prev_active_window_idx = m_curr_active_window_idx; 1703 for (size_t idx=m_curr_active_window_idx + 1; idx<num_subwindows; ++idx) 1704 { 1705 if (m_subwindows[idx]->GetCanBeActive()) 1706 { 1707 m_curr_active_window_idx = idx; 1708 handled = true; 1709 break; 1710 } 1711 } 1712 if (!handled) 1713 { 1714 for (size_t idx=0; idx<=m_prev_active_window_idx; ++idx) 1715 { 1716 if (m_subwindows[idx]->GetCanBeActive()) 1717 { 1718 m_curr_active_window_idx = idx; 1719 break; 1720 } 1721 } 1722 } 1723 } 1724 else 1725 { 1726 m_prev_active_window_idx = m_curr_active_window_idx; 1727 for (size_t idx=0; idx<num_subwindows; ++idx) 1728 { 1729 if (m_subwindows[idx]->GetCanBeActive()) 1730 { 1731 m_curr_active_window_idx = idx; 1732 break; 1733 } 1734 } 1735 } 1736 } 1737 1738 const char * 1739 GetName () const 1740 { 1741 return m_name.c_str(); 1742 } 1743 protected: 1744 std::string m_name; 1745 WINDOW *m_window; 1746 PANEL *m_panel; 1747 Window *m_parent; 1748 Windows m_subwindows; 1749 WindowDelegateSP m_delegate_sp; 1750 uint32_t m_curr_active_window_idx; 1751 uint32_t m_prev_active_window_idx; 1752 bool m_delete; 1753 bool m_needs_update; 1754 bool m_can_activate; 1755 bool m_is_subwin; 1756 1757 private: 1758 DISALLOW_COPY_AND_ASSIGN(Window); 1759 }; 1760 1761 class MenuDelegate 1762 { 1763 public: 1764 virtual ~MenuDelegate() {} 1765 1766 virtual MenuActionResult 1767 MenuDelegateAction (Menu &menu) = 0; 1768 }; 1769 1770 class Menu : public WindowDelegate 1771 { 1772 public: 1773 enum class Type 1774 { 1775 Invalid, 1776 Bar, 1777 Item, 1778 Separator 1779 }; 1780 1781 // Menubar or separator constructor 1782 Menu (Type type); 1783 1784 // Menuitem constructor 1785 Menu (const char *name, 1786 const char *key_name, 1787 int key_value, 1788 uint64_t identifier); 1789 1790 virtual ~ 1791 Menu () 1792 { 1793 } 1794 1795 const MenuDelegateSP & 1796 GetDelegate () const 1797 { 1798 return m_delegate_sp; 1799 } 1800 1801 void 1802 SetDelegate (const MenuDelegateSP &delegate_sp) 1803 { 1804 m_delegate_sp = delegate_sp; 1805 } 1806 1807 void 1808 RecalculateNameLengths(); 1809 1810 void 1811 AddSubmenu (const MenuSP &menu_sp); 1812 1813 int 1814 DrawAndRunMenu (Window &window); 1815 1816 void 1817 DrawMenuTitle (Window &window, bool highlight); 1818 1819 virtual bool 1820 WindowDelegateDraw (Window &window, bool force); 1821 1822 virtual HandleCharResult 1823 WindowDelegateHandleChar (Window &window, int key); 1824 1825 MenuActionResult 1826 ActionPrivate (Menu &menu) 1827 { 1828 MenuActionResult result = MenuActionResult::NotHandled; 1829 if (m_delegate_sp) 1830 { 1831 result = m_delegate_sp->MenuDelegateAction (menu); 1832 if (result != MenuActionResult::NotHandled) 1833 return result; 1834 } 1835 else if (m_parent) 1836 { 1837 result = m_parent->ActionPrivate(menu); 1838 if (result != MenuActionResult::NotHandled) 1839 return result; 1840 } 1841 return m_canned_result; 1842 } 1843 1844 MenuActionResult 1845 Action () 1846 { 1847 // Call the recursive action so it can try to handle it 1848 // with the menu delegate, and if not, try our parent menu 1849 return ActionPrivate (*this); 1850 } 1851 1852 void 1853 SetCannedResult (MenuActionResult result) 1854 { 1855 m_canned_result = result; 1856 } 1857 1858 Menus & 1859 GetSubmenus() 1860 { 1861 return m_submenus; 1862 } 1863 1864 const Menus & 1865 GetSubmenus() const 1866 { 1867 return m_submenus; 1868 } 1869 1870 int 1871 GetSelectedSubmenuIndex () const 1872 { 1873 return m_selected; 1874 } 1875 1876 void 1877 SetSelectedSubmenuIndex (int idx) 1878 { 1879 m_selected = idx; 1880 } 1881 1882 Type 1883 GetType () const 1884 { 1885 return m_type; 1886 } 1887 1888 int 1889 GetStartingColumn() const 1890 { 1891 return m_start_col; 1892 } 1893 1894 void 1895 SetStartingColumn(int col) 1896 { 1897 m_start_col = col; 1898 } 1899 1900 int 1901 GetKeyValue() const 1902 { 1903 return m_key_value; 1904 } 1905 1906 void 1907 SetKeyValue(int key_value) 1908 { 1909 m_key_value = key_value; 1910 } 1911 1912 std::string & 1913 GetName() 1914 { 1915 return m_name; 1916 } 1917 1918 std::string & 1919 GetKeyName() 1920 { 1921 return m_key_name; 1922 } 1923 1924 int 1925 GetDrawWidth () const 1926 { 1927 return m_max_submenu_name_length + m_max_submenu_key_name_length + 8; 1928 } 1929 1930 1931 uint64_t 1932 GetIdentifier() const 1933 { 1934 return m_identifier; 1935 } 1936 1937 void 1938 SetIdentifier (uint64_t identifier) 1939 { 1940 m_identifier = identifier; 1941 } 1942 1943 protected: 1944 std::string m_name; 1945 std::string m_key_name; 1946 uint64_t m_identifier; 1947 Type m_type; 1948 int m_key_value; 1949 int m_start_col; 1950 int m_max_submenu_name_length; 1951 int m_max_submenu_key_name_length; 1952 int m_selected; 1953 Menu *m_parent; 1954 Menus m_submenus; 1955 WindowSP m_menu_window_sp; 1956 MenuActionResult m_canned_result; 1957 MenuDelegateSP m_delegate_sp; 1958 }; 1959 1960 // Menubar or separator constructor 1961 Menu::Menu (Type type) : 1962 m_name (), 1963 m_key_name (), 1964 m_identifier (0), 1965 m_type (type), 1966 m_key_value (0), 1967 m_start_col (0), 1968 m_max_submenu_name_length (0), 1969 m_max_submenu_key_name_length (0), 1970 m_selected (0), 1971 m_parent (NULL), 1972 m_submenus (), 1973 m_canned_result (MenuActionResult::NotHandled), 1974 m_delegate_sp() 1975 { 1976 } 1977 1978 // Menuitem constructor 1979 Menu::Menu (const char *name, 1980 const char *key_name, 1981 int key_value, 1982 uint64_t identifier) : 1983 m_name (), 1984 m_key_name (), 1985 m_identifier (identifier), 1986 m_type (Type::Invalid), 1987 m_key_value (key_value), 1988 m_start_col (0), 1989 m_max_submenu_name_length (0), 1990 m_max_submenu_key_name_length (0), 1991 m_selected (0), 1992 m_parent (NULL), 1993 m_submenus (), 1994 m_canned_result (MenuActionResult::NotHandled), 1995 m_delegate_sp() 1996 { 1997 if (name && name[0]) 1998 { 1999 m_name = name; 2000 m_type = Type::Item; 2001 if (key_name && key_name[0]) 2002 m_key_name = key_name; 2003 } 2004 else 2005 { 2006 m_type = Type::Separator; 2007 } 2008 } 2009 2010 void 2011 Menu::RecalculateNameLengths() 2012 { 2013 m_max_submenu_name_length = 0; 2014 m_max_submenu_key_name_length = 0; 2015 Menus &submenus = GetSubmenus(); 2016 const size_t num_submenus = submenus.size(); 2017 for (size_t i=0; i<num_submenus; ++i) 2018 { 2019 Menu *submenu = submenus[i].get(); 2020 if (static_cast<size_t>(m_max_submenu_name_length) < submenu->m_name.size()) 2021 m_max_submenu_name_length = submenu->m_name.size(); 2022 if (static_cast<size_t>(m_max_submenu_key_name_length) < submenu->m_key_name.size()) 2023 m_max_submenu_key_name_length = submenu->m_key_name.size(); 2024 } 2025 } 2026 2027 void 2028 Menu::AddSubmenu (const MenuSP &menu_sp) 2029 { 2030 menu_sp->m_parent = this; 2031 if (static_cast<size_t>(m_max_submenu_name_length) < menu_sp->m_name.size()) 2032 m_max_submenu_name_length = menu_sp->m_name.size(); 2033 if (static_cast<size_t>(m_max_submenu_key_name_length) < menu_sp->m_key_name.size()) 2034 m_max_submenu_key_name_length = menu_sp->m_key_name.size(); 2035 m_submenus.push_back(menu_sp); 2036 } 2037 2038 void 2039 Menu::DrawMenuTitle (Window &window, bool highlight) 2040 { 2041 if (m_type == Type::Separator) 2042 { 2043 window.MoveCursor(0, window.GetCursorY()); 2044 window.PutChar(ACS_LTEE); 2045 int width = window.GetWidth(); 2046 if (width > 2) 2047 { 2048 width -= 2; 2049 for (int i=0; i< width; ++i) 2050 window.PutChar(ACS_HLINE); 2051 } 2052 window.PutChar(ACS_RTEE); 2053 } 2054 else 2055 { 2056 const int shortcut_key = m_key_value; 2057 bool underlined_shortcut = false; 2058 const attr_t hilgight_attr = A_REVERSE; 2059 if (highlight) 2060 window.AttributeOn(hilgight_attr); 2061 if (isprint(shortcut_key)) 2062 { 2063 size_t lower_pos = m_name.find(tolower(shortcut_key)); 2064 size_t upper_pos = m_name.find(toupper(shortcut_key)); 2065 const char *name = m_name.c_str(); 2066 size_t pos = std::min<size_t>(lower_pos, upper_pos); 2067 if (pos != std::string::npos) 2068 { 2069 underlined_shortcut = true; 2070 if (pos > 0) 2071 { 2072 window.PutCString(name, pos); 2073 name += pos; 2074 } 2075 const attr_t shortcut_attr = A_UNDERLINE|A_BOLD; 2076 window.AttributeOn (shortcut_attr); 2077 window.PutChar(name[0]); 2078 window.AttributeOff(shortcut_attr); 2079 name++; 2080 if (name[0]) 2081 window.PutCString(name); 2082 } 2083 } 2084 2085 if (!underlined_shortcut) 2086 { 2087 window.PutCString(m_name.c_str()); 2088 } 2089 2090 if (highlight) 2091 window.AttributeOff(hilgight_attr); 2092 2093 if (m_key_name.empty()) 2094 { 2095 if (!underlined_shortcut && isprint(m_key_value)) 2096 { 2097 window.AttributeOn (COLOR_PAIR(3)); 2098 window.Printf (" (%c)", m_key_value); 2099 window.AttributeOff (COLOR_PAIR(3)); 2100 } 2101 } 2102 else 2103 { 2104 window.AttributeOn (COLOR_PAIR(3)); 2105 window.Printf (" (%s)", m_key_name.c_str()); 2106 window.AttributeOff (COLOR_PAIR(3)); 2107 } 2108 } 2109 } 2110 2111 bool 2112 Menu::WindowDelegateDraw (Window &window, bool force) 2113 { 2114 Menus &submenus = GetSubmenus(); 2115 const size_t num_submenus = submenus.size(); 2116 const int selected_idx = GetSelectedSubmenuIndex(); 2117 Menu::Type menu_type = GetType (); 2118 switch (menu_type) 2119 { 2120 case Menu::Type::Bar: 2121 { 2122 window.SetBackground(2); 2123 window.MoveCursor(0, 0); 2124 for (size_t i=0; i<num_submenus; ++i) 2125 { 2126 Menu *menu = submenus[i].get(); 2127 if (i > 0) 2128 window.PutChar(' '); 2129 menu->SetStartingColumn (window.GetCursorX()); 2130 window.PutCString("| "); 2131 menu->DrawMenuTitle (window, false); 2132 } 2133 window.PutCString(" |"); 2134 window.DeferredRefresh(); 2135 } 2136 break; 2137 2138 case Menu::Type::Item: 2139 { 2140 int y = 1; 2141 int x = 3; 2142 // Draw the menu 2143 int cursor_x = 0; 2144 int cursor_y = 0; 2145 window.Erase(); 2146 window.SetBackground(2); 2147 window.Box(); 2148 for (size_t i=0; i<num_submenus; ++i) 2149 { 2150 const bool is_selected = 2151 (i == static_cast<size_t>(selected_idx)); 2152 window.MoveCursor(x, y + i); 2153 if (is_selected) 2154 { 2155 // Remember where we want the cursor to be 2156 cursor_x = x-1; 2157 cursor_y = y+i; 2158 } 2159 submenus[i]->DrawMenuTitle (window, is_selected); 2160 } 2161 window.MoveCursor(cursor_x, cursor_y); 2162 window.DeferredRefresh(); 2163 } 2164 break; 2165 2166 default: 2167 case Menu::Type::Separator: 2168 break; 2169 } 2170 return true; // Drawing handled... 2171 } 2172 2173 HandleCharResult 2174 Menu::WindowDelegateHandleChar (Window &window, int key) 2175 { 2176 HandleCharResult result = eKeyNotHandled; 2177 2178 Menus &submenus = GetSubmenus(); 2179 const size_t num_submenus = submenus.size(); 2180 const int selected_idx = GetSelectedSubmenuIndex(); 2181 Menu::Type menu_type = GetType (); 2182 if (menu_type == Menu::Type::Bar) 2183 { 2184 MenuSP run_menu_sp; 2185 switch (key) 2186 { 2187 case KEY_DOWN: 2188 case KEY_UP: 2189 // Show last menu or first menu 2190 if (selected_idx < static_cast<int>(num_submenus)) 2191 run_menu_sp = submenus[selected_idx]; 2192 else if (!submenus.empty()) 2193 run_menu_sp = submenus.front(); 2194 result = eKeyHandled; 2195 break; 2196 2197 case KEY_RIGHT: 2198 { 2199 ++m_selected; 2200 if (m_selected >= static_cast<int>(num_submenus)) 2201 m_selected = 0; 2202 if (m_selected < static_cast<int>(num_submenus)) 2203 run_menu_sp = submenus[m_selected]; 2204 else if (!submenus.empty()) 2205 run_menu_sp = submenus.front(); 2206 result = eKeyHandled; 2207 } 2208 break; 2209 2210 case KEY_LEFT: 2211 { 2212 --m_selected; 2213 if (m_selected < 0) 2214 m_selected = num_submenus - 1; 2215 if (m_selected < static_cast<int>(num_submenus)) 2216 run_menu_sp = submenus[m_selected]; 2217 else if (!submenus.empty()) 2218 run_menu_sp = submenus.front(); 2219 result = eKeyHandled; 2220 } 2221 break; 2222 2223 default: 2224 for (size_t i=0; i<num_submenus; ++i) 2225 { 2226 if (submenus[i]->GetKeyValue() == key) 2227 { 2228 SetSelectedSubmenuIndex(i); 2229 run_menu_sp = submenus[i]; 2230 result = eKeyHandled; 2231 break; 2232 } 2233 } 2234 break; 2235 } 2236 2237 if (run_menu_sp) 2238 { 2239 // Run the action on this menu in case we need to populate the 2240 // menu with dynamic content and also in case check marks, and 2241 // any other menu decorations need to be caclulated 2242 if (run_menu_sp->Action() == MenuActionResult::Quit) 2243 return eQuitApplication; 2244 2245 Rect menu_bounds; 2246 menu_bounds.origin.x = run_menu_sp->GetStartingColumn(); 2247 menu_bounds.origin.y = 1; 2248 menu_bounds.size.width = run_menu_sp->GetDrawWidth(); 2249 menu_bounds.size.height = run_menu_sp->GetSubmenus().size() + 2; 2250 if (m_menu_window_sp) 2251 window.GetParent()->RemoveSubWindow(m_menu_window_sp.get()); 2252 2253 m_menu_window_sp = window.GetParent()->CreateSubWindow (run_menu_sp->GetName().c_str(), 2254 menu_bounds, 2255 true); 2256 m_menu_window_sp->SetDelegate (run_menu_sp); 2257 } 2258 } 2259 else if (menu_type == Menu::Type::Item) 2260 { 2261 switch (key) 2262 { 2263 case KEY_DOWN: 2264 if (m_submenus.size() > 1) 2265 { 2266 const int start_select = m_selected; 2267 while (++m_selected != start_select) 2268 { 2269 if (static_cast<size_t>(m_selected) >= num_submenus) 2270 m_selected = 0; 2271 if (m_submenus[m_selected]->GetType() == Type::Separator) 2272 continue; 2273 else 2274 break; 2275 } 2276 return eKeyHandled; 2277 } 2278 break; 2279 2280 case KEY_UP: 2281 if (m_submenus.size() > 1) 2282 { 2283 const int start_select = m_selected; 2284 while (--m_selected != start_select) 2285 { 2286 if (m_selected < static_cast<int>(0)) 2287 m_selected = num_submenus - 1; 2288 if (m_submenus[m_selected]->GetType() == Type::Separator) 2289 continue; 2290 else 2291 break; 2292 } 2293 return eKeyHandled; 2294 } 2295 break; 2296 2297 case KEY_RETURN: 2298 if (static_cast<size_t>(selected_idx) < num_submenus) 2299 { 2300 if (submenus[selected_idx]->Action() == MenuActionResult::Quit) 2301 return eQuitApplication; 2302 window.GetParent()->RemoveSubWindow(&window); 2303 return eKeyHandled; 2304 } 2305 break; 2306 2307 case KEY_ESCAPE: // Beware: pressing escape key has 1 to 2 second delay in case other chars are entered for escaped sequences 2308 window.GetParent()->RemoveSubWindow(&window); 2309 return eKeyHandled; 2310 2311 default: 2312 { 2313 for (size_t i=0; i<num_submenus; ++i) 2314 { 2315 Menu *menu = submenus[i].get(); 2316 if (menu->GetKeyValue() == key) 2317 { 2318 SetSelectedSubmenuIndex(i); 2319 window.GetParent()->RemoveSubWindow(&window); 2320 if (menu->Action() == MenuActionResult::Quit) 2321 return eQuitApplication; 2322 return eKeyHandled; 2323 } 2324 } 2325 } 2326 break; 2327 2328 } 2329 } 2330 else if (menu_type == Menu::Type::Separator) 2331 { 2332 2333 } 2334 return result; 2335 } 2336 2337 2338 class Application 2339 { 2340 public: 2341 Application (FILE *in, FILE *out) : 2342 m_window_sp(), 2343 m_screen (NULL), 2344 m_in (in), 2345 m_out (out) 2346 { 2347 2348 } 2349 2350 ~Application () 2351 { 2352 m_window_delegates.clear(); 2353 m_window_sp.reset(); 2354 if (m_screen) 2355 { 2356 ::delscreen(m_screen); 2357 m_screen = NULL; 2358 } 2359 } 2360 2361 void 2362 Initialize () 2363 { 2364 ::setlocale(LC_ALL, ""); 2365 ::setlocale(LC_CTYPE, ""); 2366 #if 0 2367 ::initscr(); 2368 #else 2369 m_screen = ::newterm(NULL, m_out, m_in); 2370 #endif 2371 ::start_color(); 2372 ::curs_set(0); 2373 ::noecho(); 2374 ::keypad(stdscr,TRUE); 2375 } 2376 2377 void 2378 Terminate () 2379 { 2380 ::endwin(); 2381 } 2382 2383 void 2384 Run (Debugger &debugger) 2385 { 2386 bool done = false; 2387 int delay_in_tenths_of_a_second = 1; 2388 2389 // Alas the threading model in curses is a bit lame so we need to 2390 // resort to polling every 0.5 seconds. We could poll for stdin 2391 // ourselves and then pass the keys down but then we need to 2392 // translate all of the escape sequences ourselves. So we resort to 2393 // polling for input because we need to receive async process events 2394 // while in this loop. 2395 2396 halfdelay(delay_in_tenths_of_a_second); // Poll using some number of tenths of seconds seconds when calling Window::GetChar() 2397 2398 ListenerSP listener_sp (new Listener ("lldb.IOHandler.curses.Application")); 2399 ConstString broadcaster_class_target(Target::GetStaticBroadcasterClass()); 2400 ConstString broadcaster_class_process(Process::GetStaticBroadcasterClass()); 2401 ConstString broadcaster_class_thread(Thread::GetStaticBroadcasterClass()); 2402 debugger.EnableForwardEvents (listener_sp); 2403 2404 bool update = true; 2405 #if defined(__APPLE__) 2406 std::deque<int> escape_chars; 2407 #endif 2408 2409 while (!done) 2410 { 2411 if (update) 2412 { 2413 m_window_sp->Draw(false); 2414 // All windows should be calling Window::DeferredRefresh() instead 2415 // of Window::Refresh() so we can do a single update and avoid 2416 // any screen blinking 2417 update_panels(); 2418 2419 // Cursor hiding isn't working on MacOSX, so hide it in the top left corner 2420 m_window_sp->MoveCursor(0, 0); 2421 2422 doupdate(); 2423 update = false; 2424 } 2425 2426 #if defined(__APPLE__) 2427 // Terminal.app doesn't map its function keys correctly, F1-F4 default to: 2428 // \033OP, \033OQ, \033OR, \033OS, so lets take care of this here if possible 2429 int ch; 2430 if (escape_chars.empty()) 2431 ch = m_window_sp->GetChar(); 2432 else 2433 { 2434 ch = escape_chars.front(); 2435 escape_chars.pop_front(); 2436 } 2437 if (ch == KEY_ESCAPE) 2438 { 2439 int ch2 = m_window_sp->GetChar(); 2440 if (ch2 == 'O') 2441 { 2442 int ch3 = m_window_sp->GetChar(); 2443 switch (ch3) 2444 { 2445 case 'P': ch = KEY_F(1); break; 2446 case 'Q': ch = KEY_F(2); break; 2447 case 'R': ch = KEY_F(3); break; 2448 case 'S': ch = KEY_F(4); break; 2449 default: 2450 escape_chars.push_back(ch2); 2451 if (ch3 != -1) 2452 escape_chars.push_back(ch3); 2453 break; 2454 } 2455 } 2456 else if (ch2 != -1) 2457 escape_chars.push_back(ch2); 2458 } 2459 #else 2460 int ch = m_window_sp->GetChar(); 2461 2462 #endif 2463 if (ch == -1) 2464 { 2465 if (feof(m_in) || ferror(m_in)) 2466 { 2467 done = true; 2468 } 2469 else 2470 { 2471 // Just a timeout from using halfdelay(), check for events 2472 EventSP event_sp; 2473 while (listener_sp->PeekAtNextEvent()) 2474 { 2475 listener_sp->GetNextEvent(event_sp); 2476 2477 if (event_sp) 2478 { 2479 Broadcaster *broadcaster = event_sp->GetBroadcaster(); 2480 if (broadcaster) 2481 { 2482 //uint32_t event_type = event_sp->GetType(); 2483 ConstString broadcaster_class (broadcaster->GetBroadcasterClass()); 2484 if (broadcaster_class == broadcaster_class_process) 2485 { 2486 debugger.GetCommandInterpreter().UpdateExecutionContext(NULL); 2487 update = true; 2488 continue; // Don't get any key, just update our view 2489 } 2490 } 2491 } 2492 } 2493 } 2494 } 2495 else 2496 { 2497 HandleCharResult key_result = m_window_sp->HandleChar(ch); 2498 switch (key_result) 2499 { 2500 case eKeyHandled: 2501 debugger.GetCommandInterpreter().UpdateExecutionContext(NULL); 2502 update = true; 2503 break; 2504 case eKeyNotHandled: 2505 break; 2506 case eQuitApplication: 2507 done = true; 2508 break; 2509 } 2510 } 2511 } 2512 2513 debugger.CancelForwardEvents (listener_sp); 2514 2515 } 2516 2517 WindowSP & 2518 GetMainWindow () 2519 { 2520 if (!m_window_sp) 2521 m_window_sp.reset (new Window ("main", stdscr, false)); 2522 return m_window_sp; 2523 } 2524 2525 WindowDelegates & 2526 GetWindowDelegates () 2527 { 2528 return m_window_delegates; 2529 } 2530 2531 protected: 2532 WindowSP m_window_sp; 2533 WindowDelegates m_window_delegates; 2534 SCREEN *m_screen; 2535 FILE *m_in; 2536 FILE *m_out; 2537 }; 2538 2539 2540 } // namespace curses 2541 2542 2543 using namespace curses; 2544 2545 struct Row 2546 { 2547 ValueObjectSP valobj; 2548 Row *parent; 2549 int row_idx; 2550 int x; 2551 int y; 2552 bool might_have_children; 2553 bool expanded; 2554 bool calculated_children; 2555 std::vector<Row> children; 2556 2557 Row (const ValueObjectSP &v, Row *p) : 2558 valobj (v), 2559 parent (p), 2560 row_idx(0), 2561 x(1), 2562 y(1), 2563 might_have_children (v ? v->MightHaveChildren() : false), 2564 expanded (false), 2565 calculated_children (false), 2566 children() 2567 { 2568 } 2569 2570 size_t 2571 GetDepth () const 2572 { 2573 if (parent) 2574 return 1 + parent->GetDepth(); 2575 return 0; 2576 } 2577 2578 void 2579 Expand() 2580 { 2581 expanded = true; 2582 if (!calculated_children) 2583 { 2584 calculated_children = true; 2585 if (valobj) 2586 { 2587 const size_t num_children = valobj->GetNumChildren(); 2588 for (size_t i=0; i<num_children; ++i) 2589 { 2590 children.push_back(Row (valobj->GetChildAtIndex(i, true), this)); 2591 } 2592 } 2593 } 2594 } 2595 2596 void 2597 Unexpand () 2598 { 2599 expanded = false; 2600 } 2601 2602 void 2603 DrawTree (Window &window) 2604 { 2605 if (parent) 2606 parent->DrawTreeForChild (window, this, 0); 2607 2608 if (might_have_children) 2609 { 2610 // It we can get UTF8 characters to work we should try to use the "symbol" 2611 // UTF8 string below 2612 // const char *symbol = ""; 2613 // if (row.expanded) 2614 // symbol = "\xe2\x96\xbd "; 2615 // else 2616 // symbol = "\xe2\x96\xb7 "; 2617 // window.PutCString (symbol); 2618 2619 // The ACS_DARROW and ACS_RARROW don't look very nice they are just a 2620 // 'v' or '>' character... 2621 // if (expanded) 2622 // window.PutChar (ACS_DARROW); 2623 // else 2624 // window.PutChar (ACS_RARROW); 2625 // Since we can't find any good looking right arrow/down arrow 2626 // symbols, just use a diamond... 2627 window.PutChar (ACS_DIAMOND); 2628 window.PutChar (ACS_HLINE); 2629 } 2630 } 2631 2632 void 2633 DrawTreeForChild (Window &window, Row *child, uint32_t reverse_depth) 2634 { 2635 if (parent) 2636 parent->DrawTreeForChild (window, this, reverse_depth + 1); 2637 2638 if (&children.back() == child) 2639 { 2640 // Last child 2641 if (reverse_depth == 0) 2642 { 2643 window.PutChar (ACS_LLCORNER); 2644 window.PutChar (ACS_HLINE); 2645 } 2646 else 2647 { 2648 window.PutChar (' '); 2649 window.PutChar (' '); 2650 } 2651 } 2652 else 2653 { 2654 if (reverse_depth == 0) 2655 { 2656 window.PutChar (ACS_LTEE); 2657 window.PutChar (ACS_HLINE); 2658 } 2659 else 2660 { 2661 window.PutChar (ACS_VLINE); 2662 window.PutChar (' '); 2663 } 2664 } 2665 } 2666 }; 2667 2668 struct DisplayOptions 2669 { 2670 bool show_types; 2671 }; 2672 2673 class TreeItem; 2674 2675 class TreeDelegate 2676 { 2677 public: 2678 TreeDelegate() {} 2679 virtual ~TreeDelegate() {} 2680 virtual void TreeDelegateDrawTreeItem (TreeItem &item, Window &window) = 0; 2681 virtual void TreeDelegateGenerateChildren (TreeItem &item) = 0; 2682 virtual bool TreeDelegateItemSelected (TreeItem &item) = 0; // Return true if we need to update views 2683 }; 2684 typedef std::shared_ptr<TreeDelegate> TreeDelegateSP; 2685 2686 class TreeItem 2687 { 2688 public: 2689 2690 TreeItem (TreeItem *parent, TreeDelegate &delegate, bool might_have_children) : 2691 m_parent (parent), 2692 m_delegate (delegate), 2693 m_user_data (NULL), 2694 m_identifier (0), 2695 m_row_idx (-1), 2696 m_children (), 2697 m_might_have_children (might_have_children), 2698 m_is_expanded (false) 2699 { 2700 } 2701 2702 TreeItem & 2703 operator=(const TreeItem &rhs) 2704 { 2705 if (this != &rhs) 2706 { 2707 m_parent = rhs.m_parent; 2708 m_delegate = rhs.m_delegate; 2709 m_user_data = rhs.m_user_data; 2710 m_identifier = rhs.m_identifier; 2711 m_row_idx = rhs.m_row_idx; 2712 m_children = rhs.m_children; 2713 m_might_have_children = rhs.m_might_have_children; 2714 m_is_expanded = rhs.m_is_expanded; 2715 } 2716 return *this; 2717 } 2718 2719 size_t 2720 GetDepth () const 2721 { 2722 if (m_parent) 2723 return 1 + m_parent->GetDepth(); 2724 return 0; 2725 } 2726 2727 int 2728 GetRowIndex () const 2729 { 2730 return m_row_idx; 2731 } 2732 2733 void 2734 ClearChildren () 2735 { 2736 m_children.clear(); 2737 } 2738 2739 void 2740 Resize (size_t n, const TreeItem &t) 2741 { 2742 m_children.resize(n, t); 2743 } 2744 2745 TreeItem & 2746 operator [](size_t i) 2747 { 2748 return m_children[i]; 2749 } 2750 2751 void 2752 SetRowIndex (int row_idx) 2753 { 2754 m_row_idx = row_idx; 2755 } 2756 2757 size_t 2758 GetNumChildren () 2759 { 2760 m_delegate.TreeDelegateGenerateChildren (*this); 2761 return m_children.size(); 2762 } 2763 2764 void 2765 ItemWasSelected () 2766 { 2767 m_delegate.TreeDelegateItemSelected(*this); 2768 } 2769 void 2770 CalculateRowIndexes (int &row_idx) 2771 { 2772 SetRowIndex(row_idx); 2773 ++row_idx; 2774 2775 const bool expanded = IsExpanded(); 2776 2777 // The root item must calculate its children, 2778 // or we must calculate the number of children 2779 // if the item is expanded 2780 if (m_parent == NULL || expanded) 2781 GetNumChildren(); 2782 2783 for (auto &item : m_children) 2784 { 2785 if (expanded) 2786 item.CalculateRowIndexes(row_idx); 2787 else 2788 item.SetRowIndex(-1); 2789 } 2790 } 2791 2792 TreeItem * 2793 GetParent () 2794 { 2795 return m_parent; 2796 } 2797 2798 bool 2799 IsExpanded () const 2800 { 2801 return m_is_expanded; 2802 } 2803 2804 void 2805 Expand() 2806 { 2807 m_is_expanded = true; 2808 } 2809 2810 void 2811 Unexpand () 2812 { 2813 m_is_expanded = false; 2814 } 2815 2816 bool 2817 Draw (Window &window, 2818 const int first_visible_row, 2819 const uint32_t selected_row_idx, 2820 int &row_idx, 2821 int &num_rows_left) 2822 { 2823 if (num_rows_left <= 0) 2824 return false; 2825 2826 if (m_row_idx >= first_visible_row) 2827 { 2828 window.MoveCursor(2, row_idx + 1); 2829 2830 if (m_parent) 2831 m_parent->DrawTreeForChild (window, this, 0); 2832 2833 if (m_might_have_children) 2834 { 2835 // It we can get UTF8 characters to work we should try to use the "symbol" 2836 // UTF8 string below 2837 // const char *symbol = ""; 2838 // if (row.expanded) 2839 // symbol = "\xe2\x96\xbd "; 2840 // else 2841 // symbol = "\xe2\x96\xb7 "; 2842 // window.PutCString (symbol); 2843 2844 // The ACS_DARROW and ACS_RARROW don't look very nice they are just a 2845 // 'v' or '>' character... 2846 // if (expanded) 2847 // window.PutChar (ACS_DARROW); 2848 // else 2849 // window.PutChar (ACS_RARROW); 2850 // Since we can't find any good looking right arrow/down arrow 2851 // symbols, just use a diamond... 2852 window.PutChar (ACS_DIAMOND); 2853 window.PutChar (ACS_HLINE); 2854 } 2855 bool highlight = 2856 (selected_row_idx == static_cast<size_t>(m_row_idx)) && window.IsActive(); 2857 2858 if (highlight) 2859 window.AttributeOn(A_REVERSE); 2860 2861 m_delegate.TreeDelegateDrawTreeItem(*this, window); 2862 2863 if (highlight) 2864 window.AttributeOff(A_REVERSE); 2865 ++row_idx; 2866 --num_rows_left; 2867 } 2868 2869 if (num_rows_left <= 0) 2870 return false; // We are done drawing... 2871 2872 if (IsExpanded()) 2873 { 2874 for (auto &item : m_children) 2875 { 2876 // If we displayed all the rows and item.Draw() returns 2877 // false we are done drawing and can exit this for loop 2878 if (item.Draw(window, first_visible_row, selected_row_idx, row_idx, num_rows_left) == false) 2879 break; 2880 } 2881 } 2882 return num_rows_left >= 0; // Return true if not done drawing yet 2883 } 2884 2885 void 2886 DrawTreeForChild (Window &window, TreeItem *child, uint32_t reverse_depth) 2887 { 2888 if (m_parent) 2889 m_parent->DrawTreeForChild (window, this, reverse_depth + 1); 2890 2891 if (&m_children.back() == child) 2892 { 2893 // Last child 2894 if (reverse_depth == 0) 2895 { 2896 window.PutChar (ACS_LLCORNER); 2897 window.PutChar (ACS_HLINE); 2898 } 2899 else 2900 { 2901 window.PutChar (' '); 2902 window.PutChar (' '); 2903 } 2904 } 2905 else 2906 { 2907 if (reverse_depth == 0) 2908 { 2909 window.PutChar (ACS_LTEE); 2910 window.PutChar (ACS_HLINE); 2911 } 2912 else 2913 { 2914 window.PutChar (ACS_VLINE); 2915 window.PutChar (' '); 2916 } 2917 } 2918 } 2919 2920 TreeItem * 2921 GetItemForRowIndex (uint32_t row_idx) 2922 { 2923 if (static_cast<uint32_t>(m_row_idx) == row_idx) 2924 return this; 2925 if (m_children.empty()) 2926 return NULL; 2927 if (static_cast<uint32_t>(m_children.back().m_row_idx) < row_idx) 2928 return NULL; 2929 if (IsExpanded()) 2930 { 2931 for (auto &item : m_children) 2932 { 2933 TreeItem *selected_item_ptr = item.GetItemForRowIndex(row_idx); 2934 if (selected_item_ptr) 2935 return selected_item_ptr; 2936 } 2937 } 2938 return NULL; 2939 } 2940 2941 void * 2942 GetUserData() const 2943 { 2944 return m_user_data; 2945 } 2946 2947 void 2948 SetUserData (void *user_data) 2949 { 2950 m_user_data = user_data; 2951 } 2952 2953 uint64_t 2954 GetIdentifier() const 2955 { 2956 return m_identifier; 2957 } 2958 2959 void 2960 SetIdentifier (uint64_t identifier) 2961 { 2962 m_identifier = identifier; 2963 } 2964 2965 2966 void 2967 SetMightHaveChildren (bool b) 2968 { 2969 m_might_have_children = b; 2970 } 2971 2972 protected: 2973 TreeItem *m_parent; 2974 TreeDelegate &m_delegate; 2975 void *m_user_data; 2976 uint64_t m_identifier; 2977 int m_row_idx; // Zero based visible row index, -1 if not visible or for the root item 2978 std::vector<TreeItem> m_children; 2979 bool m_might_have_children; 2980 bool m_is_expanded; 2981 2982 }; 2983 2984 class TreeWindowDelegate : public WindowDelegate 2985 { 2986 public: 2987 TreeWindowDelegate (Debugger &debugger, const TreeDelegateSP &delegate_sp) : 2988 m_debugger (debugger), 2989 m_delegate_sp (delegate_sp), 2990 m_root (NULL, *delegate_sp, true), 2991 m_selected_item (NULL), 2992 m_num_rows (0), 2993 m_selected_row_idx (0), 2994 m_first_visible_row (0), 2995 m_min_x (0), 2996 m_min_y (0), 2997 m_max_x (0), 2998 m_max_y (0) 2999 { 3000 } 3001 3002 int 3003 NumVisibleRows () const 3004 { 3005 return m_max_y - m_min_y; 3006 } 3007 3008 virtual bool 3009 WindowDelegateDraw (Window &window, bool force) 3010 { 3011 ExecutionContext exe_ctx (m_debugger.GetCommandInterpreter().GetExecutionContext()); 3012 Process *process = exe_ctx.GetProcessPtr(); 3013 3014 bool display_content = false; 3015 if (process) 3016 { 3017 StateType state = process->GetState(); 3018 if (StateIsStoppedState(state, true)) 3019 { 3020 // We are stopped, so it is ok to 3021 display_content = true; 3022 } 3023 else if (StateIsRunningState(state)) 3024 { 3025 return true; // Don't do any updating when we are running 3026 } 3027 } 3028 3029 m_min_x = 2; 3030 m_min_y = 1; 3031 m_max_x = window.GetWidth() - 1; 3032 m_max_y = window.GetHeight() - 1; 3033 3034 window.Erase(); 3035 window.DrawTitleBox (window.GetName()); 3036 3037 if (display_content) 3038 { 3039 const int num_visible_rows = NumVisibleRows(); 3040 m_num_rows = 0; 3041 m_root.CalculateRowIndexes(m_num_rows); 3042 3043 // If we unexpanded while having something selected our 3044 // total number of rows is less than the num visible rows, 3045 // then make sure we show all the rows by setting the first 3046 // visible row accordingly. 3047 if (m_first_visible_row > 0 && m_num_rows < num_visible_rows) 3048 m_first_visible_row = 0; 3049 3050 // Make sure the selected row is always visible 3051 if (m_selected_row_idx < m_first_visible_row) 3052 m_first_visible_row = m_selected_row_idx; 3053 else if (m_first_visible_row + num_visible_rows <= m_selected_row_idx) 3054 m_first_visible_row = m_selected_row_idx - num_visible_rows + 1; 3055 3056 int row_idx = 0; 3057 int num_rows_left = num_visible_rows; 3058 m_root.Draw (window, m_first_visible_row, m_selected_row_idx, row_idx, num_rows_left); 3059 // Get the selected row 3060 m_selected_item = m_root.GetItemForRowIndex (m_selected_row_idx); 3061 } 3062 else 3063 { 3064 m_selected_item = NULL; 3065 } 3066 3067 window.DeferredRefresh(); 3068 3069 3070 return true; // Drawing handled 3071 } 3072 3073 3074 virtual const char * 3075 WindowDelegateGetHelpText () 3076 { 3077 return "Thread window keyboard shortcuts:"; 3078 } 3079 3080 virtual KeyHelp * 3081 WindowDelegateGetKeyHelp () 3082 { 3083 static curses::KeyHelp g_source_view_key_help[] = { 3084 { KEY_UP, "Select previous item" }, 3085 { KEY_DOWN, "Select next item" }, 3086 { KEY_RIGHT, "Expand the selected item" }, 3087 { KEY_LEFT, "Unexpand the selected item or select parent if not expanded" }, 3088 { KEY_PPAGE, "Page up" }, 3089 { KEY_NPAGE, "Page down" }, 3090 { 'h', "Show help dialog" }, 3091 { ' ', "Toggle item expansion" }, 3092 { ',', "Page up" }, 3093 { '.', "Page down" }, 3094 { '\0', NULL } 3095 }; 3096 return g_source_view_key_help; 3097 } 3098 3099 virtual HandleCharResult 3100 WindowDelegateHandleChar (Window &window, int c) 3101 { 3102 switch(c) 3103 { 3104 case ',': 3105 case KEY_PPAGE: 3106 // Page up key 3107 if (m_first_visible_row > 0) 3108 { 3109 if (m_first_visible_row > m_max_y) 3110 m_first_visible_row -= m_max_y; 3111 else 3112 m_first_visible_row = 0; 3113 m_selected_row_idx = m_first_visible_row; 3114 m_selected_item = m_root.GetItemForRowIndex(m_selected_row_idx); 3115 if (m_selected_item) 3116 m_selected_item->ItemWasSelected (); 3117 } 3118 return eKeyHandled; 3119 3120 case '.': 3121 case KEY_NPAGE: 3122 // Page down key 3123 if (m_num_rows > m_max_y) 3124 { 3125 if (m_first_visible_row + m_max_y < m_num_rows) 3126 { 3127 m_first_visible_row += m_max_y; 3128 m_selected_row_idx = m_first_visible_row; 3129 m_selected_item = m_root.GetItemForRowIndex(m_selected_row_idx); 3130 if (m_selected_item) 3131 m_selected_item->ItemWasSelected (); 3132 } 3133 } 3134 return eKeyHandled; 3135 3136 case KEY_UP: 3137 if (m_selected_row_idx > 0) 3138 { 3139 --m_selected_row_idx; 3140 m_selected_item = m_root.GetItemForRowIndex(m_selected_row_idx); 3141 if (m_selected_item) 3142 m_selected_item->ItemWasSelected (); 3143 } 3144 return eKeyHandled; 3145 case KEY_DOWN: 3146 if (m_selected_row_idx + 1 < m_num_rows) 3147 { 3148 ++m_selected_row_idx; 3149 m_selected_item = m_root.GetItemForRowIndex(m_selected_row_idx); 3150 if (m_selected_item) 3151 m_selected_item->ItemWasSelected (); 3152 } 3153 return eKeyHandled; 3154 3155 case KEY_RIGHT: 3156 if (m_selected_item) 3157 { 3158 if (!m_selected_item->IsExpanded()) 3159 m_selected_item->Expand(); 3160 } 3161 return eKeyHandled; 3162 3163 case KEY_LEFT: 3164 if (m_selected_item) 3165 { 3166 if (m_selected_item->IsExpanded()) 3167 m_selected_item->Unexpand(); 3168 else if (m_selected_item->GetParent()) 3169 { 3170 m_selected_row_idx = m_selected_item->GetParent()->GetRowIndex(); 3171 m_selected_item = m_root.GetItemForRowIndex(m_selected_row_idx); 3172 if (m_selected_item) 3173 m_selected_item->ItemWasSelected (); 3174 } 3175 } 3176 return eKeyHandled; 3177 3178 case ' ': 3179 // Toggle expansion state when SPACE is pressed 3180 if (m_selected_item) 3181 { 3182 if (m_selected_item->IsExpanded()) 3183 m_selected_item->Unexpand(); 3184 else 3185 m_selected_item->Expand(); 3186 } 3187 return eKeyHandled; 3188 3189 case 'h': 3190 window.CreateHelpSubwindow (); 3191 return eKeyHandled; 3192 3193 default: 3194 break; 3195 } 3196 return eKeyNotHandled; 3197 } 3198 3199 protected: 3200 Debugger &m_debugger; 3201 TreeDelegateSP m_delegate_sp; 3202 TreeItem m_root; 3203 TreeItem *m_selected_item; 3204 int m_num_rows; 3205 int m_selected_row_idx; 3206 int m_first_visible_row; 3207 int m_min_x; 3208 int m_min_y; 3209 int m_max_x; 3210 int m_max_y; 3211 3212 }; 3213 3214 class FrameTreeDelegate : public TreeDelegate 3215 { 3216 public: 3217 FrameTreeDelegate () : 3218 TreeDelegate() 3219 { 3220 } 3221 3222 virtual ~FrameTreeDelegate() 3223 { 3224 } 3225 3226 virtual void 3227 TreeDelegateDrawTreeItem (TreeItem &item, Window &window) 3228 { 3229 Thread* thread = (Thread*)item.GetUserData(); 3230 if (thread) 3231 { 3232 const uint64_t frame_idx = item.GetIdentifier(); 3233 StackFrameSP frame_sp = thread->GetStackFrameAtIndex(frame_idx); 3234 if (frame_sp) 3235 { 3236 StreamString strm; 3237 const SymbolContext &sc = frame_sp->GetSymbolContext(eSymbolContextEverything); 3238 ExecutionContext exe_ctx (frame_sp); 3239 //const char *frame_format = "frame #${frame.index}: ${module.file.basename}{`${function.name}${function.pc-offset}}}"; 3240 const char *frame_format = "frame #${frame.index}: {${function.name}${function.pc-offset}}}"; 3241 if (Debugger::FormatPrompt (frame_format, &sc, &exe_ctx, NULL, strm)) 3242 { 3243 int right_pad = 1; 3244 window.PutCStringTruncated(strm.GetString().c_str(), right_pad); 3245 } 3246 } 3247 } 3248 } 3249 virtual void 3250 TreeDelegateGenerateChildren (TreeItem &item) 3251 { 3252 // No children for frames yet... 3253 } 3254 3255 virtual bool 3256 TreeDelegateItemSelected (TreeItem &item) 3257 { 3258 Thread* thread = (Thread*)item.GetUserData(); 3259 if (thread) 3260 { 3261 thread->GetProcess()->GetThreadList().SetSelectedThreadByID(thread->GetID()); 3262 const uint64_t frame_idx = item.GetIdentifier(); 3263 thread->SetSelectedFrameByIndex(frame_idx); 3264 return true; 3265 } 3266 return false; 3267 } 3268 }; 3269 3270 class ThreadTreeDelegate : public TreeDelegate 3271 { 3272 public: 3273 ThreadTreeDelegate (Debugger &debugger) : 3274 TreeDelegate(), 3275 m_debugger (debugger), 3276 m_tid (LLDB_INVALID_THREAD_ID), 3277 m_stop_id (UINT32_MAX) 3278 { 3279 } 3280 3281 virtual 3282 ~ThreadTreeDelegate() 3283 { 3284 } 3285 3286 ProcessSP 3287 GetProcess () 3288 { 3289 return m_debugger.GetCommandInterpreter().GetExecutionContext().GetProcessSP(); 3290 } 3291 3292 ThreadSP 3293 GetThread (const TreeItem &item) 3294 { 3295 ProcessSP process_sp = GetProcess (); 3296 if (process_sp) 3297 return process_sp->GetThreadList().FindThreadByID(item.GetIdentifier()); 3298 return ThreadSP(); 3299 } 3300 3301 virtual void 3302 TreeDelegateDrawTreeItem (TreeItem &item, Window &window) 3303 { 3304 ThreadSP thread_sp = GetThread (item); 3305 if (thread_sp) 3306 { 3307 StreamString strm; 3308 ExecutionContext exe_ctx (thread_sp); 3309 const char *format = "thread #${thread.index}: tid = ${thread.id}{, stop reason = ${thread.stop-reason}}"; 3310 if (Debugger::FormatPrompt (format, NULL, &exe_ctx, NULL, strm)) 3311 { 3312 int right_pad = 1; 3313 window.PutCStringTruncated(strm.GetString().c_str(), right_pad); 3314 } 3315 } 3316 } 3317 virtual void 3318 TreeDelegateGenerateChildren (TreeItem &item) 3319 { 3320 ProcessSP process_sp = GetProcess (); 3321 if (process_sp && process_sp->IsAlive()) 3322 { 3323 StateType state = process_sp->GetState(); 3324 if (StateIsStoppedState(state, true)) 3325 { 3326 ThreadSP thread_sp = GetThread (item); 3327 if (thread_sp) 3328 { 3329 if (m_stop_id == process_sp->GetStopID() && thread_sp->GetID() == m_tid) 3330 return; // Children are already up to date 3331 if (!m_frame_delegate_sp) 3332 { 3333 // Always expand the thread item the first time we show it 3334 m_frame_delegate_sp.reset (new FrameTreeDelegate()); 3335 } 3336 3337 m_stop_id = process_sp->GetStopID(); 3338 m_tid = thread_sp->GetID(); 3339 3340 TreeItem t (&item, *m_frame_delegate_sp, false); 3341 size_t num_frames = thread_sp->GetStackFrameCount(); 3342 item.Resize (num_frames, t); 3343 for (size_t i=0; i<num_frames; ++i) 3344 { 3345 item[i].SetUserData(thread_sp.get()); 3346 item[i].SetIdentifier(i); 3347 } 3348 } 3349 return; 3350 } 3351 } 3352 item.ClearChildren(); 3353 } 3354 3355 virtual bool 3356 TreeDelegateItemSelected (TreeItem &item) 3357 { 3358 ProcessSP process_sp = GetProcess (); 3359 if (process_sp && process_sp->IsAlive()) 3360 { 3361 StateType state = process_sp->GetState(); 3362 if (StateIsStoppedState(state, true)) 3363 { 3364 ThreadSP thread_sp = GetThread (item); 3365 if (thread_sp) 3366 { 3367 ThreadList &thread_list = thread_sp->GetProcess()->GetThreadList(); 3368 Mutex::Locker locker (thread_list.GetMutex()); 3369 ThreadSP selected_thread_sp = thread_list.GetSelectedThread(); 3370 if (selected_thread_sp->GetID() != thread_sp->GetID()) 3371 { 3372 thread_list.SetSelectedThreadByID(thread_sp->GetID()); 3373 return true; 3374 } 3375 } 3376 } 3377 } 3378 return false; 3379 } 3380 3381 protected: 3382 Debugger &m_debugger; 3383 std::shared_ptr<FrameTreeDelegate> m_frame_delegate_sp; 3384 lldb::user_id_t m_tid; 3385 uint32_t m_stop_id; 3386 }; 3387 3388 class ThreadsTreeDelegate : public TreeDelegate 3389 { 3390 public: 3391 ThreadsTreeDelegate (Debugger &debugger) : 3392 TreeDelegate(), 3393 m_thread_delegate_sp (), 3394 m_debugger (debugger), 3395 m_stop_id (UINT32_MAX) 3396 { 3397 } 3398 3399 virtual 3400 ~ThreadsTreeDelegate() 3401 { 3402 } 3403 3404 ProcessSP 3405 GetProcess () 3406 { 3407 return m_debugger.GetCommandInterpreter().GetExecutionContext().GetProcessSP(); 3408 } 3409 3410 virtual void 3411 TreeDelegateDrawTreeItem (TreeItem &item, Window &window) 3412 { 3413 ProcessSP process_sp = GetProcess (); 3414 if (process_sp && process_sp->IsAlive()) 3415 { 3416 StreamString strm; 3417 ExecutionContext exe_ctx (process_sp); 3418 const char *format = "process ${process.id}{, name = ${process.name}}"; 3419 if (Debugger::FormatPrompt (format, NULL, &exe_ctx, NULL, strm)) 3420 { 3421 int right_pad = 1; 3422 window.PutCStringTruncated(strm.GetString().c_str(), right_pad); 3423 } 3424 } 3425 } 3426 3427 virtual void 3428 TreeDelegateGenerateChildren (TreeItem &item) 3429 { 3430 ProcessSP process_sp = GetProcess (); 3431 if (process_sp && process_sp->IsAlive()) 3432 { 3433 StateType state = process_sp->GetState(); 3434 if (StateIsStoppedState(state, true)) 3435 { 3436 const uint32_t stop_id = process_sp->GetStopID(); 3437 if (m_stop_id == stop_id) 3438 return; // Children are already up to date 3439 3440 m_stop_id = stop_id; 3441 3442 if (!m_thread_delegate_sp) 3443 { 3444 // Always expand the thread item the first time we show it 3445 //item.Expand(); 3446 m_thread_delegate_sp.reset (new ThreadTreeDelegate(m_debugger)); 3447 } 3448 3449 TreeItem t (&item, *m_thread_delegate_sp, false); 3450 ThreadList &threads = process_sp->GetThreadList(); 3451 Mutex::Locker locker (threads.GetMutex()); 3452 size_t num_threads = threads.GetSize(); 3453 item.Resize (num_threads, t); 3454 for (size_t i=0; i<num_threads; ++i) 3455 { 3456 item[i].SetIdentifier(threads.GetThreadAtIndex(i)->GetID()); 3457 item[i].SetMightHaveChildren(true); 3458 } 3459 return; 3460 } 3461 } 3462 item.ClearChildren(); 3463 } 3464 3465 virtual bool 3466 TreeDelegateItemSelected (TreeItem &item) 3467 { 3468 return false; 3469 } 3470 3471 protected: 3472 std::shared_ptr<ThreadTreeDelegate> m_thread_delegate_sp; 3473 Debugger &m_debugger; 3474 uint32_t m_stop_id; 3475 }; 3476 3477 class ValueObjectListDelegate : public WindowDelegate 3478 { 3479 public: 3480 ValueObjectListDelegate () : 3481 m_valobj_list (), 3482 m_rows (), 3483 m_selected_row (NULL), 3484 m_selected_row_idx (0), 3485 m_first_visible_row (0), 3486 m_num_rows (0), 3487 m_max_x (0), 3488 m_max_y (0) 3489 { 3490 } 3491 3492 ValueObjectListDelegate (ValueObjectList &valobj_list) : 3493 m_valobj_list (valobj_list), 3494 m_rows (), 3495 m_selected_row (NULL), 3496 m_selected_row_idx (0), 3497 m_first_visible_row (0), 3498 m_num_rows (0), 3499 m_max_x (0), 3500 m_max_y (0) 3501 { 3502 SetValues (valobj_list); 3503 } 3504 3505 virtual 3506 ~ValueObjectListDelegate() 3507 { 3508 } 3509 3510 void 3511 SetValues (ValueObjectList &valobj_list) 3512 { 3513 m_selected_row = NULL; 3514 m_selected_row_idx = 0; 3515 m_first_visible_row = 0; 3516 m_num_rows = 0; 3517 m_rows.clear(); 3518 m_valobj_list = valobj_list; 3519 const size_t num_values = m_valobj_list.GetSize(); 3520 for (size_t i=0; i<num_values; ++i) 3521 m_rows.push_back(Row(m_valobj_list.GetValueObjectAtIndex(i), NULL)); 3522 } 3523 3524 virtual bool 3525 WindowDelegateDraw (Window &window, bool force) 3526 { 3527 m_num_rows = 0; 3528 m_min_x = 2; 3529 m_min_y = 1; 3530 m_max_x = window.GetWidth() - 1; 3531 m_max_y = window.GetHeight() - 1; 3532 3533 window.Erase(); 3534 window.DrawTitleBox (window.GetName()); 3535 3536 const int num_visible_rows = NumVisibleRows(); 3537 const int num_rows = CalculateTotalNumberRows (m_rows); 3538 3539 // If we unexpanded while having something selected our 3540 // total number of rows is less than the num visible rows, 3541 // then make sure we show all the rows by setting the first 3542 // visible row accordingly. 3543 if (m_first_visible_row > 0 && num_rows < num_visible_rows) 3544 m_first_visible_row = 0; 3545 3546 // Make sure the selected row is always visible 3547 if (m_selected_row_idx < m_first_visible_row) 3548 m_first_visible_row = m_selected_row_idx; 3549 else if (m_first_visible_row + num_visible_rows <= m_selected_row_idx) 3550 m_first_visible_row = m_selected_row_idx - num_visible_rows + 1; 3551 3552 DisplayRows (window, m_rows, g_options); 3553 3554 window.DeferredRefresh(); 3555 3556 // Get the selected row 3557 m_selected_row = GetRowForRowIndex (m_selected_row_idx); 3558 // Keep the cursor on the selected row so the highlight and the cursor 3559 // are always on the same line 3560 if (m_selected_row) 3561 window.MoveCursor (m_selected_row->x, 3562 m_selected_row->y); 3563 3564 return true; // Drawing handled 3565 } 3566 3567 virtual KeyHelp * 3568 WindowDelegateGetKeyHelp () 3569 { 3570 static curses::KeyHelp g_source_view_key_help[] = { 3571 { KEY_UP, "Select previous item" }, 3572 { KEY_DOWN, "Select next item" }, 3573 { KEY_RIGHT, "Expand selected item" }, 3574 { KEY_LEFT, "Unexpand selected item or select parent if not expanded" }, 3575 { KEY_PPAGE, "Page up" }, 3576 { KEY_NPAGE, "Page down" }, 3577 { 'A', "Format as annotated address" }, 3578 { 'b', "Format as binary" }, 3579 { 'B', "Format as hex bytes with ASCII" }, 3580 { 'c', "Format as character" }, 3581 { 'd', "Format as a signed integer" }, 3582 { 'D', "Format selected value using the default format for the type" }, 3583 { 'f', "Format as float" }, 3584 { 'h', "Show help dialog" }, 3585 { 'i', "Format as instructions" }, 3586 { 'o', "Format as octal" }, 3587 { 'p', "Format as pointer" }, 3588 { 's', "Format as C string" }, 3589 { 't', "Toggle showing/hiding type names" }, 3590 { 'u', "Format as an unsigned integer" }, 3591 { 'x', "Format as hex" }, 3592 { 'X', "Format as uppercase hex" }, 3593 { ' ', "Toggle item expansion" }, 3594 { ',', "Page up" }, 3595 { '.', "Page down" }, 3596 { '\0', NULL } 3597 }; 3598 return g_source_view_key_help; 3599 } 3600 3601 3602 virtual HandleCharResult 3603 WindowDelegateHandleChar (Window &window, int c) 3604 { 3605 switch(c) 3606 { 3607 case 'x': 3608 case 'X': 3609 case 'o': 3610 case 's': 3611 case 'u': 3612 case 'd': 3613 case 'D': 3614 case 'i': 3615 case 'A': 3616 case 'p': 3617 case 'c': 3618 case 'b': 3619 case 'B': 3620 case 'f': 3621 // Change the format for the currently selected item 3622 if (m_selected_row) 3623 m_selected_row->valobj->SetFormat (FormatForChar (c)); 3624 return eKeyHandled; 3625 3626 case 't': 3627 // Toggle showing type names 3628 g_options.show_types = !g_options.show_types; 3629 return eKeyHandled; 3630 3631 case ',': 3632 case KEY_PPAGE: 3633 // Page up key 3634 if (m_first_visible_row > 0) 3635 { 3636 if (static_cast<int>(m_first_visible_row) > m_max_y) 3637 m_first_visible_row -= m_max_y; 3638 else 3639 m_first_visible_row = 0; 3640 m_selected_row_idx = m_first_visible_row; 3641 } 3642 return eKeyHandled; 3643 3644 case '.': 3645 case KEY_NPAGE: 3646 // Page down key 3647 if (m_num_rows > static_cast<size_t>(m_max_y)) 3648 { 3649 if (m_first_visible_row + m_max_y < m_num_rows) 3650 { 3651 m_first_visible_row += m_max_y; 3652 m_selected_row_idx = m_first_visible_row; 3653 } 3654 } 3655 return eKeyHandled; 3656 3657 case KEY_UP: 3658 if (m_selected_row_idx > 0) 3659 --m_selected_row_idx; 3660 return eKeyHandled; 3661 case KEY_DOWN: 3662 if (m_selected_row_idx + 1 < m_num_rows) 3663 ++m_selected_row_idx; 3664 return eKeyHandled; 3665 3666 case KEY_RIGHT: 3667 if (m_selected_row) 3668 { 3669 if (!m_selected_row->expanded) 3670 m_selected_row->Expand(); 3671 } 3672 return eKeyHandled; 3673 3674 case KEY_LEFT: 3675 if (m_selected_row) 3676 { 3677 if (m_selected_row->expanded) 3678 m_selected_row->Unexpand(); 3679 else if (m_selected_row->parent) 3680 m_selected_row_idx = m_selected_row->parent->row_idx; 3681 } 3682 return eKeyHandled; 3683 3684 case ' ': 3685 // Toggle expansion state when SPACE is pressed 3686 if (m_selected_row) 3687 { 3688 if (m_selected_row->expanded) 3689 m_selected_row->Unexpand(); 3690 else 3691 m_selected_row->Expand(); 3692 } 3693 return eKeyHandled; 3694 3695 case 'h': 3696 window.CreateHelpSubwindow (); 3697 return eKeyHandled; 3698 3699 default: 3700 break; 3701 } 3702 return eKeyNotHandled; 3703 } 3704 3705 protected: 3706 ValueObjectList m_valobj_list; 3707 std::vector<Row> m_rows; 3708 Row *m_selected_row; 3709 uint32_t m_selected_row_idx; 3710 uint32_t m_first_visible_row; 3711 uint32_t m_num_rows; 3712 int m_min_x; 3713 int m_min_y; 3714 int m_max_x; 3715 int m_max_y; 3716 3717 static Format 3718 FormatForChar (int c) 3719 { 3720 switch (c) 3721 { 3722 case 'x': return eFormatHex; 3723 case 'X': return eFormatHexUppercase; 3724 case 'o': return eFormatOctal; 3725 case 's': return eFormatCString; 3726 case 'u': return eFormatUnsigned; 3727 case 'd': return eFormatDecimal; 3728 case 'D': return eFormatDefault; 3729 case 'i': return eFormatInstruction; 3730 case 'A': return eFormatAddressInfo; 3731 case 'p': return eFormatPointer; 3732 case 'c': return eFormatChar; 3733 case 'b': return eFormatBinary; 3734 case 'B': return eFormatBytesWithASCII; 3735 case 'f': return eFormatFloat; 3736 } 3737 return eFormatDefault; 3738 } 3739 3740 bool 3741 DisplayRowObject (Window &window, 3742 Row &row, 3743 DisplayOptions &options, 3744 bool highlight, 3745 bool last_child) 3746 { 3747 ValueObject *valobj = row.valobj.get(); 3748 3749 if (valobj == NULL) 3750 return false; 3751 3752 const char *type_name = options.show_types ? valobj->GetTypeName().GetCString() : NULL; 3753 const char *name = valobj->GetName().GetCString(); 3754 const char *value = valobj->GetValueAsCString (); 3755 const char *summary = valobj->GetSummaryAsCString (); 3756 3757 window.MoveCursor (row.x, row.y); 3758 3759 row.DrawTree (window); 3760 3761 if (highlight) 3762 window.AttributeOn(A_REVERSE); 3763 3764 if (type_name && type_name[0]) 3765 window.Printf ("(%s) ", type_name); 3766 3767 if (name && name[0]) 3768 window.PutCString(name); 3769 3770 attr_t changd_attr = 0; 3771 if (valobj->GetValueDidChange()) 3772 changd_attr = COLOR_PAIR(5) | A_BOLD; 3773 3774 if (value && value[0]) 3775 { 3776 window.PutCString(" = "); 3777 if (changd_attr) 3778 window.AttributeOn(changd_attr); 3779 window.PutCString (value); 3780 if (changd_attr) 3781 window.AttributeOff(changd_attr); 3782 } 3783 3784 if (summary && summary[0]) 3785 { 3786 window.PutChar(' '); 3787 if (changd_attr) 3788 window.AttributeOn(changd_attr); 3789 window.PutCString(summary); 3790 if (changd_attr) 3791 window.AttributeOff(changd_attr); 3792 } 3793 3794 if (highlight) 3795 window.AttributeOff (A_REVERSE); 3796 3797 return true; 3798 } 3799 void 3800 DisplayRows (Window &window, 3801 std::vector<Row> &rows, 3802 DisplayOptions &options) 3803 { 3804 // > 0x25B7 3805 // \/ 0x25BD 3806 3807 bool window_is_active = window.IsActive(); 3808 for (auto &row : rows) 3809 { 3810 const bool last_child = row.parent && &rows[rows.size()-1] == &row; 3811 // Save the row index in each Row structure 3812 row.row_idx = m_num_rows; 3813 if ((m_num_rows >= m_first_visible_row) && 3814 ((m_num_rows - m_first_visible_row) < static_cast<size_t>(NumVisibleRows()))) 3815 { 3816 row.x = m_min_x; 3817 row.y = m_num_rows - m_first_visible_row + 1; 3818 if (DisplayRowObject (window, 3819 row, 3820 options, 3821 window_is_active && m_num_rows == m_selected_row_idx, 3822 last_child)) 3823 { 3824 ++m_num_rows; 3825 } 3826 else 3827 { 3828 row.x = 0; 3829 row.y = 0; 3830 } 3831 } 3832 else 3833 { 3834 row.x = 0; 3835 row.y = 0; 3836 ++m_num_rows; 3837 } 3838 3839 if (row.expanded && !row.children.empty()) 3840 { 3841 DisplayRows (window, 3842 row.children, 3843 options); 3844 } 3845 } 3846 } 3847 3848 int 3849 CalculateTotalNumberRows (const std::vector<Row> &rows) 3850 { 3851 int row_count = 0; 3852 for (const auto &row : rows) 3853 { 3854 ++row_count; 3855 if (row.expanded) 3856 row_count += CalculateTotalNumberRows(row.children); 3857 } 3858 return row_count; 3859 } 3860 static Row * 3861 GetRowForRowIndexImpl (std::vector<Row> &rows, size_t &row_index) 3862 { 3863 for (auto &row : rows) 3864 { 3865 if (row_index == 0) 3866 return &row; 3867 else 3868 { 3869 --row_index; 3870 if (row.expanded && !row.children.empty()) 3871 { 3872 Row *result = GetRowForRowIndexImpl (row.children, row_index); 3873 if (result) 3874 return result; 3875 } 3876 } 3877 } 3878 return NULL; 3879 } 3880 3881 Row * 3882 GetRowForRowIndex (size_t row_index) 3883 { 3884 return GetRowForRowIndexImpl (m_rows, row_index); 3885 } 3886 3887 int 3888 NumVisibleRows () const 3889 { 3890 return m_max_y - m_min_y; 3891 } 3892 3893 static DisplayOptions g_options; 3894 }; 3895 3896 class FrameVariablesWindowDelegate : public ValueObjectListDelegate 3897 { 3898 public: 3899 FrameVariablesWindowDelegate (Debugger &debugger) : 3900 ValueObjectListDelegate (), 3901 m_debugger (debugger), 3902 m_frame_block (NULL) 3903 { 3904 } 3905 3906 virtual 3907 ~FrameVariablesWindowDelegate() 3908 { 3909 } 3910 3911 virtual const char * 3912 WindowDelegateGetHelpText () 3913 { 3914 return "Frame variable window keyboard shortcuts:"; 3915 } 3916 3917 virtual bool 3918 WindowDelegateDraw (Window &window, bool force) 3919 { 3920 ExecutionContext exe_ctx (m_debugger.GetCommandInterpreter().GetExecutionContext()); 3921 Process *process = exe_ctx.GetProcessPtr(); 3922 Block *frame_block = NULL; 3923 StackFrame *frame = NULL; 3924 3925 if (process) 3926 { 3927 StateType state = process->GetState(); 3928 if (StateIsStoppedState(state, true)) 3929 { 3930 frame = exe_ctx.GetFramePtr(); 3931 if (frame) 3932 frame_block = frame->GetFrameBlock (); 3933 } 3934 else if (StateIsRunningState(state)) 3935 { 3936 return true; // Don't do any updating when we are running 3937 } 3938 } 3939 3940 ValueObjectList local_values; 3941 if (frame_block) 3942 { 3943 // Only update the variables if they have changed 3944 if (m_frame_block != frame_block) 3945 { 3946 m_frame_block = frame_block; 3947 3948 VariableList *locals = frame->GetVariableList(true); 3949 if (locals) 3950 { 3951 const DynamicValueType use_dynamic = eDynamicDontRunTarget; 3952 const size_t num_locals = locals->GetSize(); 3953 for (size_t i=0; i<num_locals; ++i) 3954 local_values.Append(frame->GetValueObjectForFrameVariable (locals->GetVariableAtIndex(i), use_dynamic)); 3955 // Update the values 3956 SetValues(local_values); 3957 } 3958 } 3959 } 3960 else 3961 { 3962 m_frame_block = NULL; 3963 // Update the values with an empty list if there is no frame 3964 SetValues(local_values); 3965 } 3966 3967 return ValueObjectListDelegate::WindowDelegateDraw (window, force); 3968 3969 } 3970 3971 protected: 3972 Debugger &m_debugger; 3973 Block *m_frame_block; 3974 }; 3975 3976 3977 class RegistersWindowDelegate : public ValueObjectListDelegate 3978 { 3979 public: 3980 RegistersWindowDelegate (Debugger &debugger) : 3981 ValueObjectListDelegate (), 3982 m_debugger (debugger) 3983 { 3984 } 3985 3986 virtual 3987 ~RegistersWindowDelegate() 3988 { 3989 } 3990 3991 virtual const char * 3992 WindowDelegateGetHelpText () 3993 { 3994 return "Register window keyboard shortcuts:"; 3995 } 3996 3997 virtual bool 3998 WindowDelegateDraw (Window &window, bool force) 3999 { 4000 ExecutionContext exe_ctx (m_debugger.GetCommandInterpreter().GetExecutionContext()); 4001 StackFrame *frame = exe_ctx.GetFramePtr(); 4002 4003 ValueObjectList value_list; 4004 if (frame) 4005 { 4006 if (frame->GetStackID() != m_stack_id) 4007 { 4008 m_stack_id = frame->GetStackID(); 4009 RegisterContextSP reg_ctx (frame->GetRegisterContext()); 4010 if (reg_ctx) 4011 { 4012 const uint32_t num_sets = reg_ctx->GetRegisterSetCount(); 4013 for (uint32_t set_idx = 0; set_idx < num_sets; ++set_idx) 4014 { 4015 value_list.Append(ValueObjectRegisterSet::Create (frame, reg_ctx, set_idx)); 4016 } 4017 } 4018 SetValues(value_list); 4019 } 4020 } 4021 else 4022 { 4023 Process *process = exe_ctx.GetProcessPtr(); 4024 if (process && process->IsAlive()) 4025 return true; // Don't do any updating if we are running 4026 else 4027 { 4028 // Update the values with an empty list if there 4029 // is no process or the process isn't alive anymore 4030 SetValues(value_list); 4031 } 4032 } 4033 return ValueObjectListDelegate::WindowDelegateDraw (window, force); 4034 } 4035 4036 protected: 4037 Debugger &m_debugger; 4038 StackID m_stack_id; 4039 }; 4040 4041 static const char * 4042 CursesKeyToCString (int ch) 4043 { 4044 static char g_desc[32]; 4045 if (ch >= KEY_F0 && ch < KEY_F0 + 64) 4046 { 4047 snprintf(g_desc, sizeof(g_desc), "F%u", ch - KEY_F0); 4048 return g_desc; 4049 } 4050 switch (ch) 4051 { 4052 case KEY_DOWN: return "down"; 4053 case KEY_UP: return "up"; 4054 case KEY_LEFT: return "left"; 4055 case KEY_RIGHT: return "right"; 4056 case KEY_HOME: return "home"; 4057 case KEY_BACKSPACE: return "backspace"; 4058 case KEY_DL: return "delete-line"; 4059 case KEY_IL: return "insert-line"; 4060 case KEY_DC: return "delete-char"; 4061 case KEY_IC: return "insert-char"; 4062 case KEY_CLEAR: return "clear"; 4063 case KEY_EOS: return "clear-to-eos"; 4064 case KEY_EOL: return "clear-to-eol"; 4065 case KEY_SF: return "scroll-forward"; 4066 case KEY_SR: return "scroll-backward"; 4067 case KEY_NPAGE: return "page-down"; 4068 case KEY_PPAGE: return "page-up"; 4069 case KEY_STAB: return "set-tab"; 4070 case KEY_CTAB: return "clear-tab"; 4071 case KEY_CATAB: return "clear-all-tabs"; 4072 case KEY_ENTER: return "enter"; 4073 case KEY_PRINT: return "print"; 4074 case KEY_LL: return "lower-left key"; 4075 case KEY_A1: return "upper left of keypad"; 4076 case KEY_A3: return "upper right of keypad"; 4077 case KEY_B2: return "center of keypad"; 4078 case KEY_C1: return "lower left of keypad"; 4079 case KEY_C3: return "lower right of keypad"; 4080 case KEY_BTAB: return "back-tab key"; 4081 case KEY_BEG: return "begin key"; 4082 case KEY_CANCEL: return "cancel key"; 4083 case KEY_CLOSE: return "close key"; 4084 case KEY_COMMAND: return "command key"; 4085 case KEY_COPY: return "copy key"; 4086 case KEY_CREATE: return "create key"; 4087 case KEY_END: return "end key"; 4088 case KEY_EXIT: return "exit key"; 4089 case KEY_FIND: return "find key"; 4090 case KEY_HELP: return "help key"; 4091 case KEY_MARK: return "mark key"; 4092 case KEY_MESSAGE: return "message key"; 4093 case KEY_MOVE: return "move key"; 4094 case KEY_NEXT: return "next key"; 4095 case KEY_OPEN: return "open key"; 4096 case KEY_OPTIONS: return "options key"; 4097 case KEY_PREVIOUS: return "previous key"; 4098 case KEY_REDO: return "redo key"; 4099 case KEY_REFERENCE: return "reference key"; 4100 case KEY_REFRESH: return "refresh key"; 4101 case KEY_REPLACE: return "replace key"; 4102 case KEY_RESTART: return "restart key"; 4103 case KEY_RESUME: return "resume key"; 4104 case KEY_SAVE: return "save key"; 4105 case KEY_SBEG: return "shifted begin key"; 4106 case KEY_SCANCEL: return "shifted cancel key"; 4107 case KEY_SCOMMAND: return "shifted command key"; 4108 case KEY_SCOPY: return "shifted copy key"; 4109 case KEY_SCREATE: return "shifted create key"; 4110 case KEY_SDC: return "shifted delete-character key"; 4111 case KEY_SDL: return "shifted delete-line key"; 4112 case KEY_SELECT: return "select key"; 4113 case KEY_SEND: return "shifted end key"; 4114 case KEY_SEOL: return "shifted clear-to-end-of-line key"; 4115 case KEY_SEXIT: return "shifted exit key"; 4116 case KEY_SFIND: return "shifted find key"; 4117 case KEY_SHELP: return "shifted help key"; 4118 case KEY_SHOME: return "shifted home key"; 4119 case KEY_SIC: return "shifted insert-character key"; 4120 case KEY_SLEFT: return "shifted left-arrow key"; 4121 case KEY_SMESSAGE: return "shifted message key"; 4122 case KEY_SMOVE: return "shifted move key"; 4123 case KEY_SNEXT: return "shifted next key"; 4124 case KEY_SOPTIONS: return "shifted options key"; 4125 case KEY_SPREVIOUS: return "shifted previous key"; 4126 case KEY_SPRINT: return "shifted print key"; 4127 case KEY_SREDO: return "shifted redo key"; 4128 case KEY_SREPLACE: return "shifted replace key"; 4129 case KEY_SRIGHT: return "shifted right-arrow key"; 4130 case KEY_SRSUME: return "shifted resume key"; 4131 case KEY_SSAVE: return "shifted save key"; 4132 case KEY_SSUSPEND: return "shifted suspend key"; 4133 case KEY_SUNDO: return "shifted undo key"; 4134 case KEY_SUSPEND: return "suspend key"; 4135 case KEY_UNDO: return "undo key"; 4136 case KEY_MOUSE: return "Mouse event has occurred"; 4137 case KEY_RESIZE: return "Terminal resize event"; 4138 case KEY_EVENT: return "We were interrupted by an event"; 4139 case KEY_RETURN: return "return"; 4140 case ' ': return "space"; 4141 case '\t': return "tab"; 4142 case KEY_ESCAPE: return "escape"; 4143 default: 4144 if (isprint(ch)) 4145 snprintf(g_desc, sizeof(g_desc), "%c", ch); 4146 else 4147 snprintf(g_desc, sizeof(g_desc), "\\x%2.2x", ch); 4148 return g_desc; 4149 } 4150 return NULL; 4151 } 4152 4153 HelpDialogDelegate::HelpDialogDelegate (const char *text, KeyHelp *key_help_array) : 4154 m_text (), 4155 m_first_visible_line (0) 4156 { 4157 if (text && text[0]) 4158 { 4159 m_text.SplitIntoLines(text); 4160 m_text.AppendString(""); 4161 } 4162 if (key_help_array) 4163 { 4164 for (KeyHelp *key = key_help_array; key->ch; ++key) 4165 { 4166 StreamString key_description; 4167 key_description.Printf("%10s - %s", CursesKeyToCString(key->ch), key->description); 4168 m_text.AppendString(std::move(key_description.GetString())); 4169 } 4170 } 4171 } 4172 4173 HelpDialogDelegate::~HelpDialogDelegate() 4174 { 4175 } 4176 4177 bool 4178 HelpDialogDelegate::WindowDelegateDraw (Window &window, bool force) 4179 { 4180 window.Erase(); 4181 const int window_height = window.GetHeight(); 4182 int x = 2; 4183 int y = 1; 4184 const int min_y = y; 4185 const int max_y = window_height - 1 - y; 4186 const size_t num_visible_lines = max_y - min_y + 1; 4187 const size_t num_lines = m_text.GetSize(); 4188 const char *bottom_message; 4189 if (num_lines <= num_visible_lines) 4190 bottom_message = "Press any key to exit"; 4191 else 4192 bottom_message = "Use arrows to scroll, any other key to exit"; 4193 window.DrawTitleBox(window.GetName(), bottom_message); 4194 while (y <= max_y) 4195 { 4196 window.MoveCursor(x, y); 4197 window.PutCStringTruncated(m_text.GetStringAtIndex(m_first_visible_line + y - min_y), 1); 4198 ++y; 4199 } 4200 return true; 4201 } 4202 4203 HandleCharResult 4204 HelpDialogDelegate::WindowDelegateHandleChar (Window &window, int key) 4205 { 4206 bool done = false; 4207 const size_t num_lines = m_text.GetSize(); 4208 const size_t num_visible_lines = window.GetHeight() - 2; 4209 4210 if (num_lines <= num_visible_lines) 4211 { 4212 done = true; 4213 // If we have all lines visible and don't need scrolling, then any 4214 // key press will cause us to exit 4215 } 4216 else 4217 { 4218 switch (key) 4219 { 4220 case KEY_UP: 4221 if (m_first_visible_line > 0) 4222 --m_first_visible_line; 4223 break; 4224 4225 case KEY_DOWN: 4226 if (m_first_visible_line + num_visible_lines < num_lines) 4227 ++m_first_visible_line; 4228 break; 4229 4230 case KEY_PPAGE: 4231 case ',': 4232 if (m_first_visible_line > 0) 4233 { 4234 if (static_cast<size_t>(m_first_visible_line) >= num_visible_lines) 4235 m_first_visible_line -= num_visible_lines; 4236 else 4237 m_first_visible_line = 0; 4238 } 4239 break; 4240 case KEY_NPAGE: 4241 case '.': 4242 if (m_first_visible_line + num_visible_lines < num_lines) 4243 { 4244 m_first_visible_line += num_visible_lines; 4245 if (static_cast<size_t>(m_first_visible_line) > num_lines) 4246 m_first_visible_line = num_lines - num_visible_lines; 4247 } 4248 break; 4249 default: 4250 done = true; 4251 break; 4252 } 4253 } 4254 if (done) 4255 window.GetParent()->RemoveSubWindow(&window); 4256 return eKeyHandled; 4257 } 4258 4259 class ApplicationDelegate : 4260 public WindowDelegate, 4261 public MenuDelegate 4262 { 4263 public: 4264 enum { 4265 eMenuID_LLDB = 1, 4266 eMenuID_LLDBAbout, 4267 eMenuID_LLDBExit, 4268 4269 eMenuID_Target, 4270 eMenuID_TargetCreate, 4271 eMenuID_TargetDelete, 4272 4273 eMenuID_Process, 4274 eMenuID_ProcessAttach, 4275 eMenuID_ProcessDetach, 4276 eMenuID_ProcessLaunch, 4277 eMenuID_ProcessContinue, 4278 eMenuID_ProcessHalt, 4279 eMenuID_ProcessKill, 4280 4281 eMenuID_Thread, 4282 eMenuID_ThreadStepIn, 4283 eMenuID_ThreadStepOver, 4284 eMenuID_ThreadStepOut, 4285 4286 eMenuID_View, 4287 eMenuID_ViewBacktrace, 4288 eMenuID_ViewRegisters, 4289 eMenuID_ViewSource, 4290 eMenuID_ViewVariables, 4291 4292 eMenuID_Help, 4293 eMenuID_HelpGUIHelp 4294 }; 4295 4296 ApplicationDelegate (Application &app, Debugger &debugger) : 4297 WindowDelegate (), 4298 MenuDelegate (), 4299 m_app (app), 4300 m_debugger (debugger) 4301 { 4302 } 4303 4304 virtual 4305 ~ApplicationDelegate () 4306 { 4307 } 4308 virtual bool 4309 WindowDelegateDraw (Window &window, bool force) 4310 { 4311 return false; // Drawing not handled, let standard window drawing happen 4312 } 4313 4314 virtual HandleCharResult 4315 WindowDelegateHandleChar (Window &window, int key) 4316 { 4317 switch (key) 4318 { 4319 case '\t': 4320 window.SelectNextWindowAsActive(); 4321 return eKeyHandled; 4322 4323 case 'h': 4324 window.CreateHelpSubwindow(); 4325 return eKeyHandled; 4326 4327 case KEY_ESCAPE: 4328 return eQuitApplication; 4329 4330 default: 4331 break; 4332 } 4333 return eKeyNotHandled; 4334 } 4335 4336 4337 virtual const char * 4338 WindowDelegateGetHelpText () 4339 { 4340 return "Welcome to the LLDB curses GUI.\n\n" 4341 "Press the TAB key to change the selected view.\n" 4342 "Each view has its own keyboard shortcuts, press 'h' to open a dialog to display them.\n\n" 4343 "Common key bindings for all views:"; 4344 } 4345 4346 virtual KeyHelp * 4347 WindowDelegateGetKeyHelp () 4348 { 4349 static curses::KeyHelp g_source_view_key_help[] = { 4350 { '\t', "Select next view" }, 4351 { 'h', "Show help dialog with view specific key bindings" }, 4352 { ',', "Page up" }, 4353 { '.', "Page down" }, 4354 { KEY_UP, "Select previous" }, 4355 { KEY_DOWN, "Select next" }, 4356 { KEY_LEFT, "Unexpand or select parent" }, 4357 { KEY_RIGHT, "Expand" }, 4358 { KEY_PPAGE, "Page up" }, 4359 { KEY_NPAGE, "Page down" }, 4360 { '\0', NULL } 4361 }; 4362 return g_source_view_key_help; 4363 } 4364 4365 virtual MenuActionResult 4366 MenuDelegateAction (Menu &menu) 4367 { 4368 switch (menu.GetIdentifier()) 4369 { 4370 case eMenuID_ThreadStepIn: 4371 { 4372 ExecutionContext exe_ctx = m_debugger.GetCommandInterpreter().GetExecutionContext(); 4373 if (exe_ctx.HasThreadScope()) 4374 { 4375 Process *process = exe_ctx.GetProcessPtr(); 4376 if (process && process->IsAlive() && StateIsStoppedState (process->GetState(), true)) 4377 exe_ctx.GetThreadRef().StepIn(true); 4378 } 4379 } 4380 return MenuActionResult::Handled; 4381 4382 case eMenuID_ThreadStepOut: 4383 { 4384 ExecutionContext exe_ctx = m_debugger.GetCommandInterpreter().GetExecutionContext(); 4385 if (exe_ctx.HasThreadScope()) 4386 { 4387 Process *process = exe_ctx.GetProcessPtr(); 4388 if (process && process->IsAlive() && StateIsStoppedState (process->GetState(), true)) 4389 exe_ctx.GetThreadRef().StepOut(); 4390 } 4391 } 4392 return MenuActionResult::Handled; 4393 4394 case eMenuID_ThreadStepOver: 4395 { 4396 ExecutionContext exe_ctx = m_debugger.GetCommandInterpreter().GetExecutionContext(); 4397 if (exe_ctx.HasThreadScope()) 4398 { 4399 Process *process = exe_ctx.GetProcessPtr(); 4400 if (process && process->IsAlive() && StateIsStoppedState (process->GetState(), true)) 4401 exe_ctx.GetThreadRef().StepOver(true); 4402 } 4403 } 4404 return MenuActionResult::Handled; 4405 4406 case eMenuID_ProcessContinue: 4407 { 4408 ExecutionContext exe_ctx = m_debugger.GetCommandInterpreter().GetExecutionContext(); 4409 if (exe_ctx.HasProcessScope()) 4410 { 4411 Process *process = exe_ctx.GetProcessPtr(); 4412 if (process && process->IsAlive() && StateIsStoppedState (process->GetState(), true)) 4413 process->Resume(); 4414 } 4415 } 4416 return MenuActionResult::Handled; 4417 4418 case eMenuID_ProcessKill: 4419 { 4420 ExecutionContext exe_ctx = m_debugger.GetCommandInterpreter().GetExecutionContext(); 4421 if (exe_ctx.HasProcessScope()) 4422 { 4423 Process *process = exe_ctx.GetProcessPtr(); 4424 if (process && process->IsAlive()) 4425 process->Destroy(); 4426 } 4427 } 4428 return MenuActionResult::Handled; 4429 4430 case eMenuID_ProcessHalt: 4431 { 4432 ExecutionContext exe_ctx = m_debugger.GetCommandInterpreter().GetExecutionContext(); 4433 if (exe_ctx.HasProcessScope()) 4434 { 4435 Process *process = exe_ctx.GetProcessPtr(); 4436 if (process && process->IsAlive()) 4437 process->Halt(); 4438 } 4439 } 4440 return MenuActionResult::Handled; 4441 4442 case eMenuID_ProcessDetach: 4443 { 4444 ExecutionContext exe_ctx = m_debugger.GetCommandInterpreter().GetExecutionContext(); 4445 if (exe_ctx.HasProcessScope()) 4446 { 4447 Process *process = exe_ctx.GetProcessPtr(); 4448 if (process && process->IsAlive()) 4449 process->Detach(false); 4450 } 4451 } 4452 return MenuActionResult::Handled; 4453 4454 case eMenuID_Process: 4455 { 4456 // Populate the menu with all of the threads if the process is stopped when 4457 // the Process menu gets selected and is about to display its submenu. 4458 Menus &submenus = menu.GetSubmenus(); 4459 ExecutionContext exe_ctx = m_debugger.GetCommandInterpreter().GetExecutionContext(); 4460 Process *process = exe_ctx.GetProcessPtr(); 4461 if (process && process->IsAlive() && StateIsStoppedState (process->GetState(), true)) 4462 { 4463 if (submenus.size() == 7) 4464 menu.AddSubmenu (MenuSP (new Menu(Menu::Type::Separator))); 4465 else if (submenus.size() > 8) 4466 submenus.erase (submenus.begin() + 8, submenus.end()); 4467 4468 ThreadList &threads = process->GetThreadList(); 4469 Mutex::Locker locker (threads.GetMutex()); 4470 size_t num_threads = threads.GetSize(); 4471 for (size_t i=0; i<num_threads; ++i) 4472 { 4473 ThreadSP thread_sp = threads.GetThreadAtIndex(i); 4474 char menu_char = '\0'; 4475 if (i < 9) 4476 menu_char = '1' + i; 4477 StreamString thread_menu_title; 4478 thread_menu_title.Printf("Thread %u", thread_sp->GetIndexID()); 4479 const char *thread_name = thread_sp->GetName(); 4480 if (thread_name && thread_name[0]) 4481 thread_menu_title.Printf (" %s", thread_name); 4482 else 4483 { 4484 const char *queue_name = thread_sp->GetQueueName(); 4485 if (queue_name && queue_name[0]) 4486 thread_menu_title.Printf (" %s", queue_name); 4487 } 4488 menu.AddSubmenu (MenuSP (new Menu(thread_menu_title.GetString().c_str(), NULL, menu_char, thread_sp->GetID()))); 4489 } 4490 } 4491 else if (submenus.size() > 7) 4492 { 4493 // Remove the separator and any other thread submenu items 4494 // that were previously added 4495 submenus.erase (submenus.begin() + 7, submenus.end()); 4496 } 4497 // Since we are adding and removing items we need to recalculate the name lengths 4498 menu.RecalculateNameLengths(); 4499 } 4500 return MenuActionResult::Handled; 4501 4502 case eMenuID_ViewVariables: 4503 { 4504 WindowSP main_window_sp = m_app.GetMainWindow(); 4505 WindowSP source_window_sp = main_window_sp->FindSubWindow("Source"); 4506 WindowSP variables_window_sp = main_window_sp->FindSubWindow("Variables"); 4507 WindowSP registers_window_sp = main_window_sp->FindSubWindow("Registers"); 4508 const Rect source_bounds = source_window_sp->GetBounds(); 4509 4510 if (variables_window_sp) 4511 { 4512 const Rect variables_bounds = variables_window_sp->GetBounds(); 4513 4514 main_window_sp->RemoveSubWindow(variables_window_sp.get()); 4515 4516 if (registers_window_sp) 4517 { 4518 // We have a registers window, so give all the area back to the registers window 4519 Rect registers_bounds = variables_bounds; 4520 registers_bounds.size.width = source_bounds.size.width; 4521 registers_window_sp->SetBounds(registers_bounds); 4522 } 4523 else 4524 { 4525 // We have no registers window showing so give the bottom 4526 // area back to the source view 4527 source_window_sp->Resize (source_bounds.size.width, 4528 source_bounds.size.height + variables_bounds.size.height); 4529 } 4530 } 4531 else 4532 { 4533 Rect new_variables_rect; 4534 if (registers_window_sp) 4535 { 4536 // We have a registers window so split the area of the registers 4537 // window into two columns where the left hand side will be the 4538 // variables and the right hand side will be the registers 4539 const Rect variables_bounds = registers_window_sp->GetBounds(); 4540 Rect new_registers_rect; 4541 variables_bounds.VerticalSplitPercentage (0.50, new_variables_rect, new_registers_rect); 4542 registers_window_sp->SetBounds (new_registers_rect); 4543 } 4544 else 4545 { 4546 // No variables window, grab the bottom part of the source window 4547 Rect new_source_rect; 4548 source_bounds.HorizontalSplitPercentage (0.70, new_source_rect, new_variables_rect); 4549 source_window_sp->SetBounds (new_source_rect); 4550 } 4551 WindowSP new_window_sp = main_window_sp->CreateSubWindow ("Variables", 4552 new_variables_rect, 4553 false); 4554 new_window_sp->SetDelegate (WindowDelegateSP(new FrameVariablesWindowDelegate(m_debugger))); 4555 } 4556 touchwin(stdscr); 4557 } 4558 return MenuActionResult::Handled; 4559 4560 case eMenuID_ViewRegisters: 4561 { 4562 WindowSP main_window_sp = m_app.GetMainWindow(); 4563 WindowSP source_window_sp = main_window_sp->FindSubWindow("Source"); 4564 WindowSP variables_window_sp = main_window_sp->FindSubWindow("Variables"); 4565 WindowSP registers_window_sp = main_window_sp->FindSubWindow("Registers"); 4566 const Rect source_bounds = source_window_sp->GetBounds(); 4567 4568 if (registers_window_sp) 4569 { 4570 if (variables_window_sp) 4571 { 4572 const Rect variables_bounds = variables_window_sp->GetBounds(); 4573 4574 // We have a variables window, so give all the area back to the variables window 4575 variables_window_sp->Resize (variables_bounds.size.width + registers_window_sp->GetWidth(), 4576 variables_bounds.size.height); 4577 } 4578 else 4579 { 4580 // We have no variables window showing so give the bottom 4581 // area back to the source view 4582 source_window_sp->Resize (source_bounds.size.width, 4583 source_bounds.size.height + registers_window_sp->GetHeight()); 4584 } 4585 main_window_sp->RemoveSubWindow(registers_window_sp.get()); 4586 } 4587 else 4588 { 4589 Rect new_regs_rect; 4590 if (variables_window_sp) 4591 { 4592 // We have a variables window, split it into two columns 4593 // where the left hand side will be the variables and the 4594 // right hand side will be the registers 4595 const Rect variables_bounds = variables_window_sp->GetBounds(); 4596 Rect new_vars_rect; 4597 variables_bounds.VerticalSplitPercentage (0.50, new_vars_rect, new_regs_rect); 4598 variables_window_sp->SetBounds (new_vars_rect); 4599 } 4600 else 4601 { 4602 // No registers window, grab the bottom part of the source window 4603 Rect new_source_rect; 4604 source_bounds.HorizontalSplitPercentage (0.70, new_source_rect, new_regs_rect); 4605 source_window_sp->SetBounds (new_source_rect); 4606 } 4607 WindowSP new_window_sp = main_window_sp->CreateSubWindow ("Registers", 4608 new_regs_rect, 4609 false); 4610 new_window_sp->SetDelegate (WindowDelegateSP(new RegistersWindowDelegate(m_debugger))); 4611 } 4612 touchwin(stdscr); 4613 } 4614 return MenuActionResult::Handled; 4615 4616 case eMenuID_HelpGUIHelp: 4617 m_app.GetMainWindow ()->CreateHelpSubwindow(); 4618 return MenuActionResult::Handled; 4619 4620 default: 4621 break; 4622 } 4623 4624 return MenuActionResult::NotHandled; 4625 } 4626 protected: 4627 Application &m_app; 4628 Debugger &m_debugger; 4629 }; 4630 4631 4632 class StatusBarWindowDelegate : public WindowDelegate 4633 { 4634 public: 4635 StatusBarWindowDelegate (Debugger &debugger) : 4636 m_debugger (debugger) 4637 { 4638 } 4639 4640 virtual 4641 ~StatusBarWindowDelegate () 4642 { 4643 } 4644 virtual bool 4645 WindowDelegateDraw (Window &window, bool force) 4646 { 4647 ExecutionContext exe_ctx = m_debugger.GetCommandInterpreter().GetExecutionContext(); 4648 Process *process = exe_ctx.GetProcessPtr(); 4649 Thread *thread = exe_ctx.GetThreadPtr(); 4650 StackFrame *frame = exe_ctx.GetFramePtr(); 4651 window.Erase(); 4652 window.SetBackground(2); 4653 window.MoveCursor (0, 0); 4654 if (process) 4655 { 4656 const StateType state = process->GetState(); 4657 window.Printf ("Process: %5" PRIu64 " %10s", process->GetID(), StateAsCString(state)); 4658 4659 if (StateIsStoppedState(state, true)) 4660 { 4661 StreamString strm; 4662 const char *format = "Thread: ${thread.id%tid}"; 4663 if (thread && Debugger::FormatPrompt (format, NULL, &exe_ctx, NULL, strm)) 4664 { 4665 window.MoveCursor (40, 0); 4666 window.PutCStringTruncated(strm.GetString().c_str(), 1); 4667 } 4668 4669 window.MoveCursor (60, 0); 4670 if (frame) 4671 window.Printf ("Frame: %3u PC = 0x%16.16" PRIx64, frame->GetFrameIndex(), frame->GetFrameCodeAddress().GetOpcodeLoadAddress (exe_ctx.GetTargetPtr())); 4672 } 4673 else if (state == eStateExited) 4674 { 4675 const char *exit_desc = process->GetExitDescription(); 4676 const int exit_status = process->GetExitStatus(); 4677 if (exit_desc && exit_desc[0]) 4678 window.Printf (" with status = %i (%s)", exit_status, exit_desc); 4679 else 4680 window.Printf (" with status = %i", exit_status); 4681 } 4682 } 4683 window.DeferredRefresh(); 4684 return true; 4685 } 4686 4687 protected: 4688 Debugger &m_debugger; 4689 }; 4690 4691 class SourceFileWindowDelegate : public WindowDelegate 4692 { 4693 public: 4694 SourceFileWindowDelegate (Debugger &debugger) : 4695 WindowDelegate (), 4696 m_debugger (debugger), 4697 m_sc (), 4698 m_file_sp (), 4699 m_disassembly_scope (NULL), 4700 m_disassembly_sp (), 4701 m_disassembly_range (), 4702 m_title (), 4703 m_line_width (4), 4704 m_selected_line (0), 4705 m_pc_line (0), 4706 m_stop_id (0), 4707 m_frame_idx (UINT32_MAX), 4708 m_first_visible_line (0), 4709 m_min_x (0), 4710 m_min_y (0), 4711 m_max_x (0), 4712 m_max_y (0) 4713 { 4714 } 4715 4716 virtual 4717 ~SourceFileWindowDelegate() 4718 { 4719 } 4720 4721 void 4722 Update (const SymbolContext &sc) 4723 { 4724 m_sc = sc; 4725 } 4726 4727 uint32_t 4728 NumVisibleLines () const 4729 { 4730 return m_max_y - m_min_y; 4731 } 4732 4733 virtual const char * 4734 WindowDelegateGetHelpText () 4735 { 4736 return "Source/Disassembly window keyboard shortcuts:"; 4737 } 4738 4739 virtual KeyHelp * 4740 WindowDelegateGetKeyHelp () 4741 { 4742 static curses::KeyHelp g_source_view_key_help[] = { 4743 { KEY_RETURN, "Run to selected line with one shot breakpoint" }, 4744 { KEY_UP, "Select previous source line" }, 4745 { KEY_DOWN, "Select next source line" }, 4746 { KEY_PPAGE, "Page up" }, 4747 { KEY_NPAGE, "Page down" }, 4748 { 'b', "Set breakpoint on selected source/disassembly line" }, 4749 { 'c', "Continue process" }, 4750 { 'd', "Detach and resume process" }, 4751 { 'D', "Detach with process suspended" }, 4752 { 'h', "Show help dialog" }, 4753 { 'k', "Kill process" }, 4754 { 'n', "Step over (source line)" }, 4755 { 'N', "Step over (single instruction)" }, 4756 { 'o', "Step out" }, 4757 { 's', "Step in (source line)" }, 4758 { 'S', "Step in (single instruction)" }, 4759 { ',', "Page up" }, 4760 { '.', "Page down" }, 4761 { '\0', NULL } 4762 }; 4763 return g_source_view_key_help; 4764 } 4765 4766 virtual bool 4767 WindowDelegateDraw (Window &window, bool force) 4768 { 4769 ExecutionContext exe_ctx = m_debugger.GetCommandInterpreter().GetExecutionContext(); 4770 Process *process = exe_ctx.GetProcessPtr(); 4771 Thread *thread = NULL; 4772 4773 bool update_location = false; 4774 if (process) 4775 { 4776 StateType state = process->GetState(); 4777 if (StateIsStoppedState(state, true)) 4778 { 4779 // We are stopped, so it is ok to 4780 update_location = true; 4781 } 4782 } 4783 4784 m_min_x = 1; 4785 m_min_y = 2; 4786 m_max_x = window.GetMaxX()-1; 4787 m_max_y = window.GetMaxY()-1; 4788 4789 const uint32_t num_visible_lines = NumVisibleLines(); 4790 StackFrameSP frame_sp; 4791 bool set_selected_line_to_pc = false; 4792 4793 if (update_location) 4794 { 4795 const bool process_alive = process ? process->IsAlive() : false; 4796 bool thread_changed = false; 4797 if (process_alive) 4798 { 4799 thread = exe_ctx.GetThreadPtr(); 4800 if (thread) 4801 { 4802 frame_sp = thread->GetSelectedFrame(); 4803 auto tid = thread->GetID(); 4804 thread_changed = tid != m_tid; 4805 m_tid = tid; 4806 } 4807 else 4808 { 4809 if (m_tid != LLDB_INVALID_THREAD_ID) 4810 { 4811 thread_changed = true; 4812 m_tid = LLDB_INVALID_THREAD_ID; 4813 } 4814 } 4815 } 4816 const uint32_t stop_id = process ? process->GetStopID() : 0; 4817 const bool stop_id_changed = stop_id != m_stop_id; 4818 bool frame_changed = false; 4819 m_stop_id = stop_id; 4820 m_title.Clear(); 4821 if (frame_sp) 4822 { 4823 m_sc = frame_sp->GetSymbolContext(eSymbolContextEverything); 4824 if (m_sc.module_sp) 4825 { 4826 m_title.Printf("%s", m_sc.module_sp->GetFileSpec().GetFilename().GetCString()); 4827 ConstString func_name = m_sc.GetFunctionName(); 4828 if (func_name) 4829 m_title.Printf("`%s", func_name.GetCString()); 4830 } 4831 const uint32_t frame_idx = frame_sp->GetFrameIndex(); 4832 frame_changed = frame_idx != m_frame_idx; 4833 m_frame_idx = frame_idx; 4834 } 4835 else 4836 { 4837 m_sc.Clear(true); 4838 frame_changed = m_frame_idx != UINT32_MAX; 4839 m_frame_idx = UINT32_MAX; 4840 } 4841 4842 const bool context_changed = thread_changed || frame_changed || stop_id_changed; 4843 4844 if (process_alive) 4845 { 4846 if (m_sc.line_entry.IsValid()) 4847 { 4848 m_pc_line = m_sc.line_entry.line; 4849 if (m_pc_line != UINT32_MAX) 4850 --m_pc_line; // Convert to zero based line number... 4851 // Update the selected line if the stop ID changed... 4852 if (context_changed) 4853 m_selected_line = m_pc_line; 4854 4855 if (m_file_sp && m_file_sp->FileSpecMatches(m_sc.line_entry.file)) 4856 { 4857 // Same file, nothing to do, we should either have the 4858 // lines or not (source file missing) 4859 if (m_selected_line >= static_cast<size_t>(m_first_visible_line)) 4860 { 4861 if (m_selected_line >= m_first_visible_line + num_visible_lines) 4862 m_first_visible_line = m_selected_line - 10; 4863 } 4864 else 4865 { 4866 if (m_selected_line > 10) 4867 m_first_visible_line = m_selected_line - 10; 4868 else 4869 m_first_visible_line = 0; 4870 } 4871 } 4872 else 4873 { 4874 // File changed, set selected line to the line with the PC 4875 m_selected_line = m_pc_line; 4876 m_file_sp = m_debugger.GetSourceManager().GetFile(m_sc.line_entry.file); 4877 if (m_file_sp) 4878 { 4879 const size_t num_lines = m_file_sp->GetNumLines(); 4880 int m_line_width = 1; 4881 for (size_t n = num_lines; n >= 10; n = n / 10) 4882 ++m_line_width; 4883 4884 snprintf (m_line_format, sizeof(m_line_format), " %%%iu ", m_line_width); 4885 if (num_lines < num_visible_lines || m_selected_line < num_visible_lines) 4886 m_first_visible_line = 0; 4887 else 4888 m_first_visible_line = m_selected_line - 10; 4889 } 4890 } 4891 } 4892 else 4893 { 4894 m_file_sp.reset(); 4895 } 4896 4897 if (!m_file_sp || m_file_sp->GetNumLines() == 0) 4898 { 4899 // Show disassembly 4900 bool prefer_file_cache = false; 4901 if (m_sc.function) 4902 { 4903 if (m_disassembly_scope != m_sc.function) 4904 { 4905 m_disassembly_scope = m_sc.function; 4906 m_disassembly_sp = m_sc.function->GetInstructions (exe_ctx, NULL, prefer_file_cache); 4907 if (m_disassembly_sp) 4908 { 4909 set_selected_line_to_pc = true; 4910 m_disassembly_range = m_sc.function->GetAddressRange(); 4911 } 4912 else 4913 { 4914 m_disassembly_range.Clear(); 4915 } 4916 } 4917 else 4918 { 4919 set_selected_line_to_pc = context_changed; 4920 } 4921 } 4922 else if (m_sc.symbol) 4923 { 4924 if (m_disassembly_scope != m_sc.symbol) 4925 { 4926 m_disassembly_scope = m_sc.symbol; 4927 m_disassembly_sp = m_sc.symbol->GetInstructions (exe_ctx, NULL, prefer_file_cache); 4928 if (m_disassembly_sp) 4929 { 4930 set_selected_line_to_pc = true; 4931 m_disassembly_range.GetBaseAddress() = m_sc.symbol->GetAddress(); 4932 m_disassembly_range.SetByteSize(m_sc.symbol->GetByteSize()); 4933 } 4934 else 4935 { 4936 m_disassembly_range.Clear(); 4937 } 4938 } 4939 else 4940 { 4941 set_selected_line_to_pc = context_changed; 4942 } 4943 } 4944 } 4945 } 4946 else 4947 { 4948 m_pc_line = UINT32_MAX; 4949 } 4950 } 4951 4952 const int window_width = window.GetWidth(); 4953 window.Erase(); 4954 window.DrawTitleBox ("Sources"); 4955 if (!m_title.GetString().empty()) 4956 { 4957 window.AttributeOn(A_REVERSE); 4958 window.MoveCursor(1, 1); 4959 window.PutChar(' '); 4960 window.PutCStringTruncated(m_title.GetString().c_str(), 1); 4961 int x = window.GetCursorX(); 4962 if (x < window_width - 1) 4963 { 4964 window.Printf ("%*s", window_width - x - 1, ""); 4965 } 4966 window.AttributeOff(A_REVERSE); 4967 } 4968 4969 Target *target = exe_ctx.GetTargetPtr(); 4970 const size_t num_source_lines = GetNumSourceLines(); 4971 if (num_source_lines > 0) 4972 { 4973 // Display source 4974 BreakpointLines bp_lines; 4975 if (target) 4976 { 4977 BreakpointList &bp_list = target->GetBreakpointList(); 4978 const size_t num_bps = bp_list.GetSize(); 4979 for (size_t bp_idx=0; bp_idx<num_bps; ++bp_idx) 4980 { 4981 BreakpointSP bp_sp = bp_list.GetBreakpointAtIndex(bp_idx); 4982 const size_t num_bps_locs = bp_sp->GetNumLocations(); 4983 for (size_t bp_loc_idx=0; bp_loc_idx<num_bps_locs; ++bp_loc_idx) 4984 { 4985 BreakpointLocationSP bp_loc_sp = bp_sp->GetLocationAtIndex(bp_loc_idx); 4986 LineEntry bp_loc_line_entry; 4987 if (bp_loc_sp->GetAddress().CalculateSymbolContextLineEntry (bp_loc_line_entry)) 4988 { 4989 if (m_file_sp->GetFileSpec() == bp_loc_line_entry.file) 4990 { 4991 bp_lines.insert(bp_loc_line_entry.line); 4992 } 4993 } 4994 } 4995 } 4996 } 4997 4998 const attr_t selected_highlight_attr = A_REVERSE; 4999 const attr_t pc_highlight_attr = COLOR_PAIR(1); 5000 5001 for (size_t i=0; i<num_visible_lines; ++i) 5002 { 5003 const uint32_t curr_line = m_first_visible_line + i; 5004 if (curr_line < num_source_lines) 5005 { 5006 const int line_y = m_min_y+i; 5007 window.MoveCursor(1, line_y); 5008 const bool is_pc_line = curr_line == m_pc_line; 5009 const bool line_is_selected = m_selected_line == curr_line; 5010 // Highlight the line as the PC line first, then if the selected line 5011 // isn't the same as the PC line, highlight it differently 5012 attr_t highlight_attr = 0; 5013 attr_t bp_attr = 0; 5014 if (is_pc_line) 5015 highlight_attr = pc_highlight_attr; 5016 else if (line_is_selected) 5017 highlight_attr = selected_highlight_attr; 5018 5019 if (bp_lines.find(curr_line+1) != bp_lines.end()) 5020 bp_attr = COLOR_PAIR(2); 5021 5022 if (bp_attr) 5023 window.AttributeOn(bp_attr); 5024 5025 window.Printf (m_line_format, curr_line + 1); 5026 5027 if (bp_attr) 5028 window.AttributeOff(bp_attr); 5029 5030 window.PutChar(ACS_VLINE); 5031 // Mark the line with the PC with a diamond 5032 if (is_pc_line) 5033 window.PutChar(ACS_DIAMOND); 5034 else 5035 window.PutChar(' '); 5036 5037 if (highlight_attr) 5038 window.AttributeOn(highlight_attr); 5039 const uint32_t line_len = m_file_sp->GetLineLength(curr_line + 1, false); 5040 if (line_len > 0) 5041 window.PutCString(m_file_sp->PeekLineData(curr_line + 1), line_len); 5042 5043 if (is_pc_line && frame_sp && frame_sp->GetConcreteFrameIndex() == 0) 5044 { 5045 StopInfoSP stop_info_sp; 5046 if (thread) 5047 stop_info_sp = thread->GetStopInfo(); 5048 if (stop_info_sp) 5049 { 5050 const char *stop_description = stop_info_sp->GetDescription(); 5051 if (stop_description && stop_description[0]) 5052 { 5053 size_t stop_description_len = strlen(stop_description); 5054 int desc_x = window_width - stop_description_len - 16; 5055 window.Printf ("%*s", desc_x - window.GetCursorX(), ""); 5056 //window.MoveCursor(window_width - stop_description_len - 15, line_y); 5057 window.Printf ("<<< Thread %u: %s ", thread->GetIndexID(), stop_description); 5058 } 5059 } 5060 else 5061 { 5062 window.Printf ("%*s", window_width - window.GetCursorX() - 1, ""); 5063 } 5064 } 5065 if (highlight_attr) 5066 window.AttributeOff(highlight_attr); 5067 5068 } 5069 else 5070 { 5071 break; 5072 } 5073 } 5074 } 5075 else 5076 { 5077 size_t num_disassembly_lines = GetNumDisassemblyLines(); 5078 if (num_disassembly_lines > 0) 5079 { 5080 // Display disassembly 5081 BreakpointAddrs bp_file_addrs; 5082 Target *target = exe_ctx.GetTargetPtr(); 5083 if (target) 5084 { 5085 BreakpointList &bp_list = target->GetBreakpointList(); 5086 const size_t num_bps = bp_list.GetSize(); 5087 for (size_t bp_idx=0; bp_idx<num_bps; ++bp_idx) 5088 { 5089 BreakpointSP bp_sp = bp_list.GetBreakpointAtIndex(bp_idx); 5090 const size_t num_bps_locs = bp_sp->GetNumLocations(); 5091 for (size_t bp_loc_idx=0; bp_loc_idx<num_bps_locs; ++bp_loc_idx) 5092 { 5093 BreakpointLocationSP bp_loc_sp = bp_sp->GetLocationAtIndex(bp_loc_idx); 5094 LineEntry bp_loc_line_entry; 5095 const lldb::addr_t file_addr = bp_loc_sp->GetAddress().GetFileAddress(); 5096 if (file_addr != LLDB_INVALID_ADDRESS) 5097 { 5098 if (m_disassembly_range.ContainsFileAddress(file_addr)) 5099 bp_file_addrs.insert(file_addr); 5100 } 5101 } 5102 } 5103 } 5104 5105 const attr_t selected_highlight_attr = A_REVERSE; 5106 const attr_t pc_highlight_attr = COLOR_PAIR(1); 5107 5108 StreamString strm; 5109 5110 InstructionList &insts = m_disassembly_sp->GetInstructionList(); 5111 Address pc_address; 5112 5113 if (frame_sp) 5114 pc_address = frame_sp->GetFrameCodeAddress(); 5115 const uint32_t pc_idx = pc_address.IsValid() ? insts.GetIndexOfInstructionAtAddress (pc_address) : UINT32_MAX; 5116 if (set_selected_line_to_pc) 5117 { 5118 m_selected_line = pc_idx; 5119 } 5120 5121 const uint32_t non_visible_pc_offset = (num_visible_lines / 5); 5122 if (static_cast<size_t>(m_first_visible_line) >= num_disassembly_lines) 5123 m_first_visible_line = 0; 5124 5125 if (pc_idx < num_disassembly_lines) 5126 { 5127 if (pc_idx < static_cast<uint32_t>(m_first_visible_line) || 5128 pc_idx >= m_first_visible_line + num_visible_lines) 5129 m_first_visible_line = pc_idx - non_visible_pc_offset; 5130 } 5131 5132 for (size_t i=0; i<num_visible_lines; ++i) 5133 { 5134 const uint32_t inst_idx = m_first_visible_line + i; 5135 Instruction *inst = insts.GetInstructionAtIndex(inst_idx).get(); 5136 if (!inst) 5137 break; 5138 5139 const int line_y = m_min_y+i; 5140 window.MoveCursor(1, line_y); 5141 const bool is_pc_line = frame_sp && inst_idx == pc_idx; 5142 const bool line_is_selected = m_selected_line == inst_idx; 5143 // Highlight the line as the PC line first, then if the selected line 5144 // isn't the same as the PC line, highlight it differently 5145 attr_t highlight_attr = 0; 5146 attr_t bp_attr = 0; 5147 if (is_pc_line) 5148 highlight_attr = pc_highlight_attr; 5149 else if (line_is_selected) 5150 highlight_attr = selected_highlight_attr; 5151 5152 if (bp_file_addrs.find(inst->GetAddress().GetFileAddress()) != bp_file_addrs.end()) 5153 bp_attr = COLOR_PAIR(2); 5154 5155 if (bp_attr) 5156 window.AttributeOn(bp_attr); 5157 5158 window.Printf (" 0x%16.16llx ", 5159 static_cast<unsigned long long>(inst->GetAddress().GetLoadAddress(target))); 5160 5161 if (bp_attr) 5162 window.AttributeOff(bp_attr); 5163 5164 window.PutChar(ACS_VLINE); 5165 // Mark the line with the PC with a diamond 5166 if (is_pc_line) 5167 window.PutChar(ACS_DIAMOND); 5168 else 5169 window.PutChar(' '); 5170 5171 if (highlight_attr) 5172 window.AttributeOn(highlight_attr); 5173 5174 const char *mnemonic = inst->GetMnemonic(&exe_ctx); 5175 const char *operands = inst->GetOperands(&exe_ctx); 5176 const char *comment = inst->GetComment(&exe_ctx); 5177 5178 if (mnemonic && mnemonic[0] == '\0') 5179 mnemonic = NULL; 5180 if (operands && operands[0] == '\0') 5181 operands = NULL; 5182 if (comment && comment[0] == '\0') 5183 comment = NULL; 5184 5185 strm.Clear(); 5186 5187 if (mnemonic && operands && comment) 5188 strm.Printf ("%-8s %-25s ; %s", mnemonic, operands, comment); 5189 else if (mnemonic && operands) 5190 strm.Printf ("%-8s %s", mnemonic, operands); 5191 else if (mnemonic) 5192 strm.Printf ("%s", mnemonic); 5193 5194 int right_pad = 1; 5195 window.PutCStringTruncated(strm.GetString().c_str(), right_pad); 5196 5197 if (is_pc_line && frame_sp && frame_sp->GetConcreteFrameIndex() == 0) 5198 { 5199 StopInfoSP stop_info_sp; 5200 if (thread) 5201 stop_info_sp = thread->GetStopInfo(); 5202 if (stop_info_sp) 5203 { 5204 const char *stop_description = stop_info_sp->GetDescription(); 5205 if (stop_description && stop_description[0]) 5206 { 5207 size_t stop_description_len = strlen(stop_description); 5208 int desc_x = window_width - stop_description_len - 16; 5209 window.Printf ("%*s", desc_x - window.GetCursorX(), ""); 5210 //window.MoveCursor(window_width - stop_description_len - 15, line_y); 5211 window.Printf ("<<< Thread %u: %s ", thread->GetIndexID(), stop_description); 5212 } 5213 } 5214 else 5215 { 5216 window.Printf ("%*s", window_width - window.GetCursorX() - 1, ""); 5217 } 5218 } 5219 if (highlight_attr) 5220 window.AttributeOff(highlight_attr); 5221 } 5222 } 5223 } 5224 window.DeferredRefresh(); 5225 return true; // Drawing handled 5226 } 5227 5228 size_t 5229 GetNumLines () 5230 { 5231 size_t num_lines = GetNumSourceLines(); 5232 if (num_lines == 0) 5233 num_lines = GetNumDisassemblyLines(); 5234 return num_lines; 5235 } 5236 5237 size_t 5238 GetNumSourceLines () const 5239 { 5240 if (m_file_sp) 5241 return m_file_sp->GetNumLines(); 5242 return 0; 5243 } 5244 size_t 5245 GetNumDisassemblyLines () const 5246 { 5247 if (m_disassembly_sp) 5248 return m_disassembly_sp->GetInstructionList().GetSize(); 5249 return 0; 5250 } 5251 5252 virtual HandleCharResult 5253 WindowDelegateHandleChar (Window &window, int c) 5254 { 5255 const uint32_t num_visible_lines = NumVisibleLines(); 5256 const size_t num_lines = GetNumLines (); 5257 5258 switch (c) 5259 { 5260 case ',': 5261 case KEY_PPAGE: 5262 // Page up key 5263 if (static_cast<uint32_t>(m_first_visible_line) > num_visible_lines) 5264 m_first_visible_line -= num_visible_lines; 5265 else 5266 m_first_visible_line = 0; 5267 m_selected_line = m_first_visible_line; 5268 return eKeyHandled; 5269 5270 case '.': 5271 case KEY_NPAGE: 5272 // Page down key 5273 { 5274 if (m_first_visible_line + num_visible_lines < num_lines) 5275 m_first_visible_line += num_visible_lines; 5276 else if (num_lines < num_visible_lines) 5277 m_first_visible_line = 0; 5278 else 5279 m_first_visible_line = num_lines - num_visible_lines; 5280 m_selected_line = m_first_visible_line; 5281 } 5282 return eKeyHandled; 5283 5284 case KEY_UP: 5285 if (m_selected_line > 0) 5286 { 5287 m_selected_line--; 5288 if (static_cast<size_t>(m_first_visible_line) > m_selected_line) 5289 m_first_visible_line = m_selected_line; 5290 } 5291 return eKeyHandled; 5292 5293 case KEY_DOWN: 5294 if (m_selected_line + 1 < num_lines) 5295 { 5296 m_selected_line++; 5297 if (m_first_visible_line + num_visible_lines < m_selected_line) 5298 m_first_visible_line++; 5299 } 5300 return eKeyHandled; 5301 5302 case '\r': 5303 case '\n': 5304 case KEY_ENTER: 5305 // Set a breakpoint and run to the line using a one shot breakpoint 5306 if (GetNumSourceLines() > 0) 5307 { 5308 ExecutionContext exe_ctx = m_debugger.GetCommandInterpreter().GetExecutionContext(); 5309 if (exe_ctx.HasProcessScope() && exe_ctx.GetProcessRef().IsAlive()) 5310 { 5311 BreakpointSP bp_sp = exe_ctx.GetTargetRef().CreateBreakpoint (NULL, // Don't limit the breakpoint to certain modules 5312 m_file_sp->GetFileSpec(), // Source file 5313 m_selected_line + 1, // Source line number (m_selected_line is zero based) 5314 eLazyBoolCalculate, // Check inlines using global setting 5315 eLazyBoolCalculate, // Skip prologue using global setting, 5316 false, // internal 5317 false); // request_hardware 5318 // Make breakpoint one shot 5319 bp_sp->GetOptions()->SetOneShot(true); 5320 exe_ctx.GetProcessRef().Resume(); 5321 } 5322 } 5323 else if (m_selected_line < GetNumDisassemblyLines()) 5324 { 5325 const Instruction *inst = m_disassembly_sp->GetInstructionList().GetInstructionAtIndex(m_selected_line).get(); 5326 ExecutionContext exe_ctx = m_debugger.GetCommandInterpreter().GetExecutionContext(); 5327 if (exe_ctx.HasTargetScope()) 5328 { 5329 Address addr = inst->GetAddress(); 5330 BreakpointSP bp_sp = exe_ctx.GetTargetRef().CreateBreakpoint (addr, // lldb_private::Address 5331 false, // internal 5332 false); // request_hardware 5333 // Make breakpoint one shot 5334 bp_sp->GetOptions()->SetOneShot(true); 5335 exe_ctx.GetProcessRef().Resume(); 5336 } 5337 } 5338 return eKeyHandled; 5339 5340 case 'b': // 'b' == toggle breakpoint on currently selected line 5341 if (m_selected_line < GetNumSourceLines()) 5342 { 5343 ExecutionContext exe_ctx = m_debugger.GetCommandInterpreter().GetExecutionContext(); 5344 if (exe_ctx.HasTargetScope()) 5345 { 5346 BreakpointSP bp_sp = exe_ctx.GetTargetRef().CreateBreakpoint (NULL, // Don't limit the breakpoint to certain modules 5347 m_file_sp->GetFileSpec(), // Source file 5348 m_selected_line + 1, // Source line number (m_selected_line is zero based) 5349 eLazyBoolCalculate, // Check inlines using global setting 5350 eLazyBoolCalculate, // Skip prologue using global setting, 5351 false, // internal 5352 false); // request_hardware 5353 } 5354 } 5355 else if (m_selected_line < GetNumDisassemblyLines()) 5356 { 5357 const Instruction *inst = m_disassembly_sp->GetInstructionList().GetInstructionAtIndex(m_selected_line).get(); 5358 ExecutionContext exe_ctx = m_debugger.GetCommandInterpreter().GetExecutionContext(); 5359 if (exe_ctx.HasTargetScope()) 5360 { 5361 Address addr = inst->GetAddress(); 5362 BreakpointSP bp_sp = exe_ctx.GetTargetRef().CreateBreakpoint (addr, // lldb_private::Address 5363 false, // internal 5364 false); // request_hardware 5365 } 5366 } 5367 return eKeyHandled; 5368 5369 case 'd': // 'd' == detach and let run 5370 case 'D': // 'D' == detach and keep stopped 5371 { 5372 ExecutionContext exe_ctx = m_debugger.GetCommandInterpreter().GetExecutionContext(); 5373 if (exe_ctx.HasProcessScope()) 5374 exe_ctx.GetProcessRef().Detach(c == 'D'); 5375 } 5376 return eKeyHandled; 5377 5378 case 'k': 5379 // 'k' == kill 5380 { 5381 ExecutionContext exe_ctx = m_debugger.GetCommandInterpreter().GetExecutionContext(); 5382 if (exe_ctx.HasProcessScope()) 5383 exe_ctx.GetProcessRef().Destroy(); 5384 } 5385 return eKeyHandled; 5386 5387 case 'c': 5388 // 'c' == continue 5389 { 5390 ExecutionContext exe_ctx = m_debugger.GetCommandInterpreter().GetExecutionContext(); 5391 if (exe_ctx.HasProcessScope()) 5392 exe_ctx.GetProcessRef().Resume(); 5393 } 5394 return eKeyHandled; 5395 5396 case 'o': 5397 // 'o' == step out 5398 { 5399 ExecutionContext exe_ctx = m_debugger.GetCommandInterpreter().GetExecutionContext(); 5400 if (exe_ctx.HasThreadScope() && StateIsStoppedState (exe_ctx.GetProcessRef().GetState(), true)) 5401 { 5402 exe_ctx.GetThreadRef().StepOut(); 5403 } 5404 } 5405 return eKeyHandled; 5406 case 'n': // 'n' == step over 5407 case 'N': // 'N' == step over instruction 5408 { 5409 ExecutionContext exe_ctx = m_debugger.GetCommandInterpreter().GetExecutionContext(); 5410 if (exe_ctx.HasThreadScope() && StateIsStoppedState (exe_ctx.GetProcessRef().GetState(), true)) 5411 { 5412 bool source_step = (c == 'n'); 5413 exe_ctx.GetThreadRef().StepOver(source_step); 5414 } 5415 } 5416 return eKeyHandled; 5417 case 's': // 's' == step into 5418 case 'S': // 'S' == step into instruction 5419 { 5420 ExecutionContext exe_ctx = m_debugger.GetCommandInterpreter().GetExecutionContext(); 5421 if (exe_ctx.HasThreadScope() && StateIsStoppedState (exe_ctx.GetProcessRef().GetState(), true)) 5422 { 5423 bool source_step = (c == 's'); 5424 exe_ctx.GetThreadRef().StepIn(source_step); 5425 } 5426 } 5427 return eKeyHandled; 5428 5429 case 'h': 5430 window.CreateHelpSubwindow (); 5431 return eKeyHandled; 5432 5433 default: 5434 break; 5435 } 5436 return eKeyNotHandled; 5437 } 5438 5439 protected: 5440 typedef std::set<uint32_t> BreakpointLines; 5441 typedef std::set<lldb::addr_t> BreakpointAddrs; 5442 5443 Debugger &m_debugger; 5444 SymbolContext m_sc; 5445 SourceManager::FileSP m_file_sp; 5446 SymbolContextScope *m_disassembly_scope; 5447 lldb::DisassemblerSP m_disassembly_sp; 5448 AddressRange m_disassembly_range; 5449 StreamString m_title; 5450 lldb::user_id_t m_tid; 5451 char m_line_format[8]; 5452 int m_line_width; 5453 uint32_t m_selected_line; // The selected line 5454 uint32_t m_pc_line; // The line with the PC 5455 uint32_t m_stop_id; 5456 uint32_t m_frame_idx; 5457 int m_first_visible_line; 5458 int m_min_x; 5459 int m_min_y; 5460 int m_max_x; 5461 int m_max_y; 5462 5463 }; 5464 5465 DisplayOptions ValueObjectListDelegate::g_options = { true }; 5466 5467 IOHandlerCursesGUI::IOHandlerCursesGUI (Debugger &debugger) : 5468 IOHandler (debugger, IOHandler::Type::Curses) 5469 { 5470 } 5471 5472 void 5473 IOHandlerCursesGUI::Activate () 5474 { 5475 IOHandler::Activate(); 5476 if (!m_app_ap) 5477 { 5478 m_app_ap.reset (new Application (GetInputFILE(), GetOutputFILE())); 5479 5480 5481 // This is both a window and a menu delegate 5482 std::shared_ptr<ApplicationDelegate> app_delegate_sp(new ApplicationDelegate(*m_app_ap, m_debugger)); 5483 5484 MenuDelegateSP app_menu_delegate_sp = std::static_pointer_cast<MenuDelegate>(app_delegate_sp); 5485 MenuSP lldb_menu_sp(new Menu("LLDB" , "F1", KEY_F(1), ApplicationDelegate::eMenuID_LLDB)); 5486 MenuSP exit_menuitem_sp(new Menu("Exit", NULL, 'x', ApplicationDelegate::eMenuID_LLDBExit)); 5487 exit_menuitem_sp->SetCannedResult(MenuActionResult::Quit); 5488 lldb_menu_sp->AddSubmenu (MenuSP (new Menu("About LLDB", NULL, 'a', ApplicationDelegate::eMenuID_LLDBAbout))); 5489 lldb_menu_sp->AddSubmenu (MenuSP (new Menu(Menu::Type::Separator))); 5490 lldb_menu_sp->AddSubmenu (exit_menuitem_sp); 5491 5492 MenuSP target_menu_sp(new Menu("Target" ,"F2", KEY_F(2), ApplicationDelegate::eMenuID_Target)); 5493 target_menu_sp->AddSubmenu (MenuSP (new Menu("Create", NULL, 'c', ApplicationDelegate::eMenuID_TargetCreate))); 5494 target_menu_sp->AddSubmenu (MenuSP (new Menu("Delete", NULL, 'd', ApplicationDelegate::eMenuID_TargetDelete))); 5495 5496 MenuSP process_menu_sp(new Menu("Process", "F3", KEY_F(3), ApplicationDelegate::eMenuID_Process)); 5497 process_menu_sp->AddSubmenu (MenuSP (new Menu("Attach" , NULL, 'a', ApplicationDelegate::eMenuID_ProcessAttach))); 5498 process_menu_sp->AddSubmenu (MenuSP (new Menu("Detach" , NULL, 'd', ApplicationDelegate::eMenuID_ProcessDetach))); 5499 process_menu_sp->AddSubmenu (MenuSP (new Menu("Launch" , NULL, 'l', ApplicationDelegate::eMenuID_ProcessLaunch))); 5500 process_menu_sp->AddSubmenu (MenuSP (new Menu(Menu::Type::Separator))); 5501 process_menu_sp->AddSubmenu (MenuSP (new Menu("Continue", NULL, 'c', ApplicationDelegate::eMenuID_ProcessContinue))); 5502 process_menu_sp->AddSubmenu (MenuSP (new Menu("Halt" , NULL, 'h', ApplicationDelegate::eMenuID_ProcessHalt))); 5503 process_menu_sp->AddSubmenu (MenuSP (new Menu("Kill" , NULL, 'k', ApplicationDelegate::eMenuID_ProcessKill))); 5504 5505 MenuSP thread_menu_sp(new Menu("Thread", "F4", KEY_F(4), ApplicationDelegate::eMenuID_Thread)); 5506 thread_menu_sp->AddSubmenu (MenuSP (new Menu("Step In" , NULL, 'i', ApplicationDelegate::eMenuID_ThreadStepIn))); 5507 thread_menu_sp->AddSubmenu (MenuSP (new Menu("Step Over", NULL, 'v', ApplicationDelegate::eMenuID_ThreadStepOver))); 5508 thread_menu_sp->AddSubmenu (MenuSP (new Menu("Step Out" , NULL, 'o', ApplicationDelegate::eMenuID_ThreadStepOut))); 5509 5510 MenuSP view_menu_sp(new Menu("View", "F5", KEY_F(5), ApplicationDelegate::eMenuID_View)); 5511 view_menu_sp->AddSubmenu (MenuSP (new Menu("Backtrace", NULL, 'b', ApplicationDelegate::eMenuID_ViewBacktrace))); 5512 view_menu_sp->AddSubmenu (MenuSP (new Menu("Registers", NULL, 'r', ApplicationDelegate::eMenuID_ViewRegisters))); 5513 view_menu_sp->AddSubmenu (MenuSP (new Menu("Source" , NULL, 's', ApplicationDelegate::eMenuID_ViewSource))); 5514 view_menu_sp->AddSubmenu (MenuSP (new Menu("Variables", NULL, 'v', ApplicationDelegate::eMenuID_ViewVariables))); 5515 5516 MenuSP help_menu_sp(new Menu("Help", "F6", KEY_F(6), ApplicationDelegate::eMenuID_Help)); 5517 help_menu_sp->AddSubmenu (MenuSP (new Menu("GUI Help", NULL, 'g', ApplicationDelegate::eMenuID_HelpGUIHelp))); 5518 5519 m_app_ap->Initialize(); 5520 WindowSP &main_window_sp = m_app_ap->GetMainWindow(); 5521 5522 MenuSP menubar_sp(new Menu(Menu::Type::Bar)); 5523 menubar_sp->AddSubmenu (lldb_menu_sp); 5524 menubar_sp->AddSubmenu (target_menu_sp); 5525 menubar_sp->AddSubmenu (process_menu_sp); 5526 menubar_sp->AddSubmenu (thread_menu_sp); 5527 menubar_sp->AddSubmenu (view_menu_sp); 5528 menubar_sp->AddSubmenu (help_menu_sp); 5529 menubar_sp->SetDelegate(app_menu_delegate_sp); 5530 5531 Rect content_bounds = main_window_sp->GetFrame(); 5532 Rect menubar_bounds = content_bounds.MakeMenuBar(); 5533 Rect status_bounds = content_bounds.MakeStatusBar(); 5534 Rect source_bounds; 5535 Rect variables_bounds; 5536 Rect threads_bounds; 5537 Rect source_variables_bounds; 5538 content_bounds.VerticalSplitPercentage(0.80, source_variables_bounds, threads_bounds); 5539 source_variables_bounds.HorizontalSplitPercentage(0.70, source_bounds, variables_bounds); 5540 5541 WindowSP menubar_window_sp = main_window_sp->CreateSubWindow("Menubar", menubar_bounds, false); 5542 // Let the menubar get keys if the active window doesn't handle the 5543 // keys that are typed so it can respond to menubar key presses. 5544 menubar_window_sp->SetCanBeActive(false); // Don't let the menubar become the active window 5545 menubar_window_sp->SetDelegate(menubar_sp); 5546 5547 WindowSP source_window_sp (main_window_sp->CreateSubWindow("Source", 5548 source_bounds, 5549 true)); 5550 WindowSP variables_window_sp (main_window_sp->CreateSubWindow("Variables", 5551 variables_bounds, 5552 false)); 5553 WindowSP threads_window_sp (main_window_sp->CreateSubWindow("Threads", 5554 threads_bounds, 5555 false)); 5556 WindowSP status_window_sp (main_window_sp->CreateSubWindow("Status", 5557 status_bounds, 5558 false)); 5559 status_window_sp->SetCanBeActive(false); // Don't let the status bar become the active window 5560 main_window_sp->SetDelegate (std::static_pointer_cast<WindowDelegate>(app_delegate_sp)); 5561 source_window_sp->SetDelegate (WindowDelegateSP(new SourceFileWindowDelegate(m_debugger))); 5562 variables_window_sp->SetDelegate (WindowDelegateSP(new FrameVariablesWindowDelegate(m_debugger))); 5563 TreeDelegateSP thread_delegate_sp (new ThreadsTreeDelegate(m_debugger)); 5564 threads_window_sp->SetDelegate (WindowDelegateSP(new TreeWindowDelegate(m_debugger, thread_delegate_sp))); 5565 status_window_sp->SetDelegate (WindowDelegateSP(new StatusBarWindowDelegate(m_debugger))); 5566 5567 // Show the main help window once the first time the curses GUI is launched 5568 static bool g_showed_help = false; 5569 if (!g_showed_help) 5570 { 5571 g_showed_help = true; 5572 main_window_sp->CreateHelpSubwindow(); 5573 } 5574 5575 init_pair (1, COLOR_WHITE , COLOR_BLUE ); 5576 init_pair (2, COLOR_BLACK , COLOR_WHITE ); 5577 init_pair (3, COLOR_MAGENTA , COLOR_WHITE ); 5578 init_pair (4, COLOR_MAGENTA , COLOR_BLACK ); 5579 init_pair (5, COLOR_RED , COLOR_BLACK ); 5580 5581 } 5582 } 5583 5584 void 5585 IOHandlerCursesGUI::Deactivate () 5586 { 5587 m_app_ap->Terminate(); 5588 } 5589 5590 void 5591 IOHandlerCursesGUI::Run () 5592 { 5593 m_app_ap->Run(m_debugger); 5594 SetIsDone(true); 5595 } 5596 5597 5598 IOHandlerCursesGUI::~IOHandlerCursesGUI () 5599 { 5600 5601 } 5602 5603 void 5604 IOHandlerCursesGUI::Hide () 5605 { 5606 } 5607 5608 5609 void 5610 IOHandlerCursesGUI::Refresh () 5611 { 5612 } 5613 5614 void 5615 IOHandlerCursesGUI::Cancel () 5616 { 5617 } 5618 5619 bool 5620 IOHandlerCursesGUI::Interrupt () 5621 { 5622 return false; 5623 } 5624 5625 5626 void 5627 IOHandlerCursesGUI::GotEOF() 5628 { 5629 } 5630 5631 #endif // #ifndef LLDB_DISABLE_CURSES 5632