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