1 //===-- ProcessLauncherAndroid.cpp ------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Host/FileSpec.h"
11 #include "lldb/Host/Host.h"
12 #include "lldb/Host/HostProcess.h"
13 #include "lldb/Host/android/ProcessLauncherAndroid.h"
15 #include "lldb/Target/ProcessLaunchInfo.h"
20 using namespace lldb_private;
23 DupDescriptor(const FileSpec &file_spec, int fd, int flags)
25 int target_fd = ::open(file_spec.GetCString(), flags, 0666);
30 if (::dup2(target_fd, fd) == -1)
33 return (::close(target_fd) == -1) ? false : true;
36 // If there is no PATH variable specified inside the environment then set the path to /system/bin.
37 // It is required because the default path used by execve() is wrong on android.
39 FixupEnvironment(Args& env)
41 static const char* path = "PATH=";
42 static const int path_len = ::strlen(path);
43 for (const char** args = env.GetConstArgumentVector(); *args; ++args)
44 if (::strncmp(path, *args, path_len) == 0)
46 env.AppendArgument("PATH=/system/bin");
50 ProcessLauncherAndroid::LaunchProcess(const ProcessLaunchInfo &launch_info, Error &error)
52 // TODO: Handle other launch parameters specified in launc_info
54 char exe_path[PATH_MAX];
55 launch_info.GetExecutableFile().GetPath(exe_path, sizeof(exe_path));
57 lldb::pid_t pid = ::fork();
58 if (pid == static_cast<lldb::pid_t>(-1))
61 error.SetErrorStringWithFormat("Fork failed with error message: %s", strerror(errno));
62 return HostProcess(LLDB_INVALID_PROCESS_ID);
66 if (const lldb_private::FileAction *file_action = launch_info.GetFileActionForFD(STDIN_FILENO)) {
67 FileSpec file_spec = file_action->GetFileSpec();
69 if (!DupDescriptor(file_spec, STDIN_FILENO, O_RDONLY))
73 if (const lldb_private::FileAction *file_action = launch_info.GetFileActionForFD(STDOUT_FILENO)) {
74 FileSpec file_spec = file_action->GetFileSpec();
76 if (!DupDescriptor(file_spec, STDOUT_FILENO, O_WRONLY | O_CREAT | O_TRUNC))
80 if (const lldb_private::FileAction *file_action = launch_info.GetFileActionForFD(STDERR_FILENO)) {
81 FileSpec file_spec = file_action->GetFileSpec();
83 if (!DupDescriptor(file_spec, STDERR_FILENO, O_WRONLY | O_CREAT | O_TRUNC))
88 const char **argv = launch_info.GetArguments().GetConstArgumentVector();
90 Args env = launch_info.GetEnvironmentEntries();
91 FixupEnvironment(env);
92 const char **envp = env.GetConstArgumentVector();
94 FileSpec working_dir = launch_info.GetWorkingDirectory();
97 if (::chdir(working_dir.GetCString()) != 0)
102 const_cast<char *const *>(argv),
103 const_cast<char *const *>(envp));
107 return HostProcess(pid);