summaryrefslogtreecommitdiffstats
path: root/tools/isolate/trace_child_process.py
blob: 24e8992cc7d4e826ffbf2b64dda7c1bd4c7349a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/env python
# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Executes with the command to be run and optionally waits for the go signal.

Not meant to be used directly, only meant to be used by trace_inputs.py.

This script is used by the tracer as a signal for the log parser that the child
process is the process we care about. It is especially important on kernel based
tracer because we want it to trace the relevant process tree.
"""

import subprocess
import sys


def fix_python_path(cmd):
  """Returns the fixed command line to call the right python executable."""
  out = cmd[:]
  if out[0] == 'python':
    out[0] = sys.executable
  elif out[0].endswith('.py'):
    out.insert(0, sys.executable)
  return out


def main():
  cmd = fix_python_path(sys.argv[2:])

  # The reason os.execve() is not used is that we don't want the modules
  # imported here to influence the executable being traced, so we need a fresh
  # pid and need to fork.
  # TODO(maruel): Use CreateProcess() on Windows and fork manually on OSX.
  return subprocess.call(cmd)


if __name__ == '__main__':
  sys.exit(main())