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