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 */ \
118 TM(hackc_translate) \
139 TM(hhir_checkhoist) \
147 TM(hhir_lowerbespokes) \
152 TM(hhir_refineTmps) \
153 TM(hhir_checkTypes) \
155 TM(hhir_unreachable) \
209 TM(vasm_block_count) \
211 TM(vasm_graph_color) \
221 /* Stress categories, to exercise rare paths */ \
222 TM(stress_txInterpPct) \
223 TM(stress_txInterpSeed) \
224 /* Jit bisection interval */ \
227 /* Temporary categories, to save compilation time */ \
228 TM(tmp0) TM(tmp1) TM(tmp2) TM(tmp3) \
229 TM(tmp4) TM(tmp5) TM(tmp6) TM(tmp7) \
230 TM(tmp8) TM(tmp9) TM(tmp10) TM(tmp11) \
231 TM(tmp12) TM(tmp13) TM(tmp14) TM(tmp15)
241 //////////////////////////////////////////////////////////////////////
244 * S-expression style structured pretty-printing. Implement
245 * std::string pretty() const { }, with the convention that
246 * nested structures are notated as lisp-style trees:
248 * (<typename> field0 field1)
253 * The repetitve prettyNode() templates are intended to aid
254 * implementing pretty().
257 template<typename P1
>
258 std::string
prettyNode(const char* name
, const std::vector
<P1
>& vec
) {
260 std::string retval
= string("(") + string(name
) + string(" ");
261 for(size_t i
= 0; i
< vec
.size(); i
++) {
262 retval
+= vec
[i
].pretty();
263 if (i
!= vec
.size() - 1) {
264 retval
+= string(" ");
267 return retval
+ string(")");
270 template<typename P1
>
271 std::string
prettyNode(const char* name
, const P1
& p1
) {
273 return string("(") + string(name
) + string(" ") +
278 template<> std::string
prettyNode(const char* name
, const std::string
& s
);
280 template<typename P1
, typename P2
>
281 std::string
prettyNode(const char* name
, const P1
& p1
, const P2
& p2
) {
283 return string("(") + string(name
) + string(" ") +
284 p1
.pretty() + string(" ") + p2
.pretty() +
288 void traceRelease(ATTRIBUTE_PRINTF_STRING
const char*, ...)
289 ATTRIBUTE_PRINTF(1,2);
290 void traceRelease(const std::string
& s
);
292 template<typename
... Args
>
293 void ftraceRelease(Args
&&... args
) {
294 traceRelease("%s", folly::format(std::forward
<Args
>(args
)...).str().c_str());
297 // Trace to the global ring buffer and the normal TRACE destination.
298 #define TRACE_RB(n, ...) \
299 ONTRACE(n, HPHP::Trace::traceRingBufferRelease(__VA_ARGS__)); \
300 TRACE(n, __VA_ARGS__);
301 void traceRingBufferRelease(ATTRIBUTE_PRINTF_STRING
const char* fmt
, ...)
302 ATTRIBUTE_PRINTF(1,2);
304 extern int levels
[NumModules
];
305 extern __thread
int tl_levels
[NumModules
];
306 const char* moduleName(Module mod
);
307 inline bool moduleEnabledRelease(Module tm
, int level
= 1) {
308 return levels
[tm
] + tl_levels
[tm
] >= level
;
311 // Trace::Bump that is on for release tracing.
313 BumpRelease(Module mod
, int adjust
, bool condition
= true)
318 if (m_live
) tl_levels
[m_mod
] -= m_adjust
;
321 BumpRelease(BumpRelease
&& o
) noexcept
324 , m_adjust(o
.m_adjust
)
330 if (m_live
) tl_levels
[m_mod
] += m_adjust
;
333 BumpRelease
negate() const {
334 return BumpRelease
{ m_mod
, -m_adjust
, m_live
};
337 BumpRelease(const BumpRelease
&) = delete;
338 BumpRelease
& operator=(const BumpRelease
&) = delete;
346 CompactVector
<BumpRelease
> bumpSpec(folly::StringPiece traceSpec
);
348 //////////////////////////////////////////////////////////////////////
350 #if (!defined(NDEBUG) || defined(USE_TRACE)) /* { */
355 //////////////////////////////////////////////////////////////////////
357 * Implementation of for when tracing is enabled.
360 inline bool moduleEnabled(Module tm
, int level
= 1) {
361 return moduleEnabledRelease(tm
, level
);
364 inline int moduleLevel(Module tm
) { return levels
[tm
]; }
368 const bool enabled
= true;
370 #define ONTRACE_MOD(module, n, x) do { \
371 if (HPHP::Trace::moduleEnabled(module, n)) { \
375 #define ONTRACE(n, x) ONTRACE_MOD(TRACEMOD, n, x)
377 #define TRACE(n, ...) ONTRACE(n, HPHP::Trace::trace(__VA_ARGS__))
378 #define FTRACE(n, ...) \
379 ONTRACE(n, HPHP::Trace::trace("%s", \
380 folly::format(__VA_ARGS__).str().c_str()))
381 #define TRACE_MOD(mod, level, ...) \
382 ONTRACE_MOD(mod, level, HPHP::Trace::trace(__VA_ARGS__))
383 #define FTRACE_MOD(mod, level, ...) \
384 ONTRACE_MOD(mod, level, HPHP::Trace::trace("%s", \
385 folly::format(__VA_ARGS__).str().c_str()))
386 #define TRACE_SET_MOD(name) \
387 UNUSED static const HPHP::Trace::Module TRACEMOD = HPHP::Trace::name;
390 * The Indent struct and ITRACE are used for tracing with nested
391 * indentation. Create an Indent object on the stack to increase the nesting
392 * level, then use ITRACE just as you would use FTRACE.
394 extern __thread
int indentDepth
;
396 explicit Indent(int n
= 2) : n(n
) { indentDepth
+= n
; }
397 ~Indent() { indentDepth
-= n
; }
402 // See doc comment above for usage.
403 using Bump
= BumpRelease
;
405 inline std::string
indent() {
406 return std::string(indentDepth
, ' ');
409 template<typename
... Args
>
410 inline void itraceImpl(const char* fmtRaw
, Args
&&... args
) {
411 auto const fmt
= indent() + fmtRaw
;
412 Trace::ftraceRelease(fmt
, std::forward
<Args
>(args
)...);
414 #define ITRACE(level, ...) ONTRACE((level), Trace::itraceImpl(__VA_ARGS__));
415 #define ITRACE_MOD(mod, level, ...) \
416 ONTRACE_MOD(mod, level, Trace::itraceImpl(__VA_ARGS__));
418 void trace(ATTRIBUTE_PRINTF_STRING
const char *, ...) ATTRIBUTE_PRINTF(1,2);
419 void trace(const std::string
&);
421 template<typename Pretty
>
422 inline void trace(Pretty p
) { trace(p
.pretty() + std::string("\n")); }
424 void vtrace(ATTRIBUTE_PRINTF_STRING
const char *fmt
, va_list args
)
425 ATTRIBUTE_PRINTF(1,0);
426 void dumpRingbuffer();
428 // Ensure a tracing output file has been opened.
429 void ensureInit(std::string outFile
);
430 // Set tracing levels for this thread using a module:level,... specification.
431 // If traceSpec is empty, all levels for this thread are zeroed.
432 void setTraceThread(folly::StringPiece traceSpec
);
434 //////////////////////////////////////////////////////////////////////
436 #else /* } (!defined(NDEBUG) || defined(USE_TRACE)) { */
438 //////////////////////////////////////////////////////////////////////
440 * Implementation for when tracing is disabled.
443 #define ONTRACE(...) do { } while (0)
444 #define TRACE(...) do { } while (0)
445 #define FTRACE(...) do { } while (0)
446 #define ONTRACE_MOD(...) do { } while (0)
447 #define TRACE_MOD(...) do { } while (0)
448 #define FTRACE_MOD(...) do { } while (0)
449 #define TRACE_SET_MOD(name) \
450 DEBUG_ONLY static const HPHP::Trace::Module TRACEMOD = HPHP::Trace::name;
452 #define ITRACE(...) do { } while (0)
453 #define ITRACE_MOD(...) do { } while (0)
456 always_assert(true && "If this struct is completely empty we get unused "
457 "variable warnings in code that uses it.");
460 inline std::string
indent() {
461 return std::string();
465 Bump(Module
/*mod*/, int /*adjust*/, bool /*condition*/ = true) {
466 always_assert(true && "If this struct is completely empty we get unused "
467 "variable warnings in code that uses it.");
471 const bool enabled
= false;
473 inline void trace(const char*, ...) {}
474 inline void trace(const std::string
&) {}
475 inline void vtrace(const char*, va_list) {}
476 inline bool moduleEnabled(Module
/*t*/, int /*level*/ = 1) {
479 inline int moduleLevel(Module
/*tm*/) {
482 inline void ensureInit(std::string
/*outFile*/) {}
483 inline void setTraceThread(const std::string
& /*traceSpec*/) {}
485 //////////////////////////////////////////////////////////////////////
487 #endif /* } (!defined(NDEBUG) || defined(USE_TRACE)) */
491 // Optional color utility for trace dumps; when output is a tty or
492 // when we've been told to assume it is.
493 inline const char* color(const char* color
) {
494 static auto const shouldColorize
= []() -> bool {
495 auto const traceEnv
= getenv("HPHP_TRACE_FILE");
496 auto const assumeTTY
= getenv("HPHP_TRACE_TTY");
497 if (assumeTTY
) return true;
498 if (!traceEnv
) return false;
500 !strcmp(traceEnv
, "/dev/stdout") ? isatty(1) :
501 !strcmp(traceEnv
, "/dev/stderr") ? isatty(2) :
504 return shouldColorize
? color
: "";
507 inline std::string
color(const char* fg
, const char* bg
) {
508 auto const s
= add_bgcolor(fg
, bg
);
509 return color(s
.c_str());
512 //////////////////////////////////////////////////////////////////////
514 FOLLY_CREATE_MEMBER_INVOKER(invoke_toString
, toString
);
519 template<typename Val
>
520 class FormatValue
<Val
,
522 std::is_invocable_v
<HPHP::invoke_toString
, Val
const> &&
523 // This is here because MSVC decides that StringPiece matches
524 // both this overload as well as the FormatValue overload for
525 // string-y types in folly itself.
526 !std::is_same
<Val
, StringPiece
>::value
529 explicit FormatValue(const Val
& val
) : m_val(val
) {}
531 template<typename Callback
> void format(FormatArg
& arg
, Callback
& cb
) const {
532 format_value::formatString(m_val
.toString(), arg
, cb
);