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