1af732203SDimitry Andric //===-- CommandOptionsProcessLaunch.cpp -----------------------------------===//
2af732203SDimitry Andric //
3af732203SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4af732203SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5af732203SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6af732203SDimitry Andric //
7af732203SDimitry Andric //===----------------------------------------------------------------------===//
8af732203SDimitry Andric 
9af732203SDimitry Andric #include "CommandOptionsProcessLaunch.h"
10af732203SDimitry Andric 
11af732203SDimitry Andric #include "lldb/Host/FileSystem.h"
12af732203SDimitry Andric #include "lldb/Host/HostInfo.h"
13af732203SDimitry Andric #include "lldb/Host/OptionParser.h"
14af732203SDimitry Andric #include "lldb/Interpreter/CommandCompletions.h"
15af732203SDimitry Andric #include "lldb/Interpreter/OptionArgParser.h"
16af732203SDimitry Andric #include "lldb/Target/ExecutionContext.h"
17af732203SDimitry Andric #include "lldb/Target/Platform.h"
18af732203SDimitry Andric #include "lldb/Target/Target.h"
19af732203SDimitry Andric 
20af732203SDimitry Andric #include "llvm/ADT/ArrayRef.h"
21af732203SDimitry Andric 
22af732203SDimitry Andric using namespace llvm;
23af732203SDimitry Andric using namespace lldb;
24af732203SDimitry Andric using namespace lldb_private;
25af732203SDimitry Andric 
26af732203SDimitry Andric #define LLDB_OPTIONS_process_launch
27af732203SDimitry Andric #include "CommandOptions.inc"
28af732203SDimitry Andric 
SetOptionValue(uint32_t option_idx,llvm::StringRef option_arg,ExecutionContext * execution_context)29af732203SDimitry Andric Status CommandOptionsProcessLaunch::SetOptionValue(
30af732203SDimitry Andric     uint32_t option_idx, llvm::StringRef option_arg,
31af732203SDimitry Andric     ExecutionContext *execution_context) {
32af732203SDimitry Andric   Status error;
33*5f7ddb14SDimitry Andric   const int short_option = g_process_launch_options[option_idx].short_option;
34af732203SDimitry Andric 
35af732203SDimitry Andric   switch (short_option) {
36af732203SDimitry Andric   case 's': // Stop at program entry point
37af732203SDimitry Andric     launch_info.GetFlags().Set(eLaunchFlagStopAtEntry);
38af732203SDimitry Andric     break;
39af732203SDimitry Andric 
40af732203SDimitry Andric   case 'i': // STDIN for read only
41af732203SDimitry Andric   {
42af732203SDimitry Andric     FileAction action;
43af732203SDimitry Andric     if (action.Open(STDIN_FILENO, FileSpec(option_arg), true, false))
44af732203SDimitry Andric       launch_info.AppendFileAction(action);
45af732203SDimitry Andric     break;
46af732203SDimitry Andric   }
47af732203SDimitry Andric 
48af732203SDimitry Andric   case 'o': // Open STDOUT for write only
49af732203SDimitry Andric   {
50af732203SDimitry Andric     FileAction action;
51af732203SDimitry Andric     if (action.Open(STDOUT_FILENO, FileSpec(option_arg), false, true))
52af732203SDimitry Andric       launch_info.AppendFileAction(action);
53af732203SDimitry Andric     break;
54af732203SDimitry Andric   }
55af732203SDimitry Andric 
56af732203SDimitry Andric   case 'e': // STDERR for write only
57af732203SDimitry Andric   {
58af732203SDimitry Andric     FileAction action;
59af732203SDimitry Andric     if (action.Open(STDERR_FILENO, FileSpec(option_arg), false, true))
60af732203SDimitry Andric       launch_info.AppendFileAction(action);
61af732203SDimitry Andric     break;
62af732203SDimitry Andric   }
63af732203SDimitry Andric 
64af732203SDimitry Andric   case 'P': // Process plug-in name
65af732203SDimitry Andric     launch_info.SetProcessPluginName(option_arg);
66af732203SDimitry Andric     break;
67af732203SDimitry Andric 
68af732203SDimitry Andric   case 'n': // Disable STDIO
69af732203SDimitry Andric   {
70af732203SDimitry Andric     FileAction action;
71af732203SDimitry Andric     const FileSpec dev_null(FileSystem::DEV_NULL);
72af732203SDimitry Andric     if (action.Open(STDIN_FILENO, dev_null, true, false))
73af732203SDimitry Andric       launch_info.AppendFileAction(action);
74af732203SDimitry Andric     if (action.Open(STDOUT_FILENO, dev_null, false, true))
75af732203SDimitry Andric       launch_info.AppendFileAction(action);
76af732203SDimitry Andric     if (action.Open(STDERR_FILENO, dev_null, false, true))
77af732203SDimitry Andric       launch_info.AppendFileAction(action);
78af732203SDimitry Andric     break;
79af732203SDimitry Andric   }
80af732203SDimitry Andric 
81af732203SDimitry Andric   case 'w':
82af732203SDimitry Andric     launch_info.SetWorkingDirectory(FileSpec(option_arg));
83af732203SDimitry Andric     break;
84af732203SDimitry Andric 
85af732203SDimitry Andric   case 't': // Open process in new terminal window
86af732203SDimitry Andric     launch_info.GetFlags().Set(eLaunchFlagLaunchInTTY);
87af732203SDimitry Andric     break;
88af732203SDimitry Andric 
89af732203SDimitry Andric   case 'a': {
90af732203SDimitry Andric     TargetSP target_sp =
91af732203SDimitry Andric         execution_context ? execution_context->GetTargetSP() : TargetSP();
92af732203SDimitry Andric     PlatformSP platform_sp =
93af732203SDimitry Andric         target_sp ? target_sp->GetPlatform() : PlatformSP();
94af732203SDimitry Andric     launch_info.GetArchitecture() =
95af732203SDimitry Andric         Platform::GetAugmentedArchSpec(platform_sp.get(), option_arg);
96af732203SDimitry Andric   } break;
97af732203SDimitry Andric 
98af732203SDimitry Andric   case 'A': // Disable ASLR.
99af732203SDimitry Andric   {
100af732203SDimitry Andric     bool success;
101af732203SDimitry Andric     const bool disable_aslr_arg =
102af732203SDimitry Andric         OptionArgParser::ToBoolean(option_arg, true, &success);
103af732203SDimitry Andric     if (success)
104af732203SDimitry Andric       disable_aslr = disable_aslr_arg ? eLazyBoolYes : eLazyBoolNo;
105af732203SDimitry Andric     else
106af732203SDimitry Andric       error.SetErrorStringWithFormat(
107af732203SDimitry Andric           "Invalid boolean value for disable-aslr option: '%s'",
108af732203SDimitry Andric           option_arg.empty() ? "<null>" : option_arg.str().c_str());
109af732203SDimitry Andric     break;
110af732203SDimitry Andric   }
111af732203SDimitry Andric 
112af732203SDimitry Andric   case 'X': // shell expand args.
113af732203SDimitry Andric   {
114af732203SDimitry Andric     bool success;
115af732203SDimitry Andric     const bool expand_args =
116af732203SDimitry Andric         OptionArgParser::ToBoolean(option_arg, true, &success);
117af732203SDimitry Andric     if (success)
118af732203SDimitry Andric       launch_info.SetShellExpandArguments(expand_args);
119af732203SDimitry Andric     else
120af732203SDimitry Andric       error.SetErrorStringWithFormat(
121af732203SDimitry Andric           "Invalid boolean value for shell-expand-args option: '%s'",
122af732203SDimitry Andric           option_arg.empty() ? "<null>" : option_arg.str().c_str());
123af732203SDimitry Andric     break;
124af732203SDimitry Andric   }
125af732203SDimitry Andric 
126af732203SDimitry Andric   case 'c':
127af732203SDimitry Andric     if (!option_arg.empty())
128af732203SDimitry Andric       launch_info.SetShell(FileSpec(option_arg));
129af732203SDimitry Andric     else
130af732203SDimitry Andric       launch_info.SetShell(HostInfo::GetDefaultShell());
131af732203SDimitry Andric     break;
132af732203SDimitry Andric 
133*5f7ddb14SDimitry Andric   case 'E':
134af732203SDimitry Andric     launch_info.GetEnvironment().insert(option_arg);
135af732203SDimitry Andric     break;
136af732203SDimitry Andric 
137af732203SDimitry Andric   default:
138af732203SDimitry Andric     error.SetErrorStringWithFormat("unrecognized short option character '%c'",
139af732203SDimitry Andric                                    short_option);
140af732203SDimitry Andric     break;
141af732203SDimitry Andric   }
142af732203SDimitry Andric   return error;
143af732203SDimitry Andric }
144af732203SDimitry Andric 
GetDefinitions()145af732203SDimitry Andric llvm::ArrayRef<OptionDefinition> CommandOptionsProcessLaunch::GetDefinitions() {
146af732203SDimitry Andric   return llvm::makeArrayRef(g_process_launch_options);
147af732203SDimitry Andric }
148