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