]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - source/Target/ProcessLaunchInfo.cpp
Import LLDB as of upstream SVN 241361 (git 612c075f)
[FreeBSD/FreeBSD.git] / source / Target / ProcessLaunchInfo.cpp
1 //===-- ProcessLaunchInfo.cpp -----------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "lldb/Host/Config.h"
11
12 #include "lldb/Core/Debugger.h"
13 #include "lldb/Core/Log.h"
14 #include "lldb/Target/ProcessLaunchInfo.h"
15 #include "lldb/Target/FileAction.h"
16 #include "lldb/Target/Target.h"
17
18 #if !defined(_WIN32)
19 #include <limits.h>
20 #endif
21
22 using namespace lldb;
23 using namespace lldb_private;
24
25 //----------------------------------------------------------------------------
26 // ProcessLaunchInfo member functions
27 //----------------------------------------------------------------------------
28
29 ProcessLaunchInfo::ProcessLaunchInfo () :
30     ProcessInfo(),
31     m_working_dir (),
32     m_plugin_name (),
33     m_flags (0),
34     m_file_actions (),
35     m_pty (new lldb_utility::PseudoTerminal),
36     m_resume_count (0),
37     m_monitor_callback (NULL),
38     m_monitor_callback_baton (NULL),
39     m_monitor_signals (false),
40     m_listener_sp (),
41     m_hijack_listener_sp ()
42 {
43 }
44
45 ProcessLaunchInfo::ProcessLaunchInfo(const FileSpec &stdin_file_spec,
46                                      const FileSpec &stdout_file_spec,
47                                      const FileSpec &stderr_file_spec,
48                                      const FileSpec &working_directory,
49                                      uint32_t launch_flags) :
50     ProcessInfo(),
51     m_working_dir(),
52     m_plugin_name(),
53     m_flags(launch_flags),
54     m_file_actions(),
55     m_pty(new lldb_utility::PseudoTerminal),
56     m_resume_count(0),
57     m_monitor_callback(NULL),
58     m_monitor_callback_baton(NULL),
59     m_monitor_signals(false),
60     m_listener_sp (),
61     m_hijack_listener_sp()
62 {
63     if (stdin_file_spec)
64     {
65         FileAction file_action;
66         const bool read = true;
67         const bool write = false;
68         if (file_action.Open(STDIN_FILENO, stdin_file_spec, read, write))
69             AppendFileAction (file_action);
70     }
71     if (stdout_file_spec)
72     {
73         FileAction file_action;
74         const bool read = false;
75         const bool write = true;
76         if (file_action.Open(STDOUT_FILENO, stdout_file_spec, read, write))
77             AppendFileAction (file_action);
78     }
79     if (stderr_file_spec)
80     {
81         FileAction file_action;
82         const bool read = false;
83         const bool write = true;
84         if (file_action.Open(STDERR_FILENO, stderr_file_spec, read, write))
85             AppendFileAction (file_action);
86     }
87     if (working_directory)
88         SetWorkingDirectory(working_directory);
89 }
90
91 bool
92 ProcessLaunchInfo::AppendCloseFileAction (int fd)
93 {
94     FileAction file_action;
95     if (file_action.Close (fd))
96     {
97         AppendFileAction (file_action);
98         return true;
99     }
100     return false;
101 }
102
103 bool
104 ProcessLaunchInfo::AppendDuplicateFileAction (int fd, int dup_fd)
105 {
106     FileAction file_action;
107     if (file_action.Duplicate (fd, dup_fd))
108     {
109         AppendFileAction (file_action);
110         return true;
111     }
112     return false;
113 }
114
115 bool
116 ProcessLaunchInfo::AppendOpenFileAction(int fd, const FileSpec &file_spec,
117                                         bool read, bool write)
118 {
119     FileAction file_action;
120     if (file_action.Open(fd, file_spec, read, write))
121     {
122         AppendFileAction (file_action);
123         return true;
124     }
125     return false;
126 }
127
128 bool
129 ProcessLaunchInfo::AppendSuppressFileAction (int fd, bool read, bool write)
130 {
131     FileAction file_action;
132     if (file_action.Open(fd, FileSpec{"/dev/null", false}, read, write))
133     {
134         AppendFileAction (file_action);
135         return true;
136     }
137     return false;
138 }
139
140 const FileAction *
141 ProcessLaunchInfo::GetFileActionAtIndex(size_t idx) const
142 {
143     if (idx < m_file_actions.size())
144         return &m_file_actions[idx];
145     return NULL;
146 }
147
148 const FileAction *
149 ProcessLaunchInfo::GetFileActionForFD(int fd) const
150 {
151     for (size_t idx=0, count=m_file_actions.size(); idx < count; ++idx)
152     {
153         if (m_file_actions[idx].GetFD () == fd)
154             return &m_file_actions[idx];
155     }
156     return NULL;
157 }
158
159 const FileSpec &
160 ProcessLaunchInfo::GetWorkingDirectory() const
161 {
162     return m_working_dir;
163 }
164
165 void
166 ProcessLaunchInfo::SetWorkingDirectory(const FileSpec &working_dir)
167 {
168     m_working_dir = working_dir;
169 }
170
171 const char *
172 ProcessLaunchInfo::GetProcessPluginName () const
173 {
174     if (m_plugin_name.empty())
175         return NULL;
176     return m_plugin_name.c_str();
177 }
178
179 void
180 ProcessLaunchInfo::SetProcessPluginName (const char *plugin)
181 {
182     if (plugin && plugin[0])
183         m_plugin_name.assign (plugin);
184     else
185         m_plugin_name.clear();
186 }
187
188 const FileSpec &
189 ProcessLaunchInfo::GetShell () const
190 {
191     return m_shell;
192 }
193
194 void
195 ProcessLaunchInfo::SetShell (const FileSpec &shell)
196 {
197     m_shell = shell;
198     if (m_shell)
199     {
200         m_shell.ResolveExecutableLocation();
201         m_flags.Set (lldb::eLaunchFlagLaunchInShell);
202     }
203     else
204         m_flags.Clear (lldb::eLaunchFlagLaunchInShell);
205 }
206
207 void
208 ProcessLaunchInfo::SetLaunchInSeparateProcessGroup (bool separate)
209 {
210     if (separate)
211         m_flags.Set(lldb::eLaunchFlagLaunchInSeparateProcessGroup);
212     else
213         m_flags.Clear (lldb::eLaunchFlagLaunchInSeparateProcessGroup);
214 }
215
216 void
217 ProcessLaunchInfo::SetShellExpandArguments (bool expand)
218 {
219     if (expand)
220         m_flags.Set(lldb::eLaunchFlagShellExpandArguments);
221     else
222         m_flags.Clear(lldb::eLaunchFlagShellExpandArguments);
223 }
224
225 void
226 ProcessLaunchInfo::Clear ()
227 {
228     ProcessInfo::Clear();
229     m_working_dir.Clear();
230     m_plugin_name.clear();
231     m_shell.Clear();
232     m_flags.Clear();
233     m_file_actions.clear();
234     m_resume_count = 0;
235     m_listener_sp.reset();
236     m_hijack_listener_sp.reset();
237 }
238
239 void
240 ProcessLaunchInfo::SetMonitorProcessCallback (Host::MonitorChildProcessCallback callback,
241                            void *baton,
242                            bool monitor_signals)
243 {
244     m_monitor_callback = callback;
245     m_monitor_callback_baton = baton;
246     m_monitor_signals = monitor_signals;
247 }
248
249 bool
250 ProcessLaunchInfo::MonitorProcess () const
251 {
252     if (m_monitor_callback && ProcessIDIsValid())
253     {
254         Host::StartMonitoringChildProcess (m_monitor_callback,
255                                            m_monitor_callback_baton,
256                                            GetProcessID(),
257                                            m_monitor_signals);
258         return true;
259     }
260     return false;
261 }
262
263 void
264 ProcessLaunchInfo::SetDetachOnError (bool enable)
265 {
266     if (enable)
267         m_flags.Set(lldb::eLaunchFlagDetachOnError);
268     else
269         m_flags.Clear(lldb::eLaunchFlagDetachOnError);
270 }
271
272 void
273 ProcessLaunchInfo::FinalizeFileActions (Target *target, bool default_to_use_pty)
274 {
275     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
276
277     // If nothing for stdin or stdout or stderr was specified, then check the process for any default
278     // settings that were set with "settings set"
279     if (GetFileActionForFD(STDIN_FILENO) == NULL ||
280         GetFileActionForFD(STDOUT_FILENO) == NULL ||
281         GetFileActionForFD(STDERR_FILENO) == NULL)
282     {
283         if (log)
284             log->Printf ("ProcessLaunchInfo::%s at least one of stdin/stdout/stderr was not set, evaluating default handling",
285                          __FUNCTION__);
286
287         if (m_flags.Test(eLaunchFlagDisableSTDIO))
288         {
289             if (log)
290                 log->Printf ("ProcessLaunchInfo::%s eLaunchFlagDisableSTDIO set, adding suppression action for stdin, stdout and stderr",
291                              __FUNCTION__);
292             AppendSuppressFileAction (STDIN_FILENO , true, false);
293             AppendSuppressFileAction (STDOUT_FILENO, false, true);
294             AppendSuppressFileAction (STDERR_FILENO, false, true);
295         }
296         else
297         {
298             // Check for any values that might have gotten set with any of:
299             // (lldb) settings set target.input-path
300             // (lldb) settings set target.output-path
301             // (lldb) settings set target.error-path
302             FileSpec in_file_spec;
303             FileSpec out_file_spec;
304             FileSpec err_file_spec;
305             if (target)
306             {
307                 // Only override with the target settings if we don't already have
308                 // an action for in, out or error
309                 if (GetFileActionForFD(STDIN_FILENO) == NULL)
310                     in_file_spec = target->GetStandardInputPath();
311                 if (GetFileActionForFD(STDOUT_FILENO) == NULL)
312                     out_file_spec = target->GetStandardOutputPath();
313                 if (GetFileActionForFD(STDERR_FILENO) == NULL)
314                     err_file_spec = target->GetStandardErrorPath();
315             }
316
317             if (log)
318                 log->Printf ("ProcessLaunchInfo::%s target stdin='%s', target stdout='%s', stderr='%s'",
319                              __FUNCTION__,
320                               in_file_spec ?  in_file_spec.GetCString() : "<null>",
321                              out_file_spec ? out_file_spec.GetCString() : "<null>",
322                              err_file_spec ? err_file_spec.GetCString() : "<null>");
323
324             if (in_file_spec)
325             {
326                 AppendOpenFileAction(STDIN_FILENO, in_file_spec, true, false);
327                 if (log)
328                     log->Printf ("ProcessLaunchInfo::%s appended stdin open file action for %s",
329                                  __FUNCTION__, in_file_spec.GetCString());
330             }
331
332             if (out_file_spec)
333             {
334                 AppendOpenFileAction(STDOUT_FILENO, out_file_spec, false, true);
335                 if (log)
336                     log->Printf ("ProcessLaunchInfo::%s appended stdout open file action for %s",
337                                  __FUNCTION__, out_file_spec.GetCString());
338             }
339
340             if (err_file_spec)
341             {
342                 AppendOpenFileAction(STDERR_FILENO, err_file_spec, false, true);
343                 if (log)
344                     log->Printf ("ProcessLaunchInfo::%s appended stderr open file action for %s",
345                                  __FUNCTION__, err_file_spec.GetCString());
346             }
347
348             if (default_to_use_pty && (!in_file_spec || !out_file_spec || !err_file_spec))
349             {
350                 if (log)
351                     log->Printf ("ProcessLaunchInfo::%s default_to_use_pty is set, and at least one stdin/stderr/stdout is unset, so generating a pty to use for it",
352                                  __FUNCTION__);
353
354                 int open_flags = O_RDWR | O_NOCTTY;
355 #if !defined(_MSC_VER)
356                 // We really shouldn't be specifying platform specific flags
357                 // that are intended for a system call in generic code.  But
358                 // this will have to do for now.
359                 open_flags |= O_CLOEXEC;
360 #endif
361                 if (m_pty->OpenFirstAvailableMaster(open_flags, NULL, 0))
362                 {
363                     const FileSpec slave_file_spec{m_pty->GetSlaveName(NULL, 0), false};
364
365                     // Only use the slave tty if we don't have anything specified for
366                     // input and don't have an action for stdin
367                     if (!in_file_spec && GetFileActionForFD(STDIN_FILENO) == NULL)
368                     {
369                         AppendOpenFileAction(STDIN_FILENO, slave_file_spec, true, false);
370                     }
371
372                     // Only use the slave tty if we don't have anything specified for
373                     // output and don't have an action for stdout
374                     if (!out_file_spec && GetFileActionForFD(STDOUT_FILENO) == NULL)
375                     {
376                         AppendOpenFileAction(STDOUT_FILENO, slave_file_spec, false, true);
377                     }
378
379                     // Only use the slave tty if we don't have anything specified for
380                     // error and don't have an action for stderr
381                     if (!err_file_spec && GetFileActionForFD(STDERR_FILENO) == NULL)
382                     {
383                         AppendOpenFileAction(STDERR_FILENO, slave_file_spec, false, true);
384                     }
385                 }
386             }
387         }
388     }
389 }
390
391
392 bool
393 ProcessLaunchInfo::ConvertArgumentsForLaunchingInShell (Error &error,
394                                                         bool localhost,
395                                                         bool will_debug,
396                                                         bool first_arg_is_full_shell_command,
397                                                         int32_t num_resumes)
398 {
399     error.Clear();
400
401     if (GetFlags().Test (eLaunchFlagLaunchInShell))
402     {
403         if (m_shell)
404         {
405             std::string shell_executable = m_shell.GetPath();
406
407             const char **argv = GetArguments().GetConstArgumentVector ();
408             if (argv == NULL || argv[0] == NULL)
409                 return false;
410             Args shell_arguments;
411             std::string safe_arg;
412             shell_arguments.AppendArgument (shell_executable.c_str());
413             const llvm::Triple &triple = GetArchitecture().GetTriple();
414             if (triple.getOS() == llvm::Triple::Win32 && !triple.isWindowsCygwinEnvironment())
415                 shell_arguments.AppendArgument("/C");
416             else
417                 shell_arguments.AppendArgument("-c");
418
419             StreamString shell_command;
420             if (will_debug)
421             {
422                 // Add a modified PATH environment variable in case argv[0]
423                 // is a relative path.
424                 const char *argv0 = argv[0];
425                 FileSpec arg_spec(argv0, false);
426                 if (arg_spec.IsRelative())
427                 {
428                     // We have a relative path to our executable which may not work if
429                     // we just try to run "a.out" (without it being converted to "./a.out")
430                     FileSpec working_dir = GetWorkingDirectory();
431                     // Be sure to put quotes around PATH's value in case any paths have spaces...
432                     std::string new_path("PATH=\"");
433                     const size_t empty_path_len = new_path.size();
434
435                     if (working_dir)
436                     {
437                         new_path += working_dir.GetPath();
438                     }
439                     else
440                     {
441                         char current_working_dir[PATH_MAX];
442                         const char *cwd = getcwd(current_working_dir, sizeof(current_working_dir));
443                         if (cwd && cwd[0])
444                             new_path += cwd;
445                     }
446                     const char *curr_path = getenv("PATH");
447                     if (curr_path)
448                     {
449                         if (new_path.size() > empty_path_len)
450                             new_path += ':';
451                         new_path += curr_path;
452                     }
453                     new_path += "\" ";
454                     shell_command.PutCString(new_path.c_str());
455                 }
456
457                 if (triple.getOS() != llvm::Triple::Win32 || triple.isWindowsCygwinEnvironment())
458                     shell_command.PutCString("exec");
459
460                 // Only Apple supports /usr/bin/arch being able to specify the architecture
461                 if (GetArchitecture().IsValid() &&                                          // Valid architecture
462                     GetArchitecture().GetTriple().getVendor() == llvm::Triple::Apple &&     // Apple only
463                     GetArchitecture().GetCore() != ArchSpec::eCore_x86_64_x86_64h)          // Don't do this for x86_64h
464                 {
465                     shell_command.Printf(" /usr/bin/arch -arch %s", GetArchitecture().GetArchitectureName());
466                     // Set the resume count to 2:
467                     // 1 - stop in shell
468                     // 2 - stop in /usr/bin/arch
469                     // 3 - then we will stop in our program
470                     SetResumeCount(num_resumes + 1);
471                 }
472                 else
473                 {
474                     // Set the resume count to 1:
475                     // 1 - stop in shell
476                     // 2 - then we will stop in our program
477                     SetResumeCount(num_resumes);
478                 }
479             }
480
481             if (first_arg_is_full_shell_command)
482             {
483                 // There should only be one argument that is the shell command itself to be used as is
484                 if (argv[0] && !argv[1])
485                     shell_command.Printf("%s", argv[0]);
486                 else
487                     return false;
488             }
489             else
490             {
491                 for (size_t i=0; argv[i] != NULL; ++i)
492                 {
493                     const char *arg = Args::GetShellSafeArgument (argv[i], safe_arg);
494                     shell_command.Printf(" %s", arg);
495                 }
496             }
497             shell_arguments.AppendArgument (shell_command.GetString().c_str());
498             m_executable = m_shell;
499             m_arguments = shell_arguments;
500             return true;
501         }
502         else
503         {
504             error.SetErrorString ("invalid shell path");
505         }
506     }
507     else
508     {
509         error.SetErrorString ("not launching in shell");
510     }
511     return false;
512 }
513
514 Listener &
515 ProcessLaunchInfo::GetListenerForProcess (Debugger &debugger)
516 {
517     if (m_listener_sp)
518         return *m_listener_sp;
519     else
520         return debugger.GetListener();
521 }