11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci// This tests that tracing can be enabled dynamically with the
41cb0ef41Sopenharmony_ci// trace_events module.
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciconst common = require('../common');
71cb0ef41Sopenharmony_citry {
81cb0ef41Sopenharmony_ci  require('trace_events');
91cb0ef41Sopenharmony_ci} catch {
101cb0ef41Sopenharmony_ci  common.skip('missing trace events');
111cb0ef41Sopenharmony_ci}
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ciconst assert = require('assert');
141cb0ef41Sopenharmony_ciconst cp = require('child_process');
151cb0ef41Sopenharmony_ciconst fs = require('fs');
161cb0ef41Sopenharmony_ciconst path = require('path');
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ciconst enable = `require("trace_events").createTracing(
191cb0ef41Sopenharmony_ci{ categories: ["node.async_hooks"] }).enable();`;
201cb0ef41Sopenharmony_ciconst code =
211cb0ef41Sopenharmony_ci  'setTimeout(() => { for (let i = 0; i < 100000; i++) { "test" + i } }, 1)';
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir');
241cb0ef41Sopenharmony_ciconst filename = path.join(tmpdir.path, 'node_trace.1.log');
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_citmpdir.refresh();
271cb0ef41Sopenharmony_ciconst proc = cp.spawnSync(
281cb0ef41Sopenharmony_ci  process.execPath,
291cb0ef41Sopenharmony_ci  ['-e', enable + code ],
301cb0ef41Sopenharmony_ci  {
311cb0ef41Sopenharmony_ci    cwd: tmpdir.path,
321cb0ef41Sopenharmony_ci    env: { ...process.env,
331cb0ef41Sopenharmony_ci           'NODE_DEBUG_NATIVE': 'tracing',
341cb0ef41Sopenharmony_ci           'NODE_DEBUG': 'tracing' }
351cb0ef41Sopenharmony_ci  });
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ciconsole.log('process exit with signal:', proc.signal);
381cb0ef41Sopenharmony_ciconsole.log('process stderr:', proc.stderr.toString());
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ciassert.strictEqual(proc.status, 0);
411cb0ef41Sopenharmony_ciassert(fs.existsSync(filename));
421cb0ef41Sopenharmony_ciconst data = fs.readFileSync(filename, 'utf-8');
431cb0ef41Sopenharmony_ciconst traces = JSON.parse(data).traceEvents;
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_cifunction filterTimeoutTraces(trace) {
461cb0ef41Sopenharmony_ci  if (trace.pid !== proc.pid)
471cb0ef41Sopenharmony_ci    return false;
481cb0ef41Sopenharmony_ci  if (trace.cat !== 'node,node.async_hooks')
491cb0ef41Sopenharmony_ci    return false;
501cb0ef41Sopenharmony_ci  if (trace.name !== 'Timeout')
511cb0ef41Sopenharmony_ci    return false;
521cb0ef41Sopenharmony_ci  return true;
531cb0ef41Sopenharmony_ci}
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci{
561cb0ef41Sopenharmony_ci  const timeoutTraces = traces.filter(filterTimeoutTraces);
571cb0ef41Sopenharmony_ci  assert.notDeepStrictEqual(timeoutTraces, []);
581cb0ef41Sopenharmony_ci  const threads = new Set();
591cb0ef41Sopenharmony_ci  for (const trace of timeoutTraces) {
601cb0ef41Sopenharmony_ci    threads.add(trace.tid);
611cb0ef41Sopenharmony_ci  }
621cb0ef41Sopenharmony_ci  assert.notDeepStrictEqual(timeoutTraces, []);
631cb0ef41Sopenharmony_ci}
64