3 +----------------------------------------------------------------------+
5 +----------------------------------------------------------------------+
6 | Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
7 +----------------------------------------------------------------------+
8 | This source file is subject to version 3.01 of the PHP license, |
9 | that is bundled with this package in the file LICENSE, and is |
10 | available through the world-wide-web at the following url: |
11 | http://www.php.net/license/3_01.txt |
12 | If you did not receive a copy of the PHP license and are unable to |
13 | obtain it through the world-wide-web, please send a note to |
14 | license@php.net so we can mail you a copy immediately. |
15 +----------------------------------------------------------------------+
24 #include <folly/Format.h>
25 #include <folly/functional/Invoke.h>
26 #include <folly/portability/Unistd.h>
28 #include "hphp/util/assertions.h"
29 #include "hphp/util/compact-vector.h"
30 #include "hphp/util/portability.h"
31 #include "hphp/util/text-color.h"
34 * Runtime-selectable trace facility. A trace statement has both a module and a
35 * level associated with it; Enable tracing a module by setting the TRACE
36 * environment variable to a comma-separated list of module:level pairs. E.g.:
38 * env TRACE=mcg:1,bcinterp:3,tmp0:1 ./hhvm/hhvm ...
40 * In a source file, select the compilation unit's module by calling the
41 * TRACE_SET_MOD macro. E.g.,
46 * TRACE(0, "See this for any trace-enabled build: %d\n", foo);
47 * TRACE(1, "Trace-level must be 1 or higher for this one\n");
49 * While the levels are arbitrary integers, code so far is following a
50 * rough convention of 1-5, where 1 is minimal and 5 is quite verbose.
52 * Normally trace information is printed to /tmp/hphp.log. You can
53 * override the environment variable HPHP_TRACE_FILE to change
54 * this. (Note you can set it to something like /dev/stderr or
55 * /dev/stdout if you want the logs printed to your terminal).
57 * When printing to the terminal, some traces will know how to use
58 * colorization. You can set HPHP_TRACE_TTY to tell the tracing
59 * facility to assume it should colorize even if the output file isn't
64 * Trace levels can be bumped on a per-module, per-scope basis. This
65 * lets you run code that has a set of trace levels in a mode as if
66 * they were all higher.
71 * Trace::Bump bumper{Trace::mcg, 2};
72 * FTRACE(1, "asd\n"); // only fires at level >= 3
74 * FTRACE(1, "asd\n"); // back to normal
77 * There is also support for conditionally bumping in the bumper:
80 * Trace::Bump bumper{Trace::mcg, 2, somePredicate(foo)};
81 * // Only bumped if somePredicate(foo) returned true.
84 * Note however that if you use that form, `somePredicate' will be
85 * evaluated even if tracing is off.
91 #define TRACE_MODULES \
92 TM(tprefix) /* Meta: prefix with string */ \
93 TM(traceAsync) /* Meta: lazy writes to disk */ \
112 TM(extern_compiler) \
138 TM(hhir_checkhoist) \
149 TM(hhir_refineTmps) \
151 TM(hhir_unreachable) \
202 TM(vasm_block_count) \
204 TM(vasm_graph_color) \
214 /* Stress categories, to exercise rare paths */ \
215 TM(stress_txInterpPct) \
216 TM(stress_txInterpSeed) \
217 /* Jit bisection interval */ \
220 /* Temporary categories, to save compilation time */ \
221 TM(tmp0) TM(tmp1) TM(tmp2) TM(tmp3) \
222 TM(tmp4) TM(tmp5) TM(tmp6) TM(tmp7) \
223 TM(tmp8) TM(tmp9) TM(tmp10) TM(tmp11) \
224 TM(tmp12) TM(tmp13) TM(tmp14) TM(tmp15)
234 //////////////////////////////////////////////////////////////////////
237 * S-expression style structured pretty-printing. Implement
238 * std::string pretty() const { }, with the convention that
239 * nested structures are notated as lisp-style trees:
241 * (<typename> field0 field1)
246 * The repetitve prettyNode() templates are intended to aid
247 * implementing pretty().
250 template<typename P1
>
251 std::string
prettyNode(const char* name
, const std::vector
<P1
>& vec
) {
253 std::string retval
= string("(") + string(name
) + string(" ");
254 for(size_t i
= 0; i
< vec
.size(); i
++) {
255 retval
+= vec
[i
].pretty();
256 if (i
!= vec
.size() - 1) {
257 retval
+= string(" ");
260 return retval
+ string(")");
263 template<typename P1
>
264 std::string
prettyNode(const char* name
, const P1
& p1
) {
266 return string("(") + string(name
) + string(" ") +
271 template<> std::string
prettyNode(const char* name
, const std::string
& s
);
273 template<typename P1
, typename P2
>
274 std::string
prettyNode(const char* name
, const P1
& p1
, const P2
& p2
) {
276 return string("(") + string(name
) + string(" ") +
277 p1
.pretty() + string(" ") + p2
.pretty() +
281 void traceRelease(ATTRIBUTE_PRINTF_STRING
const char*, ...)
282 ATTRIBUTE_PRINTF(1,2);
283 void traceRelease(const std::string
& s
);
285 template<typename
... Args
>
286 void ftraceRelease(Args
&&... args
) {
287 traceRelease("%s", folly::format(std::forward
<Args
>(args
)...).str().c_str());
290 // Trace to the global ring buffer and the normal TRACE destination.
291 #define TRACE_RB(n, ...) \
292 ONTRACE(n, HPHP::Trace::traceRingBufferRelease(__VA_ARGS__)); \
293 TRACE(n, __VA_ARGS__);
294 void traceRingBufferRelease(ATTRIBUTE_PRINTF_STRING
const char* fmt
, ...)
295 ATTRIBUTE_PRINTF(1,2);
297 extern int levels
[NumModules
];
298 extern __thread
int tl_levels
[NumModules
];
299 const char* moduleName(Module mod
);
300 inline bool moduleEnabledRelease(Module tm
, int level
= 1) {
301 return levels
[tm
] + tl_levels
[tm
] >= level
;
304 // Trace::Bump that is on for release tracing.
306 BumpRelease(Module mod
, int adjust
, bool condition
= true)
311 if (m_live
) tl_levels
[m_mod
] -= m_adjust
;
314 BumpRelease(BumpRelease
&& o
) noexcept
317 , m_adjust(o
.m_adjust
)
323 if (m_live
) tl_levels
[m_mod
] += m_adjust
;
326 BumpRelease
negate() const {
327 return BumpRelease
{ m_mod
, -m_adjust
, m_live
};
330 BumpRelease(const BumpRelease
&) = delete;
331 BumpRelease
& operator=(const BumpRelease
&) = delete;
339 CompactVector
<BumpRelease
> bumpSpec(folly::StringPiece traceSpec
);
341 //////////////////////////////////////////////////////////////////////
343 #if (!defined(NDEBUG) || defined(USE_TRACE)) /* { */
348 //////////////////////////////////////////////////////////////////////
350 * Implementation of for when tracing is enabled.
353 inline bool moduleEnabled(Module tm
, int level
= 1) {
354 return moduleEnabledRelease(tm
, level
);
357 inline int moduleLevel(Module tm
) { return levels
[tm
]; }
361 const bool enabled
= true;
363 #define ONTRACE_MOD(module, n, x) do { \
364 if (HPHP::Trace::moduleEnabled(module, n)) { \
368 #define ONTRACE(n, x) ONTRACE_MOD(TRACEMOD, n, x)
370 #define TRACE(n, ...) ONTRACE(n, HPHP::Trace::trace(__VA_ARGS__))
371 #define FTRACE(n, ...) \
372 ONTRACE(n, HPHP::Trace::trace("%s", \
373 folly::format(__VA_ARGS__).str().c_str()))
374 #define TRACE_MOD(mod, level, ...) \
375 ONTRACE_MOD(mod, level, HPHP::Trace::trace(__VA_ARGS__))
376 #define FTRACE_MOD(mod, level, ...) \
377 ONTRACE_MOD(mod, level, HPHP::Trace::trace("%s", \
378 folly::format(__VA_ARGS__).str().c_str()))
379 #define TRACE_SET_MOD(name) \
380 UNUSED static const HPHP::Trace::Module TRACEMOD = HPHP::Trace::name;
383 * The Indent struct and ITRACE are used for tracing with nested
384 * indentation. Create an Indent object on the stack to increase the nesting
385 * level, then use ITRACE just as you would use FTRACE.
387 extern __thread
int indentDepth
;
389 explicit Indent(int n
= 2) : n(n
) { indentDepth
+= n
; }
390 ~Indent() { indentDepth
-= n
; }
395 // See doc comment above for usage.
396 using Bump
= BumpRelease
;
398 inline std::string
indent() {
399 return std::string(indentDepth
, ' ');
402 template<typename
... Args
>
403 inline void itraceImpl(const char* fmtRaw
, Args
&&... args
) {
404 auto const fmt
= indent() + fmtRaw
;
405 Trace::ftraceRelease(fmt
, std::forward
<Args
>(args
)...);
407 #define ITRACE(level, ...) ONTRACE((level), Trace::itraceImpl(__VA_ARGS__));
408 #define ITRACE_MOD(mod, level, ...) \
409 ONTRACE_MOD(mod, level, Trace::itraceImpl(__VA_ARGS__));
411 void trace(ATTRIBUTE_PRINTF_STRING
const char *, ...) ATTRIBUTE_PRINTF(1,2);
412 void trace(const std::string
&);
414 template<typename Pretty
>
415 inline void trace(Pretty p
) { trace(p
.pretty() + std::string("\n")); }
417 void vtrace(ATTRIBUTE_PRINTF_STRING
const char *fmt
, va_list args
)
418 ATTRIBUTE_PRINTF(1,0);
419 void dumpRingbuffer();
421 // Ensure a tracing output file has been opened.
422 void ensureInit(std::string outFile
);
423 // Set tracing levels for this thread using a module:level,... specification.
424 // If traceSpec is empty, all levels for this thread are zeroed.
425 void setTraceThread(folly::StringPiece traceSpec
);
427 //////////////////////////////////////////////////////////////////////
429 #else /* } (!defined(NDEBUG) || defined(USE_TRACE)) { */
431 //////////////////////////////////////////////////////////////////////
433 * Implementation for when tracing is disabled.
436 #define ONTRACE(...) do { } while (0)
437 #define TRACE(...) do { } while (0)
438 #define FTRACE(...) do { } while (0)
439 #define ONTRACE_MOD(...) do { } while (0)
440 #define TRACE_MOD(...) do { } while (0)
441 #define FTRACE_MOD(...) do { } while (0)
442 #define TRACE_SET_MOD(name) \
443 DEBUG_ONLY static const HPHP::Trace::Module TRACEMOD = HPHP::Trace::name;
445 #define ITRACE(...) do { } while (0)
446 #define ITRACE_MOD(...) do { } while (0)
449 always_assert(true && "If this struct is completely empty we get unused "
450 "variable warnings in code that uses it.");
453 inline std::string
indent() {
454 return std::string();
458 Bump(Module
/*mod*/, int /*adjust*/, bool /*condition*/ = true) {
459 always_assert(true && "If this struct is completely empty we get unused "
460 "variable warnings in code that uses it.");
464 const bool enabled
= false;
466 inline void trace(const char*, ...) {}
467 inline void trace(const std::string
&) {}
468 inline void vtrace(const char*, va_list) {}
469 inline bool moduleEnabled(Module
/*t*/, int /*level*/ = 1) {
472 inline int moduleLevel(Module
/*tm*/) {
475 inline void ensureInit(std::string
/*outFile*/) {}
476 inline void setTraceThread(const std::string
& /*traceSpec*/) {}
478 //////////////////////////////////////////////////////////////////////
480 #endif /* } (!defined(NDEBUG) || defined(USE_TRACE)) */
484 // Optional color utility for trace dumps; when output is a tty or
485 // when we've been told to assume it is.
486 inline const char* color(const char* color
) {
487 static auto const shouldColorize
= []() -> bool {
488 auto const traceEnv
= getenv("HPHP_TRACE_FILE");
489 auto const assumeTTY
= getenv("HPHP_TRACE_TTY");
490 if (assumeTTY
) return true;
491 if (!traceEnv
) return false;
493 !strcmp(traceEnv
, "/dev/stdout") ? isatty(1) :
494 !strcmp(traceEnv
, "/dev/stderr") ? isatty(2) :
497 return shouldColorize
? color
: "";
500 inline std::string
color(const char* fg
, const char* bg
) {
501 auto const s
= add_bgcolor(fg
, bg
);
502 return color(s
.c_str());
505 //////////////////////////////////////////////////////////////////////
507 FOLLY_CREATE_MEMBER_INVOKER(invoke_toString
, toString
);
512 template<typename Val
>
513 class FormatValue
<Val
,
515 std::is_invocable_v
<HPHP::invoke_toString
, Val
const> &&
516 // This is here because MSVC decides that StringPiece matches
517 // both this overload as well as the FormatValue overload for
518 // string-y types in folly itself.
519 !std::is_same
<Val
, StringPiece
>::value
522 explicit FormatValue(const Val
& val
) : m_val(val
) {}
524 template<typename Callback
> void format(FormatArg
& arg
, Callback
& cb
) const {
525 format_value::formatString(m_val
.toString(), arg
, cb
);