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