xref: /freebsd-src/contrib/llvm-project/lldb/source/Commands/CommandOptionsProcessLaunch.cpp (revision fcaf7f8644a9988098ac6be2165bce3ea4786e91)
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"
15*fcaf7f86SDimitry Andric #include "lldb/Interpreter/CommandObject.h"
16*fcaf7f86SDimitry 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 
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 
37e8d8bef9SDimitry Andric   switch (short_option) {
38e8d8bef9SDimitry Andric   case 's': // Stop at program entry point
39e8d8bef9SDimitry Andric     launch_info.GetFlags().Set(eLaunchFlagStopAtEntry);
40e8d8bef9SDimitry Andric     break;
41e8d8bef9SDimitry Andric 
42e8d8bef9SDimitry Andric   case 'i': // STDIN for read only
43e8d8bef9SDimitry Andric   {
44e8d8bef9SDimitry Andric     FileAction action;
45e8d8bef9SDimitry Andric     if (action.Open(STDIN_FILENO, FileSpec(option_arg), true, false))
46e8d8bef9SDimitry Andric       launch_info.AppendFileAction(action);
47e8d8bef9SDimitry Andric     break;
48e8d8bef9SDimitry Andric   }
49e8d8bef9SDimitry Andric 
50e8d8bef9SDimitry Andric   case 'o': // Open STDOUT for write only
51e8d8bef9SDimitry Andric   {
52e8d8bef9SDimitry Andric     FileAction action;
53e8d8bef9SDimitry Andric     if (action.Open(STDOUT_FILENO, FileSpec(option_arg), false, true))
54e8d8bef9SDimitry Andric       launch_info.AppendFileAction(action);
55e8d8bef9SDimitry Andric     break;
56e8d8bef9SDimitry Andric   }
57e8d8bef9SDimitry Andric 
58e8d8bef9SDimitry Andric   case 'e': // STDERR for write only
59e8d8bef9SDimitry Andric   {
60e8d8bef9SDimitry Andric     FileAction action;
61e8d8bef9SDimitry Andric     if (action.Open(STDERR_FILENO, FileSpec(option_arg), false, true))
62e8d8bef9SDimitry Andric       launch_info.AppendFileAction(action);
63e8d8bef9SDimitry Andric     break;
64e8d8bef9SDimitry Andric   }
65e8d8bef9SDimitry Andric 
66e8d8bef9SDimitry Andric   case 'P': // Process plug-in name
67e8d8bef9SDimitry Andric     launch_info.SetProcessPluginName(option_arg);
68e8d8bef9SDimitry Andric     break;
69e8d8bef9SDimitry Andric 
70e8d8bef9SDimitry Andric   case 'n': // Disable STDIO
71e8d8bef9SDimitry Andric   {
72e8d8bef9SDimitry Andric     FileAction action;
73e8d8bef9SDimitry Andric     const FileSpec dev_null(FileSystem::DEV_NULL);
74e8d8bef9SDimitry Andric     if (action.Open(STDIN_FILENO, dev_null, true, false))
75e8d8bef9SDimitry Andric       launch_info.AppendFileAction(action);
76e8d8bef9SDimitry Andric     if (action.Open(STDOUT_FILENO, dev_null, false, true))
77e8d8bef9SDimitry Andric       launch_info.AppendFileAction(action);
78e8d8bef9SDimitry Andric     if (action.Open(STDERR_FILENO, dev_null, false, true))
79e8d8bef9SDimitry Andric       launch_info.AppendFileAction(action);
80e8d8bef9SDimitry Andric     break;
81e8d8bef9SDimitry Andric   }
82e8d8bef9SDimitry Andric 
83e8d8bef9SDimitry Andric   case 'w':
84e8d8bef9SDimitry Andric     launch_info.SetWorkingDirectory(FileSpec(option_arg));
85e8d8bef9SDimitry Andric     break;
86e8d8bef9SDimitry Andric 
87e8d8bef9SDimitry Andric   case 't': // Open process in new terminal window
88e8d8bef9SDimitry Andric     launch_info.GetFlags().Set(eLaunchFlagLaunchInTTY);
89e8d8bef9SDimitry Andric     break;
90e8d8bef9SDimitry Andric 
91e8d8bef9SDimitry Andric   case 'a': {
92e8d8bef9SDimitry Andric     TargetSP target_sp =
93e8d8bef9SDimitry Andric         execution_context ? execution_context->GetTargetSP() : TargetSP();
94e8d8bef9SDimitry Andric     PlatformSP platform_sp =
95e8d8bef9SDimitry Andric         target_sp ? target_sp->GetPlatform() : PlatformSP();
96e8d8bef9SDimitry Andric     launch_info.GetArchitecture() =
97e8d8bef9SDimitry Andric         Platform::GetAugmentedArchSpec(platform_sp.get(), option_arg);
98e8d8bef9SDimitry Andric   } break;
99e8d8bef9SDimitry Andric 
100e8d8bef9SDimitry Andric   case 'A': // Disable ASLR.
101e8d8bef9SDimitry Andric   {
102e8d8bef9SDimitry Andric     bool success;
103e8d8bef9SDimitry Andric     const bool disable_aslr_arg =
104e8d8bef9SDimitry Andric         OptionArgParser::ToBoolean(option_arg, true, &success);
105e8d8bef9SDimitry Andric     if (success)
106e8d8bef9SDimitry Andric       disable_aslr = disable_aslr_arg ? eLazyBoolYes : eLazyBoolNo;
107e8d8bef9SDimitry Andric     else
108e8d8bef9SDimitry Andric       error.SetErrorStringWithFormat(
109e8d8bef9SDimitry Andric           "Invalid boolean value for disable-aslr option: '%s'",
110e8d8bef9SDimitry Andric           option_arg.empty() ? "<null>" : option_arg.str().c_str());
111e8d8bef9SDimitry Andric     break;
112e8d8bef9SDimitry Andric   }
113e8d8bef9SDimitry Andric 
114e8d8bef9SDimitry Andric   case 'X': // shell expand args.
115e8d8bef9SDimitry Andric   {
116e8d8bef9SDimitry Andric     bool success;
117e8d8bef9SDimitry Andric     const bool expand_args =
118e8d8bef9SDimitry Andric         OptionArgParser::ToBoolean(option_arg, true, &success);
119e8d8bef9SDimitry Andric     if (success)
120e8d8bef9SDimitry Andric       launch_info.SetShellExpandArguments(expand_args);
121e8d8bef9SDimitry Andric     else
122e8d8bef9SDimitry Andric       error.SetErrorStringWithFormat(
123e8d8bef9SDimitry Andric           "Invalid boolean value for shell-expand-args option: '%s'",
124e8d8bef9SDimitry Andric           option_arg.empty() ? "<null>" : option_arg.str().c_str());
125e8d8bef9SDimitry Andric     break;
126e8d8bef9SDimitry Andric   }
127e8d8bef9SDimitry Andric 
128e8d8bef9SDimitry Andric   case 'c':
129e8d8bef9SDimitry Andric     if (!option_arg.empty())
130e8d8bef9SDimitry Andric       launch_info.SetShell(FileSpec(option_arg));
131e8d8bef9SDimitry Andric     else
132e8d8bef9SDimitry Andric       launch_info.SetShell(HostInfo::GetDefaultShell());
133e8d8bef9SDimitry Andric     break;
134e8d8bef9SDimitry Andric 
135fe6060f1SDimitry Andric   case 'E':
136e8d8bef9SDimitry Andric     launch_info.GetEnvironment().insert(option_arg);
137e8d8bef9SDimitry Andric     break;
138e8d8bef9SDimitry Andric 
139e8d8bef9SDimitry Andric   default:
140e8d8bef9SDimitry Andric     error.SetErrorStringWithFormat("unrecognized short option character '%c'",
141e8d8bef9SDimitry Andric                                    short_option);
142e8d8bef9SDimitry Andric     break;
143e8d8bef9SDimitry Andric   }
144e8d8bef9SDimitry Andric   return error;
145e8d8bef9SDimitry Andric }
146e8d8bef9SDimitry Andric 
147e8d8bef9SDimitry Andric llvm::ArrayRef<OptionDefinition> CommandOptionsProcessLaunch::GetDefinitions() {
148e8d8bef9SDimitry Andric   return llvm::makeArrayRef(g_process_launch_options);
149e8d8bef9SDimitry Andric }
150