blob: 0d42e0e35b92e7e3691d7b6eb7092375efe03978 [file] [log] [blame]
Reid Spencer566ac282004-09-11 04:59:30 +00001//===-- Process.cpp - Implement OS Process Concept --------------*- C++ -*-===//
Misha Brukman10468d82005-04-21 22:55:34 +00002//
Reid Spencer566ac282004-09-11 04:59:30 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman10468d82005-04-21 22:55:34 +00007//
Reid Spencer566ac282004-09-11 04:59:30 +00008//===----------------------------------------------------------------------===//
9//
Hans Wennborgcfe341f2014-06-20 01:36:00 +000010// This file implements the operating system Process concept.
Reid Spencer566ac282004-09-11 04:59:30 +000011//
12//===----------------------------------------------------------------------===//
13
Ehsan Akhgari33d1ae52014-06-30 19:54:20 +000014#include "llvm/ADT/StringExtras.h"
Reid Spencer97d4a172004-12-27 06:15:29 +000015#include "llvm/Config/config.h"
Chandler Carruth97683aa2012-12-31 11:17:50 +000016#include "llvm/Support/ErrorHandling.h"
Ehsan Akhgari33d1ae52014-06-30 19:54:20 +000017#include "llvm/Support/FileSystem.h"
Chandler Carruthbe810232013-01-02 10:22:59 +000018#include "llvm/Support/Process.h"
Ehsan Akhgari33d1ae52014-06-30 19:54:20 +000019#include "llvm/Support/Program.h"
Reid Spencer566ac282004-09-11 04:59:30 +000020
Chandler Carruth5473dfb2012-12-31 11:45:20 +000021using namespace llvm;
Reid Spencer566ac282004-09-11 04:59:30 +000022using namespace sys;
23
24//===----------------------------------------------------------------------===//
25//=== WARNING: Implementation here must contain only TRULY operating system
Misha Brukman10468d82005-04-21 22:55:34 +000026//=== independent code.
Reid Spencer566ac282004-09-11 04:59:30 +000027//===----------------------------------------------------------------------===//
28
Chandler Carruth97683aa2012-12-31 11:17:50 +000029// Empty virtual destructor to anchor the vtable for the process class.
30process::~process() {}
31
32self_process *process::get_self() {
33 // Use a function local static for thread safe initialization and allocate it
34 // as a raw pointer to ensure it is never destroyed.
35 static self_process *SP = new self_process();
36
37 return SP;
38}
39
40// The destructor for the self_process subclass must never actually be
41// executed. There should be at most one instance of this class, and that
42// instance should live until the process terminates to avoid the potential for
43// racy accesses during shutdown.
44self_process::~self_process() {
45 llvm_unreachable("This destructor must never be executed!");
46}
47
Chandler Carruthef7f9682013-01-04 23:19:55 +000048/// \brief A helper function to compute the elapsed wall-time since the program
49/// started.
50///
51/// Note that this routine actually computes the elapsed wall time since the
52/// first time it was called. However, we arrange to have it called during the
53/// startup of the process to get approximately correct results.
54static TimeValue getElapsedWallTime() {
55 static TimeValue &StartTime = *new TimeValue(TimeValue::now());
56 return TimeValue::now() - StartTime;
57}
58
59/// \brief A special global variable to ensure we call \c getElapsedWallTime
60/// during global initialization of the program.
61///
62/// Note that this variable is never referenced elsewhere. Doing so could
63/// create race conditions during program startup or shutdown.
64static volatile TimeValue DummyTimeValue = getElapsedWallTime();
65
66// Implement this routine by using the static helpers above. They're already
67// portable.
68TimeValue self_process::get_wall_time() const {
69 return getElapsedWallTime();
70}
71
Ehsan Akhgari33d1ae52014-06-30 19:54:20 +000072Optional<std::string> Process::FindInEnvPath(const std::string& EnvName,
73 const std::string& FileName)
74{
75 Optional<std::string> FoundPath;
76 Optional<std::string> OptPath = Process::GetEnv(EnvName);
77 if (!OptPath.hasValue())
78 return FoundPath;
79
80 const char EnvPathSeparatorStr[] = {EnvPathSeparator, '\0'};
81 SmallVector<StringRef, 8> Dirs;
82 SplitString(OptPath.getValue(), Dirs, EnvPathSeparatorStr);
83
84 for (const auto &Dir : Dirs) {
85 if (Dir.empty())
86 continue;
87
88 SmallString<128> FilePath(Dir);
89 path::append(FilePath, FileName);
90 if (fs::exists(Twine(FilePath))) {
91 FoundPath = FilePath.str();
92 break;
93 }
94 }
95
96 return FoundPath;
97}
98
Chandler Carruthef7f9682013-01-04 23:19:55 +000099
Nico Rieck92d649a2013-09-11 00:36:48 +0000100#define COLOR(FGBG, CODE, BOLD) "\033[0;" BOLD FGBG CODE "m"
101
102#define ALLCOLORS(FGBG,BOLD) {\
103 COLOR(FGBG, "0", BOLD),\
104 COLOR(FGBG, "1", BOLD),\
105 COLOR(FGBG, "2", BOLD),\
106 COLOR(FGBG, "3", BOLD),\
107 COLOR(FGBG, "4", BOLD),\
108 COLOR(FGBG, "5", BOLD),\
109 COLOR(FGBG, "6", BOLD),\
110 COLOR(FGBG, "7", BOLD)\
111 }
112
113static const char colorcodes[2][2][8][10] = {
114 { ALLCOLORS("3",""), ALLCOLORS("3","1;") },
115 { ALLCOLORS("4",""), ALLCOLORS("4","1;") }
116};
117
Reid Spencer566ac282004-09-11 04:59:30 +0000118// Include the platform-specific parts of this class.
Reid Spencer97d4a172004-12-27 06:15:29 +0000119#ifdef LLVM_ON_UNIX
Reid Spencerc892a0d2005-01-09 23:29:00 +0000120#include "Unix/Process.inc"
Reid Spencer97d4a172004-12-27 06:15:29 +0000121#endif
122#ifdef LLVM_ON_WIN32
Michael J. Spencer447762d2010-11-29 18:16:10 +0000123#include "Windows/Process.inc"
Reid Spencer97d4a172004-12-27 06:15:29 +0000124#endif