blob: 188578e9fe69de5602c1237669583b3f4fa16712 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- 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
Deepak Panickal429222c2013-10-15 15:46:40 +000012#include <stdio.h>
Eli Friedmana382d472010-06-09 09:50:17 +000013#include <string.h>
14#include <stdlib.h>
15#include <limits.h>
Eli Friedman07b16272010-06-09 19:11:30 +000016#include <fcntl.h>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017
Greg Clayton49668762014-08-01 18:32:07 +000018// Includes for pipe()
19#if defined(_WIN32)
20#include <io.h>
21#include <fcntl.h>
22#else
23#include <unistd.h>
24#endif
25
Chris Lattner30fdc8d2010-06-08 16:52:24 +000026#include <string>
27
Deepak Panickal429222c2013-10-15 15:46:40 +000028#include <thread>
Jim Inghame6bc6cb2012-02-08 05:23:15 +000029#include "lldb/API/SBBreakpoint.h"
Eli Friedmana382d472010-06-09 09:50:17 +000030#include "lldb/API/SBCommandInterpreter.h"
31#include "lldb/API/SBCommandReturnObject.h"
32#include "lldb/API/SBCommunication.h"
33#include "lldb/API/SBDebugger.h"
34#include "lldb/API/SBEvent.h"
35#include "lldb/API/SBHostOS.h"
36#include "lldb/API/SBListener.h"
Jim Ingham85e8b812011-02-19 02:53:09 +000037#include "lldb/API/SBStream.h"
Eli Friedmana382d472010-06-09 09:50:17 +000038#include "lldb/API/SBTarget.h"
39#include "lldb/API/SBThread.h"
40#include "lldb/API/SBProcess.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000041
Zachary Turner40a069a2014-09-11 20:26:49 +000042#include "llvm/Support/DataTypes.h"
43
Chris Lattner30fdc8d2010-06-08 16:52:24 +000044using namespace lldb;
45
46static void reset_stdin_termios ();
Greg Claytonf571b892012-02-02 19:28:31 +000047static bool g_old_stdin_termios_is_valid = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000048static struct termios g_old_stdin_termios;
49
Caroline Ticedd759852010-09-09 17:45:09 +000050static char *g_debugger_name = (char *) "";
Caroline Ticeefed6132010-11-19 20:47:54 +000051static Driver *g_driver = NULL;
Caroline Ticedd759852010-09-09 17:45:09 +000052
Chris Lattner30fdc8d2010-06-08 16:52:24 +000053// In the Driver::MainLoop, we change the terminal settings. This function is
54// added as an atexit handler to make sure we clean them up.
55static void
56reset_stdin_termios ()
57{
Greg Claytonf571b892012-02-02 19:28:31 +000058 if (g_old_stdin_termios_is_valid)
59 {
60 g_old_stdin_termios_is_valid = false;
61 ::tcsetattr (STDIN_FILENO, TCSANOW, &g_old_stdin_termios);
62 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000063}
64
Greg Claytone0d378b2011-03-24 21:19:54 +000065typedef struct
Chris Lattner30fdc8d2010-06-08 16:52:24 +000066{
Greg Claytone0d378b2011-03-24 21:19:54 +000067 uint32_t usage_mask; // Used to mark options that can be used together. If (1 << n & usage_mask) != 0
68 // then this option belongs to option set n.
69 bool required; // This option is required (in the current usage level)
70 const char * long_option; // Full name for this option.
Greg Clayton3bcdfc02012-12-04 00:32:51 +000071 int short_option; // Single character for this option.
Greg Claytone0d378b2011-03-24 21:19:54 +000072 int option_has_arg; // no_argument, required_argument or optional_argument
Greg Claytonab65b342011-04-13 22:47:15 +000073 uint32_t completion_type; // Cookie the option class can use to do define the argument completion.
Greg Claytone0d378b2011-03-24 21:19:54 +000074 lldb::CommandArgumentType argument_type; // Type of argument this option takes
75 const char * usage_text; // Full text explaining what this options does and what (if any) argument to
76 // pass it.
77} OptionDefinition;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000078
Jim Inghame64f0dc2011-09-13 23:25:31 +000079#define LLDB_3_TO_5 LLDB_OPT_SET_3|LLDB_OPT_SET_4|LLDB_OPT_SET_5
80#define LLDB_4_TO_5 LLDB_OPT_SET_4|LLDB_OPT_SET_5
81
Greg Claytone0d378b2011-03-24 21:19:54 +000082static OptionDefinition g_options[] =
83{
Filipe Cabecinhasd0b87d82012-09-11 18:11:16 +000084 { LLDB_OPT_SET_1, true , "help" , 'h', no_argument , 0, eArgTypeNone,
Jim Ingham12e9a202011-09-15 21:30:02 +000085 "Prints out the usage information for the LLDB debugger." },
Filipe Cabecinhasd0b87d82012-09-11 18:11:16 +000086 { LLDB_OPT_SET_2, true , "version" , 'v', no_argument , 0, eArgTypeNone,
Jim Ingham12e9a202011-09-15 21:30:02 +000087 "Prints out the current version number of the LLDB debugger." },
Filipe Cabecinhasd0b87d82012-09-11 18:11:16 +000088 { LLDB_OPT_SET_3, true , "arch" , 'a', required_argument, 0, eArgTypeArchitecture,
Jim Ingham12e9a202011-09-15 21:30:02 +000089 "Tells the debugger to use the specified architecture when starting and running the program. <architecture> must "
90 "be one of the architectures for which the program was compiled." },
Filipe Cabecinhasd0b87d82012-09-11 18:11:16 +000091 { LLDB_OPT_SET_3, true , "file" , 'f', required_argument, 0, eArgTypeFilename,
Jim Ingham12e9a202011-09-15 21:30:02 +000092 "Tells the debugger to use the file <filename> as the program to be debugged." },
Jason Molenda67c3cf52012-10-24 03:29:40 +000093 { LLDB_OPT_SET_3, false, "core" , 'c', required_argument, 0, eArgTypeFilename,
Johnny Cheneb46f782012-08-15 22:10:42 +000094 "Tells the debugger to use the fullpath to <path> as the core file." },
Jim Inghamf0c63b92014-02-05 21:35:09 +000095 { LLDB_OPT_SET_5, true , "attach-pid" , 'p', required_argument, 0, eArgTypePid,
96 "Tells the debugger to attach to a process with the given pid." },
Filipe Cabecinhasd0b87d82012-09-11 18:11:16 +000097 { LLDB_OPT_SET_4, true , "attach-name" , 'n', required_argument, 0, eArgTypeProcessName,
Jim Ingham12e9a202011-09-15 21:30:02 +000098 "Tells the debugger to attach to a process with the given name." },
Filipe Cabecinhasd0b87d82012-09-11 18:11:16 +000099 { LLDB_OPT_SET_4, true , "wait-for" , 'w', no_argument , 0, eArgTypeNone,
Jim Ingham12e9a202011-09-15 21:30:02 +0000100 "Tells the debugger to wait for a process with the given pid or name to launch before attaching." },
Filipe Cabecinhasd0b87d82012-09-11 18:11:16 +0000101 { LLDB_3_TO_5, false, "source" , 's', required_argument, 0, eArgTypeFilename,
Jim Ingham47ea51f2013-09-17 01:53:35 +0000102 "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." },
Jim Inghamed3252f2013-09-14 00:20:24 +0000103 { LLDB_3_TO_5, false, "one-line" , 'o', required_argument, 0, eArgTypeNone,
Jim Ingham47ea51f2013-09-17 01:53:35 +0000104 "Tells the debugger to execute this one-line lldb command after any file provided on the command line has been loaded." },
Jim Inghamed3252f2013-09-14 00:20:24 +0000105 { LLDB_3_TO_5, false, "source-before-file" , 'S', required_argument, 0, eArgTypeFilename,
Jim Ingham47ea51f2013-09-17 01:53:35 +0000106 "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." },
Jim Inghamed3252f2013-09-14 00:20:24 +0000107 { LLDB_3_TO_5, false, "one-line-before-file" , 'O', required_argument, 0, eArgTypeNone,
Jim Ingham47ea51f2013-09-17 01:53:35 +0000108 "Tells the debugger to execute this one-line lldb command before any file provided on the command line has been loaded." },
Jim Inghamf0c63b92014-02-05 21:35:09 +0000109 { LLDB_3_TO_5, false, "source-quietly" , 'Q', no_argument , 0, eArgTypeNone,
110 "Tells the debugger suppress output from commands provided in the -s, -S, -O and -o commands." },
Filipe Cabecinhasd0b87d82012-09-11 18:11:16 +0000111 { LLDB_3_TO_5, false, "editor" , 'e', no_argument , 0, eArgTypeNone,
Jim Ingham12e9a202011-09-15 21:30:02 +0000112 "Tells the debugger to open source files using the host's \"external editor\" mechanism." },
Filipe Cabecinhasd0b87d82012-09-11 18:11:16 +0000113 { LLDB_3_TO_5, false, "no-lldbinit" , 'x', no_argument , 0, eArgTypeNone,
Jim Ingham12e9a202011-09-15 21:30:02 +0000114 "Do not automatically parse any '.lldbinit' files." },
Jim Inghamed3252f2013-09-14 00:20:24 +0000115 { LLDB_3_TO_5, false, "no-use-colors" , 'X', no_argument , 0, eArgTypeNone,
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000116 "Do not use colors." },
117 { LLDB_OPT_SET_6, true , "python-path" , 'P', no_argument , 0, eArgTypeNone,
Jim Inghame2231ac2012-12-21 22:22:26 +0000118 "Prints out the path to the lldb.py file for this version of lldb." },
Jim Inghamf0c63b92014-02-05 21:35:09 +0000119 { LLDB_3_TO_5, false, "script-language", 'l', required_argument, 0, eArgTypeScriptLang,
120 "Tells the debugger to use the specified scripting language for user-defined scripts, rather than the default. "
121 "Valid scripting languages that can be specified include Python, Perl, Ruby and Tcl. Currently only the Python "
122 "extensions have been implemented." },
123 { LLDB_3_TO_5, false, "debug" , 'd', no_argument , 0, eArgTypeNone,
124 "Tells the debugger to print out extra information for debugging itself." },
Filipe Cabecinhasd0b87d82012-09-11 18:11:16 +0000125 { 0, false, NULL , 0 , 0 , 0, eArgTypeNone, NULL }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000126};
127
Jim Inghame64f0dc2011-09-13 23:25:31 +0000128static const uint32_t last_option_set_with_args = 2;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000129
130Driver::Driver () :
131 SBBroadcaster ("Driver"),
Jim Ingham06942692011-08-13 00:22:20 +0000132 m_debugger (SBDebugger::Create(false)),
Greg Clayton44d93782014-01-27 23:43:24 +0000133 m_option_data ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000134{
Greg Claytonfc3f0272011-05-29 04:06:55 +0000135 // We want to be able to handle CTRL+D in the terminal to have it terminate
136 // certain input
137 m_debugger.SetCloseInputOnEOF (false);
Caroline Ticedd759852010-09-09 17:45:09 +0000138 g_debugger_name = (char *) m_debugger.GetInstanceName();
139 if (g_debugger_name == NULL)
140 g_debugger_name = (char *) "";
Caroline Ticeefed6132010-11-19 20:47:54 +0000141 g_driver = this;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000142}
143
144Driver::~Driver ()
145{
Caroline Ticeefed6132010-11-19 20:47:54 +0000146 g_driver = NULL;
147 g_debugger_name = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000148}
149
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000150
Greg Claytonc982c762010-07-09 20:39:50 +0000151// This function takes INDENT, which tells how many spaces to output at the front
152// of each line; TEXT, which is the text that is to be output. It outputs the
153// text, on multiple lines if necessary, to RESULT, with INDENT spaces at the
154// front of each line. It breaks lines on spaces, tabs or newlines, shortening
155// the line if necessary to not break in the middle of a word. It assumes that
156// each output line should contain a maximum of OUTPUT_MAX_COLUMNS characters.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000157
158void
Greg Claytonc982c762010-07-09 20:39:50 +0000159OutputFormattedUsageText (FILE *out, int indent, const char *text, int output_max_columns)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000160{
161 int len = strlen (text);
162 std::string text_string (text);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000163
164 // Force indentation to be reasonable.
165 if (indent >= output_max_columns)
166 indent = 0;
167
168 // Will it all fit on one line?
169
170 if (len + indent < output_max_columns)
171 // Output as a single line
Greg Claytonc982c762010-07-09 20:39:50 +0000172 fprintf (out, "%*s%s\n", indent, "", text);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000173 else
174 {
175 // We need to break it up into multiple lines.
176 int text_width = output_max_columns - indent - 1;
177 int start = 0;
178 int end = start;
179 int final_end = len;
180 int sub_len;
181
182 while (end < final_end)
183 {
184 // Dont start the 'text' on a space, since we're already outputting the indentation.
185 while ((start < final_end) && (text[start] == ' '))
186 start++;
187
188 end = start + text_width;
189 if (end > final_end)
190 end = final_end;
191 else
192 {
193 // If we're not at the end of the text, make sure we break the line on white space.
194 while (end > start
195 && text[end] != ' ' && text[end] != '\t' && text[end] != '\n')
196 end--;
197 }
198 sub_len = end - start;
199 std::string substring = text_string.substr (start, sub_len);
Greg Claytonc982c762010-07-09 20:39:50 +0000200 fprintf (out, "%*s%s\n", indent, "", substring.c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000201 start = end + 1;
202 }
203 }
204}
205
206void
Greg Claytone0d378b2011-03-24 21:19:54 +0000207ShowUsage (FILE *out, OptionDefinition *option_table, Driver::OptionData data)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000208{
209 uint32_t screen_width = 80;
210 uint32_t indent_level = 0;
211 const char *name = "lldb";
Jim Ingham86511212010-06-15 18:47:14 +0000212
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000213 fprintf (out, "\nUsage:\n\n");
214
215 indent_level += 2;
216
217
218 // First, show each usage level set of options, e.g. <cmd> [options-for-level-0]
219 // <cmd> [options-for-level-1]
220 // etc.
221
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000222 uint32_t num_options;
Jim Ingham86511212010-06-15 18:47:14 +0000223 uint32_t num_option_sets = 0;
224
225 for (num_options = 0; option_table[num_options].long_option != NULL; ++num_options)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000226 {
Jim Ingham86511212010-06-15 18:47:14 +0000227 uint32_t this_usage_mask = option_table[num_options].usage_mask;
228 if (this_usage_mask == LLDB_OPT_SET_ALL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000229 {
Jim Ingham86511212010-06-15 18:47:14 +0000230 if (num_option_sets == 0)
231 num_option_sets = 1;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000232 }
233 else
234 {
Greg Claytonc982c762010-07-09 20:39:50 +0000235 for (uint32_t j = 0; j < LLDB_MAX_NUM_OPTION_SETS; j++)
Jim Ingham86511212010-06-15 18:47:14 +0000236 {
237 if (this_usage_mask & 1 << j)
238 {
239 if (num_option_sets <= j)
240 num_option_sets = j + 1;
241 }
242 }
243 }
244 }
245
246 for (uint32_t opt_set = 0; opt_set < num_option_sets; opt_set++)
247 {
248 uint32_t opt_set_mask;
249
250 opt_set_mask = 1 << opt_set;
251
252 if (opt_set > 0)
253 fprintf (out, "\n");
Greg Claytonc982c762010-07-09 20:39:50 +0000254 fprintf (out, "%*s%s", indent_level, "", name);
Jim Ingham556e6532011-08-16 23:15:02 +0000255 bool is_help_line = false;
Jim Ingham86511212010-06-15 18:47:14 +0000256
257 for (uint32_t i = 0; i < num_options; ++i)
258 {
259 if (option_table[i].usage_mask & opt_set_mask)
260 {
Caroline Ticedeaab222010-10-01 19:59:14 +0000261 CommandArgumentType arg_type = option_table[i].argument_type;
Greg Clayton9d0402b2011-02-20 02:15:07 +0000262 const char *arg_name = SBCommandInterpreter::GetArgumentTypeAsCString (arg_type);
Jim Ingham556e6532011-08-16 23:15:02 +0000263 // This is a bit of a hack, but there's no way to say certain options don't have arguments yet...
264 // so we do it by hand here.
265 if (option_table[i].short_option == 'h')
266 is_help_line = true;
267
Jim Ingham86511212010-06-15 18:47:14 +0000268 if (option_table[i].required)
269 {
270 if (option_table[i].option_has_arg == required_argument)
Greg Clayton9d0402b2011-02-20 02:15:07 +0000271 fprintf (out, " -%c <%s>", option_table[i].short_option, arg_name);
Jim Ingham86511212010-06-15 18:47:14 +0000272 else if (option_table[i].option_has_arg == optional_argument)
Greg Clayton9d0402b2011-02-20 02:15:07 +0000273 fprintf (out, " -%c [<%s>]", option_table[i].short_option, arg_name);
Jim Ingham86511212010-06-15 18:47:14 +0000274 else
275 fprintf (out, " -%c", option_table[i].short_option);
276 }
277 else
278 {
279 if (option_table[i].option_has_arg == required_argument)
Greg Clayton9d0402b2011-02-20 02:15:07 +0000280 fprintf (out, " [-%c <%s>]", option_table[i].short_option, arg_name);
Jim Ingham86511212010-06-15 18:47:14 +0000281 else if (option_table[i].option_has_arg == optional_argument)
Greg Clayton9d0402b2011-02-20 02:15:07 +0000282 fprintf (out, " [-%c [<%s>]]", option_table[i].short_option, arg_name);
Jim Ingham86511212010-06-15 18:47:14 +0000283 else
284 fprintf (out, " [-%c]", option_table[i].short_option);
285 }
286 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000287 }
Jim Inghame64f0dc2011-09-13 23:25:31 +0000288 if (!is_help_line && (opt_set <= last_option_set_with_args))
Jim Ingham556e6532011-08-16 23:15:02 +0000289 fprintf (out, " [[--] <PROGRAM-ARG-1> [<PROGRAM_ARG-2> ...]]");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000290 }
291
292 fprintf (out, "\n\n");
293
294 // Now print out all the detailed information about the various options: long form, short form and help text:
295 // -- long_name <argument>
296 // - short <argument>
297 // help text
298
299 // This variable is used to keep track of which options' info we've printed out, because some options can be in
300 // more than one usage level, but we only want to print the long form of its information once.
301
302 Driver::OptionData::OptionSet options_seen;
303 Driver::OptionData::OptionSet::iterator pos;
304
305 indent_level += 5;
306
Jim Ingham86511212010-06-15 18:47:14 +0000307 for (uint32_t i = 0; i < num_options; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000308 {
309 // Only print this option if we haven't already seen it.
310 pos = options_seen.find (option_table[i].short_option);
311 if (pos == options_seen.end())
312 {
Caroline Ticedeaab222010-10-01 19:59:14 +0000313 CommandArgumentType arg_type = option_table[i].argument_type;
Greg Clayton9d0402b2011-02-20 02:15:07 +0000314 const char *arg_name = SBCommandInterpreter::GetArgumentTypeAsCString (arg_type);
Caroline Ticedeaab222010-10-01 19:59:14 +0000315
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000316 options_seen.insert (option_table[i].short_option);
Greg Claytonc982c762010-07-09 20:39:50 +0000317 fprintf (out, "%*s-%c ", indent_level, "", option_table[i].short_option);
Caroline Ticedeaab222010-10-01 19:59:14 +0000318 if (arg_type != eArgTypeNone)
Greg Clayton9d0402b2011-02-20 02:15:07 +0000319 fprintf (out, "<%s>", arg_name);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000320 fprintf (out, "\n");
Greg Claytonc982c762010-07-09 20:39:50 +0000321 fprintf (out, "%*s--%s ", indent_level, "", option_table[i].long_option);
Caroline Ticedeaab222010-10-01 19:59:14 +0000322 if (arg_type != eArgTypeNone)
Greg Clayton9d0402b2011-02-20 02:15:07 +0000323 fprintf (out, "<%s>", arg_name);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000324 fprintf (out, "\n");
325 indent_level += 5;
Greg Claytonc982c762010-07-09 20:39:50 +0000326 OutputFormattedUsageText (out, indent_level, option_table[i].usage_text, screen_width);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000327 indent_level -= 5;
328 fprintf (out, "\n");
329 }
330 }
331
332 indent_level -= 5;
Jim Inghamf0c63b92014-02-05 21:35:09 +0000333
334 fprintf (out, "\n%*sNotes:\n",
335 indent_level, "");
336 indent_level += 5;
337
Jim Ingham47ea51f2013-09-17 01:53:35 +0000338 fprintf (out, "\n%*sMultiple \"-s\" and \"-o\" options can be provided. They will be processed from left to right in order, "
339 "\n%*swith the source files and commands interleaved. The same is true of the \"-S\" and \"-O\" options."
340 "\n%*sThe before file and after file sets can intermixed freely, the command parser will sort them out."
341 "\n%*sThe order of the file specifiers (\"-c\", \"-f\", etc.) is not significant in this regard.\n\n",
342 indent_level, "",
343 indent_level, "",
344 indent_level, "",
345 indent_level, "");
346
Jim Inghamf0c63b92014-02-05 21:35:09 +0000347 fprintf (out, "\n%*sIf you don't provide -f then the first argument will be the file to be debugged"
348 "\n%*swhich means that '%s -- <filename> [<ARG1> [<ARG2>]]' also works."
349 "\n%*sBut remember to end the options with \"--\" if any of your arguments have a \"-\" in them.\n\n",
Jim Inghama9deaf92011-08-16 23:57:58 +0000350 indent_level, "",
351 indent_level, "",
352 name,
353 indent_level, "");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000354}
355
356void
Greg Claytone0d378b2011-03-24 21:19:54 +0000357BuildGetOptTable (OptionDefinition *expanded_option_table, std::vector<struct option> &getopt_table,
Caroline Tice4ab31c92010-10-12 21:57:09 +0000358 uint32_t num_options)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000359{
360 if (num_options == 0)
361 return;
362
363 uint32_t i;
364 uint32_t j;
365 std::bitset<256> option_seen;
366
Caroline Tice4ab31c92010-10-12 21:57:09 +0000367 getopt_table.resize (num_options + 1);
368
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000369 for (i = 0, j = 0; i < num_options; ++i)
Greg Claytonc982c762010-07-09 20:39:50 +0000370 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000371 char short_opt = expanded_option_table[i].short_option;
Greg Claytonc982c762010-07-09 20:39:50 +0000372
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000373 if (option_seen.test(short_opt) == false)
Greg Claytonc982c762010-07-09 20:39:50 +0000374 {
Caroline Tice4ab31c92010-10-12 21:57:09 +0000375 getopt_table[j].name = expanded_option_table[i].long_option;
376 getopt_table[j].has_arg = expanded_option_table[i].option_has_arg;
377 getopt_table[j].flag = NULL;
378 getopt_table[j].val = expanded_option_table[i].short_option;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000379 option_seen.set(short_opt);
380 ++j;
Greg Claytonc982c762010-07-09 20:39:50 +0000381 }
382 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000383
Caroline Tice4ab31c92010-10-12 21:57:09 +0000384 getopt_table[j].name = NULL;
385 getopt_table[j].has_arg = 0;
386 getopt_table[j].flag = NULL;
387 getopt_table[j].val = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000388
389}
390
Greg Clayton66111032010-06-23 01:19:29 +0000391Driver::OptionData::OptionData () :
Greg Clayton8d846da2010-12-08 22:23:24 +0000392 m_args(),
Greg Clayton66111032010-06-23 01:19:29 +0000393 m_script_lang (lldb::eScriptLanguageDefault),
Johnny Cheneb46f782012-08-15 22:10:42 +0000394 m_core_file (),
Greg Claytonc982c762010-07-09 20:39:50 +0000395 m_crash_log (),
Jim Inghamed3252f2013-09-14 00:20:24 +0000396 m_initial_commands (),
397 m_after_file_commands (),
Greg Clayton66111032010-06-23 01:19:29 +0000398 m_debug_mode (false),
Jim Inghamed3252f2013-09-14 00:20:24 +0000399 m_source_quietly(false),
Greg Claytonc982c762010-07-09 20:39:50 +0000400 m_print_version (false),
Jim Inghame2231ac2012-12-21 22:22:26 +0000401 m_print_python_path (false),
Greg Clayton66111032010-06-23 01:19:29 +0000402 m_print_help (false),
Jim Inghame64f0dc2011-09-13 23:25:31 +0000403 m_wait_for(false),
404 m_process_name(),
405 m_process_pid(LLDB_INVALID_PROCESS_ID),
Daniel Dunbara08823f2011-10-31 22:50:49 +0000406 m_use_external_editor(false),
Stephen Wilson71c21d12011-04-11 19:41:40 +0000407 m_seen_options()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000408{
Greg Clayton66111032010-06-23 01:19:29 +0000409}
410
411Driver::OptionData::~OptionData ()
412{
413}
414
415void
416Driver::OptionData::Clear ()
417{
Greg Clayton8d846da2010-12-08 22:23:24 +0000418 m_args.clear ();
Greg Clayton66111032010-06-23 01:19:29 +0000419 m_script_lang = lldb::eScriptLanguageDefault;
Jim Inghamed3252f2013-09-14 00:20:24 +0000420 m_initial_commands.clear ();
421 m_after_file_commands.clear ();
Greg Clayton66111032010-06-23 01:19:29 +0000422 m_debug_mode = false;
Jim Inghamed3252f2013-09-14 00:20:24 +0000423 m_source_quietly = false;
Greg Clayton66111032010-06-23 01:19:29 +0000424 m_print_help = false;
425 m_print_version = false;
Jim Inghame2231ac2012-12-21 22:22:26 +0000426 m_print_python_path = false;
Jim Inghame40e4212010-08-30 19:44:40 +0000427 m_use_external_editor = false;
Jim Inghame64f0dc2011-09-13 23:25:31 +0000428 m_wait_for = false;
429 m_process_name.erase();
430 m_process_pid = LLDB_INVALID_PROCESS_ID;
Greg Clayton66111032010-06-23 01:19:29 +0000431}
432
433void
Jim Inghamed3252f2013-09-14 00:20:24 +0000434Driver::OptionData::AddInitialCommand (const char *command, bool before_file, bool is_file, SBError &error)
435{
436 std::vector<std::pair<bool, std::string> > *command_set;
437 if (before_file)
438 command_set = &(m_initial_commands);
439 else
440 command_set = &(m_after_file_commands);
441
442 if (is_file)
443 {
444 SBFileSpec file(command);
445 if (file.Exists())
446 command_set->push_back (std::pair<bool, std::string> (true, optarg));
447 else if (file.ResolveExecutableLocation())
448 {
449 char final_path[PATH_MAX];
450 file.GetPath (final_path, sizeof(final_path));
451 std::string path_str (final_path);
452 command_set->push_back (std::pair<bool, std::string> (true, path_str));
453 }
454 else
455 error.SetErrorStringWithFormat("file specified in --source (-s) option doesn't exist: '%s'", optarg);
456 }
457 else
458 command_set->push_back (std::pair<bool, std::string> (false, optarg));
459}
460
461void
Greg Clayton66111032010-06-23 01:19:29 +0000462Driver::ResetOptionValues ()
463{
464 m_option_data.Clear ();
465}
466
467const char *
468Driver::GetFilename() const
469{
Greg Clayton8d846da2010-12-08 22:23:24 +0000470 if (m_option_data.m_args.empty())
Greg Clayton66111032010-06-23 01:19:29 +0000471 return NULL;
Greg Clayton8d846da2010-12-08 22:23:24 +0000472 return m_option_data.m_args.front().c_str();
Greg Clayton66111032010-06-23 01:19:29 +0000473}
474
475const char *
476Driver::GetCrashLogFilename() const
477{
478 if (m_option_data.m_crash_log.empty())
479 return NULL;
480 return m_option_data.m_crash_log.c_str();
481}
482
483lldb::ScriptLanguage
484Driver::GetScriptLanguage() const
485{
486 return m_option_data.m_script_lang;
487}
488
Jim Inghamed3252f2013-09-14 00:20:24 +0000489void
Greg Clayton06357c92014-07-30 17:38:47 +0000490Driver::WriteInitialCommands (bool before_file, SBStream &strm)
Greg Clayton66111032010-06-23 01:19:29 +0000491{
Greg Clayton06357c92014-07-30 17:38:47 +0000492 std::vector<std::pair<bool, std::string> > &command_set = before_file ? m_option_data.m_initial_commands :
493 m_option_data.m_after_file_commands;
Jim Inghamed3252f2013-09-14 00:20:24 +0000494
Greg Clayton06357c92014-07-30 17:38:47 +0000495 for (const auto &command_pair : command_set)
Jim Inghamed3252f2013-09-14 00:20:24 +0000496 {
Greg Clayton06357c92014-07-30 17:38:47 +0000497 const char *command = command_pair.second.c_str();
498 if (command_pair.first)
499 strm.Printf("command source -s %i '%s'\n", m_option_data.m_source_quietly, command);
500 else
501 strm.Printf("%s\n", command);
Jim Inghamed3252f2013-09-14 00:20:24 +0000502 }
Greg Clayton66111032010-06-23 01:19:29 +0000503}
504
505bool
506Driver::GetDebugMode() const
507{
508 return m_option_data.m_debug_mode;
509}
510
511
512// Check the arguments that were passed to this program to make sure they are valid and to get their
513// argument values (if any). Return a boolean value indicating whether or not to start up the full
514// debugger (i.e. the Command Interpreter) or not. Return FALSE if the arguments were invalid OR
515// if the user only wanted help or version information.
516
517SBError
Deepak Panickal429222c2013-10-15 15:46:40 +0000518Driver::ParseArgs (int argc, const char *argv[], FILE *out_fh, bool &exiting)
Greg Clayton66111032010-06-23 01:19:29 +0000519{
520 ResetOptionValues ();
521
522 SBCommandReturnObject result;
523
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000524 SBError error;
525 std::string option_string;
526 struct option *long_options = NULL;
Caroline Tice4ab31c92010-10-12 21:57:09 +0000527 std::vector<struct option> long_options_vector;
Greg Claytonc982c762010-07-09 20:39:50 +0000528 uint32_t num_options;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000529
Greg Claytonc982c762010-07-09 20:39:50 +0000530 for (num_options = 0; g_options[num_options].long_option != NULL; ++num_options)
531 /* Do Nothing. */;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000532
533 if (num_options == 0)
534 {
535 if (argc > 1)
536 error.SetErrorStringWithFormat ("invalid number of options");
537 return error;
538 }
539
Caroline Tice4ab31c92010-10-12 21:57:09 +0000540 BuildGetOptTable (g_options, long_options_vector, num_options);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000541
Caroline Tice4ab31c92010-10-12 21:57:09 +0000542 if (long_options_vector.empty())
543 long_options = NULL;
544 else
545 long_options = &long_options_vector.front();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000546
547 if (long_options == NULL)
548 {
549 error.SetErrorStringWithFormat ("invalid long options");
550 return error;
551 }
552
Greg Claytonb7ad58a2013-04-04 20:35:24 +0000553 // Build the option_string argument for call to getopt_long_only.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000554
555 for (int i = 0; long_options[i].name != NULL; ++i)
556 {
557 if (long_options[i].flag == NULL)
558 {
559 option_string.push_back ((char) long_options[i].val);
560 switch (long_options[i].has_arg)
561 {
562 default:
563 case no_argument:
564 break;
565 case required_argument:
566 option_string.push_back (':');
567 break;
568 case optional_argument:
569 option_string.append ("::");
570 break;
571 }
572 }
573 }
574
Jim Ingham06942692011-08-13 00:22:20 +0000575 // This is kind of a pain, but since we make the debugger in the Driver's constructor, we can't
576 // know at that point whether we should read in init files yet. So we don't read them in in the
577 // Driver constructor, then set the flags back to "read them in" here, and then if we see the
578 // "-n" flag, we'll turn it off again. Finally we have to read them in by hand later in the
579 // main loop.
580
581 m_debugger.SkipLLDBInitFiles (false);
582 m_debugger.SkipAppInitFiles (false);
583
Greg Claytonb7ad58a2013-04-04 20:35:24 +0000584 // Prepare for & make calls to getopt_long_only.
Eli Friedmanadb35022010-06-13 19:18:49 +0000585#if __GLIBC__
586 optind = 0;
587#else
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000588 optreset = 1;
589 optind = 1;
Eli Friedmanadb35022010-06-13 19:18:49 +0000590#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000591 int val;
592 while (1)
593 {
594 int long_options_index = -1;
Greg Claytonb7ad58a2013-04-04 20:35:24 +0000595 val = ::getopt_long_only (argc, const_cast<char **>(argv), option_string.c_str(), long_options, &long_options_index);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000596
597 if (val == -1)
598 break;
599 else if (val == '?')
600 {
Greg Clayton66111032010-06-23 01:19:29 +0000601 m_option_data.m_print_help = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000602 error.SetErrorStringWithFormat ("unknown or ambiguous option");
603 break;
604 }
605 else if (val == 0)
606 continue;
607 else
608 {
Greg Clayton66111032010-06-23 01:19:29 +0000609 m_option_data.m_seen_options.insert ((char) val);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000610 if (long_options_index == -1)
611 {
612 for (int i = 0;
613 long_options[i].name || long_options[i].has_arg || long_options[i].flag || long_options[i].val;
614 ++i)
615 {
616 if (long_options[i].val == val)
617 {
618 long_options_index = i;
619 break;
620 }
621 }
622 }
623
624 if (long_options_index >= 0)
625 {
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000626 const int short_option = g_options[long_options_index].short_option;
Greg Clayton66111032010-06-23 01:19:29 +0000627
628 switch (short_option)
629 {
630 case 'h':
631 m_option_data.m_print_help = true;
632 break;
633
634 case 'v':
635 m_option_data.m_print_version = true;
636 break;
637
Jim Inghame2231ac2012-12-21 22:22:26 +0000638 case 'P':
639 m_option_data.m_print_python_path = true;
640 break;
641
Greg Clayton66111032010-06-23 01:19:29 +0000642 case 'c':
Johnny Cheneb46f782012-08-15 22:10:42 +0000643 {
644 SBFileSpec file(optarg);
645 if (file.Exists())
646 {
647 m_option_data.m_core_file = optarg;
648 }
649 else
650 error.SetErrorStringWithFormat("file specified in --core (-c) option doesn't exist: '%s'", optarg);
651 }
Greg Clayton66111032010-06-23 01:19:29 +0000652 break;
Johnny Cheneb46f782012-08-15 22:10:42 +0000653
Jim Inghame40e4212010-08-30 19:44:40 +0000654 case 'e':
655 m_option_data.m_use_external_editor = true;
656 break;
Greg Clayton6eee5aa2010-10-11 01:05:37 +0000657
Jim Inghame64f0dc2011-09-13 23:25:31 +0000658 case 'x':
Greg Clayton6eee5aa2010-10-11 01:05:37 +0000659 m_debugger.SkipLLDBInitFiles (true);
Jim Ingham06942692011-08-13 00:22:20 +0000660 m_debugger.SkipAppInitFiles (true);
Greg Clayton6eee5aa2010-10-11 01:05:37 +0000661 break;
662
Jim Inghamed3252f2013-09-14 00:20:24 +0000663 case 'X':
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000664 m_debugger.SetUseColor (false);
665 break;
666
Greg Clayton66111032010-06-23 01:19:29 +0000667 case 'f':
668 {
669 SBFileSpec file(optarg);
670 if (file.Exists())
Greg Clayton8d846da2010-12-08 22:23:24 +0000671 {
672 m_option_data.m_args.push_back (optarg);
673 }
Caroline Tice428a9a52010-09-10 04:48:55 +0000674 else if (file.ResolveExecutableLocation())
675 {
676 char path[PATH_MAX];
Johnny Chen25f3a3c2011-08-10 22:06:24 +0000677 file.GetPath (path, sizeof(path));
Greg Clayton8d846da2010-12-08 22:23:24 +0000678 m_option_data.m_args.push_back (path);
Caroline Tice428a9a52010-09-10 04:48:55 +0000679 }
Greg Clayton66111032010-06-23 01:19:29 +0000680 else
681 error.SetErrorStringWithFormat("file specified in --file (-f) option doesn't exist: '%s'", optarg);
682 }
683 break;
684
685 case 'a':
686 if (!m_debugger.SetDefaultArchitecture (optarg))
687 error.SetErrorStringWithFormat("invalid architecture in the -a or --arch option: '%s'", optarg);
688 break;
689
690 case 'l':
691 m_option_data.m_script_lang = m_debugger.GetScriptingLanguage (optarg);
692 break;
693
694 case 'd':
695 m_option_data.m_debug_mode = true;
696 break;
697
Jim Inghamf0c63b92014-02-05 21:35:09 +0000698 case 'Q':
Jim Inghamed3252f2013-09-14 00:20:24 +0000699 m_option_data.m_source_quietly = true;
700 break;
701
Jim Inghame64f0dc2011-09-13 23:25:31 +0000702 case 'n':
703 m_option_data.m_process_name = optarg;
704 break;
705
706 case 'w':
707 m_option_data.m_wait_for = true;
708 break;
709
710 case 'p':
711 {
712 char *remainder;
713 m_option_data.m_process_pid = strtol (optarg, &remainder, 0);
714 if (remainder == optarg || *remainder != '\0')
715 error.SetErrorStringWithFormat ("Could not convert process PID: \"%s\" into a pid.",
716 optarg);
717 }
718 break;
Greg Clayton66111032010-06-23 01:19:29 +0000719 case 's':
Jim Inghamed3252f2013-09-14 00:20:24 +0000720 m_option_data.AddInitialCommand(optarg, false, true, error);
Greg Clayton66111032010-06-23 01:19:29 +0000721 break;
Jim Inghamed3252f2013-09-14 00:20:24 +0000722 case 'o':
723 m_option_data.AddInitialCommand(optarg, false, false, error);
724 break;
725 case 'S':
726 m_option_data.AddInitialCommand(optarg, true, true, error);
727 break;
728 case 'O':
729 m_option_data.AddInitialCommand(optarg, true, false, error);
730 break;
Greg Clayton66111032010-06-23 01:19:29 +0000731 default:
732 m_option_data.m_print_help = true;
733 error.SetErrorStringWithFormat ("unrecognized option %c", short_option);
734 break;
735 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000736 }
737 else
738 {
739 error.SetErrorStringWithFormat ("invalid option with value %i", val);
740 }
741 if (error.Fail())
Caroline Tice4ab31c92010-10-12 21:57:09 +0000742 {
Greg Clayton66111032010-06-23 01:19:29 +0000743 return error;
Caroline Tice4ab31c92010-10-12 21:57:09 +0000744 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000745 }
746 }
Jim Ingham86511212010-06-15 18:47:14 +0000747
Greg Clayton66111032010-06-23 01:19:29 +0000748 if (error.Fail() || m_option_data.m_print_help)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000749 {
750 ShowUsage (out_fh, g_options, m_option_data);
Deepak Panickal429222c2013-10-15 15:46:40 +0000751 exiting = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000752 }
753 else if (m_option_data.m_print_version)
754 {
Greg Clayton66111032010-06-23 01:19:29 +0000755 ::fprintf (out_fh, "%s\n", m_debugger.GetVersionString());
Deepak Panickal429222c2013-10-15 15:46:40 +0000756 exiting = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000757 }
Jim Inghame2231ac2012-12-21 22:22:26 +0000758 else if (m_option_data.m_print_python_path)
759 {
760 SBFileSpec python_file_spec = SBHostOS::GetLLDBPythonPath();
761 if (python_file_spec.IsValid())
762 {
763 char python_path[PATH_MAX];
764 size_t num_chars = python_file_spec.GetPath(python_path, PATH_MAX);
765 if (num_chars < PATH_MAX)
766 {
767 ::fprintf (out_fh, "%s\n", python_path);
768 }
769 else
770 ::fprintf (out_fh, "<PATH TOO LONG>\n");
771 }
772 else
773 ::fprintf (out_fh, "<COULD NOT FIND PATH>\n");
Deepak Panickal429222c2013-10-15 15:46:40 +0000774 exiting = true;
Jim Inghame2231ac2012-12-21 22:22:26 +0000775 }
Jim Inghame64f0dc2011-09-13 23:25:31 +0000776 else if (m_option_data.m_process_name.empty() && m_option_data.m_process_pid == LLDB_INVALID_PROCESS_ID)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000777 {
Greg Clayton8d846da2010-12-08 22:23:24 +0000778 // Any arguments that are left over after option parsing are for
779 // the program. If a file was specified with -f then the filename
780 // is already in the m_option_data.m_args array, and any remaining args
781 // are arguments for the inferior program. If no file was specified with
782 // -f, then what is left is the program name followed by any arguments.
783
Greg Claytonb7ad58a2013-04-04 20:35:24 +0000784 // Skip any options we consumed with getopt_long_only
Greg Clayton8d846da2010-12-08 22:23:24 +0000785 argc -= optind;
786 argv += optind;
787
788 if (argc > 0)
789 {
790 for (int arg_idx=0; arg_idx<argc; ++arg_idx)
791 {
792 const char *arg = argv[arg_idx];
793 if (arg)
794 m_option_data.m_args.push_back (arg);
795 }
796 }
797
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000798 }
Jim Inghame64f0dc2011-09-13 23:25:31 +0000799 else
800 {
Greg Claytonb7ad58a2013-04-04 20:35:24 +0000801 // Skip any options we consumed with getopt_long_only
Jim Inghame64f0dc2011-09-13 23:25:31 +0000802 argc -= optind;
Greg Clayton23f59502012-07-17 03:23:13 +0000803 //argv += optind; // Commented out to keep static analyzer happy
Jim Inghame64f0dc2011-09-13 23:25:31 +0000804
805 if (argc > 0)
806 ::fprintf (out_fh, "Warning: program arguments are ignored when attaching.\n");
807 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000808
Greg Clayton66111032010-06-23 01:19:29 +0000809 return error;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000810}
811
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000812void
813Driver::MainLoop ()
814{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000815 if (::tcgetattr(STDIN_FILENO, &g_old_stdin_termios) == 0)
Greg Claytonf571b892012-02-02 19:28:31 +0000816 {
817 g_old_stdin_termios_is_valid = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000818 atexit (reset_stdin_termios);
Greg Claytonf571b892012-02-02 19:28:31 +0000819 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000820
821 ::setbuf (stdin, NULL);
822 ::setbuf (stdout, NULL);
823
Greg Clayton66111032010-06-23 01:19:29 +0000824 m_debugger.SetErrorFileHandle (stderr, false);
825 m_debugger.SetOutputFileHandle (stdout, false);
Greg Clayton06357c92014-07-30 17:38:47 +0000826 m_debugger.SetInputFileHandle (stdin, false); // Don't take ownership of STDIN yet...
827
Jim Inghame40e4212010-08-30 19:44:40 +0000828 m_debugger.SetUseExternalEditor(m_option_data.m_use_external_editor);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000829
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000830 struct winsize window_size;
831 if (isatty (STDIN_FILENO)
832 && ::ioctl (STDIN_FILENO, TIOCGWINSZ, &window_size) == 0)
833 {
Caroline Tice3df9a8d2010-09-04 00:03:46 +0000834 if (window_size.ws_col > 0)
Greg Claytona7015092010-09-18 01:14:36 +0000835 m_debugger.SetTerminalWidth (window_size.ws_col);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000836 }
837
Greg Clayton44d93782014-01-27 23:43:24 +0000838 SBCommandInterpreter sb_interpreter = m_debugger.GetCommandInterpreter();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000839
Greg Clayton44d93782014-01-27 23:43:24 +0000840 // Before we handle any options from the command line, we parse the
841 // .lldbinit file in the user's home directory.
842 SBCommandReturnObject result;
843 sb_interpreter.SourceInitFileInHomeDirectory(result);
844 if (GetDebugMode())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000845 {
Greg Clayton44d93782014-01-27 23:43:24 +0000846 result.PutError (m_debugger.GetErrorFileHandle());
847 result.PutOutput (m_debugger.GetOutputFileHandle());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000848 }
Greg Clayton44d93782014-01-27 23:43:24 +0000849
850 // Now we handle options we got from the command line
Ed Mastef8314532014-07-30 19:26:11 +0000851 SBStream commands_stream;
Greg Clayton06357c92014-07-30 17:38:47 +0000852
Greg Clayton44d93782014-01-27 23:43:24 +0000853 // First source in the commands specified to be run before the file arguments are processed.
Greg Clayton06357c92014-07-30 17:38:47 +0000854 WriteInitialCommands(true, commands_stream);
855
Greg Clayton44d93782014-01-27 23:43:24 +0000856 const size_t num_args = m_option_data.m_args.size();
857 if (num_args > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000858 {
Jim Ingham5b1fe952014-08-07 23:01:31 +0000859 char arch_name[64];
860 if (m_debugger.GetDefaultArchitecture (arch_name, sizeof (arch_name)))
861 commands_stream.Printf("target create --arch=%s \"%s\"", arch_name, m_option_data.m_args[0].c_str());
862 else
863 commands_stream.Printf("target create \"%s\"", m_option_data.m_args[0].c_str());
864
Greg Clayton06357c92014-07-30 17:38:47 +0000865 if (!m_option_data.m_core_file.empty())
866 {
867 commands_stream.Printf(" --core \"%s\"", m_option_data.m_core_file.c_str());
868 }
869 commands_stream.Printf("\n");
Greg Clayton44d93782014-01-27 23:43:24 +0000870
871 if (num_args > 1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000872 {
Greg Clayton06357c92014-07-30 17:38:47 +0000873 commands_stream.Printf ("settings set -- target.run-args ");
Greg Clayton44d93782014-01-27 23:43:24 +0000874 for (size_t arg_idx = 1; arg_idx < num_args; ++arg_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000875 {
Greg Clayton06357c92014-07-30 17:38:47 +0000876 const char *arg_cstr = m_option_data.m_args[arg_idx].c_str();
877 if (strchr(arg_cstr, '"') == NULL)
878 commands_stream.Printf(" \"%s\"", arg_cstr);
879 else
880 commands_stream.Printf(" '%s'", arg_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000881 }
Greg Clayton06357c92014-07-30 17:38:47 +0000882 commands_stream.Printf("\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000883 }
884 }
Greg Clayton06357c92014-07-30 17:38:47 +0000885 else if (!m_option_data.m_core_file.empty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000886 {
Greg Clayton06357c92014-07-30 17:38:47 +0000887 commands_stream.Printf("target create --core \"%s\"\n", m_option_data.m_core_file.c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000888 }
Greg Clayton9c0b64c2014-02-06 18:22:44 +0000889 else if (!m_option_data.m_process_name.empty())
890 {
Greg Clayton06357c92014-07-30 17:38:47 +0000891 commands_stream.Printf ("process attach --name \"%s\"", m_option_data.m_process_name.c_str());
892
893 if (m_option_data.m_wait_for)
894 commands_stream.Printf(" --waitfor");
895
896 commands_stream.Printf("\n");
897
Greg Clayton9c0b64c2014-02-06 18:22:44 +0000898 }
899 else if (LLDB_INVALID_PROCESS_ID != m_option_data.m_process_pid)
900 {
Greg Clayton06357c92014-07-30 17:38:47 +0000901 commands_stream.Printf ("process attach --pid %" PRIu64 "\n", m_option_data.m_process_pid);
Greg Clayton9c0b64c2014-02-06 18:22:44 +0000902 }
Greg Clayton44d93782014-01-27 23:43:24 +0000903
Greg Clayton06357c92014-07-30 17:38:47 +0000904 WriteInitialCommands(false, commands_stream);
905
Greg Clayton44d93782014-01-27 23:43:24 +0000906 // Now that all option parsing is done, we try and parse the .lldbinit
907 // file in the current working directory
908 sb_interpreter.SourceInitFileInCurrentWorkingDirectory (result);
909 if (GetDebugMode())
910 {
911 result.PutError(m_debugger.GetErrorFileHandle());
912 result.PutOutput(m_debugger.GetOutputFileHandle());
913 }
914
915 bool handle_events = true;
916 bool spawn_thread = false;
Greg Clayton06357c92014-07-30 17:38:47 +0000917
918 // Check if we have any data in the commands stream, and if so, save it to a temp file
Greg Clayton49668762014-08-01 18:32:07 +0000919 // so we can then run the command interpreter using the file contents.
920 const char *commands_data = commands_stream.GetData();
921 const size_t commands_size = commands_stream.GetSize();
922 if (commands_data && commands_size)
Greg Clayton06357c92014-07-30 17:38:47 +0000923 {
Greg Clayton49668762014-08-01 18:32:07 +0000924 enum PIPES { READ, WRITE }; // Constants 0 and 1 for READ and WRITE
Greg Clayton06357c92014-07-30 17:38:47 +0000925
Greg Clayton49668762014-08-01 18:32:07 +0000926 bool success = true;
927 int fds[2] = { -1, -1 };
928 int err = 0;
929#ifdef _WIN32
930 err = _pipe(fds, commands_size, O_BINARY);
931#else
932 err = pipe(fds);
933#endif
934 if (err == 0)
935 {
Saleem Abdulrasool9e5b4982014-09-08 02:47:13 +0000936 ssize_t nrwr = write(fds[WRITE], commands_data, commands_size);
937 if (nrwr < 0)
938 {
939 fprintf(stderr, "error: write(%i, %p, %zd) failed (errno = %i) "
940 "when trying to open LLDB commands pipe\n",
941 fds[WRITE], commands_data, commands_size, errno);
942 success = false;
943 }
944 else if (static_cast<size_t>(nrwr) == commands_size)
Greg Clayton06357c92014-07-30 17:38:47 +0000945 {
Greg Clayton49668762014-08-01 18:32:07 +0000946 // Close the write end of the pipe so when we give the read end to
947 // the debugger/command interpreter it will exit when it consumes all
948 // of the data
949#ifdef _WIN32
950 _close(fds[WRITE]); fds[WRITE] = -1;
951#else
952 close(fds[WRITE]); fds[WRITE] = -1;
953#endif
954 // Now open the read file descriptor in a FILE * that we can give to
955 // the debugger as an input handle
956 FILE *commands_file = fdopen(fds[READ], "r");
957 if (commands_file)
958 {
959 fds[READ] = -1; // The FILE * 'commands_file' now owns the read descriptor
960 // Hand ownership if the FILE * over to the debugger for "commands_file".
961 m_debugger.SetInputFileHandle (commands_file, true);
962 m_debugger.RunCommandInterpreter(handle_events, spawn_thread);
963 }
964 else
965 {
Saleem Abdulrasool9e5b4982014-09-08 02:47:13 +0000966 fprintf(stderr,
967 "error: fdopen(%i, \"r\") failed (errno = %i) when "
968 "trying to open LLDB commands pipe\n",
969 fds[READ], errno);
Greg Clayton49668762014-08-01 18:32:07 +0000970 success = false;
971 }
Greg Clayton06357c92014-07-30 17:38:47 +0000972 }
Saleem Abdulrasool9e5b4982014-09-08 02:47:13 +0000973 else
974 {
975 assert(!"partial writes not handled");
976 success = false;
977 }
Greg Clayton06357c92014-07-30 17:38:47 +0000978 }
Greg Clayton49668762014-08-01 18:32:07 +0000979 else
980 {
981 fprintf(stderr, "error: can't create pipe file descriptors for LLDB commands\n");
982 success = false;
983 }
984
985 // Close any pipes that we still have ownership of
986 if ( fds[WRITE] != -1)
987 {
988#ifdef _WIN32
989 _close(fds[WRITE]); fds[WRITE] = -1;
990#else
991 close(fds[WRITE]); fds[WRITE] = -1;
992#endif
993
994 }
995
996 if ( fds[READ] != -1)
997 {
998#ifdef _WIN32
999 _close(fds[READ]); fds[READ] = -1;
1000#else
1001 close(fds[READ]); fds[READ] = -1;
1002#endif
1003 }
1004
1005 // Something went wrong with command pipe
1006 if (!success)
1007 {
1008 exit(1);
1009 }
1010
Greg Clayton06357c92014-07-30 17:38:47 +00001011 }
1012
1013 // Now set the input file handle to STDIN and run the command
1014 // interpreter again in interactive mode and let the debugger
1015 // take ownership of stdin
1016 m_debugger.SetInputFileHandle (stdin, true);
Greg Clayton44d93782014-01-27 23:43:24 +00001017 m_debugger.RunCommandInterpreter(handle_events, spawn_thread);
1018
1019 reset_stdin_termios();
1020 fclose (stdin);
1021
1022 SBDebugger::Destroy (m_debugger);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001023}
1024
Greg Clayton44d93782014-01-27 23:43:24 +00001025
Jim Inghamc46fe7c2013-02-22 22:56:55 +00001026void
1027Driver::ResizeWindow (unsigned short col)
1028{
1029 GetDebugger().SetTerminalWidth (col);
Jim Inghamc46fe7c2013-02-22 22:56:55 +00001030}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001031
Caroline Ticedd759852010-09-09 17:45:09 +00001032void
1033sigwinch_handler (int signo)
1034{
1035 struct winsize window_size;
1036 if (isatty (STDIN_FILENO)
1037 && ::ioctl (STDIN_FILENO, TIOCGWINSZ, &window_size) == 0)
1038 {
Jim Ingham57190ba2012-04-26 21:39:32 +00001039 if ((window_size.ws_col > 0) && g_driver != NULL)
Caroline Ticedd759852010-09-09 17:45:09 +00001040 {
Jim Inghamc46fe7c2013-02-22 22:56:55 +00001041 g_driver->ResizeWindow (window_size.ws_col);
Caroline Ticedd759852010-09-09 17:45:09 +00001042 }
1043 }
1044}
1045
Caroline Ticeefed6132010-11-19 20:47:54 +00001046void
1047sigint_handler (int signo)
1048{
1049 static bool g_interrupt_sent = false;
1050 if (g_driver)
1051 {
1052 if (!g_interrupt_sent)
1053 {
1054 g_interrupt_sent = true;
1055 g_driver->GetDebugger().DispatchInputInterrupt();
1056 g_interrupt_sent = false;
1057 return;
1058 }
1059 }
1060
1061 exit (signo);
1062}
1063
Jim Inghamc5917d92012-11-30 20:23:19 +00001064void
1065sigtstp_handler (int signo)
1066{
1067 g_driver->GetDebugger().SaveInputTerminalState();
1068 signal (signo, SIG_DFL);
1069 kill (getpid(), signo);
1070 signal (signo, sigtstp_handler);
1071}
1072
1073void
1074sigcont_handler (int signo)
1075{
1076 g_driver->GetDebugger().RestoreInputTerminalState();
1077 signal (signo, SIG_DFL);
1078 kill (getpid(), signo);
1079 signal (signo, sigcont_handler);
1080}
1081
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001082int
Jim Inghama462f5c2011-01-27 20:15:39 +00001083main (int argc, char const *argv[], const char *envp[])
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001084{
Deepak Panickal99fbc072014-03-03 15:39:47 +00001085#ifdef _MSC_VER
1086 // disable buffering on windows
1087 setvbuf(stdout, NULL, _IONBF, 0);
1088 setvbuf(stdin , NULL, _IONBF, 0);
1089#endif
1090
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001091 SBDebugger::Initialize();
1092
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00001093 SBHostOS::ThreadCreated ("<lldb.driver.main-thread>");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001094
Greg Clayton3fcbed62010-10-19 03:25:40 +00001095 signal (SIGPIPE, SIG_IGN);
Caroline Ticedd759852010-09-09 17:45:09 +00001096 signal (SIGWINCH, sigwinch_handler);
Caroline Ticeefed6132010-11-19 20:47:54 +00001097 signal (SIGINT, sigint_handler);
Jim Inghamc5917d92012-11-30 20:23:19 +00001098 signal (SIGTSTP, sigtstp_handler);
1099 signal (SIGCONT, sigcont_handler);
Caroline Ticedd759852010-09-09 17:45:09 +00001100
Greg Clayton66111032010-06-23 01:19:29 +00001101 // Create a scope for driver so that the driver object will destroy itself
1102 // before SBDebugger::Terminate() is called.
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001103 {
Greg Clayton66111032010-06-23 01:19:29 +00001104 Driver driver;
1105
Deepak Panickal429222c2013-10-15 15:46:40 +00001106 bool exiting = false;
1107 SBError error (driver.ParseArgs (argc, argv, stdout, exiting));
Greg Clayton66111032010-06-23 01:19:29 +00001108 if (error.Fail())
1109 {
1110 const char *error_cstr = error.GetCString ();
1111 if (error_cstr)
1112 ::fprintf (stderr, "error: %s\n", error_cstr);
1113 }
Deepak Panickal429222c2013-10-15 15:46:40 +00001114 else if (!exiting)
Greg Clayton66111032010-06-23 01:19:29 +00001115 {
1116 driver.MainLoop ();
1117 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001118 }
1119
1120 SBDebugger::Terminate();
1121 return 0;
1122}