summaryrefslogtreecommitdiffstats
path: root/build/android/valgrind_tools.py
blob: bc917e62546e92739b7e484b667b860a925e2346 (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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# Copyright (c) 2011 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.

"""
Classes in this file define additional actions that need to be taken to run a
test under some kind of runtime error detection tool.

The interface is intended to be used as follows.

1. For tests that simply run a native process (i.e. no activity is spawned):

Call tool.CopyFiles().
Prepend test command line with tool.GetTestWrapper().

2. For tests that spawn an activity:

Call tool.CopyFiles().
Call tool.SetupEnvironment().
Run the test as usual.
Call tool.CleanUpEnvironment().
"""

import os.path
import sys

from run_tests_helper import CHROME_DIR


class BaseTool(object):
  """A tool that does nothing."""

  def __init__(self, *args, **kwargs):
    pass

  def GetTestWrapper(self):
    """Returns a string that is to be prepended to the test command line."""
    return ''

  def CopyFiles(self):
    """Copies tool-specific files to the device, create directories, etc."""
    pass

  def SetupEnvironment(self):
    """Sets up the system environment for a test.

    This is a good place to set system properties.
    """
    pass

  def CleanUpEnvironment(self):
    """Cleans up environment."""
    pass

  def GetTimeoutScale(self):
    """Returns a multiplier that should be applied to timeout values."""
    return 1.0

  def NeedsDebugInfo(self):
    """Whether this tool requires debug info.

    Returns True if this tool can not work with stripped binaries.
    """
    return False


class ValgrindTool(BaseTool):
  """Base abstract class for Valgrind tools."""

  VG_DIR = '/data/local/tmp/valgrind'
  VGLOGS_DIR = '/data/local/tmp/vglogs'

  def __init__(self, adb, renderer=False):
    self.adb = adb
    if renderer:
      # exactly 31 chars, SystemProperties::PROP_NAME_MAX
      self.wrap_property = 'wrap.com.android.chrome:sandbox'
    else:
      self.wrap_property = 'wrap.com.android.chrome'

  def CopyFiles(self):
    """Copies Valgrind tools to the device."""
    self.adb.RunShellCommand('rm -r %s; mkdir %s' %
                             (ValgrindTool.VG_DIR, ValgrindTool.VG_DIR))
    self.adb.RunShellCommand('rm -r %s; mkdir %s' %
                             (ValgrindTool.VGLOGS_DIR, ValgrindTool.VGLOGS_DIR))
    files = self.GetFilesForTool()
    for f in files:
      self.adb.PushIfNeeded(os.path.join(CHROME_DIR, f),
                            os.path.join(ValgrindTool.VG_DIR,
                                         os.path.basename(f)))

  def SetupEnvironment(self):
    """Sets up device environment."""
    self.adb.RunShellCommand('chmod 777 /data/local/tmp')
    self.adb.RunShellCommand('setprop %s "logwrapper %s"' % (
        self.wrap_property, self.GetTestWrapper()))
    self.adb.RunShellCommand('setprop chrome.timeout_scale %f' % (
        self.GetTimeoutScale()))

  def CleanUpEnvironment(self):
    """Cleans up device environment."""
    self.adb.RunShellCommand('setprop %s ""' % (self.wrap_property,))
    self.adb.RunShellCommand('setprop chrome.timeout_scale ""')

  def GetFilesForTool(self):
    """Returns a list of file names for the tool."""
    raise NotImplementedError()

  def NeedsDebugInfo(self):
    """Whether this tool requires debug info.

    Returns True if this tool can not work with stripped binaries.
    """
    return True


class MemcheckTool(ValgrindTool):
  """Memcheck tool."""

  def __init__(self, adb, renderer=False):
    super(MemcheckTool, self).__init__(adb, renderer)

  def GetFilesForTool(self):
    """Returns a list of file names for the tool."""
    return ['tools/valgrind/android/vg-chrome-wrapper.sh',
            'tools/valgrind/memcheck/suppressions.txt',
            'tools/valgrind/memcheck/suppressions_android.txt']

  def GetTestWrapper(self):
    """Returns a string that is to be prepended to the test command line."""
    return ValgrindTool.VG_DIR + '/' + 'vg-chrome-wrapper.sh'

  def GetTimeoutScale(self):
    """Returns a multiplier that should be applied to timeout values."""
    return 30


class TSanTool(ValgrindTool):
  """ThreadSanitizer tool. See http://code.google.com/p/data-race-test ."""

  def __init__(self, adb, renderer=False):
    super(TSanTool, self).__init__(adb, renderer)

  def GetFilesForTool(self):
    """Returns a list of file names for the tool."""
    return ['tools/valgrind/android/vg-chrome-wrapper-tsan.sh',
            'tools/valgrind/tsan/suppressions.txt',
            'tools/valgrind/tsan/suppressions_android.txt',
            'tools/valgrind/tsan/ignores.txt']

  def GetTestWrapper(self):
    """Returns a string that is to be prepended to the test command line."""
    return ValgrindTool.VG_DIR + '/' + 'vg-chrome-wrapper-tsan.sh'

  def GetTimeoutScale(self):
    """Returns a multiplier that should be applied to timeout values."""
    return 30


TOOL_REGISTRY = {
  'memcheck': lambda x: MemcheckTool(x, False),
  'memcheck-renderer': lambda x: MemcheckTool(x, True),
  'tsan': lambda x: TSanTool(x, False),
  'tsan-renderer': lambda x: TSanTool(x, True)
}


def CreateTool(tool_name, adb):
  """Creates a tool with the specified tool name.

  Args:
    tool_name: Name of the tool to create.
    adb: ADB interface the tool will use.
  """
  if not tool_name:
    return BaseTool()

  ctor = TOOL_REGISTRY.get(tool_name)
  if ctor:
    return ctor(adb)
  else:
    print 'Unknown tool %s, available tools: %s' % (
      tool_name, ', '.join(sorted(TOOL_REGISTRY.keys())))
    sys.exit(1)