1 //===-- Driver.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 #include "Driver.h"
11 
12 #include <getopt.h>
13 #include <libgen.h>
14 #include <sys/ioctl.h>
15 #include <termios.h>
16 #include <unistd.h>
17 #include <string.h>
18 #include <stdlib.h>
19 #include <limits.h>
20 #include <fcntl.h>
21 #include <inttypes.h>
22 
23 #include <string>
24 
25 #include "IOChannel.h"
26 #include "lldb/API/SBBreakpoint.h"
27 #include "lldb/API/SBCommandInterpreter.h"
28 #include "lldb/API/SBCommandReturnObject.h"
29 #include "lldb/API/SBCommunication.h"
30 #include "lldb/API/SBDebugger.h"
31 #include "lldb/API/SBEvent.h"
32 #include "lldb/API/SBHostOS.h"
33 #include "lldb/API/SBListener.h"
34 #include "lldb/API/SBStream.h"
35 #include "lldb/API/SBTarget.h"
36 #include "lldb/API/SBThread.h"
37 #include "lldb/API/SBProcess.h"
38 
39 using namespace lldb;
40 
41 static void reset_stdin_termios ();
42 static bool g_old_stdin_termios_is_valid = false;
43 static struct termios g_old_stdin_termios;
44 
45 static char *g_debugger_name =  (char *) "";
46 static Driver *g_driver = NULL;
47 
48 // In the Driver::MainLoop, we change the terminal settings.  This function is
49 // added as an atexit handler to make sure we clean them up.
50 static void
51 reset_stdin_termios ()
52 {
53     if (g_old_stdin_termios_is_valid)
54     {
55         g_old_stdin_termios_is_valid = false;
56         ::tcsetattr (STDIN_FILENO, TCSANOW, &g_old_stdin_termios);
57     }
58 }
59 
60 typedef struct
61 {
62     uint32_t usage_mask;                     // Used to mark options that can be used together.  If (1 << n & usage_mask) != 0
63                                              // then this option belongs to option set n.
64     bool required;                           // This option is required (in the current usage level)
65     const char * long_option;                // Full name for this option.
66     int short_option;                        // Single character for this option.
67     int option_has_arg;                      // no_argument, required_argument or optional_argument
68     uint32_t completion_type;                // Cookie the option class can use to do define the argument completion.
69     lldb::CommandArgumentType argument_type; // Type of argument this option takes
70     const char *  usage_text;                // Full text explaining what this options does and what (if any) argument to
71                                              // pass it.
72 } OptionDefinition;
73 
74 #define LLDB_3_TO_5 LLDB_OPT_SET_3|LLDB_OPT_SET_4|LLDB_OPT_SET_5
75 #define LLDB_4_TO_5 LLDB_OPT_SET_4|LLDB_OPT_SET_5
76 
77 static OptionDefinition g_options[] =
78 {
79     { LLDB_OPT_SET_1,    true , "help"           , 'h', no_argument      , 0,  eArgTypeNone,
80         "Prints out the usage information for the LLDB debugger." },
81     { LLDB_OPT_SET_2,    true , "version"        , 'v', no_argument      , 0,  eArgTypeNone,
82         "Prints out the current version number of the LLDB debugger." },
83     { LLDB_OPT_SET_3,    true , "arch"           , 'a', required_argument, 0,  eArgTypeArchitecture,
84         "Tells the debugger to use the specified architecture when starting and running the program.  <architecture> must "
85         "be one of the architectures for which the program was compiled." },
86     { LLDB_OPT_SET_3,    true , "file"           , 'f', required_argument, 0,  eArgTypeFilename,
87         "Tells the debugger to use the file <filename> as the program to be debugged." },
88     { LLDB_OPT_SET_3,    false, "core"           , 'c', required_argument, 0,  eArgTypeFilename,
89         "Tells the debugger to use the fullpath to <path> as the core file." },
90     { LLDB_OPT_SET_4,    true , "attach-name"    , 'n', required_argument, 0,  eArgTypeProcessName,
91         "Tells the debugger to attach to a process with the given name." },
92     { LLDB_OPT_SET_4,    true , "wait-for"       , 'w', no_argument      , 0,  eArgTypeNone,
93         "Tells the debugger to wait for a process with the given pid or name to launch before attaching." },
94     { LLDB_OPT_SET_5,    true , "attach-pid"     , 'p', required_argument, 0,  eArgTypePid,
95         "Tells the debugger to attach to a process with the given pid." },
96     { LLDB_3_TO_5,       false, "script-language", 'l', required_argument, 0,  eArgTypeScriptLang,
97         "Tells the debugger to use the specified scripting language for user-defined scripts, rather than the default.  "
98         "Valid scripting languages that can be specified include Python, Perl, Ruby and Tcl.  Currently only the Python "
99         "extensions have been implemented." },
100     { LLDB_3_TO_5,       false, "debug"          , 'd', no_argument      , 0,  eArgTypeNone,
101         "Tells the debugger to print out extra information for debugging itself." },
102     { LLDB_3_TO_5,       false, "source"         , 's', required_argument, 0,  eArgTypeFilename,
103         "Tells the debugger to read in and execute the file <file>, which should contain lldb commands." },
104     { LLDB_3_TO_5,       false, "editor"         , 'e', no_argument      , 0,  eArgTypeNone,
105         "Tells the debugger to open source files using the host's \"external editor\" mechanism." },
106     { LLDB_3_TO_5,       false, "no-lldbinit"    , 'x', no_argument      , 0,  eArgTypeNone,
107         "Do not automatically parse any '.lldbinit' files." },
108     { LLDB_OPT_SET_6,    true , "python-path"        , 'P', no_argument      , 0,  eArgTypeNone,
109         "Prints out the path to the lldb.py file for this version of lldb." },
110     { 0,                 false, NULL             , 0  , 0                , 0,  eArgTypeNone,         NULL }
111 };
112 
113 static const uint32_t last_option_set_with_args = 2;
114 
115 Driver::Driver () :
116     SBBroadcaster ("Driver"),
117     m_debugger (SBDebugger::Create(false)),
118     m_editline_pty (),
119     m_editline_slave_fh (NULL),
120     m_editline_reader (),
121     m_io_channel_ap (),
122     m_option_data (),
123     m_waiting_for_command (false),
124     m_done(false)
125 {
126     // We want to be able to handle CTRL+D in the terminal to have it terminate
127     // certain input
128     m_debugger.SetCloseInputOnEOF (false);
129     g_debugger_name = (char *) m_debugger.GetInstanceName();
130     if (g_debugger_name == NULL)
131         g_debugger_name = (char *) "";
132     g_driver = this;
133 }
134 
135 Driver::~Driver ()
136 {
137     g_driver = NULL;
138     g_debugger_name = NULL;
139 }
140 
141 void
142 Driver::CloseIOChannelFile ()
143 {
144     // Write an End of File sequence to the file descriptor to ensure any
145     // read functions can exit.
146     char eof_str[] = "\x04";
147     ::write (m_editline_pty.GetMasterFileDescriptor(), eof_str, strlen(eof_str));
148 
149     m_editline_pty.CloseMasterFileDescriptor();
150 
151     if (m_editline_slave_fh)
152     {
153         ::fclose (m_editline_slave_fh);
154         m_editline_slave_fh = NULL;
155     }
156 }
157 
158 // This function takes INDENT, which tells how many spaces to output at the front
159 // of each line; TEXT, which is the text that is to be output. It outputs the
160 // text, on multiple lines if necessary, to RESULT, with INDENT spaces at the
161 // front of each line.  It breaks lines on spaces, tabs or newlines, shortening
162 // the line if necessary to not break in the middle of a word. It assumes that
163 // each output line should contain a maximum of OUTPUT_MAX_COLUMNS characters.
164 
165 void
166 OutputFormattedUsageText (FILE *out, int indent, const char *text, int output_max_columns)
167 {
168     int len = strlen (text);
169     std::string text_string (text);
170 
171     // Force indentation to be reasonable.
172     if (indent >= output_max_columns)
173         indent = 0;
174 
175     // Will it all fit on one line?
176 
177     if (len + indent < output_max_columns)
178         // Output as a single line
179         fprintf (out, "%*s%s\n", indent, "", text);
180     else
181     {
182         // We need to break it up into multiple lines.
183         int text_width = output_max_columns - indent - 1;
184         int start = 0;
185         int end = start;
186         int final_end = len;
187         int sub_len;
188 
189         while (end < final_end)
190         {
191               // Dont start the 'text' on a space, since we're already outputting the indentation.
192               while ((start < final_end) && (text[start] == ' '))
193                   start++;
194 
195               end = start + text_width;
196               if (end > final_end)
197                   end = final_end;
198               else
199               {
200                   // If we're not at the end of the text, make sure we break the line on white space.
201                   while (end > start
202                          && text[end] != ' ' && text[end] != '\t' && text[end] != '\n')
203                       end--;
204               }
205               sub_len = end - start;
206               std::string substring = text_string.substr (start, sub_len);
207               fprintf (out, "%*s%s\n", indent, "", substring.c_str());
208               start = end + 1;
209         }
210     }
211 }
212 
213 void
214 ShowUsage (FILE *out, OptionDefinition *option_table, Driver::OptionData data)
215 {
216     uint32_t screen_width = 80;
217     uint32_t indent_level = 0;
218     const char *name = "lldb";
219 
220     fprintf (out, "\nUsage:\n\n");
221 
222     indent_level += 2;
223 
224 
225     // First, show each usage level set of options, e.g. <cmd> [options-for-level-0]
226     //                                                   <cmd> [options-for-level-1]
227     //                                                   etc.
228 
229     uint32_t num_options;
230     uint32_t num_option_sets = 0;
231 
232     for (num_options = 0; option_table[num_options].long_option != NULL; ++num_options)
233     {
234         uint32_t this_usage_mask = option_table[num_options].usage_mask;
235         if (this_usage_mask == LLDB_OPT_SET_ALL)
236         {
237             if (num_option_sets == 0)
238                 num_option_sets = 1;
239         }
240         else
241         {
242             for (uint32_t j = 0; j < LLDB_MAX_NUM_OPTION_SETS; j++)
243             {
244                 if (this_usage_mask & 1 << j)
245                 {
246                     if (num_option_sets <= j)
247                         num_option_sets = j + 1;
248                 }
249             }
250         }
251     }
252 
253     for (uint32_t opt_set = 0; opt_set < num_option_sets; opt_set++)
254     {
255         uint32_t opt_set_mask;
256 
257         opt_set_mask = 1 << opt_set;
258 
259         if (opt_set > 0)
260             fprintf (out, "\n");
261         fprintf (out, "%*s%s", indent_level, "", name);
262         bool is_help_line = false;
263 
264         for (uint32_t i = 0; i < num_options; ++i)
265         {
266             if (option_table[i].usage_mask & opt_set_mask)
267             {
268                 CommandArgumentType arg_type = option_table[i].argument_type;
269                 const char *arg_name = SBCommandInterpreter::GetArgumentTypeAsCString (arg_type);
270                 // This is a bit of a hack, but there's no way to say certain options don't have arguments yet...
271                 // so we do it by hand here.
272                 if (option_table[i].short_option == 'h')
273                     is_help_line = true;
274 
275                 if (option_table[i].required)
276                 {
277                     if (option_table[i].option_has_arg == required_argument)
278                         fprintf (out, " -%c <%s>", option_table[i].short_option, arg_name);
279                     else if (option_table[i].option_has_arg == optional_argument)
280                         fprintf (out, " -%c [<%s>]", option_table[i].short_option, arg_name);
281                     else
282                         fprintf (out, " -%c", option_table[i].short_option);
283                 }
284                 else
285                 {
286                     if (option_table[i].option_has_arg == required_argument)
287                         fprintf (out, " [-%c <%s>]", option_table[i].short_option, arg_name);
288                     else if (option_table[i].option_has_arg == optional_argument)
289                         fprintf (out, " [-%c [<%s>]]", option_table[i].short_option, arg_name);
290                     else
291                         fprintf (out, " [-%c]", option_table[i].short_option);
292                 }
293             }
294         }
295         if (!is_help_line && (opt_set <= last_option_set_with_args))
296             fprintf (out, " [[--] <PROGRAM-ARG-1> [<PROGRAM_ARG-2> ...]]");
297     }
298 
299     fprintf (out, "\n\n");
300 
301     // Now print out all the detailed information about the various options:  long form, short form and help text:
302     //   -- long_name <argument>
303     //   - short <argument>
304     //   help text
305 
306     // This variable is used to keep track of which options' info we've printed out, because some options can be in
307     // more than one usage level, but we only want to print the long form of its information once.
308 
309     Driver::OptionData::OptionSet options_seen;
310     Driver::OptionData::OptionSet::iterator pos;
311 
312     indent_level += 5;
313 
314     for (uint32_t i = 0; i < num_options; ++i)
315     {
316         // Only print this option if we haven't already seen it.
317         pos = options_seen.find (option_table[i].short_option);
318         if (pos == options_seen.end())
319         {
320             CommandArgumentType arg_type = option_table[i].argument_type;
321             const char *arg_name = SBCommandInterpreter::GetArgumentTypeAsCString (arg_type);
322 
323             options_seen.insert (option_table[i].short_option);
324             fprintf (out, "%*s-%c ", indent_level, "", option_table[i].short_option);
325             if (arg_type != eArgTypeNone)
326                 fprintf (out, "<%s>", arg_name);
327             fprintf (out, "\n");
328             fprintf (out, "%*s--%s ", indent_level, "", option_table[i].long_option);
329             if (arg_type != eArgTypeNone)
330                 fprintf (out, "<%s>", arg_name);
331             fprintf (out, "\n");
332             indent_level += 5;
333             OutputFormattedUsageText (out, indent_level, option_table[i].usage_text, screen_width);
334             indent_level -= 5;
335             fprintf (out, "\n");
336         }
337     }
338 
339     indent_level -= 5;
340 
341     fprintf (out, "\n%*s(If you don't provide -f then the first argument will be the file to be debugged"
342                   "\n%*s so '%s -- <filename> [<ARG1> [<ARG2>]]' also works."
343                   "\n%*s Remember to end the options with \"--\" if any of your arguments have a \"-\" in them.)\n\n",
344              indent_level, "",
345              indent_level, "",
346              name,
347              indent_level, "");
348 }
349 
350 void
351 BuildGetOptTable (OptionDefinition *expanded_option_table, std::vector<struct option> &getopt_table,
352                   uint32_t num_options)
353 {
354     if (num_options == 0)
355         return;
356 
357     uint32_t i;
358     uint32_t j;
359     std::bitset<256> option_seen;
360 
361     getopt_table.resize (num_options + 1);
362 
363     for (i = 0, j = 0; i < num_options; ++i)
364     {
365         char short_opt = expanded_option_table[i].short_option;
366 
367         if (option_seen.test(short_opt) == false)
368         {
369             getopt_table[j].name    = expanded_option_table[i].long_option;
370             getopt_table[j].has_arg = expanded_option_table[i].option_has_arg;
371             getopt_table[j].flag    = NULL;
372             getopt_table[j].val     = expanded_option_table[i].short_option;
373             option_seen.set(short_opt);
374             ++j;
375         }
376     }
377 
378     getopt_table[j].name    = NULL;
379     getopt_table[j].has_arg = 0;
380     getopt_table[j].flag    = NULL;
381     getopt_table[j].val     = 0;
382 
383 }
384 
385 Driver::OptionData::OptionData () :
386     m_args(),
387     m_script_lang (lldb::eScriptLanguageDefault),
388     m_core_file (),
389     m_crash_log (),
390     m_source_command_files (),
391     m_debug_mode (false),
392     m_print_version (false),
393     m_print_python_path (false),
394     m_print_help (false),
395     m_wait_for(false),
396     m_process_name(),
397     m_process_pid(LLDB_INVALID_PROCESS_ID),
398     m_use_external_editor(false),
399     m_seen_options()
400 {
401 }
402 
403 Driver::OptionData::~OptionData ()
404 {
405 }
406 
407 void
408 Driver::OptionData::Clear ()
409 {
410     m_args.clear ();
411     m_script_lang = lldb::eScriptLanguageDefault;
412     m_source_command_files.clear ();
413     m_debug_mode = false;
414     m_print_help = false;
415     m_print_version = false;
416     m_print_python_path = false;
417     m_use_external_editor = false;
418     m_wait_for = false;
419     m_process_name.erase();
420     m_process_pid = LLDB_INVALID_PROCESS_ID;
421 }
422 
423 void
424 Driver::ResetOptionValues ()
425 {
426     m_option_data.Clear ();
427 }
428 
429 const char *
430 Driver::GetFilename() const
431 {
432     if (m_option_data.m_args.empty())
433         return NULL;
434     return m_option_data.m_args.front().c_str();
435 }
436 
437 const char *
438 Driver::GetCrashLogFilename() const
439 {
440     if (m_option_data.m_crash_log.empty())
441         return NULL;
442     return m_option_data.m_crash_log.c_str();
443 }
444 
445 lldb::ScriptLanguage
446 Driver::GetScriptLanguage() const
447 {
448     return m_option_data.m_script_lang;
449 }
450 
451 size_t
452 Driver::GetNumSourceCommandFiles () const
453 {
454     return m_option_data.m_source_command_files.size();
455 }
456 
457 const char *
458 Driver::GetSourceCommandFileAtIndex (uint32_t idx) const
459 {
460     if (idx < m_option_data.m_source_command_files.size())
461         return m_option_data.m_source_command_files[idx].c_str();
462     return NULL;
463 }
464 
465 bool
466 Driver::GetDebugMode() const
467 {
468     return m_option_data.m_debug_mode;
469 }
470 
471 
472 // Check the arguments that were passed to this program to make sure they are valid and to get their
473 // argument values (if any).  Return a boolean value indicating whether or not to start up the full
474 // debugger (i.e. the Command Interpreter) or not.  Return FALSE if the arguments were invalid OR
475 // if the user only wanted help or version information.
476 
477 SBError
478 Driver::ParseArgs (int argc, const char *argv[], FILE *out_fh, bool &exit)
479 {
480     ResetOptionValues ();
481 
482     SBCommandReturnObject result;
483 
484     SBError error;
485     std::string option_string;
486     struct option *long_options = NULL;
487     std::vector<struct option> long_options_vector;
488     uint32_t num_options;
489 
490     for (num_options = 0; g_options[num_options].long_option != NULL; ++num_options)
491         /* Do Nothing. */;
492 
493     if (num_options == 0)
494     {
495         if (argc > 1)
496             error.SetErrorStringWithFormat ("invalid number of options");
497         return error;
498     }
499 
500     BuildGetOptTable (g_options, long_options_vector, num_options);
501 
502     if (long_options_vector.empty())
503         long_options = NULL;
504     else
505         long_options = &long_options_vector.front();
506 
507     if (long_options == NULL)
508     {
509         error.SetErrorStringWithFormat ("invalid long options");
510         return error;
511     }
512 
513     // Build the option_string argument for call to getopt_long.
514 
515     for (int i = 0; long_options[i].name != NULL; ++i)
516     {
517         if (long_options[i].flag == NULL)
518         {
519             option_string.push_back ((char) long_options[i].val);
520             switch (long_options[i].has_arg)
521             {
522                 default:
523                 case no_argument:
524                     break;
525                 case required_argument:
526                     option_string.push_back (':');
527                     break;
528                 case optional_argument:
529                     option_string.append ("::");
530                     break;
531             }
532         }
533     }
534 
535     // This is kind of a pain, but since we make the debugger in the Driver's constructor, we can't
536     // know at that point whether we should read in init files yet.  So we don't read them in in the
537     // Driver constructor, then set the flags back to "read them in" here, and then if we see the
538     // "-n" flag, we'll turn it off again.  Finally we have to read them in by hand later in the
539     // main loop.
540 
541     m_debugger.SkipLLDBInitFiles (false);
542     m_debugger.SkipAppInitFiles (false);
543 
544     // Prepare for & make calls to getopt_long.
545 #if __GLIBC__
546     optind = 0;
547 #else
548     optreset = 1;
549     optind = 1;
550 #endif
551     int val;
552     while (1)
553     {
554         int long_options_index = -1;
555         val = ::getopt_long (argc, const_cast<char **>(argv), option_string.c_str(), long_options, &long_options_index);
556 
557         if (val == -1)
558             break;
559         else if (val == '?')
560         {
561             m_option_data.m_print_help = true;
562             error.SetErrorStringWithFormat ("unknown or ambiguous option");
563             break;
564         }
565         else if (val == 0)
566             continue;
567         else
568         {
569             m_option_data.m_seen_options.insert ((char) val);
570             if (long_options_index == -1)
571             {
572                 for (int i = 0;
573                      long_options[i].name || long_options[i].has_arg || long_options[i].flag || long_options[i].val;
574                      ++i)
575                 {
576                     if (long_options[i].val == val)
577                     {
578                         long_options_index = i;
579                         break;
580                     }
581                 }
582             }
583 
584             if (long_options_index >= 0)
585             {
586                 const int short_option = g_options[long_options_index].short_option;
587 
588                 switch (short_option)
589                 {
590                     case 'h':
591                         m_option_data.m_print_help = true;
592                         break;
593 
594                     case 'v':
595                         m_option_data.m_print_version = true;
596                         break;
597 
598                     case 'P':
599                         m_option_data.m_print_python_path = true;
600                         break;
601 
602                     case 'c':
603                         {
604                             SBFileSpec file(optarg);
605                             if (file.Exists())
606                             {
607                                 m_option_data.m_core_file = optarg;
608                             }
609                             else
610                                 error.SetErrorStringWithFormat("file specified in --core (-c) option doesn't exist: '%s'", optarg);
611                         }
612                         break;
613 
614                     case 'e':
615                         m_option_data.m_use_external_editor = true;
616                         break;
617 
618                     case 'x':
619                         m_debugger.SkipLLDBInitFiles (true);
620                         m_debugger.SkipAppInitFiles (true);
621                         break;
622 
623                     case 'f':
624                         {
625                             SBFileSpec file(optarg);
626                             if (file.Exists())
627                             {
628                                 m_option_data.m_args.push_back (optarg);
629                             }
630                             else if (file.ResolveExecutableLocation())
631                             {
632                                 char path[PATH_MAX];
633                                 file.GetPath (path, sizeof(path));
634                                 m_option_data.m_args.push_back (path);
635                             }
636                             else
637                                 error.SetErrorStringWithFormat("file specified in --file (-f) option doesn't exist: '%s'", optarg);
638                         }
639                         break;
640 
641                     case 'a':
642                         if (!m_debugger.SetDefaultArchitecture (optarg))
643                             error.SetErrorStringWithFormat("invalid architecture in the -a or --arch option: '%s'", optarg);
644                         break;
645 
646                     case 'l':
647                         m_option_data.m_script_lang = m_debugger.GetScriptingLanguage (optarg);
648                         break;
649 
650                     case 'd':
651                         m_option_data.m_debug_mode = true;
652                         break;
653 
654                     case 'n':
655                         m_option_data.m_process_name = optarg;
656                         break;
657 
658                     case 'w':
659                         m_option_data.m_wait_for = true;
660                         break;
661 
662                     case 'p':
663                         {
664                             char *remainder;
665                             m_option_data.m_process_pid = strtol (optarg, &remainder, 0);
666                             if (remainder == optarg || *remainder != '\0')
667                                 error.SetErrorStringWithFormat ("Could not convert process PID: \"%s\" into a pid.",
668                                                                 optarg);
669                         }
670                         break;
671                     case 's':
672                         {
673                             SBFileSpec file(optarg);
674                             if (file.Exists())
675                                 m_option_data.m_source_command_files.push_back (optarg);
676                             else if (file.ResolveExecutableLocation())
677                             {
678                                 char final_path[PATH_MAX];
679                                 file.GetPath (final_path, sizeof(final_path));
680                                 std::string path_str (final_path);
681                                 m_option_data.m_source_command_files.push_back (path_str);
682                             }
683                             else
684                                 error.SetErrorStringWithFormat("file specified in --source (-s) option doesn't exist: '%s'", optarg);
685                         }
686                         break;
687 
688                     default:
689                         m_option_data.m_print_help = true;
690                         error.SetErrorStringWithFormat ("unrecognized option %c", short_option);
691                         break;
692                 }
693             }
694             else
695             {
696                 error.SetErrorStringWithFormat ("invalid option with value %i", val);
697             }
698             if (error.Fail())
699             {
700                 return error;
701             }
702         }
703     }
704 
705     if (error.Fail() || m_option_data.m_print_help)
706     {
707         ShowUsage (out_fh, g_options, m_option_data);
708         exit = true;
709     }
710     else if (m_option_data.m_print_version)
711     {
712         ::fprintf (out_fh, "%s\n", m_debugger.GetVersionString());
713         exit = true;
714     }
715     else if (m_option_data.m_print_python_path)
716     {
717         SBFileSpec python_file_spec = SBHostOS::GetLLDBPythonPath();
718         if (python_file_spec.IsValid())
719         {
720             char python_path[PATH_MAX];
721             size_t num_chars = python_file_spec.GetPath(python_path, PATH_MAX);
722             if (num_chars < PATH_MAX)
723             {
724                 ::fprintf (out_fh, "%s\n", python_path);
725             }
726             else
727                 ::fprintf (out_fh, "<PATH TOO LONG>\n");
728         }
729         else
730             ::fprintf (out_fh, "<COULD NOT FIND PATH>\n");
731         exit = true;
732     }
733     else if (m_option_data.m_process_name.empty() && m_option_data.m_process_pid == LLDB_INVALID_PROCESS_ID)
734     {
735         // Any arguments that are left over after option parsing are for
736         // the program. If a file was specified with -f then the filename
737         // is already in the m_option_data.m_args array, and any remaining args
738         // are arguments for the inferior program. If no file was specified with
739         // -f, then what is left is the program name followed by any arguments.
740 
741         // Skip any options we consumed with getopt_long
742         argc -= optind;
743         argv += optind;
744 
745         if (argc > 0)
746         {
747             for (int arg_idx=0; arg_idx<argc; ++arg_idx)
748             {
749                 const char *arg = argv[arg_idx];
750                 if (arg)
751                     m_option_data.m_args.push_back (arg);
752             }
753         }
754 
755     }
756     else
757     {
758         // Skip any options we consumed with getopt_long
759         argc -= optind;
760         //argv += optind; // Commented out to keep static analyzer happy
761 
762         if (argc > 0)
763             ::fprintf (out_fh, "Warning: program arguments are ignored when attaching.\n");
764     }
765 
766     return error;
767 }
768 
769 size_t
770 Driver::GetProcessSTDOUT ()
771 {
772     //  The process has stuff waiting for stdout; get it and write it out to the appropriate place.
773     char stdio_buffer[1024];
774     size_t len;
775     size_t total_bytes = 0;
776     while ((len = m_debugger.GetSelectedTarget().GetProcess().GetSTDOUT (stdio_buffer, sizeof (stdio_buffer))) > 0)
777     {
778         m_io_channel_ap->OutWrite (stdio_buffer, len, ASYNC);
779         total_bytes += len;
780     }
781     return total_bytes;
782 }
783 
784 size_t
785 Driver::GetProcessSTDERR ()
786 {
787     //  The process has stuff waiting for stderr; get it and write it out to the appropriate place.
788     char stdio_buffer[1024];
789     size_t len;
790     size_t total_bytes = 0;
791     while ((len = m_debugger.GetSelectedTarget().GetProcess().GetSTDERR (stdio_buffer, sizeof (stdio_buffer))) > 0)
792     {
793         m_io_channel_ap->ErrWrite (stdio_buffer, len, ASYNC);
794         total_bytes += len;
795     }
796     return total_bytes;
797 }
798 
799 void
800 Driver::UpdateSelectedThread ()
801 {
802     using namespace lldb;
803     SBProcess process(m_debugger.GetSelectedTarget().GetProcess());
804     if (process.IsValid())
805     {
806         SBThread curr_thread (process.GetSelectedThread());
807         SBThread thread;
808         StopReason curr_thread_stop_reason = eStopReasonInvalid;
809         curr_thread_stop_reason = curr_thread.GetStopReason();
810 
811         if (!curr_thread.IsValid() ||
812             curr_thread_stop_reason == eStopReasonInvalid ||
813             curr_thread_stop_reason == eStopReasonNone)
814         {
815             // Prefer a thread that has just completed its plan over another thread as current thread.
816             SBThread plan_thread;
817             SBThread other_thread;
818             const size_t num_threads = process.GetNumThreads();
819             size_t i;
820             for (i = 0; i < num_threads; ++i)
821             {
822                 thread = process.GetThreadAtIndex(i);
823                 StopReason thread_stop_reason = thread.GetStopReason();
824                 switch (thread_stop_reason)
825                 {
826                 case eStopReasonInvalid:
827                 case eStopReasonNone:
828                     break;
829 
830                 case eStopReasonTrace:
831                 case eStopReasonBreakpoint:
832                 case eStopReasonWatchpoint:
833                 case eStopReasonSignal:
834                 case eStopReasonException:
835                 case eStopReasonExec:
836                 case eStopReasonThreadExiting:
837                     if (!other_thread.IsValid())
838                         other_thread = thread;
839                     break;
840                 case eStopReasonPlanComplete:
841                     if (!plan_thread.IsValid())
842                         plan_thread = thread;
843                     break;
844                 }
845             }
846             if (plan_thread.IsValid())
847                 process.SetSelectedThread (plan_thread);
848             else if (other_thread.IsValid())
849                 process.SetSelectedThread (other_thread);
850             else
851             {
852                 if (curr_thread.IsValid())
853                     thread = curr_thread;
854                 else
855                     thread = process.GetThreadAtIndex(0);
856 
857                 if (thread.IsValid())
858                     process.SetSelectedThread (thread);
859             }
860         }
861     }
862 }
863 
864 // This function handles events that were broadcast by the process.
865 void
866 Driver::HandleBreakpointEvent (const SBEvent &event)
867 {
868     using namespace lldb;
869     const uint32_t event_type = SBBreakpoint::GetBreakpointEventTypeFromEvent (event);
870 
871     if (event_type & eBreakpointEventTypeAdded
872         || event_type & eBreakpointEventTypeRemoved
873         || event_type & eBreakpointEventTypeEnabled
874         || event_type & eBreakpointEventTypeDisabled
875         || event_type & eBreakpointEventTypeCommandChanged
876         || event_type & eBreakpointEventTypeConditionChanged
877         || event_type & eBreakpointEventTypeIgnoreChanged
878         || event_type & eBreakpointEventTypeLocationsResolved)
879     {
880         // Don't do anything about these events, since the breakpoint commands already echo these actions.
881     }
882     else if (event_type & eBreakpointEventTypeLocationsAdded)
883     {
884         char message[256];
885         uint32_t num_new_locations = SBBreakpoint::GetNumBreakpointLocationsFromEvent(event);
886         if (num_new_locations > 0)
887         {
888             SBBreakpoint breakpoint = SBBreakpoint::GetBreakpointFromEvent(event);
889             int message_len = ::snprintf (message, sizeof(message), "%d location%s added to breakpoint %d\n",
890                                           num_new_locations,
891                                           num_new_locations == 1 ? "" : "s",
892                                           breakpoint.GetID());
893             m_io_channel_ap->OutWrite(message, message_len, ASYNC);
894         }
895     }
896     else if (event_type & eBreakpointEventTypeLocationsRemoved)
897     {
898        // These locations just get disabled, not sure it is worth spamming folks about this on the command line.
899     }
900     else if (event_type & eBreakpointEventTypeLocationsResolved)
901     {
902        // This might be an interesting thing to note, but I'm going to leave it quiet for now, it just looked noisy.
903     }
904 }
905 
906 // This function handles events that were broadcast by the process.
907 void
908 Driver::HandleProcessEvent (const SBEvent &event)
909 {
910     using namespace lldb;
911     const uint32_t event_type = event.GetType();
912 
913     if (event_type & SBProcess::eBroadcastBitSTDOUT)
914     {
915         // The process has stdout available, get it and write it out to the
916         // appropriate place.
917         GetProcessSTDOUT ();
918     }
919     else if (event_type & SBProcess::eBroadcastBitSTDERR)
920     {
921         // The process has stderr available, get it and write it out to the
922         // appropriate place.
923         GetProcessSTDERR ();
924     }
925     else if (event_type & SBProcess::eBroadcastBitStateChanged)
926     {
927         // Drain all stout and stderr so we don't see any output come after
928         // we print our prompts
929         GetProcessSTDOUT ();
930         GetProcessSTDERR ();
931         // Something changed in the process;  get the event and report the process's current status and location to
932         // the user.
933         StateType event_state = SBProcess::GetStateFromEvent (event);
934         if (event_state == eStateInvalid)
935             return;
936 
937         SBProcess process (SBProcess::GetProcessFromEvent (event));
938         assert (process.IsValid());
939 
940         switch (event_state)
941         {
942         case eStateInvalid:
943         case eStateUnloaded:
944         case eStateConnected:
945         case eStateAttaching:
946         case eStateLaunching:
947         case eStateStepping:
948         case eStateDetached:
949             {
950                 char message[1024];
951                 int message_len = ::snprintf (message, sizeof(message), "Process %" PRIu64 " %s\n", process.GetProcessID(),
952                                               m_debugger.StateAsCString (event_state));
953                 m_io_channel_ap->OutWrite(message, message_len, ASYNC);
954             }
955             break;
956 
957         case eStateRunning:
958             // Don't be chatty when we run...
959             break;
960 
961         case eStateExited:
962             {
963                 SBCommandReturnObject result;
964                 m_debugger.GetCommandInterpreter().HandleCommand("process status", result, false);
965                 m_io_channel_ap->ErrWrite (result.GetError(), result.GetErrorSize(), ASYNC);
966                 m_io_channel_ap->OutWrite (result.GetOutput(), result.GetOutputSize(), ASYNC);
967             }
968             break;
969 
970         case eStateStopped:
971         case eStateCrashed:
972         case eStateSuspended:
973             // Make sure the program hasn't been auto-restarted:
974             if (SBProcess::GetRestartedFromEvent (event))
975             {
976                 size_t num_reasons = SBProcess::GetNumRestartedReasonsFromEvent(event);
977                 if (num_reasons > 0)
978                 {
979                 // FIXME: Do we want to report this, or would that just be annoyingly chatty?
980                     if (num_reasons == 1)
981                     {
982                         char message[1024];
983                         const char *reason = SBProcess::GetRestartedReasonAtIndexFromEvent (event, 0);
984                         int message_len = ::snprintf (message, sizeof(message), "Process %" PRIu64 " stopped and restarted: %s\n",
985                                               process.GetProcessID(), reason ? reason : "<UNKNOWN REASON>");
986                         m_io_channel_ap->OutWrite(message, message_len, ASYNC);
987                     }
988                     else
989                     {
990                         char message[1024];
991                         int message_len = ::snprintf (message, sizeof(message), "Process %" PRIu64 " stopped and restarted, reasons:\n",
992                                               process.GetProcessID());
993                         m_io_channel_ap->OutWrite(message, message_len, ASYNC);
994                         for (size_t i = 0; i < num_reasons; i++)
995                         {
996                             const char *reason = SBProcess::GetRestartedReasonAtIndexFromEvent (event, i);
997                             int message_len = ::snprintf(message, sizeof(message), "\t%s\n", reason ? reason : "<UNKNOWN REASON>");
998                             m_io_channel_ap->OutWrite(message, message_len, ASYNC);
999                         }
1000                     }
1001                 }
1002             }
1003             else
1004             {
1005                 if (GetDebugger().GetSelectedTarget() == process.GetTarget())
1006                 {
1007                     SBCommandReturnObject result;
1008                     UpdateSelectedThread ();
1009                     m_debugger.GetCommandInterpreter().HandleCommand("process status", result, false);
1010                     m_io_channel_ap->ErrWrite (result.GetError(), result.GetErrorSize(), ASYNC);
1011                     m_io_channel_ap->OutWrite (result.GetOutput(), result.GetOutputSize(), ASYNC);
1012                 }
1013                 else
1014                 {
1015                     SBStream out_stream;
1016                     uint32_t target_idx = GetDebugger().GetIndexOfTarget(process.GetTarget());
1017                     if (target_idx != UINT32_MAX)
1018                         out_stream.Printf ("Target %d: (", target_idx);
1019                     else
1020                         out_stream.Printf ("Target <unknown index>: (");
1021                     process.GetTarget().GetDescription (out_stream, eDescriptionLevelBrief);
1022                     out_stream.Printf (") stopped.\n");
1023                     m_io_channel_ap->OutWrite (out_stream.GetData(), out_stream.GetSize(), ASYNC);
1024                 }
1025             }
1026             break;
1027         }
1028     }
1029 }
1030 
1031 void
1032 Driver::HandleThreadEvent (const SBEvent &event)
1033 {
1034     // At present the only thread event we handle is the Frame Changed event, and all we do for that is just
1035     // reprint the thread status for that thread.
1036     using namespace lldb;
1037     const uint32_t event_type = event.GetType();
1038     if (event_type == SBThread::eBroadcastBitStackChanged
1039         || event_type == SBThread::eBroadcastBitThreadSelected)
1040     {
1041         SBThread thread = SBThread::GetThreadFromEvent (event);
1042         if (thread.IsValid())
1043         {
1044             SBStream out_stream;
1045             thread.GetStatus(out_stream);
1046             m_io_channel_ap->OutWrite (out_stream.GetData (), out_stream.GetSize (), ASYNC);
1047         }
1048     }
1049 }
1050 
1051 //  This function handles events broadcast by the IOChannel (HasInput, UserInterrupt, or ThreadShouldExit).
1052 
1053 bool
1054 Driver::HandleIOEvent (const SBEvent &event)
1055 {
1056     bool quit = false;
1057 
1058     const uint32_t event_type = event.GetType();
1059 
1060     if (event_type & IOChannel::eBroadcastBitHasUserInput)
1061     {
1062         // We got some input (i.e. a command string) from the user; pass it off to the command interpreter for
1063         // handling.
1064 
1065         const char *command_string = SBEvent::GetCStringFromEvent(event);
1066         if (command_string == NULL)
1067             command_string = "";
1068         SBCommandReturnObject result;
1069 
1070         // We don't want the result to bypass the OutWrite function in IOChannel, as this can result in odd
1071         // output orderings and problems with the prompt.
1072         m_debugger.GetCommandInterpreter().HandleCommand (command_string, result, true);
1073 
1074         const bool only_if_no_immediate = true;
1075 
1076         const size_t output_size = result.GetOutputSize();
1077 
1078         if (output_size > 0)
1079             m_io_channel_ap->OutWrite (result.GetOutput(only_if_no_immediate), output_size, NO_ASYNC);
1080 
1081         const size_t error_size = result.GetErrorSize();
1082 
1083         if (error_size > 0)
1084             m_io_channel_ap->OutWrite (result.GetError(only_if_no_immediate), error_size, NO_ASYNC);
1085 
1086         // We are done getting and running our command, we can now clear the
1087         // m_waiting_for_command so we can get another one.
1088         m_waiting_for_command = false;
1089 
1090         // If our editline input reader is active, it means another input reader
1091         // got pushed onto the input reader and caused us to become deactivated.
1092         // When the input reader above us gets popped, we will get re-activated
1093         // and our prompt will refresh in our callback
1094         if (m_editline_reader.IsActive())
1095         {
1096             ReadyForCommand ();
1097         }
1098     }
1099     else if (event_type & IOChannel::eBroadcastBitUserInterrupt)
1100     {
1101         // This is here to handle control-c interrupts from the user.  It has not yet really been implemented.
1102         // TO BE DONE:  PROPERLY HANDLE CONTROL-C FROM USER
1103         //m_io_channel_ap->CancelInput();
1104         // Anything else?  Send Interrupt to process?
1105     }
1106     else if ((event_type & IOChannel::eBroadcastBitThreadShouldExit) ||
1107              (event_type & IOChannel::eBroadcastBitThreadDidExit))
1108     {
1109         // If the IOChannel thread is trying to go away, then it is definitely
1110         // time to end the debugging session.
1111         quit = true;
1112     }
1113 
1114     return quit;
1115 }
1116 
1117 void
1118 Driver::MasterThreadBytesReceived (void *baton, const void *src, size_t src_len)
1119 {
1120     Driver *driver = (Driver*)baton;
1121     driver->GetFromMaster ((const char *)src, src_len);
1122 }
1123 
1124 void
1125 Driver::GetFromMaster (const char *src, size_t src_len)
1126 {
1127     // Echo the characters back to the Debugger's stdout, that way if you
1128     // type characters while a command is running, you'll see what you've typed.
1129     FILE *out_fh = m_debugger.GetOutputFileHandle();
1130     if (out_fh)
1131         ::fwrite (src, 1, src_len, out_fh);
1132 }
1133 
1134 size_t
1135 Driver::EditLineInputReaderCallback
1136 (
1137     void *baton,
1138     SBInputReader *reader,
1139     InputReaderAction notification,
1140     const char *bytes,
1141     size_t bytes_len
1142 )
1143 {
1144     Driver *driver = (Driver *)baton;
1145 
1146     switch (notification)
1147     {
1148     case eInputReaderActivate:
1149         break;
1150 
1151     case eInputReaderReactivate:
1152         driver->ReadyForCommand();
1153         break;
1154 
1155     case eInputReaderDeactivate:
1156         break;
1157 
1158     case eInputReaderAsynchronousOutputWritten:
1159         if (driver->m_io_channel_ap.get() != NULL)
1160             driver->m_io_channel_ap->RefreshPrompt();
1161         break;
1162 
1163     case eInputReaderInterrupt:
1164         if (driver->m_io_channel_ap.get() != NULL)
1165         {
1166             SBProcess process(driver->GetDebugger().GetSelectedTarget().GetProcess());
1167             if (!driver->m_io_channel_ap->EditLineHasCharacters()
1168                 &&  process.IsValid()
1169                 && (process.GetState() == lldb::eStateRunning || process.GetState() == lldb::eStateAttaching))
1170             {
1171                 process.SendAsyncInterrupt ();
1172             }
1173             else
1174             {
1175                 driver->m_io_channel_ap->OutWrite ("^C\n", 3, NO_ASYNC);
1176                 // I wish I could erase the entire input line, but there's no public API for that.
1177                 driver->m_io_channel_ap->EraseCharsBeforeCursor();
1178                 driver->m_io_channel_ap->RefreshPrompt();
1179             }
1180         }
1181         break;
1182 
1183     case eInputReaderEndOfFile:
1184         if (driver->m_io_channel_ap.get() != NULL)
1185         {
1186             driver->m_io_channel_ap->OutWrite ("^D\n", 3, NO_ASYNC);
1187             driver->m_io_channel_ap->RefreshPrompt ();
1188         }
1189         write (driver->m_editline_pty.GetMasterFileDescriptor(), "quit\n", 5);
1190         break;
1191 
1192     case eInputReaderGotToken:
1193         write (driver->m_editline_pty.GetMasterFileDescriptor(), bytes, bytes_len);
1194         break;
1195 
1196     case eInputReaderDone:
1197         break;
1198     }
1199     return bytes_len;
1200 }
1201 
1202 void
1203 Driver::MainLoop ()
1204 {
1205     char error_str[1024];
1206     if (m_editline_pty.OpenFirstAvailableMaster(O_RDWR|O_NOCTTY, error_str, sizeof(error_str)) == false)
1207     {
1208         ::fprintf (stderr, "error: failed to open driver pseudo terminal : %s", error_str);
1209         exit(1);
1210     }
1211     else
1212     {
1213         const char *driver_slave_name = m_editline_pty.GetSlaveName (error_str, sizeof(error_str));
1214         if (driver_slave_name == NULL)
1215         {
1216             ::fprintf (stderr, "error: failed to get slave name for driver pseudo terminal : %s", error_str);
1217             exit(2);
1218         }
1219         else
1220         {
1221             m_editline_slave_fh = ::fopen (driver_slave_name, "r+");
1222             if (m_editline_slave_fh == NULL)
1223             {
1224                 SBError error;
1225                 error.SetErrorToErrno();
1226                 ::fprintf (stderr, "error: failed to get open slave for driver pseudo terminal : %s",
1227                            error.GetCString());
1228                 exit(3);
1229             }
1230 
1231             ::setbuf (m_editline_slave_fh, NULL);
1232         }
1233     }
1234 
1235     lldb_utility::PseudoTerminal editline_output_pty;
1236     FILE *editline_output_slave_fh = NULL;
1237 
1238     if (editline_output_pty.OpenFirstAvailableMaster (O_RDWR|O_NOCTTY, error_str, sizeof (error_str)) == false)
1239     {
1240         ::fprintf (stderr, "error: failed to open output pseudo terminal : %s", error_str);
1241         exit(1);
1242     }
1243     else
1244     {
1245         const char *output_slave_name = editline_output_pty.GetSlaveName (error_str, sizeof(error_str));
1246         if (output_slave_name == NULL)
1247         {
1248             ::fprintf (stderr, "error: failed to get slave name for output pseudo terminal : %s", error_str);
1249             exit(2);
1250         }
1251         else
1252         {
1253             editline_output_slave_fh = ::fopen (output_slave_name, "r+");
1254             if (editline_output_slave_fh == NULL)
1255             {
1256                 SBError error;
1257                 error.SetErrorToErrno();
1258                 ::fprintf (stderr, "error: failed to get open slave for output pseudo terminal : %s",
1259                            error.GetCString());
1260                 exit(3);
1261             }
1262             ::setbuf (editline_output_slave_fh, NULL);
1263         }
1264     }
1265 
1266    // struct termios stdin_termios;
1267 
1268     if (::tcgetattr(STDIN_FILENO, &g_old_stdin_termios) == 0)
1269     {
1270         g_old_stdin_termios_is_valid = true;
1271         atexit (reset_stdin_termios);
1272     }
1273 
1274     ::setbuf (stdin, NULL);
1275     ::setbuf (stdout, NULL);
1276 
1277     m_debugger.SetErrorFileHandle (stderr, false);
1278     m_debugger.SetOutputFileHandle (stdout, false);
1279     m_debugger.SetInputFileHandle (stdin, true);
1280 
1281     m_debugger.SetUseExternalEditor(m_option_data.m_use_external_editor);
1282 
1283     // You have to drain anything that comes to the master side of the PTY.  master_out_comm is
1284     // for that purpose.  The reason you need to do this is a curious reason...  editline will echo
1285     // characters to the PTY when it gets characters while el_gets is not running, and then when
1286     // you call el_gets (or el_getc) it will try to reset the terminal back to raw mode which blocks
1287     // if there are unconsumed characters in the out buffer.
1288     // However, you don't need to do anything with the characters, since editline will dump these
1289     // unconsumed characters after printing the prompt again in el_gets.
1290 
1291     SBCommunication master_out_comm("driver.editline");
1292     master_out_comm.SetCloseOnEOF (false);
1293     master_out_comm.AdoptFileDesriptor(m_editline_pty.GetMasterFileDescriptor(), false);
1294     master_out_comm.SetReadThreadBytesReceivedCallback(Driver::MasterThreadBytesReceived, this);
1295 
1296     if (master_out_comm.ReadThreadStart () == false)
1297     {
1298         ::fprintf (stderr, "error: failed to start master out read thread");
1299         exit(5);
1300     }
1301 
1302     SBCommandInterpreter sb_interpreter = m_debugger.GetCommandInterpreter();
1303 
1304     m_io_channel_ap.reset (new IOChannel(m_editline_slave_fh, editline_output_slave_fh, stdout, stderr, this));
1305 
1306     SBCommunication out_comm_2("driver.editline_output");
1307     out_comm_2.SetCloseOnEOF (false);
1308     out_comm_2.AdoptFileDesriptor (editline_output_pty.GetMasterFileDescriptor(), false);
1309     out_comm_2.SetReadThreadBytesReceivedCallback (IOChannel::LibeditOutputBytesReceived, m_io_channel_ap.get());
1310 
1311     if (out_comm_2.ReadThreadStart () == false)
1312     {
1313         ::fprintf (stderr, "error: failed to start libedit output read thread");
1314         exit (5);
1315     }
1316 
1317 
1318     struct winsize window_size;
1319     if (isatty (STDIN_FILENO)
1320         && ::ioctl (STDIN_FILENO, TIOCGWINSZ, &window_size) == 0)
1321     {
1322         if (window_size.ws_col > 0)
1323             m_debugger.SetTerminalWidth (window_size.ws_col);
1324     }
1325 
1326     // Since input can be redirected by the debugger, we must insert our editline
1327     // input reader in the queue so we know when our reader should be active
1328     // and so we can receive bytes only when we are supposed to.
1329     SBError err (m_editline_reader.Initialize (m_debugger,
1330                                                Driver::EditLineInputReaderCallback, // callback
1331                                                this,                              // baton
1332                                                eInputReaderGranularityByte,       // token_size
1333                                                NULL,                              // end token - NULL means never done
1334                                                NULL,                              // prompt - taken care of elsewhere
1335                                                false));                           // echo input - don't need Debugger
1336                                                                                   // to do this, we handle it elsewhere
1337 
1338     if (err.Fail())
1339     {
1340         ::fprintf (stderr, "error: %s", err.GetCString());
1341         exit (6);
1342     }
1343 
1344     m_debugger.PushInputReader (m_editline_reader);
1345 
1346     SBListener listener(m_debugger.GetListener());
1347     if (listener.IsValid())
1348     {
1349 
1350         listener.StartListeningForEventClass(m_debugger,
1351                                          SBTarget::GetBroadcasterClassName(),
1352                                          SBTarget::eBroadcastBitBreakpointChanged);
1353         listener.StartListeningForEventClass(m_debugger,
1354                                          SBThread::GetBroadcasterClassName(),
1355                                          SBThread::eBroadcastBitStackChanged |
1356                                          SBThread::eBroadcastBitThreadSelected);
1357         listener.StartListeningForEvents (*m_io_channel_ap,
1358                                           IOChannel::eBroadcastBitHasUserInput |
1359                                           IOChannel::eBroadcastBitUserInterrupt |
1360                                           IOChannel::eBroadcastBitThreadShouldExit |
1361                                           IOChannel::eBroadcastBitThreadDidStart |
1362                                           IOChannel::eBroadcastBitThreadDidExit);
1363 
1364         if (m_io_channel_ap->Start ())
1365         {
1366             bool iochannel_thread_exited = false;
1367 
1368             listener.StartListeningForEvents (sb_interpreter.GetBroadcaster(),
1369                                               SBCommandInterpreter::eBroadcastBitQuitCommandReceived |
1370                                               SBCommandInterpreter::eBroadcastBitAsynchronousOutputData |
1371                                               SBCommandInterpreter::eBroadcastBitAsynchronousErrorData);
1372 
1373             // Before we handle any options from the command line, we parse the
1374             // .lldbinit file in the user's home directory.
1375             SBCommandReturnObject result;
1376             sb_interpreter.SourceInitFileInHomeDirectory(result);
1377             if (GetDebugMode())
1378             {
1379                 result.PutError (m_debugger.GetErrorFileHandle());
1380                 result.PutOutput (m_debugger.GetOutputFileHandle());
1381             }
1382 
1383             // Now we handle options we got from the command line
1384             char command_string[PATH_MAX * 2];
1385             const size_t num_source_command_files = GetNumSourceCommandFiles();
1386             const bool dump_stream_only_if_no_immediate = true;
1387             if (num_source_command_files > 0)
1388             {
1389                 for (size_t i=0; i < num_source_command_files; ++i)
1390                 {
1391                     const char *command_file = GetSourceCommandFileAtIndex(i);
1392                     ::snprintf (command_string, sizeof(command_string), "command source '%s'", command_file);
1393                     m_debugger.GetCommandInterpreter().HandleCommand (command_string, result, false);
1394                     if (GetDebugMode())
1395                     {
1396                         result.PutError (m_debugger.GetErrorFileHandle());
1397                         result.PutOutput (m_debugger.GetOutputFileHandle());
1398                     }
1399 
1400                     // if the command sourcing generated an error - dump the result object
1401                     if (result.Succeeded() == false)
1402                     {
1403                         const size_t output_size = result.GetOutputSize();
1404                         if (output_size > 0)
1405                             m_io_channel_ap->OutWrite (result.GetOutput(dump_stream_only_if_no_immediate), output_size, NO_ASYNC);
1406                         const size_t error_size = result.GetErrorSize();
1407                         if (error_size > 0)
1408                             m_io_channel_ap->OutWrite (result.GetError(dump_stream_only_if_no_immediate), error_size, NO_ASYNC);
1409                     }
1410 
1411                     result.Clear();
1412                 }
1413             }
1414 
1415             // Was there a core file specified?
1416             std::string core_file_spec("");
1417             if (!m_option_data.m_core_file.empty())
1418                 core_file_spec.append("--core ").append(m_option_data.m_core_file);
1419 
1420             const size_t num_args = m_option_data.m_args.size();
1421             if (num_args > 0)
1422             {
1423                 char arch_name[64];
1424                 if (m_debugger.GetDefaultArchitecture (arch_name, sizeof (arch_name)))
1425                     ::snprintf (command_string,
1426                                 sizeof (command_string),
1427                                 "target create --arch=%s %s \"%s\"",
1428                                 arch_name,
1429                                 core_file_spec.c_str(),
1430                                 m_option_data.m_args[0].c_str());
1431                 else
1432                     ::snprintf (command_string,
1433                                 sizeof(command_string),
1434                                 "target create %s \"%s\"",
1435                                 core_file_spec.c_str(),
1436                                 m_option_data.m_args[0].c_str());
1437 
1438                 m_debugger.HandleCommand (command_string);
1439 
1440                 if (num_args > 1)
1441                 {
1442                     m_debugger.HandleCommand ("settings clear target.run-args");
1443                     char arg_cstr[1024];
1444                     for (size_t arg_idx = 1; arg_idx < num_args; ++arg_idx)
1445                     {
1446                         ::snprintf (arg_cstr,
1447                                     sizeof(arg_cstr),
1448                                     "settings append target.run-args \"%s\"",
1449                                     m_option_data.m_args[arg_idx].c_str());
1450                         m_debugger.HandleCommand (arg_cstr);
1451                     }
1452                 }
1453             }
1454             else if (!core_file_spec.empty())
1455             {
1456                 ::snprintf (command_string,
1457                             sizeof(command_string),
1458                             "target create %s",
1459                             core_file_spec.c_str());
1460                 m_debugger.HandleCommand (command_string);;
1461             }
1462 
1463             // Now that all option parsing is done, we try and parse the .lldbinit
1464             // file in the current working directory
1465             sb_interpreter.SourceInitFileInCurrentWorkingDirectory (result);
1466             if (GetDebugMode())
1467             {
1468                 result.PutError(m_debugger.GetErrorFileHandle());
1469                 result.PutOutput(m_debugger.GetOutputFileHandle());
1470             }
1471 
1472             SBEvent event;
1473 
1474             // Make sure the IO channel is started up before we try to tell it we
1475             // are ready for input
1476             listener.WaitForEventForBroadcasterWithType (UINT32_MAX,
1477                                                          *m_io_channel_ap,
1478                                                          IOChannel::eBroadcastBitThreadDidStart,
1479                                                          event);
1480             // If we were asked to attach, then do that here:
1481             // I'm going to use the command string rather than directly
1482             // calling the API's because then I don't have to recode the
1483             // event handling here.
1484             if (!m_option_data.m_process_name.empty()
1485                 || m_option_data.m_process_pid != LLDB_INVALID_PROCESS_ID)
1486             {
1487                 std::string command_str("process attach ");
1488                 if (m_option_data.m_process_pid != LLDB_INVALID_PROCESS_ID)
1489                 {
1490                     command_str.append("-p ");
1491                     char pid_buffer[32];
1492                     ::snprintf (pid_buffer, sizeof(pid_buffer), "%" PRIu64, m_option_data.m_process_pid);
1493                     command_str.append(pid_buffer);
1494                 }
1495                 else
1496                 {
1497                     command_str.append("-n \"");
1498                     command_str.append(m_option_data.m_process_name);
1499                     command_str.push_back('\"');
1500                     if (m_option_data.m_wait_for)
1501                         command_str.append(" -w");
1502                 }
1503 
1504                 if (m_debugger.GetOutputFileHandle())
1505                     ::fprintf (m_debugger.GetOutputFileHandle(),
1506                                "Attaching to process with:\n    %s\n",
1507                                command_str.c_str());
1508 
1509                 // Force the attach to be synchronous:
1510                 bool orig_async = m_debugger.GetAsync();
1511                 m_debugger.SetAsync(true);
1512                 m_debugger.HandleCommand(command_str.c_str());
1513                 m_debugger.SetAsync(orig_async);
1514             }
1515 
1516             ReadyForCommand ();
1517 
1518             while (!GetIsDone())
1519             {
1520                 listener.WaitForEvent (UINT32_MAX, event);
1521                 if (event.IsValid())
1522                 {
1523                     if (event.GetBroadcaster().IsValid())
1524                     {
1525                         uint32_t event_type = event.GetType();
1526                         if (event.BroadcasterMatchesRef (*m_io_channel_ap))
1527                         {
1528                             if ((event_type & IOChannel::eBroadcastBitThreadShouldExit) ||
1529                                 (event_type & IOChannel::eBroadcastBitThreadDidExit))
1530                             {
1531                                 SetIsDone();
1532                                 if (event_type & IOChannel::eBroadcastBitThreadDidExit)
1533                                     iochannel_thread_exited = true;
1534                             }
1535                             else
1536                             {
1537                                 if (HandleIOEvent (event))
1538                                     SetIsDone();
1539                             }
1540                         }
1541                         else if (SBProcess::EventIsProcessEvent (event))
1542                         {
1543                             HandleProcessEvent (event);
1544                         }
1545                         else if (SBBreakpoint::EventIsBreakpointEvent (event))
1546                         {
1547                             HandleBreakpointEvent (event);
1548                         }
1549                         else if (SBThread::EventIsThreadEvent (event))
1550                         {
1551                             HandleThreadEvent (event);
1552                         }
1553                         else if (event.BroadcasterMatchesRef (sb_interpreter.GetBroadcaster()))
1554                         {
1555                             // TODO: deprecate the eBroadcastBitQuitCommandReceived event
1556                             // now that we have SBCommandInterpreter::SetCommandOverrideCallback()
1557                             // that can take over a command
1558                             if (event_type & SBCommandInterpreter::eBroadcastBitQuitCommandReceived)
1559                             {
1560                                 SetIsDone();
1561                             }
1562                             else if (event_type & SBCommandInterpreter::eBroadcastBitAsynchronousErrorData)
1563                             {
1564                                 const char *data = SBEvent::GetCStringFromEvent (event);
1565                                 m_io_channel_ap->ErrWrite (data, strlen(data), ASYNC);
1566                             }
1567                             else if (event_type & SBCommandInterpreter::eBroadcastBitAsynchronousOutputData)
1568                             {
1569                                 const char *data = SBEvent::GetCStringFromEvent (event);
1570                                 m_io_channel_ap->OutWrite (data, strlen(data), ASYNC);
1571                             }
1572                         }
1573                     }
1574                 }
1575             }
1576 
1577             editline_output_pty.CloseMasterFileDescriptor();
1578             master_out_comm.Disconnect();
1579             out_comm_2.Disconnect();
1580             reset_stdin_termios();
1581             fclose (stdin);
1582 
1583             CloseIOChannelFile ();
1584 
1585             if (!iochannel_thread_exited)
1586             {
1587                 event.Clear();
1588                 listener.GetNextEventForBroadcasterWithType (*m_io_channel_ap,
1589                                                              IOChannel::eBroadcastBitThreadDidExit,
1590                                                              event);
1591                 if (!event.IsValid())
1592                 {
1593                     // Send end EOF to the driver file descriptor
1594                     m_io_channel_ap->Stop();
1595                 }
1596             }
1597 
1598             SBDebugger::Destroy (m_debugger);
1599         }
1600     }
1601 }
1602 
1603 
1604 void
1605 Driver::ReadyForCommand ()
1606 {
1607     if (m_waiting_for_command == false)
1608     {
1609         m_waiting_for_command = true;
1610         BroadcastEventByType (Driver::eBroadcastBitReadyForInput, true);
1611     }
1612 }
1613 
1614 void
1615 Driver::ResizeWindow (unsigned short col)
1616 {
1617     GetDebugger().SetTerminalWidth (col);
1618     if (m_io_channel_ap.get() != NULL)
1619     {
1620         m_io_channel_ap->ElResize();
1621     }
1622 }
1623 
1624 void
1625 sigwinch_handler (int signo)
1626 {
1627     struct winsize window_size;
1628     if (isatty (STDIN_FILENO)
1629         && ::ioctl (STDIN_FILENO, TIOCGWINSZ, &window_size) == 0)
1630     {
1631         if ((window_size.ws_col > 0) && g_driver != NULL)
1632         {
1633             g_driver->ResizeWindow (window_size.ws_col);
1634         }
1635     }
1636 }
1637 
1638 void
1639 sigint_handler (int signo)
1640 {
1641 	static bool g_interrupt_sent = false;
1642     if (g_driver)
1643 	{
1644 		if (!g_interrupt_sent)
1645 		{
1646 			g_interrupt_sent = true;
1647         	g_driver->GetDebugger().DispatchInputInterrupt();
1648 			g_interrupt_sent = false;
1649 			return;
1650 		}
1651 	}
1652 
1653 	exit (signo);
1654 }
1655 
1656 void
1657 sigtstp_handler (int signo)
1658 {
1659     g_driver->GetDebugger().SaveInputTerminalState();
1660     signal (signo, SIG_DFL);
1661     kill (getpid(), signo);
1662     signal (signo, sigtstp_handler);
1663 }
1664 
1665 void
1666 sigcont_handler (int signo)
1667 {
1668     g_driver->GetDebugger().RestoreInputTerminalState();
1669     signal (signo, SIG_DFL);
1670     kill (getpid(), signo);
1671     signal (signo, sigcont_handler);
1672 }
1673 
1674 int
1675 main (int argc, char const *argv[], const char *envp[])
1676 {
1677     SBDebugger::Initialize();
1678 
1679     SBHostOS::ThreadCreated ("<lldb.driver.main-thread>");
1680 
1681     signal (SIGPIPE, SIG_IGN);
1682     signal (SIGWINCH, sigwinch_handler);
1683     signal (SIGINT, sigint_handler);
1684     signal (SIGTSTP, sigtstp_handler);
1685     signal (SIGCONT, sigcont_handler);
1686 
1687     // Create a scope for driver so that the driver object will destroy itself
1688     // before SBDebugger::Terminate() is called.
1689     {
1690         Driver driver;
1691 
1692         bool exit = false;
1693         SBError error (driver.ParseArgs (argc, argv, stdout, exit));
1694         if (error.Fail())
1695         {
1696             const char *error_cstr = error.GetCString ();
1697             if (error_cstr)
1698                 ::fprintf (stderr, "error: %s\n", error_cstr);
1699         }
1700         else if (!exit)
1701         {
1702             driver.MainLoop ();
1703         }
1704     }
1705 
1706     SBDebugger::Terminate();
1707     return 0;
1708 }
1709