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