blob: 0d0d03d7152c2b0479f812457456ba485acff50e [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
18#include <string>
19
Deepak Panickal429222c2013-10-15 15:46:40 +000020#include <thread>
Jim Inghame6bc6cb2012-02-08 05:23:15 +000021#include "lldb/API/SBBreakpoint.h"
Eli Friedmana382d472010-06-09 09:50:17 +000022#include "lldb/API/SBCommandInterpreter.h"
23#include "lldb/API/SBCommandReturnObject.h"
24#include "lldb/API/SBCommunication.h"
25#include "lldb/API/SBDebugger.h"
26#include "lldb/API/SBEvent.h"
27#include "lldb/API/SBHostOS.h"
28#include "lldb/API/SBListener.h"
Jim Ingham85e8b812011-02-19 02:53:09 +000029#include "lldb/API/SBStream.h"
Eli Friedmana382d472010-06-09 09:50:17 +000030#include "lldb/API/SBTarget.h"
31#include "lldb/API/SBThread.h"
32#include "lldb/API/SBProcess.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000033
34using namespace lldb;
35
36static void reset_stdin_termios ();
Greg Claytonf571b892012-02-02 19:28:31 +000037static bool g_old_stdin_termios_is_valid = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000038static struct termios g_old_stdin_termios;
39
Caroline Ticedd759852010-09-09 17:45:09 +000040static char *g_debugger_name = (char *) "";
Caroline Ticeefed6132010-11-19 20:47:54 +000041static Driver *g_driver = NULL;
Caroline Ticedd759852010-09-09 17:45:09 +000042
Chris Lattner30fdc8d2010-06-08 16:52:24 +000043// In the Driver::MainLoop, we change the terminal settings. This function is
44// added as an atexit handler to make sure we clean them up.
45static void
46reset_stdin_termios ()
47{
Greg Claytonf571b892012-02-02 19:28:31 +000048 if (g_old_stdin_termios_is_valid)
49 {
50 g_old_stdin_termios_is_valid = false;
51 ::tcsetattr (STDIN_FILENO, TCSANOW, &g_old_stdin_termios);
52 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000053}
54
Greg Claytone0d378b2011-03-24 21:19:54 +000055typedef struct
Chris Lattner30fdc8d2010-06-08 16:52:24 +000056{
Greg Claytone0d378b2011-03-24 21:19:54 +000057 uint32_t usage_mask; // Used to mark options that can be used together. If (1 << n & usage_mask) != 0
58 // then this option belongs to option set n.
59 bool required; // This option is required (in the current usage level)
60 const char * long_option; // Full name for this option.
Greg Clayton3bcdfc02012-12-04 00:32:51 +000061 int short_option; // Single character for this option.
Greg Claytone0d378b2011-03-24 21:19:54 +000062 int option_has_arg; // no_argument, required_argument or optional_argument
Greg Claytonab65b342011-04-13 22:47:15 +000063 uint32_t completion_type; // Cookie the option class can use to do define the argument completion.
Greg Claytone0d378b2011-03-24 21:19:54 +000064 lldb::CommandArgumentType argument_type; // Type of argument this option takes
65 const char * usage_text; // Full text explaining what this options does and what (if any) argument to
66 // pass it.
67} OptionDefinition;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000068
Jim Inghame64f0dc2011-09-13 23:25:31 +000069#define LLDB_3_TO_5 LLDB_OPT_SET_3|LLDB_OPT_SET_4|LLDB_OPT_SET_5
70#define LLDB_4_TO_5 LLDB_OPT_SET_4|LLDB_OPT_SET_5
71
Greg Claytone0d378b2011-03-24 21:19:54 +000072static OptionDefinition g_options[] =
73{
Filipe Cabecinhasd0b87d82012-09-11 18:11:16 +000074 { LLDB_OPT_SET_1, true , "help" , 'h', no_argument , 0, eArgTypeNone,
Jim Ingham12e9a202011-09-15 21:30:02 +000075 "Prints out the usage information for the LLDB debugger." },
Filipe Cabecinhasd0b87d82012-09-11 18:11:16 +000076 { LLDB_OPT_SET_2, true , "version" , 'v', no_argument , 0, eArgTypeNone,
Jim Ingham12e9a202011-09-15 21:30:02 +000077 "Prints out the current version number of the LLDB debugger." },
Filipe Cabecinhasd0b87d82012-09-11 18:11:16 +000078 { LLDB_OPT_SET_3, true , "arch" , 'a', required_argument, 0, eArgTypeArchitecture,
Jim Ingham12e9a202011-09-15 21:30:02 +000079 "Tells the debugger to use the specified architecture when starting and running the program. <architecture> must "
80 "be one of the architectures for which the program was compiled." },
Filipe Cabecinhasd0b87d82012-09-11 18:11:16 +000081 { LLDB_OPT_SET_3, true , "file" , 'f', required_argument, 0, eArgTypeFilename,
Jim Ingham12e9a202011-09-15 21:30:02 +000082 "Tells the debugger to use the file <filename> as the program to be debugged." },
Jason Molenda67c3cf52012-10-24 03:29:40 +000083 { LLDB_OPT_SET_3, false, "core" , 'c', required_argument, 0, eArgTypeFilename,
Johnny Cheneb46f782012-08-15 22:10:42 +000084 "Tells the debugger to use the fullpath to <path> as the core file." },
Jim Inghamf0c63b92014-02-05 21:35:09 +000085 { LLDB_OPT_SET_5, true , "attach-pid" , 'p', required_argument, 0, eArgTypePid,
86 "Tells the debugger to attach to a process with the given pid." },
Filipe Cabecinhasd0b87d82012-09-11 18:11:16 +000087 { LLDB_OPT_SET_4, true , "attach-name" , 'n', required_argument, 0, eArgTypeProcessName,
Jim Ingham12e9a202011-09-15 21:30:02 +000088 "Tells the debugger to attach to a process with the given name." },
Filipe Cabecinhasd0b87d82012-09-11 18:11:16 +000089 { LLDB_OPT_SET_4, true , "wait-for" , 'w', no_argument , 0, eArgTypeNone,
Jim Ingham12e9a202011-09-15 21:30:02 +000090 "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 +000091 { LLDB_3_TO_5, false, "source" , 's', required_argument, 0, eArgTypeFilename,
Jim Ingham47ea51f2013-09-17 01:53:35 +000092 "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 +000093 { LLDB_3_TO_5, false, "one-line" , 'o', required_argument, 0, eArgTypeNone,
Jim Ingham47ea51f2013-09-17 01:53:35 +000094 "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 +000095 { LLDB_3_TO_5, false, "source-before-file" , 'S', required_argument, 0, eArgTypeFilename,
Jim Ingham47ea51f2013-09-17 01:53:35 +000096 "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 +000097 { LLDB_3_TO_5, false, "one-line-before-file" , 'O', required_argument, 0, eArgTypeNone,
Jim Ingham47ea51f2013-09-17 01:53:35 +000098 "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 +000099 { LLDB_3_TO_5, false, "source-quietly" , 'Q', no_argument , 0, eArgTypeNone,
100 "Tells the debugger suppress output from commands provided in the -s, -S, -O and -o commands." },
Filipe Cabecinhasd0b87d82012-09-11 18:11:16 +0000101 { LLDB_3_TO_5, false, "editor" , 'e', no_argument , 0, eArgTypeNone,
Jim Ingham12e9a202011-09-15 21:30:02 +0000102 "Tells the debugger to open source files using the host's \"external editor\" mechanism." },
Filipe Cabecinhasd0b87d82012-09-11 18:11:16 +0000103 { LLDB_3_TO_5, false, "no-lldbinit" , 'x', no_argument , 0, eArgTypeNone,
Jim Ingham12e9a202011-09-15 21:30:02 +0000104 "Do not automatically parse any '.lldbinit' files." },
Jim Inghamed3252f2013-09-14 00:20:24 +0000105 { LLDB_3_TO_5, false, "no-use-colors" , 'X', no_argument , 0, eArgTypeNone,
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000106 "Do not use colors." },
107 { LLDB_OPT_SET_6, true , "python-path" , 'P', no_argument , 0, eArgTypeNone,
Jim Inghame2231ac2012-12-21 22:22:26 +0000108 "Prints out the path to the lldb.py file for this version of lldb." },
Jim Inghamf0c63b92014-02-05 21:35:09 +0000109 { LLDB_3_TO_5, false, "script-language", 'l', required_argument, 0, eArgTypeScriptLang,
110 "Tells the debugger to use the specified scripting language for user-defined scripts, rather than the default. "
111 "Valid scripting languages that can be specified include Python, Perl, Ruby and Tcl. Currently only the Python "
112 "extensions have been implemented." },
113 { LLDB_3_TO_5, false, "debug" , 'd', no_argument , 0, eArgTypeNone,
114 "Tells the debugger to print out extra information for debugging itself." },
Filipe Cabecinhasd0b87d82012-09-11 18:11:16 +0000115 { 0, false, NULL , 0 , 0 , 0, eArgTypeNone, NULL }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000116};
117
Jim Inghame64f0dc2011-09-13 23:25:31 +0000118static const uint32_t last_option_set_with_args = 2;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000119
120Driver::Driver () :
121 SBBroadcaster ("Driver"),
Jim Ingham06942692011-08-13 00:22:20 +0000122 m_debugger (SBDebugger::Create(false)),
Greg Clayton44d93782014-01-27 23:43:24 +0000123 m_option_data ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000124{
Greg Claytonfc3f0272011-05-29 04:06:55 +0000125 // We want to be able to handle CTRL+D in the terminal to have it terminate
126 // certain input
127 m_debugger.SetCloseInputOnEOF (false);
Caroline Ticedd759852010-09-09 17:45:09 +0000128 g_debugger_name = (char *) m_debugger.GetInstanceName();
129 if (g_debugger_name == NULL)
130 g_debugger_name = (char *) "";
Caroline Ticeefed6132010-11-19 20:47:54 +0000131 g_driver = this;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000132}
133
134Driver::~Driver ()
135{
Caroline Ticeefed6132010-11-19 20:47:54 +0000136 g_driver = NULL;
137 g_debugger_name = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000138}
139
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000140
Greg Claytonc982c762010-07-09 20:39:50 +0000141// This function takes INDENT, which tells how many spaces to output at the front
142// of each line; TEXT, which is the text that is to be output. It outputs the
143// text, on multiple lines if necessary, to RESULT, with INDENT spaces at the
144// front of each line. It breaks lines on spaces, tabs or newlines, shortening
145// the line if necessary to not break in the middle of a word. It assumes that
146// each output line should contain a maximum of OUTPUT_MAX_COLUMNS characters.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000147
148void
Greg Claytonc982c762010-07-09 20:39:50 +0000149OutputFormattedUsageText (FILE *out, int indent, const char *text, int output_max_columns)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000150{
151 int len = strlen (text);
152 std::string text_string (text);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000153
154 // Force indentation to be reasonable.
155 if (indent >= output_max_columns)
156 indent = 0;
157
158 // Will it all fit on one line?
159
160 if (len + indent < output_max_columns)
161 // Output as a single line
Greg Claytonc982c762010-07-09 20:39:50 +0000162 fprintf (out, "%*s%s\n", indent, "", text);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000163 else
164 {
165 // We need to break it up into multiple lines.
166 int text_width = output_max_columns - indent - 1;
167 int start = 0;
168 int end = start;
169 int final_end = len;
170 int sub_len;
171
172 while (end < final_end)
173 {
174 // Dont start the 'text' on a space, since we're already outputting the indentation.
175 while ((start < final_end) && (text[start] == ' '))
176 start++;
177
178 end = start + text_width;
179 if (end > final_end)
180 end = final_end;
181 else
182 {
183 // If we're not at the end of the text, make sure we break the line on white space.
184 while (end > start
185 && text[end] != ' ' && text[end] != '\t' && text[end] != '\n')
186 end--;
187 }
188 sub_len = end - start;
189 std::string substring = text_string.substr (start, sub_len);
Greg Claytonc982c762010-07-09 20:39:50 +0000190 fprintf (out, "%*s%s\n", indent, "", substring.c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000191 start = end + 1;
192 }
193 }
194}
195
196void
Greg Claytone0d378b2011-03-24 21:19:54 +0000197ShowUsage (FILE *out, OptionDefinition *option_table, Driver::OptionData data)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000198{
199 uint32_t screen_width = 80;
200 uint32_t indent_level = 0;
201 const char *name = "lldb";
Jim Ingham86511212010-06-15 18:47:14 +0000202
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000203 fprintf (out, "\nUsage:\n\n");
204
205 indent_level += 2;
206
207
208 // First, show each usage level set of options, e.g. <cmd> [options-for-level-0]
209 // <cmd> [options-for-level-1]
210 // etc.
211
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000212 uint32_t num_options;
Jim Ingham86511212010-06-15 18:47:14 +0000213 uint32_t num_option_sets = 0;
214
215 for (num_options = 0; option_table[num_options].long_option != NULL; ++num_options)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000216 {
Jim Ingham86511212010-06-15 18:47:14 +0000217 uint32_t this_usage_mask = option_table[num_options].usage_mask;
218 if (this_usage_mask == LLDB_OPT_SET_ALL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000219 {
Jim Ingham86511212010-06-15 18:47:14 +0000220 if (num_option_sets == 0)
221 num_option_sets = 1;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000222 }
223 else
224 {
Greg Claytonc982c762010-07-09 20:39:50 +0000225 for (uint32_t j = 0; j < LLDB_MAX_NUM_OPTION_SETS; j++)
Jim Ingham86511212010-06-15 18:47:14 +0000226 {
227 if (this_usage_mask & 1 << j)
228 {
229 if (num_option_sets <= j)
230 num_option_sets = j + 1;
231 }
232 }
233 }
234 }
235
236 for (uint32_t opt_set = 0; opt_set < num_option_sets; opt_set++)
237 {
238 uint32_t opt_set_mask;
239
240 opt_set_mask = 1 << opt_set;
241
242 if (opt_set > 0)
243 fprintf (out, "\n");
Greg Claytonc982c762010-07-09 20:39:50 +0000244 fprintf (out, "%*s%s", indent_level, "", name);
Jim Ingham556e6532011-08-16 23:15:02 +0000245 bool is_help_line = false;
Jim Ingham86511212010-06-15 18:47:14 +0000246
247 for (uint32_t i = 0; i < num_options; ++i)
248 {
249 if (option_table[i].usage_mask & opt_set_mask)
250 {
Caroline Ticedeaab222010-10-01 19:59:14 +0000251 CommandArgumentType arg_type = option_table[i].argument_type;
Greg Clayton9d0402b2011-02-20 02:15:07 +0000252 const char *arg_name = SBCommandInterpreter::GetArgumentTypeAsCString (arg_type);
Jim Ingham556e6532011-08-16 23:15:02 +0000253 // This is a bit of a hack, but there's no way to say certain options don't have arguments yet...
254 // so we do it by hand here.
255 if (option_table[i].short_option == 'h')
256 is_help_line = true;
257
Jim Ingham86511212010-06-15 18:47:14 +0000258 if (option_table[i].required)
259 {
260 if (option_table[i].option_has_arg == required_argument)
Greg Clayton9d0402b2011-02-20 02:15:07 +0000261 fprintf (out, " -%c <%s>", option_table[i].short_option, arg_name);
Jim Ingham86511212010-06-15 18:47:14 +0000262 else if (option_table[i].option_has_arg == optional_argument)
Greg Clayton9d0402b2011-02-20 02:15:07 +0000263 fprintf (out, " -%c [<%s>]", option_table[i].short_option, arg_name);
Jim Ingham86511212010-06-15 18:47:14 +0000264 else
265 fprintf (out, " -%c", option_table[i].short_option);
266 }
267 else
268 {
269 if (option_table[i].option_has_arg == required_argument)
Greg Clayton9d0402b2011-02-20 02:15:07 +0000270 fprintf (out, " [-%c <%s>]", option_table[i].short_option, arg_name);
Jim Ingham86511212010-06-15 18:47:14 +0000271 else if (option_table[i].option_has_arg == optional_argument)
Greg Clayton9d0402b2011-02-20 02:15:07 +0000272 fprintf (out, " [-%c [<%s>]]", option_table[i].short_option, arg_name);
Jim Ingham86511212010-06-15 18:47:14 +0000273 else
274 fprintf (out, " [-%c]", option_table[i].short_option);
275 }
276 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000277 }
Jim Inghame64f0dc2011-09-13 23:25:31 +0000278 if (!is_help_line && (opt_set <= last_option_set_with_args))
Jim Ingham556e6532011-08-16 23:15:02 +0000279 fprintf (out, " [[--] <PROGRAM-ARG-1> [<PROGRAM_ARG-2> ...]]");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000280 }
281
282 fprintf (out, "\n\n");
283
284 // Now print out all the detailed information about the various options: long form, short form and help text:
285 // -- long_name <argument>
286 // - short <argument>
287 // help text
288
289 // This variable is used to keep track of which options' info we've printed out, because some options can be in
290 // more than one usage level, but we only want to print the long form of its information once.
291
292 Driver::OptionData::OptionSet options_seen;
293 Driver::OptionData::OptionSet::iterator pos;
294
295 indent_level += 5;
296
Jim Ingham86511212010-06-15 18:47:14 +0000297 for (uint32_t i = 0; i < num_options; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000298 {
299 // Only print this option if we haven't already seen it.
300 pos = options_seen.find (option_table[i].short_option);
301 if (pos == options_seen.end())
302 {
Caroline Ticedeaab222010-10-01 19:59:14 +0000303 CommandArgumentType arg_type = option_table[i].argument_type;
Greg Clayton9d0402b2011-02-20 02:15:07 +0000304 const char *arg_name = SBCommandInterpreter::GetArgumentTypeAsCString (arg_type);
Caroline Ticedeaab222010-10-01 19:59:14 +0000305
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000306 options_seen.insert (option_table[i].short_option);
Greg Claytonc982c762010-07-09 20:39:50 +0000307 fprintf (out, "%*s-%c ", indent_level, "", option_table[i].short_option);
Caroline Ticedeaab222010-10-01 19:59:14 +0000308 if (arg_type != eArgTypeNone)
Greg Clayton9d0402b2011-02-20 02:15:07 +0000309 fprintf (out, "<%s>", arg_name);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000310 fprintf (out, "\n");
Greg Claytonc982c762010-07-09 20:39:50 +0000311 fprintf (out, "%*s--%s ", indent_level, "", option_table[i].long_option);
Caroline Ticedeaab222010-10-01 19:59:14 +0000312 if (arg_type != eArgTypeNone)
Greg Clayton9d0402b2011-02-20 02:15:07 +0000313 fprintf (out, "<%s>", arg_name);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000314 fprintf (out, "\n");
315 indent_level += 5;
Greg Claytonc982c762010-07-09 20:39:50 +0000316 OutputFormattedUsageText (out, indent_level, option_table[i].usage_text, screen_width);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000317 indent_level -= 5;
318 fprintf (out, "\n");
319 }
320 }
321
322 indent_level -= 5;
Jim Inghamf0c63b92014-02-05 21:35:09 +0000323
324 fprintf (out, "\n%*sNotes:\n",
325 indent_level, "");
326 indent_level += 5;
327
Jim Ingham47ea51f2013-09-17 01:53:35 +0000328 fprintf (out, "\n%*sMultiple \"-s\" and \"-o\" options can be provided. They will be processed from left to right in order, "
329 "\n%*swith the source files and commands interleaved. The same is true of the \"-S\" and \"-O\" options."
330 "\n%*sThe before file and after file sets can intermixed freely, the command parser will sort them out."
331 "\n%*sThe order of the file specifiers (\"-c\", \"-f\", etc.) is not significant in this regard.\n\n",
332 indent_level, "",
333 indent_level, "",
334 indent_level, "",
335 indent_level, "");
336
Jim Inghamf0c63b92014-02-05 21:35:09 +0000337 fprintf (out, "\n%*sIf you don't provide -f then the first argument will be the file to be debugged"
338 "\n%*swhich means that '%s -- <filename> [<ARG1> [<ARG2>]]' also works."
339 "\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 +0000340 indent_level, "",
341 indent_level, "",
342 name,
343 indent_level, "");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000344}
345
346void
Greg Claytone0d378b2011-03-24 21:19:54 +0000347BuildGetOptTable (OptionDefinition *expanded_option_table, std::vector<struct option> &getopt_table,
Caroline Tice4ab31c92010-10-12 21:57:09 +0000348 uint32_t num_options)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000349{
350 if (num_options == 0)
351 return;
352
353 uint32_t i;
354 uint32_t j;
355 std::bitset<256> option_seen;
356
Caroline Tice4ab31c92010-10-12 21:57:09 +0000357 getopt_table.resize (num_options + 1);
358
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000359 for (i = 0, j = 0; i < num_options; ++i)
Greg Claytonc982c762010-07-09 20:39:50 +0000360 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000361 char short_opt = expanded_option_table[i].short_option;
Greg Claytonc982c762010-07-09 20:39:50 +0000362
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000363 if (option_seen.test(short_opt) == false)
Greg Claytonc982c762010-07-09 20:39:50 +0000364 {
Caroline Tice4ab31c92010-10-12 21:57:09 +0000365 getopt_table[j].name = expanded_option_table[i].long_option;
366 getopt_table[j].has_arg = expanded_option_table[i].option_has_arg;
367 getopt_table[j].flag = NULL;
368 getopt_table[j].val = expanded_option_table[i].short_option;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000369 option_seen.set(short_opt);
370 ++j;
Greg Claytonc982c762010-07-09 20:39:50 +0000371 }
372 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000373
Caroline Tice4ab31c92010-10-12 21:57:09 +0000374 getopt_table[j].name = NULL;
375 getopt_table[j].has_arg = 0;
376 getopt_table[j].flag = NULL;
377 getopt_table[j].val = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000378
379}
380
Greg Clayton66111032010-06-23 01:19:29 +0000381Driver::OptionData::OptionData () :
Greg Clayton8d846da2010-12-08 22:23:24 +0000382 m_args(),
Greg Clayton66111032010-06-23 01:19:29 +0000383 m_script_lang (lldb::eScriptLanguageDefault),
Johnny Cheneb46f782012-08-15 22:10:42 +0000384 m_core_file (),
Greg Claytonc982c762010-07-09 20:39:50 +0000385 m_crash_log (),
Jim Inghamed3252f2013-09-14 00:20:24 +0000386 m_initial_commands (),
387 m_after_file_commands (),
Greg Clayton66111032010-06-23 01:19:29 +0000388 m_debug_mode (false),
Jim Inghamed3252f2013-09-14 00:20:24 +0000389 m_source_quietly(false),
Greg Claytonc982c762010-07-09 20:39:50 +0000390 m_print_version (false),
Jim Inghame2231ac2012-12-21 22:22:26 +0000391 m_print_python_path (false),
Greg Clayton66111032010-06-23 01:19:29 +0000392 m_print_help (false),
Jim Inghame64f0dc2011-09-13 23:25:31 +0000393 m_wait_for(false),
394 m_process_name(),
395 m_process_pid(LLDB_INVALID_PROCESS_ID),
Daniel Dunbara08823f2011-10-31 22:50:49 +0000396 m_use_external_editor(false),
Stephen Wilson71c21d12011-04-11 19:41:40 +0000397 m_seen_options()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000398{
Greg Clayton66111032010-06-23 01:19:29 +0000399}
400
401Driver::OptionData::~OptionData ()
402{
403}
404
405void
406Driver::OptionData::Clear ()
407{
Greg Clayton8d846da2010-12-08 22:23:24 +0000408 m_args.clear ();
Greg Clayton66111032010-06-23 01:19:29 +0000409 m_script_lang = lldb::eScriptLanguageDefault;
Jim Inghamed3252f2013-09-14 00:20:24 +0000410 m_initial_commands.clear ();
411 m_after_file_commands.clear ();
Greg Clayton66111032010-06-23 01:19:29 +0000412 m_debug_mode = false;
Jim Inghamed3252f2013-09-14 00:20:24 +0000413 m_source_quietly = false;
Greg Clayton66111032010-06-23 01:19:29 +0000414 m_print_help = false;
415 m_print_version = false;
Jim Inghame2231ac2012-12-21 22:22:26 +0000416 m_print_python_path = false;
Jim Inghame40e4212010-08-30 19:44:40 +0000417 m_use_external_editor = false;
Jim Inghame64f0dc2011-09-13 23:25:31 +0000418 m_wait_for = false;
419 m_process_name.erase();
420 m_process_pid = LLDB_INVALID_PROCESS_ID;
Greg Clayton66111032010-06-23 01:19:29 +0000421}
422
423void
Jim Inghamed3252f2013-09-14 00:20:24 +0000424Driver::OptionData::AddInitialCommand (const char *command, bool before_file, bool is_file, SBError &error)
425{
426 std::vector<std::pair<bool, std::string> > *command_set;
427 if (before_file)
428 command_set = &(m_initial_commands);
429 else
430 command_set = &(m_after_file_commands);
431
432 if (is_file)
433 {
434 SBFileSpec file(command);
435 if (file.Exists())
436 command_set->push_back (std::pair<bool, std::string> (true, optarg));
437 else if (file.ResolveExecutableLocation())
438 {
439 char final_path[PATH_MAX];
440 file.GetPath (final_path, sizeof(final_path));
441 std::string path_str (final_path);
442 command_set->push_back (std::pair<bool, std::string> (true, path_str));
443 }
444 else
445 error.SetErrorStringWithFormat("file specified in --source (-s) option doesn't exist: '%s'", optarg);
446 }
447 else
448 command_set->push_back (std::pair<bool, std::string> (false, optarg));
449}
450
451void
Greg Clayton66111032010-06-23 01:19:29 +0000452Driver::ResetOptionValues ()
453{
454 m_option_data.Clear ();
455}
456
457const char *
458Driver::GetFilename() const
459{
Greg Clayton8d846da2010-12-08 22:23:24 +0000460 if (m_option_data.m_args.empty())
Greg Clayton66111032010-06-23 01:19:29 +0000461 return NULL;
Greg Clayton8d846da2010-12-08 22:23:24 +0000462 return m_option_data.m_args.front().c_str();
Greg Clayton66111032010-06-23 01:19:29 +0000463}
464
465const char *
466Driver::GetCrashLogFilename() const
467{
468 if (m_option_data.m_crash_log.empty())
469 return NULL;
470 return m_option_data.m_crash_log.c_str();
471}
472
473lldb::ScriptLanguage
474Driver::GetScriptLanguage() const
475{
476 return m_option_data.m_script_lang;
477}
478
Jim Inghamed3252f2013-09-14 00:20:24 +0000479void
Greg Clayton06357c92014-07-30 17:38:47 +0000480Driver::WriteInitialCommands (bool before_file, SBStream &strm)
Greg Clayton66111032010-06-23 01:19:29 +0000481{
Greg Clayton06357c92014-07-30 17:38:47 +0000482 std::vector<std::pair<bool, std::string> > &command_set = before_file ? m_option_data.m_initial_commands :
483 m_option_data.m_after_file_commands;
Jim Inghamed3252f2013-09-14 00:20:24 +0000484
Greg Clayton06357c92014-07-30 17:38:47 +0000485 for (const auto &command_pair : command_set)
Jim Inghamed3252f2013-09-14 00:20:24 +0000486 {
Greg Clayton06357c92014-07-30 17:38:47 +0000487 const char *command = command_pair.second.c_str();
488 if (command_pair.first)
489 strm.Printf("command source -s %i '%s'\n", m_option_data.m_source_quietly, command);
490 else
491 strm.Printf("%s\n", command);
Jim Inghamed3252f2013-09-14 00:20:24 +0000492 }
Greg Clayton66111032010-06-23 01:19:29 +0000493}
494
495bool
496Driver::GetDebugMode() const
497{
498 return m_option_data.m_debug_mode;
499}
500
501
502// Check the arguments that were passed to this program to make sure they are valid and to get their
503// argument values (if any). Return a boolean value indicating whether or not to start up the full
504// debugger (i.e. the Command Interpreter) or not. Return FALSE if the arguments were invalid OR
505// if the user only wanted help or version information.
506
507SBError
Deepak Panickal429222c2013-10-15 15:46:40 +0000508Driver::ParseArgs (int argc, const char *argv[], FILE *out_fh, bool &exiting)
Greg Clayton66111032010-06-23 01:19:29 +0000509{
510 ResetOptionValues ();
511
512 SBCommandReturnObject result;
513
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000514 SBError error;
515 std::string option_string;
516 struct option *long_options = NULL;
Caroline Tice4ab31c92010-10-12 21:57:09 +0000517 std::vector<struct option> long_options_vector;
Greg Claytonc982c762010-07-09 20:39:50 +0000518 uint32_t num_options;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000519
Greg Claytonc982c762010-07-09 20:39:50 +0000520 for (num_options = 0; g_options[num_options].long_option != NULL; ++num_options)
521 /* Do Nothing. */;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000522
523 if (num_options == 0)
524 {
525 if (argc > 1)
526 error.SetErrorStringWithFormat ("invalid number of options");
527 return error;
528 }
529
Caroline Tice4ab31c92010-10-12 21:57:09 +0000530 BuildGetOptTable (g_options, long_options_vector, num_options);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000531
Caroline Tice4ab31c92010-10-12 21:57:09 +0000532 if (long_options_vector.empty())
533 long_options = NULL;
534 else
535 long_options = &long_options_vector.front();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000536
537 if (long_options == NULL)
538 {
539 error.SetErrorStringWithFormat ("invalid long options");
540 return error;
541 }
542
Greg Claytonb7ad58a2013-04-04 20:35:24 +0000543 // Build the option_string argument for call to getopt_long_only.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000544
545 for (int i = 0; long_options[i].name != NULL; ++i)
546 {
547 if (long_options[i].flag == NULL)
548 {
549 option_string.push_back ((char) long_options[i].val);
550 switch (long_options[i].has_arg)
551 {
552 default:
553 case no_argument:
554 break;
555 case required_argument:
556 option_string.push_back (':');
557 break;
558 case optional_argument:
559 option_string.append ("::");
560 break;
561 }
562 }
563 }
564
Jim Ingham06942692011-08-13 00:22:20 +0000565 // This is kind of a pain, but since we make the debugger in the Driver's constructor, we can't
566 // know at that point whether we should read in init files yet. So we don't read them in in the
567 // Driver constructor, then set the flags back to "read them in" here, and then if we see the
568 // "-n" flag, we'll turn it off again. Finally we have to read them in by hand later in the
569 // main loop.
570
571 m_debugger.SkipLLDBInitFiles (false);
572 m_debugger.SkipAppInitFiles (false);
573
Greg Claytonb7ad58a2013-04-04 20:35:24 +0000574 // Prepare for & make calls to getopt_long_only.
Eli Friedmanadb35022010-06-13 19:18:49 +0000575#if __GLIBC__
576 optind = 0;
577#else
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000578 optreset = 1;
579 optind = 1;
Eli Friedmanadb35022010-06-13 19:18:49 +0000580#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000581 int val;
582 while (1)
583 {
584 int long_options_index = -1;
Greg Claytonb7ad58a2013-04-04 20:35:24 +0000585 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 +0000586
587 if (val == -1)
588 break;
589 else if (val == '?')
590 {
Greg Clayton66111032010-06-23 01:19:29 +0000591 m_option_data.m_print_help = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000592 error.SetErrorStringWithFormat ("unknown or ambiguous option");
593 break;
594 }
595 else if (val == 0)
596 continue;
597 else
598 {
Greg Clayton66111032010-06-23 01:19:29 +0000599 m_option_data.m_seen_options.insert ((char) val);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000600 if (long_options_index == -1)
601 {
602 for (int i = 0;
603 long_options[i].name || long_options[i].has_arg || long_options[i].flag || long_options[i].val;
604 ++i)
605 {
606 if (long_options[i].val == val)
607 {
608 long_options_index = i;
609 break;
610 }
611 }
612 }
613
614 if (long_options_index >= 0)
615 {
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000616 const int short_option = g_options[long_options_index].short_option;
Greg Clayton66111032010-06-23 01:19:29 +0000617
618 switch (short_option)
619 {
620 case 'h':
621 m_option_data.m_print_help = true;
622 break;
623
624 case 'v':
625 m_option_data.m_print_version = true;
626 break;
627
Jim Inghame2231ac2012-12-21 22:22:26 +0000628 case 'P':
629 m_option_data.m_print_python_path = true;
630 break;
631
Greg Clayton66111032010-06-23 01:19:29 +0000632 case 'c':
Johnny Cheneb46f782012-08-15 22:10:42 +0000633 {
634 SBFileSpec file(optarg);
635 if (file.Exists())
636 {
637 m_option_data.m_core_file = optarg;
638 }
639 else
640 error.SetErrorStringWithFormat("file specified in --core (-c) option doesn't exist: '%s'", optarg);
641 }
Greg Clayton66111032010-06-23 01:19:29 +0000642 break;
Johnny Cheneb46f782012-08-15 22:10:42 +0000643
Jim Inghame40e4212010-08-30 19:44:40 +0000644 case 'e':
645 m_option_data.m_use_external_editor = true;
646 break;
Greg Clayton6eee5aa2010-10-11 01:05:37 +0000647
Jim Inghame64f0dc2011-09-13 23:25:31 +0000648 case 'x':
Greg Clayton6eee5aa2010-10-11 01:05:37 +0000649 m_debugger.SkipLLDBInitFiles (true);
Jim Ingham06942692011-08-13 00:22:20 +0000650 m_debugger.SkipAppInitFiles (true);
Greg Clayton6eee5aa2010-10-11 01:05:37 +0000651 break;
652
Jim Inghamed3252f2013-09-14 00:20:24 +0000653 case 'X':
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000654 m_debugger.SetUseColor (false);
655 break;
656
Greg Clayton66111032010-06-23 01:19:29 +0000657 case 'f':
658 {
659 SBFileSpec file(optarg);
660 if (file.Exists())
Greg Clayton8d846da2010-12-08 22:23:24 +0000661 {
662 m_option_data.m_args.push_back (optarg);
663 }
Caroline Tice428a9a52010-09-10 04:48:55 +0000664 else if (file.ResolveExecutableLocation())
665 {
666 char path[PATH_MAX];
Johnny Chen25f3a3c2011-08-10 22:06:24 +0000667 file.GetPath (path, sizeof(path));
Greg Clayton8d846da2010-12-08 22:23:24 +0000668 m_option_data.m_args.push_back (path);
Caroline Tice428a9a52010-09-10 04:48:55 +0000669 }
Greg Clayton66111032010-06-23 01:19:29 +0000670 else
671 error.SetErrorStringWithFormat("file specified in --file (-f) option doesn't exist: '%s'", optarg);
672 }
673 break;
674
675 case 'a':
676 if (!m_debugger.SetDefaultArchitecture (optarg))
677 error.SetErrorStringWithFormat("invalid architecture in the -a or --arch option: '%s'", optarg);
678 break;
679
680 case 'l':
681 m_option_data.m_script_lang = m_debugger.GetScriptingLanguage (optarg);
682 break;
683
684 case 'd':
685 m_option_data.m_debug_mode = true;
686 break;
687
Jim Inghamf0c63b92014-02-05 21:35:09 +0000688 case 'Q':
Jim Inghamed3252f2013-09-14 00:20:24 +0000689 m_option_data.m_source_quietly = true;
690 break;
691
Jim Inghame64f0dc2011-09-13 23:25:31 +0000692 case 'n':
693 m_option_data.m_process_name = optarg;
694 break;
695
696 case 'w':
697 m_option_data.m_wait_for = true;
698 break;
699
700 case 'p':
701 {
702 char *remainder;
703 m_option_data.m_process_pid = strtol (optarg, &remainder, 0);
704 if (remainder == optarg || *remainder != '\0')
705 error.SetErrorStringWithFormat ("Could not convert process PID: \"%s\" into a pid.",
706 optarg);
707 }
708 break;
Greg Clayton66111032010-06-23 01:19:29 +0000709 case 's':
Jim Inghamed3252f2013-09-14 00:20:24 +0000710 m_option_data.AddInitialCommand(optarg, false, true, error);
Greg Clayton66111032010-06-23 01:19:29 +0000711 break;
Jim Inghamed3252f2013-09-14 00:20:24 +0000712 case 'o':
713 m_option_data.AddInitialCommand(optarg, false, false, error);
714 break;
715 case 'S':
716 m_option_data.AddInitialCommand(optarg, true, true, error);
717 break;
718 case 'O':
719 m_option_data.AddInitialCommand(optarg, true, false, error);
720 break;
Greg Clayton66111032010-06-23 01:19:29 +0000721 default:
722 m_option_data.m_print_help = true;
723 error.SetErrorStringWithFormat ("unrecognized option %c", short_option);
724 break;
725 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000726 }
727 else
728 {
729 error.SetErrorStringWithFormat ("invalid option with value %i", val);
730 }
731 if (error.Fail())
Caroline Tice4ab31c92010-10-12 21:57:09 +0000732 {
Greg Clayton66111032010-06-23 01:19:29 +0000733 return error;
Caroline Tice4ab31c92010-10-12 21:57:09 +0000734 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000735 }
736 }
Jim Ingham86511212010-06-15 18:47:14 +0000737
Greg Clayton66111032010-06-23 01:19:29 +0000738 if (error.Fail() || m_option_data.m_print_help)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000739 {
740 ShowUsage (out_fh, g_options, m_option_data);
Deepak Panickal429222c2013-10-15 15:46:40 +0000741 exiting = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000742 }
743 else if (m_option_data.m_print_version)
744 {
Greg Clayton66111032010-06-23 01:19:29 +0000745 ::fprintf (out_fh, "%s\n", m_debugger.GetVersionString());
Deepak Panickal429222c2013-10-15 15:46:40 +0000746 exiting = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000747 }
Jim Inghame2231ac2012-12-21 22:22:26 +0000748 else if (m_option_data.m_print_python_path)
749 {
750 SBFileSpec python_file_spec = SBHostOS::GetLLDBPythonPath();
751 if (python_file_spec.IsValid())
752 {
753 char python_path[PATH_MAX];
754 size_t num_chars = python_file_spec.GetPath(python_path, PATH_MAX);
755 if (num_chars < PATH_MAX)
756 {
757 ::fprintf (out_fh, "%s\n", python_path);
758 }
759 else
760 ::fprintf (out_fh, "<PATH TOO LONG>\n");
761 }
762 else
763 ::fprintf (out_fh, "<COULD NOT FIND PATH>\n");
Deepak Panickal429222c2013-10-15 15:46:40 +0000764 exiting = true;
Jim Inghame2231ac2012-12-21 22:22:26 +0000765 }
Jim Inghame64f0dc2011-09-13 23:25:31 +0000766 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 +0000767 {
Greg Clayton8d846da2010-12-08 22:23:24 +0000768 // Any arguments that are left over after option parsing are for
769 // the program. If a file was specified with -f then the filename
770 // is already in the m_option_data.m_args array, and any remaining args
771 // are arguments for the inferior program. If no file was specified with
772 // -f, then what is left is the program name followed by any arguments.
773
Greg Claytonb7ad58a2013-04-04 20:35:24 +0000774 // Skip any options we consumed with getopt_long_only
Greg Clayton8d846da2010-12-08 22:23:24 +0000775 argc -= optind;
776 argv += optind;
777
778 if (argc > 0)
779 {
780 for (int arg_idx=0; arg_idx<argc; ++arg_idx)
781 {
782 const char *arg = argv[arg_idx];
783 if (arg)
784 m_option_data.m_args.push_back (arg);
785 }
786 }
787
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000788 }
Jim Inghame64f0dc2011-09-13 23:25:31 +0000789 else
790 {
Greg Claytonb7ad58a2013-04-04 20:35:24 +0000791 // Skip any options we consumed with getopt_long_only
Jim Inghame64f0dc2011-09-13 23:25:31 +0000792 argc -= optind;
Greg Clayton23f59502012-07-17 03:23:13 +0000793 //argv += optind; // Commented out to keep static analyzer happy
Jim Inghame64f0dc2011-09-13 23:25:31 +0000794
795 if (argc > 0)
796 ::fprintf (out_fh, "Warning: program arguments are ignored when attaching.\n");
797 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000798
Greg Clayton66111032010-06-23 01:19:29 +0000799 return error;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000800}
801
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000802void
803Driver::MainLoop ()
804{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000805 if (::tcgetattr(STDIN_FILENO, &g_old_stdin_termios) == 0)
Greg Claytonf571b892012-02-02 19:28:31 +0000806 {
807 g_old_stdin_termios_is_valid = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000808 atexit (reset_stdin_termios);
Greg Claytonf571b892012-02-02 19:28:31 +0000809 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000810
811 ::setbuf (stdin, NULL);
812 ::setbuf (stdout, NULL);
813
Greg Clayton66111032010-06-23 01:19:29 +0000814 m_debugger.SetErrorFileHandle (stderr, false);
815 m_debugger.SetOutputFileHandle (stdout, false);
Greg Clayton06357c92014-07-30 17:38:47 +0000816 m_debugger.SetInputFileHandle (stdin, false); // Don't take ownership of STDIN yet...
817
Jim Inghame40e4212010-08-30 19:44:40 +0000818 m_debugger.SetUseExternalEditor(m_option_data.m_use_external_editor);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000819
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000820 struct winsize window_size;
821 if (isatty (STDIN_FILENO)
822 && ::ioctl (STDIN_FILENO, TIOCGWINSZ, &window_size) == 0)
823 {
Caroline Tice3df9a8d2010-09-04 00:03:46 +0000824 if (window_size.ws_col > 0)
Greg Claytona7015092010-09-18 01:14:36 +0000825 m_debugger.SetTerminalWidth (window_size.ws_col);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000826 }
827
Greg Clayton44d93782014-01-27 23:43:24 +0000828 SBCommandInterpreter sb_interpreter = m_debugger.GetCommandInterpreter();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000829
Greg Clayton44d93782014-01-27 23:43:24 +0000830 // Before we handle any options from the command line, we parse the
831 // .lldbinit file in the user's home directory.
832 SBCommandReturnObject result;
833 sb_interpreter.SourceInitFileInHomeDirectory(result);
834 if (GetDebugMode())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000835 {
Greg Clayton44d93782014-01-27 23:43:24 +0000836 result.PutError (m_debugger.GetErrorFileHandle());
837 result.PutOutput (m_debugger.GetOutputFileHandle());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000838 }
Greg Clayton44d93782014-01-27 23:43:24 +0000839
840 // Now we handle options we got from the command line
Ed Mastef8314532014-07-30 19:26:11 +0000841 SBStream commands_stream;
Greg Clayton06357c92014-07-30 17:38:47 +0000842
Greg Clayton44d93782014-01-27 23:43:24 +0000843 // First source in the commands specified to be run before the file arguments are processed.
Greg Clayton06357c92014-07-30 17:38:47 +0000844 WriteInitialCommands(true, commands_stream);
845
Greg Clayton44d93782014-01-27 23:43:24 +0000846 const size_t num_args = m_option_data.m_args.size();
847 if (num_args > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000848 {
Greg Clayton06357c92014-07-30 17:38:47 +0000849 commands_stream.Printf("target create \"%s\"", m_option_data.m_args[0].c_str());
850 if (!m_option_data.m_core_file.empty())
851 {
852 commands_stream.Printf(" --core \"%s\"", m_option_data.m_core_file.c_str());
853 }
854 commands_stream.Printf("\n");
Greg Clayton44d93782014-01-27 23:43:24 +0000855
856 if (num_args > 1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000857 {
Greg Clayton06357c92014-07-30 17:38:47 +0000858 commands_stream.Printf ("settings set -- target.run-args ");
Greg Clayton44d93782014-01-27 23:43:24 +0000859 for (size_t arg_idx = 1; arg_idx < num_args; ++arg_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000860 {
Greg Clayton06357c92014-07-30 17:38:47 +0000861 const char *arg_cstr = m_option_data.m_args[arg_idx].c_str();
862 if (strchr(arg_cstr, '"') == NULL)
863 commands_stream.Printf(" \"%s\"", arg_cstr);
864 else
865 commands_stream.Printf(" '%s'", arg_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000866 }
Greg Clayton06357c92014-07-30 17:38:47 +0000867 commands_stream.Printf("\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000868 }
869 }
Greg Clayton06357c92014-07-30 17:38:47 +0000870 else if (!m_option_data.m_core_file.empty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000871 {
Greg Clayton06357c92014-07-30 17:38:47 +0000872 commands_stream.Printf("target create --core \"%s\"\n", m_option_data.m_core_file.c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000873 }
Greg Clayton9c0b64c2014-02-06 18:22:44 +0000874 else if (!m_option_data.m_process_name.empty())
875 {
Greg Clayton06357c92014-07-30 17:38:47 +0000876 commands_stream.Printf ("process attach --name \"%s\"", m_option_data.m_process_name.c_str());
877
878 if (m_option_data.m_wait_for)
879 commands_stream.Printf(" --waitfor");
880
881 commands_stream.Printf("\n");
882
Greg Clayton9c0b64c2014-02-06 18:22:44 +0000883 }
884 else if (LLDB_INVALID_PROCESS_ID != m_option_data.m_process_pid)
885 {
Greg Clayton06357c92014-07-30 17:38:47 +0000886 commands_stream.Printf ("process attach --pid %" PRIu64 "\n", m_option_data.m_process_pid);
Greg Clayton9c0b64c2014-02-06 18:22:44 +0000887 }
Greg Clayton44d93782014-01-27 23:43:24 +0000888
Greg Clayton06357c92014-07-30 17:38:47 +0000889 WriteInitialCommands(false, commands_stream);
890
Greg Clayton44d93782014-01-27 23:43:24 +0000891 // Now that all option parsing is done, we try and parse the .lldbinit
892 // file in the current working directory
893 sb_interpreter.SourceInitFileInCurrentWorkingDirectory (result);
894 if (GetDebugMode())
895 {
896 result.PutError(m_debugger.GetErrorFileHandle());
897 result.PutOutput(m_debugger.GetOutputFileHandle());
898 }
899
900 bool handle_events = true;
901 bool spawn_thread = false;
Greg Clayton06357c92014-07-30 17:38:47 +0000902
903 // Check if we have any data in the commands stream, and if so, save it to a temp file
904 // so we can then run the command interpreter using the file contents.
905 if (commands_stream.GetData() && commands_stream.GetSize())
906 {
907 char lldb_cmds_file[PATH_MAX];
908 SBFileSpec lldb_temp_dir_spec = SBHostOS::GetLLDBPath (lldb::ePathTypeLLDBTempSystemDir);
909 lldb_temp_dir_spec.SetFilename("lldb-cmds.XXXXXX");
910
911 if (lldb_temp_dir_spec.GetPath(lldb_cmds_file, sizeof(lldb_cmds_file)))
912 {
913 int fd = mkstemp(lldb_cmds_file);
914 if (fd == -1)
915 {
916 fprintf(stderr, "error: can't create temporary file for LLDB commands\n");
917 exit (1);
918 }
919 FILE *file = fdopen(fd, "r+");
920 if (file == NULL)
921 {
922 fprintf(stderr, "error: fdopen(%i, \"r+\") failed (errno = %i)\n", fd, errno);
923 exit (2);
924 }
925 // Redirect the stream to a file and it will save its temp buffer out to the file on disk
926 commands_stream.RedirectToFileHandle(file, true);
927
928 // Close the stream which will close the file and flush it to disk
929 commands_stream.Clear();
930
931 // Now re-open the file so we can use it as an input file handle for the real
932 // command interpreter
933 FILE *commands_file = ::fopen(lldb_cmds_file, "r");
934 if (commands_file)
935 {
936 // Hand ownership over to the debugger for "commands_file".
937 m_debugger.SetInputFileHandle (commands_file, true);
938 m_debugger.RunCommandInterpreter(handle_events, spawn_thread);
939 }
940 else
941 {
942 fprintf(stderr, "error: fopen(\"%s\", \"r\") failed (errno = %i) when trying to open LLDB commands file\n", lldb_cmds_file, errno);
943 exit (3);
944 }
945 }
946 }
947
948 // Now set the input file handle to STDIN and run the command
949 // interpreter again in interactive mode and let the debugger
950 // take ownership of stdin
951 m_debugger.SetInputFileHandle (stdin, true);
Greg Clayton44d93782014-01-27 23:43:24 +0000952 m_debugger.RunCommandInterpreter(handle_events, spawn_thread);
953
954 reset_stdin_termios();
955 fclose (stdin);
956
957 SBDebugger::Destroy (m_debugger);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000958}
959
Greg Clayton44d93782014-01-27 23:43:24 +0000960
Jim Inghamc46fe7c2013-02-22 22:56:55 +0000961void
962Driver::ResizeWindow (unsigned short col)
963{
964 GetDebugger().SetTerminalWidth (col);
Jim Inghamc46fe7c2013-02-22 22:56:55 +0000965}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000966
Caroline Ticedd759852010-09-09 17:45:09 +0000967void
968sigwinch_handler (int signo)
969{
970 struct winsize window_size;
971 if (isatty (STDIN_FILENO)
972 && ::ioctl (STDIN_FILENO, TIOCGWINSZ, &window_size) == 0)
973 {
Jim Ingham57190ba2012-04-26 21:39:32 +0000974 if ((window_size.ws_col > 0) && g_driver != NULL)
Caroline Ticedd759852010-09-09 17:45:09 +0000975 {
Jim Inghamc46fe7c2013-02-22 22:56:55 +0000976 g_driver->ResizeWindow (window_size.ws_col);
Caroline Ticedd759852010-09-09 17:45:09 +0000977 }
978 }
979}
980
Caroline Ticeefed6132010-11-19 20:47:54 +0000981void
982sigint_handler (int signo)
983{
984 static bool g_interrupt_sent = false;
985 if (g_driver)
986 {
987 if (!g_interrupt_sent)
988 {
989 g_interrupt_sent = true;
990 g_driver->GetDebugger().DispatchInputInterrupt();
991 g_interrupt_sent = false;
992 return;
993 }
994 }
995
996 exit (signo);
997}
998
Jim Inghamc5917d92012-11-30 20:23:19 +0000999void
1000sigtstp_handler (int signo)
1001{
1002 g_driver->GetDebugger().SaveInputTerminalState();
1003 signal (signo, SIG_DFL);
1004 kill (getpid(), signo);
1005 signal (signo, sigtstp_handler);
1006}
1007
1008void
1009sigcont_handler (int signo)
1010{
1011 g_driver->GetDebugger().RestoreInputTerminalState();
1012 signal (signo, SIG_DFL);
1013 kill (getpid(), signo);
1014 signal (signo, sigcont_handler);
1015}
1016
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001017int
Jim Inghama462f5c2011-01-27 20:15:39 +00001018main (int argc, char const *argv[], const char *envp[])
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001019{
Deepak Panickal99fbc072014-03-03 15:39:47 +00001020#ifdef _MSC_VER
1021 // disable buffering on windows
1022 setvbuf(stdout, NULL, _IONBF, 0);
1023 setvbuf(stdin , NULL, _IONBF, 0);
1024#endif
1025
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001026 SBDebugger::Initialize();
1027
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00001028 SBHostOS::ThreadCreated ("<lldb.driver.main-thread>");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001029
Greg Clayton3fcbed62010-10-19 03:25:40 +00001030 signal (SIGPIPE, SIG_IGN);
Caroline Ticedd759852010-09-09 17:45:09 +00001031 signal (SIGWINCH, sigwinch_handler);
Caroline Ticeefed6132010-11-19 20:47:54 +00001032 signal (SIGINT, sigint_handler);
Jim Inghamc5917d92012-11-30 20:23:19 +00001033 signal (SIGTSTP, sigtstp_handler);
1034 signal (SIGCONT, sigcont_handler);
Caroline Ticedd759852010-09-09 17:45:09 +00001035
Greg Clayton66111032010-06-23 01:19:29 +00001036 // Create a scope for driver so that the driver object will destroy itself
1037 // before SBDebugger::Terminate() is called.
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001038 {
Greg Clayton66111032010-06-23 01:19:29 +00001039 Driver driver;
1040
Deepak Panickal429222c2013-10-15 15:46:40 +00001041 bool exiting = false;
1042 SBError error (driver.ParseArgs (argc, argv, stdout, exiting));
Greg Clayton66111032010-06-23 01:19:29 +00001043 if (error.Fail())
1044 {
1045 const char *error_cstr = error.GetCString ();
1046 if (error_cstr)
1047 ::fprintf (stderr, "error: %s\n", error_cstr);
1048 }
Deepak Panickal429222c2013-10-15 15:46:40 +00001049 else if (!exiting)
Greg Clayton66111032010-06-23 01:19:29 +00001050 {
1051 driver.MainLoop ();
1052 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001053 }
1054
1055 SBDebugger::Terminate();
1056 return 0;
1057}