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