1 //===-- Driver.cpp ----------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
18 // Includes for pipe()
22 #elif defined(__ANDROID_NDK__)
31 #include "lldb/API/SBBreakpoint.h"
32 #include "lldb/API/SBCommandInterpreter.h"
33 #include "lldb/API/SBCommandReturnObject.h"
34 #include "lldb/API/SBCommunication.h"
35 #include "lldb/API/SBDebugger.h"
36 #include "lldb/API/SBEvent.h"
37 #include "lldb/API/SBHostOS.h"
38 #include "lldb/API/SBListener.h"
39 #include "lldb/API/SBStream.h"
40 #include "lldb/API/SBTarget.h"
41 #include "lldb/API/SBThread.h"
42 #include "lldb/API/SBProcess.h"
44 #if !defined(__APPLE__)
45 #include "llvm/Support/DataTypes.h"
50 static void reset_stdin_termios ();
51 static bool g_old_stdin_termios_is_valid = false;
52 static struct termios g_old_stdin_termios;
54 static Driver *g_driver = NULL;
56 // In the Driver::MainLoop, we change the terminal settings. This function is
57 // added as an atexit handler to make sure we clean them up.
59 reset_stdin_termios ()
61 if (g_old_stdin_termios_is_valid)
63 g_old_stdin_termios_is_valid = false;
64 ::tcsetattr (STDIN_FILENO, TCSANOW, &g_old_stdin_termios);
70 uint32_t usage_mask; // Used to mark options that can be used together. If (1 << n & usage_mask) != 0
71 // then this option belongs to option set n.
72 bool required; // This option is required (in the current usage level)
73 const char * long_option; // Full name for this option.
74 int short_option; // Single character for this option.
75 int option_has_arg; // no_argument, required_argument or optional_argument
76 uint32_t completion_type; // Cookie the option class can use to do define the argument completion.
77 lldb::CommandArgumentType argument_type; // Type of argument this option takes
78 const char * usage_text; // Full text explaining what this options does and what (if any) argument to
82 #define LLDB_3_TO_5 LLDB_OPT_SET_3|LLDB_OPT_SET_4|LLDB_OPT_SET_5
83 #define LLDB_4_TO_5 LLDB_OPT_SET_4|LLDB_OPT_SET_5
85 static OptionDefinition g_options[] =
87 { LLDB_OPT_SET_1, true , "help" , 'h', no_argument , 0, eArgTypeNone,
88 "Prints out the usage information for the LLDB debugger." },
89 { LLDB_OPT_SET_2, true , "version" , 'v', no_argument , 0, eArgTypeNone,
90 "Prints out the current version number of the LLDB debugger." },
91 { LLDB_OPT_SET_3, true , "arch" , 'a', required_argument, 0, eArgTypeArchitecture,
92 "Tells the debugger to use the specified architecture when starting and running the program. <architecture> must "
93 "be one of the architectures for which the program was compiled." },
94 { LLDB_OPT_SET_3, true , "file" , 'f', required_argument, 0, eArgTypeFilename,
95 "Tells the debugger to use the file <filename> as the program to be debugged." },
96 { LLDB_OPT_SET_3, false, "core" , 'c', required_argument, 0, eArgTypeFilename,
97 "Tells the debugger to use the fullpath to <path> as the core file." },
98 { LLDB_OPT_SET_5, true , "attach-pid" , 'p', required_argument, 0, eArgTypePid,
99 "Tells the debugger to attach to a process with the given pid." },
100 { LLDB_OPT_SET_4, true , "attach-name" , 'n', required_argument, 0, eArgTypeProcessName,
101 "Tells the debugger to attach to a process with the given name." },
102 { LLDB_OPT_SET_4, true , "wait-for" , 'w', no_argument , 0, eArgTypeNone,
103 "Tells the debugger to wait for a process with the given pid or name to launch before attaching." },
104 { LLDB_3_TO_5, false, "source" , 's', required_argument, 0, eArgTypeFilename,
105 "Tells the debugger to read in and execute the lldb commands in the given file, after any file provided on the command line has been loaded." },
106 { LLDB_3_TO_5, false, "one-line" , 'o', required_argument, 0, eArgTypeNone,
107 "Tells the debugger to execute this one-line lldb command after any file provided on the command line has been loaded." },
108 { LLDB_3_TO_5, false, "source-before-file" , 'S', required_argument, 0, eArgTypeFilename,
109 "Tells the debugger to read in and execute the lldb commands in the given file, before any file provided on the command line has been loaded." },
110 { LLDB_3_TO_5, false, "one-line-before-file" , 'O', required_argument, 0, eArgTypeNone,
111 "Tells the debugger to execute this one-line lldb command before any file provided on the command line has been loaded." },
112 { LLDB_3_TO_5, false, "one-line-on-crash" , 'k', required_argument, 0, eArgTypeNone,
113 "When in batch mode, tells the debugger to execute this one-line lldb command if the target crashes." },
114 { LLDB_3_TO_5, false, "source-on-crash" , 'K', required_argument, 0, eArgTypeFilename,
115 "When in batch mode, tells the debugger to source this file of lldb commands if the target crashes." },
116 { LLDB_3_TO_5, false, "source-quietly" , 'Q', no_argument , 0, eArgTypeNone,
117 "Tells the debugger to execute this one-line lldb command before any file provided on the command line has been loaded." },
118 { LLDB_3_TO_5, false, "batch" , 'b', no_argument , 0, eArgTypeNone,
119 "Tells the debugger to running the commands from -s, -S, -o & -O, and then quit. However if any run command stopped due to a signal or crash, "
120 "the debugger will return to the interactive prompt at the place of the crash." },
121 { LLDB_3_TO_5, false, "editor" , 'e', no_argument , 0, eArgTypeNone,
122 "Tells the debugger to open source files using the host's \"external editor\" mechanism." },
123 { LLDB_3_TO_5, false, "no-lldbinit" , 'x', no_argument , 0, eArgTypeNone,
124 "Do not automatically parse any '.lldbinit' files." },
125 { LLDB_3_TO_5, false, "no-use-colors" , 'X', no_argument , 0, eArgTypeNone,
126 "Do not use colors." },
127 { LLDB_OPT_SET_6, true , "python-path" , 'P', no_argument , 0, eArgTypeNone,
128 "Prints out the path to the lldb.py file for this version of lldb." },
129 { LLDB_3_TO_5, false, "script-language", 'l', required_argument, 0, eArgTypeScriptLang,
130 "Tells the debugger to use the specified scripting language for user-defined scripts, rather than the default. "
131 "Valid scripting languages that can be specified include Python, Perl, Ruby and Tcl. Currently only the Python "
132 "extensions have been implemented." },
133 { LLDB_3_TO_5, false, "debug" , 'd', no_argument , 0, eArgTypeNone,
134 "Tells the debugger to print out extra information for debugging itself." },
135 { 0, false, NULL , 0 , 0 , 0, eArgTypeNone, NULL }
138 static const uint32_t last_option_set_with_args = 2;
141 SBBroadcaster ("Driver"),
142 m_debugger (SBDebugger::Create(false)),
145 // We want to be able to handle CTRL+D in the terminal to have it terminate
147 m_debugger.SetCloseInputOnEOF (false);
157 // This function takes INDENT, which tells how many spaces to output at the front
158 // of each line; TEXT, which is the text that is to be output. It outputs the
159 // text, on multiple lines if necessary, to RESULT, with INDENT spaces at the
160 // front of each line. It breaks lines on spaces, tabs or newlines, shortening
161 // the line if necessary to not break in the middle of a word. It assumes that
162 // each output line should contain a maximum of OUTPUT_MAX_COLUMNS characters.
165 OutputFormattedUsageText (FILE *out, int indent, const char *text, int output_max_columns)
167 int len = strlen (text);
168 std::string text_string (text);
170 // Force indentation to be reasonable.
171 if (indent >= output_max_columns)
174 // Will it all fit on one line?
176 if (len + indent < output_max_columns)
177 // Output as a single line
178 fprintf (out, "%*s%s\n", indent, "", text);
181 // We need to break it up into multiple lines.
182 int text_width = output_max_columns - indent - 1;
188 while (end < final_end)
190 // Dont start the 'text' on a space, since we're already outputting the indentation.
191 while ((start < final_end) && (text[start] == ' '))
194 end = start + text_width;
199 // If we're not at the end of the text, make sure we break the line on white space.
201 && text[end] != ' ' && text[end] != '\t' && text[end] != '\n')
204 sub_len = end - start;
205 std::string substring = text_string.substr (start, sub_len);
206 fprintf (out, "%*s%s\n", indent, "", substring.c_str());
213 ShowUsage (FILE *out, OptionDefinition *option_table, Driver::OptionData data)
215 uint32_t screen_width = 80;
216 uint32_t indent_level = 0;
217 const char *name = "lldb";
219 fprintf (out, "\nUsage:\n\n");
224 // First, show each usage level set of options, e.g. <cmd> [options-for-level-0]
225 // <cmd> [options-for-level-1]
228 uint32_t num_options;
229 uint32_t num_option_sets = 0;
231 for (num_options = 0; option_table[num_options].long_option != NULL; ++num_options)
233 uint32_t this_usage_mask = option_table[num_options].usage_mask;
234 if (this_usage_mask == LLDB_OPT_SET_ALL)
236 if (num_option_sets == 0)
241 for (uint32_t j = 0; j < LLDB_MAX_NUM_OPTION_SETS; j++)
243 if (this_usage_mask & 1 << j)
245 if (num_option_sets <= j)
246 num_option_sets = j + 1;
252 for (uint32_t opt_set = 0; opt_set < num_option_sets; opt_set++)
254 uint32_t opt_set_mask;
256 opt_set_mask = 1 << opt_set;
260 fprintf (out, "%*s%s", indent_level, "", name);
261 bool is_help_line = false;
263 for (uint32_t i = 0; i < num_options; ++i)
265 if (option_table[i].usage_mask & opt_set_mask)
267 CommandArgumentType arg_type = option_table[i].argument_type;
268 const char *arg_name = SBCommandInterpreter::GetArgumentTypeAsCString (arg_type);
269 // This is a bit of a hack, but there's no way to say certain options don't have arguments yet...
270 // so we do it by hand here.
271 if (option_table[i].short_option == 'h')
274 if (option_table[i].required)
276 if (option_table[i].option_has_arg == required_argument)
277 fprintf (out, " -%c <%s>", option_table[i].short_option, arg_name);
278 else if (option_table[i].option_has_arg == optional_argument)
279 fprintf (out, " -%c [<%s>]", option_table[i].short_option, arg_name);
281 fprintf (out, " -%c", option_table[i].short_option);
285 if (option_table[i].option_has_arg == required_argument)
286 fprintf (out, " [-%c <%s>]", option_table[i].short_option, arg_name);
287 else if (option_table[i].option_has_arg == optional_argument)
288 fprintf (out, " [-%c [<%s>]]", option_table[i].short_option, arg_name);
290 fprintf (out, " [-%c]", option_table[i].short_option);
294 if (!is_help_line && (opt_set <= last_option_set_with_args))
295 fprintf (out, " [[--] <PROGRAM-ARG-1> [<PROGRAM_ARG-2> ...]]");
298 fprintf (out, "\n\n");
300 // Now print out all the detailed information about the various options: long form, short form and help text:
301 // -- long_name <argument>
302 // - short <argument>
305 // This variable is used to keep track of which options' info we've printed out, because some options can be in
306 // more than one usage level, but we only want to print the long form of its information once.
308 Driver::OptionData::OptionSet options_seen;
309 Driver::OptionData::OptionSet::iterator pos;
313 for (uint32_t i = 0; i < num_options; ++i)
315 // Only print this option if we haven't already seen it.
316 pos = options_seen.find (option_table[i].short_option);
317 if (pos == options_seen.end())
319 CommandArgumentType arg_type = option_table[i].argument_type;
320 const char *arg_name = SBCommandInterpreter::GetArgumentTypeAsCString (arg_type);
322 options_seen.insert (option_table[i].short_option);
323 fprintf (out, "%*s-%c ", indent_level, "", option_table[i].short_option);
324 if (arg_type != eArgTypeNone)
325 fprintf (out, "<%s>", arg_name);
327 fprintf (out, "%*s--%s ", indent_level, "", option_table[i].long_option);
328 if (arg_type != eArgTypeNone)
329 fprintf (out, "<%s>", arg_name);
332 OutputFormattedUsageText (out, indent_level, option_table[i].usage_text, screen_width);
340 fprintf (out, "\n%*sNotes:\n",
344 fprintf (out, "\n%*sMultiple \"-s\" and \"-o\" options can be provided. They will be processed from left to right in order, "
345 "\n%*swith the source files and commands interleaved. The same is true of the \"-S\" and \"-O\" options."
346 "\n%*sThe before file and after file sets can intermixed freely, the command parser will sort them out."
347 "\n%*sThe order of the file specifiers (\"-c\", \"-f\", etc.) is not significant in this regard.\n\n",
353 fprintf (out, "\n%*sIf you don't provide -f then the first argument will be the file to be debugged"
354 "\n%*swhich means that '%s -- <filename> [<ARG1> [<ARG2>]]' also works."
355 "\n%*sBut remember to end the options with \"--\" if any of your arguments have a \"-\" in them.\n\n",
363 BuildGetOptTable (OptionDefinition *expanded_option_table, std::vector<struct option> &getopt_table,
364 uint32_t num_options)
366 if (num_options == 0)
371 std::bitset<256> option_seen;
373 getopt_table.resize (num_options + 1);
375 for (i = 0, j = 0; i < num_options; ++i)
377 char short_opt = expanded_option_table[i].short_option;
379 if (option_seen.test(short_opt) == false)
381 getopt_table[j].name = expanded_option_table[i].long_option;
382 getopt_table[j].has_arg = expanded_option_table[i].option_has_arg;
383 getopt_table[j].flag = NULL;
384 getopt_table[j].val = expanded_option_table[i].short_option;
385 option_seen.set(short_opt);
390 getopt_table[j].name = NULL;
391 getopt_table[j].has_arg = 0;
392 getopt_table[j].flag = NULL;
393 getopt_table[j].val = 0;
397 Driver::OptionData::OptionData () :
399 m_script_lang (lldb::eScriptLanguageDefault),
402 m_initial_commands (),
403 m_after_file_commands (),
404 m_after_crash_commands(),
405 m_debug_mode (false),
406 m_source_quietly(false),
407 m_print_version (false),
408 m_print_python_path (false),
409 m_print_help (false),
412 m_process_pid(LLDB_INVALID_PROCESS_ID),
413 m_use_external_editor(false),
419 Driver::OptionData::~OptionData ()
424 Driver::OptionData::Clear ()
427 m_script_lang = lldb::eScriptLanguageDefault;
428 m_initial_commands.clear ();
429 m_after_file_commands.clear ();
430 // If there is a local .lldbinit, source that:
431 SBFileSpec local_lldbinit("./.lldbinit", true);
432 if (local_lldbinit.Exists())
435 local_lldbinit.GetPath(path, 2047);
436 InitialCmdEntry entry(path, true, true);
437 m_after_file_commands.push_back (entry);
440 m_debug_mode = false;
441 m_source_quietly = false;
442 m_print_help = false;
443 m_print_version = false;
444 m_print_python_path = false;
445 m_use_external_editor = false;
447 m_process_name.erase();
449 m_after_crash_commands.clear();
451 m_process_pid = LLDB_INVALID_PROCESS_ID;
455 Driver::OptionData::AddInitialCommand (const char *command, CommandPlacement placement, bool is_file, SBError &error)
457 std::vector<InitialCmdEntry> *command_set;
460 case eCommandPlacementBeforeFile:
461 command_set = &(m_initial_commands);
463 case eCommandPlacementAfterFile:
464 command_set = &(m_after_file_commands);
466 case eCommandPlacementAfterCrash:
467 command_set = &(m_after_crash_commands);
473 SBFileSpec file(command);
475 command_set->push_back (InitialCmdEntry(command, is_file));
476 else if (file.ResolveExecutableLocation())
478 char final_path[PATH_MAX];
479 file.GetPath (final_path, sizeof(final_path));
480 command_set->push_back (InitialCmdEntry(final_path, is_file));
483 error.SetErrorStringWithFormat("file specified in --source (-s) option doesn't exist: '%s'", optarg);
486 command_set->push_back (InitialCmdEntry(command, is_file));
490 Driver::ResetOptionValues ()
492 m_option_data.Clear ();
496 Driver::GetFilename() const
498 if (m_option_data.m_args.empty())
500 return m_option_data.m_args.front().c_str();
504 Driver::GetCrashLogFilename() const
506 if (m_option_data.m_crash_log.empty())
508 return m_option_data.m_crash_log.c_str();
512 Driver::GetScriptLanguage() const
514 return m_option_data.m_script_lang;
518 Driver::WriteCommandsForSourcing (CommandPlacement placement, SBStream &strm)
520 std::vector<OptionData::InitialCmdEntry> *command_set;
523 case eCommandPlacementBeforeFile:
524 command_set = &m_option_data.m_initial_commands;
526 case eCommandPlacementAfterFile:
527 command_set = &m_option_data.m_after_file_commands;
529 case eCommandPlacementAfterCrash:
530 command_set = &m_option_data.m_after_crash_commands;
534 for (const auto &command_entry : *command_set)
536 const char *command = command_entry.contents.c_str();
537 if (command_entry.is_file)
539 bool source_quietly = m_option_data.m_source_quietly || command_entry.source_quietly;
540 strm.Printf("command source -s %i '%s'\n", source_quietly, command);
543 strm.Printf("%s\n", command);
548 Driver::GetDebugMode() const
550 return m_option_data.m_debug_mode;
554 // Check the arguments that were passed to this program to make sure they are valid and to get their
555 // argument values (if any). Return a boolean value indicating whether or not to start up the full
556 // debugger (i.e. the Command Interpreter) or not. Return FALSE if the arguments were invalid OR
557 // if the user only wanted help or version information.
560 Driver::ParseArgs (int argc, const char *argv[], FILE *out_fh, bool &exiting)
562 ResetOptionValues ();
564 SBCommandReturnObject result;
567 std::string option_string;
568 struct option *long_options = NULL;
569 std::vector<struct option> long_options_vector;
570 uint32_t num_options;
572 for (num_options = 0; g_options[num_options].long_option != NULL; ++num_options)
575 if (num_options == 0)
578 error.SetErrorStringWithFormat ("invalid number of options");
582 BuildGetOptTable (g_options, long_options_vector, num_options);
584 if (long_options_vector.empty())
587 long_options = &long_options_vector.front();
589 if (long_options == NULL)
591 error.SetErrorStringWithFormat ("invalid long options");
595 // Build the option_string argument for call to getopt_long_only.
597 for (int i = 0; long_options[i].name != NULL; ++i)
599 if (long_options[i].flag == NULL)
601 option_string.push_back ((char) long_options[i].val);
602 switch (long_options[i].has_arg)
607 case required_argument:
608 option_string.push_back (':');
610 case optional_argument:
611 option_string.append ("::");
617 // This is kind of a pain, but since we make the debugger in the Driver's constructor, we can't
618 // know at that point whether we should read in init files yet. So we don't read them in in the
619 // Driver constructor, then set the flags back to "read them in" here, and then if we see the
620 // "-n" flag, we'll turn it off again. Finally we have to read them in by hand later in the
623 m_debugger.SkipLLDBInitFiles (false);
624 m_debugger.SkipAppInitFiles (false);
626 // Prepare for & make calls to getopt_long_only.
636 int long_options_index = -1;
637 val = ::getopt_long_only (argc, const_cast<char **>(argv), option_string.c_str(), long_options, &long_options_index);
643 m_option_data.m_print_help = true;
644 error.SetErrorStringWithFormat ("unknown or ambiguous option");
651 m_option_data.m_seen_options.insert ((char) val);
652 if (long_options_index == -1)
655 long_options[i].name || long_options[i].has_arg || long_options[i].flag || long_options[i].val;
658 if (long_options[i].val == val)
660 long_options_index = i;
666 if (long_options_index >= 0)
668 const int short_option = g_options[long_options_index].short_option;
670 switch (short_option)
673 m_option_data.m_print_help = true;
677 m_option_data.m_print_version = true;
681 m_option_data.m_print_python_path = true;
685 m_option_data.m_batch = true;
690 SBFileSpec file(optarg);
693 m_option_data.m_core_file = optarg;
696 error.SetErrorStringWithFormat("file specified in --core (-c) option doesn't exist: '%s'", optarg);
701 m_option_data.m_use_external_editor = true;
705 m_debugger.SkipLLDBInitFiles (true);
706 m_debugger.SkipAppInitFiles (true);
710 m_debugger.SetUseColor (false);
715 SBFileSpec file(optarg);
718 m_option_data.m_args.push_back (optarg);
720 else if (file.ResolveExecutableLocation())
723 file.GetPath (path, sizeof(path));
724 m_option_data.m_args.push_back (path);
727 error.SetErrorStringWithFormat("file specified in --file (-f) option doesn't exist: '%s'", optarg);
732 if (!m_debugger.SetDefaultArchitecture (optarg))
733 error.SetErrorStringWithFormat("invalid architecture in the -a or --arch option: '%s'", optarg);
737 m_option_data.m_script_lang = m_debugger.GetScriptingLanguage (optarg);
741 m_option_data.m_debug_mode = true;
745 m_option_data.m_source_quietly = true;
749 m_option_data.AddInitialCommand(optarg, eCommandPlacementAfterCrash, true, error);
752 m_option_data.AddInitialCommand(optarg, eCommandPlacementAfterCrash, false, error);
756 m_option_data.m_process_name = optarg;
760 m_option_data.m_wait_for = true;
766 m_option_data.m_process_pid = strtol (optarg, &remainder, 0);
767 if (remainder == optarg || *remainder != '\0')
768 error.SetErrorStringWithFormat ("Could not convert process PID: \"%s\" into a pid.",
773 m_option_data.AddInitialCommand(optarg, eCommandPlacementAfterFile, true, error);
776 m_option_data.AddInitialCommand(optarg, eCommandPlacementAfterFile, false, error);
779 m_option_data.AddInitialCommand(optarg, eCommandPlacementBeforeFile, true, error);
782 m_option_data.AddInitialCommand(optarg, eCommandPlacementBeforeFile, false, error);
785 m_option_data.m_print_help = true;
786 error.SetErrorStringWithFormat ("unrecognized option %c", short_option);
792 error.SetErrorStringWithFormat ("invalid option with value %i", val);
801 if (error.Fail() || m_option_data.m_print_help)
803 ShowUsage (out_fh, g_options, m_option_data);
806 else if (m_option_data.m_print_version)
808 ::fprintf (out_fh, "%s\n", m_debugger.GetVersionString());
811 else if (m_option_data.m_print_python_path)
813 SBFileSpec python_file_spec = SBHostOS::GetLLDBPythonPath();
814 if (python_file_spec.IsValid())
816 char python_path[PATH_MAX];
817 size_t num_chars = python_file_spec.GetPath(python_path, PATH_MAX);
818 if (num_chars < PATH_MAX)
820 ::fprintf (out_fh, "%s\n", python_path);
823 ::fprintf (out_fh, "<PATH TOO LONG>\n");
826 ::fprintf (out_fh, "<COULD NOT FIND PATH>\n");
829 else if (m_option_data.m_process_name.empty() && m_option_data.m_process_pid == LLDB_INVALID_PROCESS_ID)
831 // Any arguments that are left over after option parsing are for
832 // the program. If a file was specified with -f then the filename
833 // is already in the m_option_data.m_args array, and any remaining args
834 // are arguments for the inferior program. If no file was specified with
835 // -f, then what is left is the program name followed by any arguments.
837 // Skip any options we consumed with getopt_long_only
843 for (int arg_idx=0; arg_idx<argc; ++arg_idx)
845 const char *arg = argv[arg_idx];
847 m_option_data.m_args.push_back (arg);
854 // Skip any options we consumed with getopt_long_only
856 //argv += optind; // Commented out to keep static analyzer happy
859 ::fprintf (out_fh, "Warning: program arguments are ignored when attaching.\n");
866 PrepareCommandsForSourcing (const char *commands_data, size_t commands_size, int fds[2])
868 enum PIPES { READ, WRITE }; // Constants 0 and 1 for READ and WRITE
870 ::FILE *commands_file = NULL;
875 err = _pipe(fds, commands_size, O_BINARY);
881 ssize_t nrwr = write(fds[WRITE], commands_data, commands_size);
884 fprintf(stderr, "error: write(%i, %p, %" PRIu64 ") failed (errno = %i) "
885 "when trying to open LLDB commands pipe\n",
886 fds[WRITE], commands_data, static_cast<uint64_t>(commands_size), errno);
888 else if (static_cast<size_t>(nrwr) == commands_size)
890 // Close the write end of the pipe so when we give the read end to
891 // the debugger/command interpreter it will exit when it consumes all
894 _close(fds[WRITE]); fds[WRITE] = -1;
896 close(fds[WRITE]); fds[WRITE] = -1;
898 // Now open the read file descriptor in a FILE * that we can give to
899 // the debugger as an input handle
900 commands_file = fdopen(fds[READ], "r");
903 fds[READ] = -1; // The FILE * 'commands_file' now owns the read descriptor
904 // Hand ownership if the FILE * over to the debugger for "commands_file".
909 "error: fdopen(%i, \"r\") failed (errno = %i) when "
910 "trying to open LLDB commands pipe\n",
917 fprintf(stderr, "error: can't create pipe file descriptors for LLDB commands\n");
920 return commands_file;
924 CleanupAfterCommandSourcing (int fds[2])
926 enum PIPES { READ, WRITE }; // Constants 0 and 1 for READ and WRITE
928 // Close any pipes that we still have ownership of
929 if ( fds[WRITE] != -1)
932 _close(fds[WRITE]); fds[WRITE] = -1;
934 close(fds[WRITE]); fds[WRITE] = -1;
939 if ( fds[READ] != -1)
942 _close(fds[READ]); fds[READ] = -1;
944 close(fds[READ]); fds[READ] = -1;
951 EscapeString (std::string arg)
953 std::string::size_type pos = 0;
954 while ((pos = arg.find_first_of("\"\\", pos)) != std::string::npos)
956 arg.insert (pos, 1, '\\');
959 return '"' + arg + '"';
965 if (::tcgetattr(STDIN_FILENO, &g_old_stdin_termios) == 0)
967 g_old_stdin_termios_is_valid = true;
968 atexit (reset_stdin_termios);
971 ::setbuf (stdin, NULL);
972 ::setbuf (stdout, NULL);
974 m_debugger.SetErrorFileHandle (stderr, false);
975 m_debugger.SetOutputFileHandle (stdout, false);
976 m_debugger.SetInputFileHandle (stdin, false); // Don't take ownership of STDIN yet...
978 m_debugger.SetUseExternalEditor(m_option_data.m_use_external_editor);
980 struct winsize window_size;
981 if (isatty (STDIN_FILENO)
982 && ::ioctl (STDIN_FILENO, TIOCGWINSZ, &window_size) == 0)
984 if (window_size.ws_col > 0)
985 m_debugger.SetTerminalWidth (window_size.ws_col);
988 SBCommandInterpreter sb_interpreter = m_debugger.GetCommandInterpreter();
990 // Before we handle any options from the command line, we parse the
991 // .lldbinit file in the user's home directory.
992 SBCommandReturnObject result;
993 sb_interpreter.SourceInitFileInHomeDirectory(result);
996 result.PutError (m_debugger.GetErrorFileHandle());
997 result.PutOutput (m_debugger.GetOutputFileHandle());
1000 // Now we handle options we got from the command line
1001 SBStream commands_stream;
1003 // First source in the commands specified to be run before the file arguments are processed.
1004 WriteCommandsForSourcing(eCommandPlacementBeforeFile, commands_stream);
1006 const size_t num_args = m_option_data.m_args.size();
1010 if (m_debugger.GetDefaultArchitecture (arch_name, sizeof (arch_name)))
1011 commands_stream.Printf("target create --arch=%s %s", arch_name, EscapeString(m_option_data.m_args[0]).c_str());
1013 commands_stream.Printf("target create %s", EscapeString(m_option_data.m_args[0]).c_str());
1015 if (!m_option_data.m_core_file.empty())
1017 commands_stream.Printf(" --core %s", EscapeString(m_option_data.m_core_file).c_str());
1019 commands_stream.Printf("\n");
1023 commands_stream.Printf ("settings set -- target.run-args ");
1024 for (size_t arg_idx = 1; arg_idx < num_args; ++arg_idx)
1025 commands_stream.Printf(" %s", EscapeString(m_option_data.m_args[arg_idx]).c_str());
1026 commands_stream.Printf("\n");
1029 else if (!m_option_data.m_core_file.empty())
1031 commands_stream.Printf("target create --core %s\n", EscapeString(m_option_data.m_core_file).c_str());
1033 else if (!m_option_data.m_process_name.empty())
1035 commands_stream.Printf ("process attach --name %s", EscapeString(m_option_data.m_process_name).c_str());
1037 if (m_option_data.m_wait_for)
1038 commands_stream.Printf(" --waitfor");
1040 commands_stream.Printf("\n");
1043 else if (LLDB_INVALID_PROCESS_ID != m_option_data.m_process_pid)
1045 commands_stream.Printf ("process attach --pid %" PRIu64 "\n", m_option_data.m_process_pid);
1048 WriteCommandsForSourcing(eCommandPlacementAfterFile, commands_stream);
1052 result.PutError(m_debugger.GetErrorFileHandle());
1053 result.PutOutput(m_debugger.GetOutputFileHandle());
1056 bool handle_events = true;
1057 bool spawn_thread = false;
1059 // Check if we have any data in the commands stream, and if so, save it to a temp file
1060 // so we can then run the command interpreter using the file contents.
1061 const char *commands_data = commands_stream.GetData();
1062 const size_t commands_size = commands_stream.GetSize();
1064 // The command file might have requested that we quit, this variable will track that.
1065 bool quit_requested = false;
1066 bool stopped_for_crash = false;
1067 if (commands_data && commands_size)
1069 int initial_commands_fds[2];
1070 bool success = true;
1071 FILE *commands_file = PrepareCommandsForSourcing (commands_data, commands_size, initial_commands_fds);
1074 m_debugger.SetInputFileHandle (commands_file, true);
1076 // Set the debugger into Sync mode when running the command file. Otherwise command files
1077 // that run the target won't run in a sensible way.
1078 bool old_async = m_debugger.GetAsync();
1079 m_debugger.SetAsync(false);
1082 SBCommandInterpreterRunOptions options;
1083 options.SetStopOnError (true);
1084 if (m_option_data.m_batch)
1085 options.SetStopOnCrash (true);
1087 m_debugger.RunCommandInterpreter(handle_events,
1094 if (m_option_data.m_batch && stopped_for_crash && !m_option_data.m_after_crash_commands.empty())
1096 int crash_command_fds[2];
1097 SBStream crash_commands_stream;
1098 WriteCommandsForSourcing (eCommandPlacementAfterCrash, crash_commands_stream);
1099 const char *crash_commands_data = crash_commands_stream.GetData();
1100 const size_t crash_commands_size = crash_commands_stream.GetSize();
1101 commands_file = PrepareCommandsForSourcing (crash_commands_data, crash_commands_size, crash_command_fds);
1104 bool local_quit_requested;
1105 bool local_stopped_for_crash;
1106 m_debugger.SetInputFileHandle (commands_file, true);
1108 m_debugger.RunCommandInterpreter(handle_events,
1112 local_quit_requested,
1113 local_stopped_for_crash);
1114 if (local_quit_requested)
1115 quit_requested = true;
1119 m_debugger.SetAsync(old_async);
1124 // Close any pipes that we still have ownership of
1125 CleanupAfterCommandSourcing(initial_commands_fds);
1127 // Something went wrong with command pipe
1135 // Now set the input file handle to STDIN and run the command
1136 // interpreter again in interactive mode and let the debugger
1137 // take ownership of stdin
1139 bool go_interactive = true;
1141 go_interactive = false;
1142 else if (m_option_data.m_batch && !stopped_for_crash)
1143 go_interactive = false;
1147 m_debugger.SetInputFileHandle (stdin, true);
1148 m_debugger.RunCommandInterpreter(handle_events, spawn_thread);
1151 reset_stdin_termios();
1154 SBDebugger::Destroy (m_debugger);
1159 Driver::ResizeWindow (unsigned short col)
1161 GetDebugger().SetTerminalWidth (col);
1165 sigwinch_handler (int signo)
1167 struct winsize window_size;
1168 if (isatty (STDIN_FILENO)
1169 && ::ioctl (STDIN_FILENO, TIOCGWINSZ, &window_size) == 0)
1171 if ((window_size.ws_col > 0) && g_driver != NULL)
1173 g_driver->ResizeWindow (window_size.ws_col);
1179 sigint_handler (int signo)
1181 static bool g_interrupt_sent = false;
1184 if (!g_interrupt_sent)
1186 g_interrupt_sent = true;
1187 g_driver->GetDebugger().DispatchInputInterrupt();
1188 g_interrupt_sent = false;
1197 sigtstp_handler (int signo)
1199 g_driver->GetDebugger().SaveInputTerminalState();
1200 signal (signo, SIG_DFL);
1201 kill (getpid(), signo);
1202 signal (signo, sigtstp_handler);
1206 sigcont_handler (int signo)
1208 g_driver->GetDebugger().RestoreInputTerminalState();
1209 signal (signo, SIG_DFL);
1210 kill (getpid(), signo);
1211 signal (signo, sigcont_handler);
1215 main (int argc, char const *argv[], const char *envp[])
1218 // disable buffering on windows
1219 setvbuf(stdout, NULL, _IONBF, 0);
1220 setvbuf(stdin , NULL, _IONBF, 0);
1223 SBDebugger::Initialize();
1225 SBHostOS::ThreadCreated ("<lldb.driver.main-thread>");
1227 signal (SIGPIPE, SIG_IGN);
1228 signal (SIGWINCH, sigwinch_handler);
1229 signal (SIGINT, sigint_handler);
1230 signal (SIGTSTP, sigtstp_handler);
1231 signal (SIGCONT, sigcont_handler);
1233 // Create a scope for driver so that the driver object will destroy itself
1234 // before SBDebugger::Terminate() is called.
1238 bool exiting = false;
1239 SBError error (driver.ParseArgs (argc, argv, stdout, exiting));
1242 const char *error_cstr = error.GetCString ();
1244 ::fprintf (stderr, "error: %s\n", error_cstr);
1252 SBDebugger::Terminate();