10127ef0fSEd Maste //===-- ProcessInfo.cpp -----------------------------------------*- C++ -*-===//
20127ef0fSEd Maste //
30127ef0fSEd Maste // The LLVM Compiler Infrastructure
40127ef0fSEd Maste //
50127ef0fSEd Maste // This file is distributed under the University of Illinois Open Source
60127ef0fSEd Maste // License. See LICENSE.TXT for details.
70127ef0fSEd Maste //
80127ef0fSEd Maste //===----------------------------------------------------------------------===//
90127ef0fSEd Maste
10435933ddSDimitry Andric #include "lldb/Target/ProcessInfo.h"
11435933ddSDimitry Andric
124bb0738eSEd Maste #include <climits>
134bb0738eSEd Maste
14435933ddSDimitry Andric #include "lldb/Host/PosixApi.h"
15f678e45dSDimitry Andric #include "lldb/Utility/Stream.h"
16435933ddSDimitry Andric
17435933ddSDimitry Andric #include "llvm/ADT/SmallString.h"
180127ef0fSEd Maste
190127ef0fSEd Maste using namespace lldb;
200127ef0fSEd Maste using namespace lldb_private;
210127ef0fSEd Maste
ProcessInfo()22435933ddSDimitry Andric ProcessInfo::ProcessInfo()
23435933ddSDimitry Andric : m_executable(), m_arguments(), m_environment(), m_uid(UINT32_MAX),
24435933ddSDimitry Andric m_gid(UINT32_MAX), m_arch(), m_pid(LLDB_INVALID_PROCESS_ID) {}
250127ef0fSEd Maste
ProcessInfo(const char * name,const ArchSpec & arch,lldb::pid_t pid)26435933ddSDimitry Andric ProcessInfo::ProcessInfo(const char *name, const ArchSpec &arch,
27435933ddSDimitry Andric lldb::pid_t pid)
28*b5893f02SDimitry Andric : m_executable(name), m_arguments(), m_environment(), m_uid(UINT32_MAX),
29*b5893f02SDimitry Andric m_gid(UINT32_MAX), m_arch(arch), m_pid(pid) {}
300127ef0fSEd Maste
Clear()31435933ddSDimitry Andric void ProcessInfo::Clear() {
320127ef0fSEd Maste m_executable.Clear();
330127ef0fSEd Maste m_arguments.Clear();
344ba319b5SDimitry Andric m_environment.clear();
350127ef0fSEd Maste m_uid = UINT32_MAX;
360127ef0fSEd Maste m_gid = UINT32_MAX;
370127ef0fSEd Maste m_arch.Clear();
380127ef0fSEd Maste m_pid = LLDB_INVALID_PROCESS_ID;
390127ef0fSEd Maste }
400127ef0fSEd Maste
GetName() const41435933ddSDimitry Andric const char *ProcessInfo::GetName() const {
420127ef0fSEd Maste return m_executable.GetFilename().GetCString();
430127ef0fSEd Maste }
440127ef0fSEd Maste
GetNameLength() const45435933ddSDimitry Andric size_t ProcessInfo::GetNameLength() const {
460127ef0fSEd Maste return m_executable.GetFilename().GetLength();
470127ef0fSEd Maste }
480127ef0fSEd Maste
Dump(Stream & s,Platform * platform) const49435933ddSDimitry Andric void ProcessInfo::Dump(Stream &s, Platform *platform) const {
504bb0738eSEd Maste s << "Executable: " << GetName() << "\n";
514bb0738eSEd Maste s << "Triple: ";
524bb0738eSEd Maste m_arch.DumpTriple(s);
534bb0738eSEd Maste s << "\n";
544bb0738eSEd Maste
554bb0738eSEd Maste s << "Arguments:\n";
564bb0738eSEd Maste m_arguments.Dump(s);
574bb0738eSEd Maste
584ba319b5SDimitry Andric s.Format("Environment:\n{0}", m_environment);
594bb0738eSEd Maste }
604bb0738eSEd Maste
SetExecutableFile(const FileSpec & exe_file,bool add_exe_file_as_first_arg)61435933ddSDimitry Andric void ProcessInfo::SetExecutableFile(const FileSpec &exe_file,
62435933ddSDimitry Andric bool add_exe_file_as_first_arg) {
63435933ddSDimitry Andric if (exe_file) {
640127ef0fSEd Maste m_executable = exe_file;
65435933ddSDimitry Andric if (add_exe_file_as_first_arg) {
66*b5893f02SDimitry Andric llvm::SmallString<128> filename;
67435933ddSDimitry Andric exe_file.GetPath(filename);
68435933ddSDimitry Andric if (!filename.empty())
690127ef0fSEd Maste m_arguments.InsertArgumentAtIndex(0, filename);
700127ef0fSEd Maste }
71435933ddSDimitry Andric } else {
720127ef0fSEd Maste m_executable.Clear();
730127ef0fSEd Maste }
740127ef0fSEd Maste }
750127ef0fSEd Maste
GetArg0() const76435933ddSDimitry Andric llvm::StringRef ProcessInfo::GetArg0() const {
77435933ddSDimitry Andric return m_arg0;
780127ef0fSEd Maste }
790127ef0fSEd Maste
SetArg0(llvm::StringRef arg)80435933ddSDimitry Andric void ProcessInfo::SetArg0(llvm::StringRef arg) {
810127ef0fSEd Maste m_arg0 = arg;
820127ef0fSEd Maste }
830127ef0fSEd Maste
SetArguments(char const ** argv,bool first_arg_is_executable)84435933ddSDimitry Andric void ProcessInfo::SetArguments(char const **argv,
85435933ddSDimitry Andric bool first_arg_is_executable) {
860127ef0fSEd Maste m_arguments.SetArguments(argv);
870127ef0fSEd Maste
880127ef0fSEd Maste // Is the first argument the executable?
89435933ddSDimitry Andric if (first_arg_is_executable) {
900127ef0fSEd Maste const char *first_arg = m_arguments.GetArgumentAtIndex(0);
91435933ddSDimitry Andric if (first_arg) {
924ba319b5SDimitry Andric // Yes the first argument is an executable, set it as the executable in
934ba319b5SDimitry Andric // the launch options. Don't resolve the file path as the path could be a
944ba319b5SDimitry Andric // remote platform path
95*b5893f02SDimitry Andric m_executable.SetFile(first_arg, FileSpec::Style::native);
960127ef0fSEd Maste }
970127ef0fSEd Maste }
980127ef0fSEd Maste }
994bb0738eSEd Maste
SetArguments(const Args & args,bool first_arg_is_executable)100435933ddSDimitry Andric void ProcessInfo::SetArguments(const Args &args, bool first_arg_is_executable) {
1010127ef0fSEd Maste // Copy all arguments
1020127ef0fSEd Maste m_arguments = args;
1030127ef0fSEd Maste
1040127ef0fSEd Maste // Is the first argument the executable?
105435933ddSDimitry Andric if (first_arg_is_executable) {
1060127ef0fSEd Maste const char *first_arg = m_arguments.GetArgumentAtIndex(0);
107435933ddSDimitry Andric if (first_arg) {
1084ba319b5SDimitry Andric // Yes the first argument is an executable, set it as the executable in
1094ba319b5SDimitry Andric // the launch options. Don't resolve the file path as the path could be a
1104ba319b5SDimitry Andric // remote platform path
111*b5893f02SDimitry Andric m_executable.SetFile(first_arg, FileSpec::Style::native);
1120127ef0fSEd Maste }
1130127ef0fSEd Maste }
1140127ef0fSEd Maste }
115