Remove typedef decl errors
[hiphop-php.git] / hphp / util / trace.cpp
blob7d4b0410a41166494750864b508467ea0775c6ac
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
17 // We can't do this on MSVC, it's all debug or all release.
18 #ifndef _MSC_VER
20 * Forcibly define USE_TRACE, so we get the debug trace.h interface included
21 * here. This allows mixed compilation, where some units were compiled
22 * DEBUG and others compiled RELEASE, to successfully link.
24 #ifndef USE_TRACE
25 # define USE_TRACE 1
26 #endif
27 #endif
28 #include "hphp/util/trace.h"
30 #include <iostream>
31 #include <string.h>
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <stdarg.h>
35 #include <string>
37 #include <folly/portability/PThread.h>
39 #include "hphp/util/ringbuffer.h"
41 namespace HPHP {
43 TRACE_SET_MOD(tprefix);
45 namespace Trace {
47 int levels[NumModules];
48 __thread int tl_levels[NumModules];
49 __thread int indentDepth = 0;
51 static FILE* out{nullptr};
53 static const char *tokNames[] = {
54 #define TM(x) #x,
55 TRACE_MODULES
56 #undef TM
59 namespace {
61 const char* kPIDPlaceholder = "%{pid}";
64 * Dummy class to get some code to run before main().
66 struct Init {
67 private:
68 static Module name2mod(folly::StringPiece name) {
69 for (int i = 0; i < NumModules; i++) {
70 if (name == tokNames[i]) {
71 return (Module)i;
74 return (Module)-1;
77 public:
78 Init() {
79 /* Parse the environment for flags. */
80 const char *envName = "TRACE";
81 const char *env = getenv(envName);
82 if (env) {
83 EnsureInitFile(getenv("HPHP_TRACE_FILE"));
84 InitFromSpec(env, levels);
85 } else {
86 // If TRACE env var is not set, nothing should be traced...
87 // but if it does, use stderr.
88 out = stderr;
92 static void EnsureInitFile(const char* file) {
93 if (out && out != stderr) return;
95 auto const filename = [&] () -> std::string {
96 if (!file) return "/tmp/hphp.log";
97 std::string result{file};
98 size_t idx;
99 if ((idx = result.find(kPIDPlaceholder)) != std::string::npos) {
100 result.replace(idx, strlen(kPIDPlaceholder), std::to_string(getpid()));
102 return result;
103 }();
105 out = fopen(filename.c_str(),
106 getenv("HPHP_TRACE_APPEND") ? "a" : "w");
107 if (!out) {
108 fprintf(
109 stderr,
110 "could not create log file (%s); using stderr\n",
111 filename.c_str()
113 out = stderr;
117 static void InitFromSpec(folly::StringPiece spec, int* levels) {
118 std::vector<folly::StringPiece> pieces;
119 folly::split(",", spec, pieces);
120 for (auto piece : pieces) {
121 folly::StringPiece moduleName;
122 int level;
123 try {
124 if (!folly::split(":", piece, moduleName, level)) {
125 moduleName = piece;
126 level = 1;
128 } catch (const std::exception& re) {
129 std::cerr <<
130 folly::format("Ignoring invalid TRACE component: {}\n", piece);
131 continue;
134 int mod = name2mod(moduleName);
135 if (mod >= 0) levels[mod] = level;
137 static auto const groups = {
138 Trace::minstr,
139 Trace::interpOne,
140 Trace::dispatchBB,
141 Trace::decreftype,
143 for (auto g : groups) {
144 if (mod == g) {
145 levels[Trace::statgroups] = std::max(levels[Trace::statgroups], 1);
146 break;
153 Init i;
157 const char* moduleName(Module mod) {
158 return tokNames[mod];
161 void flush() {
162 if (!moduleEnabledRelease(Trace::traceAsync)) {
163 fflush(out);
167 #ifdef USE_TRACE
168 void ensureInit(std::string outFile) {
169 Init::EnsureInitFile(outFile.c_str());
172 void setTraceThread(folly::StringPiece traceSpec) {
173 for (auto& level : tl_levels) level = 0;
174 Init::InitFromSpec(traceSpec, tl_levels);
177 CompactVector<BumpRelease> bumpSpec(folly::StringPiece traceSpec) {
178 std::array<int, NumModules> modules{};
180 Init::InitFromSpec(traceSpec, modules.data());
181 CompactVector<BumpRelease> result;
182 for (int i = 0; i < NumModules; i++) {
183 if (modules[i]) {
184 result.emplace_back(static_cast<Module>(i), -modules[i]);
187 return result;
190 void vtrace(const char *fmt, va_list ap) {
191 static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
192 static bool hphp_trace_ringbuffer = getenv("HPHP_TRACE_RINGBUFFER");
193 if (hphp_trace_ringbuffer) {
194 vtraceRingbuffer(fmt, ap);
195 } else {
196 ONTRACE(1, pthread_mutex_lock(&mtx));
197 ONTRACE(1, fprintf(out, "t%#08x: ",
198 int((int64_t)pthread_self() & 0xFFFFFFFF)));
199 vfprintf(out, fmt, ap);
200 ONTRACE(1, pthread_mutex_unlock(&mtx));
201 flush();
205 void trace(const char *fmt, ...) {
206 va_list ap;
207 va_start(ap, fmt);
208 vtrace(fmt, ap);
209 va_end(ap);
211 #endif
213 void traceRelease(const char* fmt, ...) {
214 va_list ap;
215 va_start(ap, fmt);
216 vtrace(fmt, ap);
217 va_end(ap);
220 void traceRingBufferRelease(const char *fmt, ...) {
221 va_list ap;
222 va_start(ap, fmt);
223 vtraceRingbuffer(fmt, ap);
224 va_end(ap);
227 #ifdef USE_TRACE
228 void trace(const std::string& s) {
229 trace("%s", s.c_str());
231 #endif
233 void traceRelease(const std::string& s) {
234 traceRelease("%s", s.c_str());
237 template<>
238 std::string prettyNode(const char* name, const std::string& s) {
239 using std::string;
240 return string("(") + string(name) + string(" ") +
242 string(")");
245 } } // HPHP::Trace