| OLD | NEW |
| 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 # pylint: disable=W0212 | 5 # pylint: disable=W0212 |
| 6 | 6 |
| 7 import fcntl | 7 import fcntl |
| 8 import logging | 8 import logging |
| 9 import os | 9 import os |
| 10 import psutil | 10 import psutil |
| 11 | 11 |
| 12 from devil import devil_env | |
| 13 from devil.utils import cmd_helper | 12 from devil.utils import cmd_helper |
| 14 from pylib import constants | 13 from pylib import constants |
| 15 from pylib import valgrind_tools | 14 from pylib import valgrind_tools |
| 16 | 15 |
| 17 | 16 |
| 18 def _GetProcessStartTime(pid): | 17 def _GetProcessStartTime(pid): |
| 19 return psutil.Process(pid).create_time | 18 return psutil.Process(pid).create_time |
| 20 | 19 |
| 21 | 20 |
| 22 class _FileLock(object): | 21 class _FileLock(object): |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 Exception on failure to forward the port. | 70 Exception on failure to forward the port. |
| 72 """ | 71 """ |
| 73 if not tool: | 72 if not tool: |
| 74 tool = valgrind_tools.CreateTool(None, device) | 73 tool = valgrind_tools.CreateTool(None, device) |
| 75 with _FileLock(Forwarder._LOCK_PATH): | 74 with _FileLock(Forwarder._LOCK_PATH): |
| 76 instance = Forwarder._GetInstanceLocked(tool) | 75 instance = Forwarder._GetInstanceLocked(tool) |
| 77 instance._InitDeviceLocked(device, tool) | 76 instance._InitDeviceLocked(device, tool) |
| 78 | 77 |
| 79 device_serial = str(device) | 78 device_serial = str(device) |
| 80 redirection_commands = [ | 79 redirection_commands = [ |
| 81 ['--adb=' + devil_env.config.adb_path, | 80 ['--adb=' + constants.GetAdbPath(), |
| 82 '--serial-id=' + device_serial, | 81 '--serial-id=' + device_serial, |
| 83 '--map', str(device_port), str(host_port)] | 82 '--map', str(device_port), str(host_port)] |
| 84 for device_port, host_port in port_pairs] | 83 for device_port, host_port in port_pairs] |
| 85 logging.info('Forwarding using commands: %s', redirection_commands) | 84 logging.info('Forwarding using commands: %s', redirection_commands) |
| 86 | 85 |
| 87 for redirection_command in redirection_commands: | 86 for redirection_command in redirection_commands: |
| 88 try: | 87 try: |
| 89 (exit_code, output) = cmd_helper.GetCmdStatusAndOutput( | 88 (exit_code, output) = cmd_helper.GetCmdStatusAndOutput( |
| 90 [instance._host_forwarder_path] + redirection_command) | 89 [instance._host_forwarder_path] + redirection_command) |
| 91 except OSError as e: | 90 except OSError as e: |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 191 | 190 |
| 192 Args: | 191 Args: |
| 193 tool: Tool class to use to get wrapper, if necessary, for executing the | 192 tool: Tool class to use to get wrapper, if necessary, for executing the |
| 194 forwarder (see valgrind_tools.py). | 193 forwarder (see valgrind_tools.py). |
| 195 """ | 194 """ |
| 196 assert not Forwarder._instance | 195 assert not Forwarder._instance |
| 197 self._tool = tool | 196 self._tool = tool |
| 198 self._initialized_devices = set() | 197 self._initialized_devices = set() |
| 199 self._device_to_host_port_map = dict() | 198 self._device_to_host_port_map = dict() |
| 200 self._host_to_device_port_map = dict() | 199 self._host_to_device_port_map = dict() |
| 201 self._host_forwarder_path = devil_env.config.forwarder_host_path | 200 self._host_forwarder_path = os.path.join( |
| 201 constants.GetOutDirectory(), 'host_forwarder') |
| 202 assert os.path.exists(self._host_forwarder_path), 'Please build forwarder2' | 202 assert os.path.exists(self._host_forwarder_path), 'Please build forwarder2' |
| 203 self._device_forwarder_path_on_host = ( | 203 self._device_forwarder_path_on_host = os.path.join( |
| 204 devil_env.config.forwarder_device_path) | 204 constants.GetOutDirectory(), 'forwarder_dist') |
| 205 self._InitHostLocked() | 205 self._InitHostLocked() |
| 206 | 206 |
| 207 @staticmethod | 207 @staticmethod |
| 208 def _UnmapDevicePortLocked(device_port, device): | 208 def _UnmapDevicePortLocked(device_port, device): |
| 209 """Internal method used by UnmapDevicePort(). | 209 """Internal method used by UnmapDevicePort(). |
| 210 | 210 |
| 211 Note that the global lock must be acquired before calling this method. | 211 Note that the global lock must be acquired before calling this method. |
| 212 """ | 212 """ |
| 213 instance = Forwarder._GetInstanceLocked(None) | 213 instance = Forwarder._GetInstanceLocked(None) |
| 214 serial = str(device) | 214 serial = str(device) |
| 215 serial_with_port = (serial, device_port) | 215 serial_with_port = (serial, device_port) |
| 216 if not serial_with_port in instance._device_to_host_port_map: | 216 if not serial_with_port in instance._device_to_host_port_map: |
| 217 logging.error('Trying to unmap non-forwarded port %d', device_port) | 217 logging.error('Trying to unmap non-forwarded port %d', device_port) |
| 218 return | 218 return |
| 219 redirection_command = ['--adb=' + devil_env.config.adb_path, | 219 redirection_command = ['--adb=' + constants.GetAdbPath(), |
| 220 '--serial-id=' + serial, | 220 '--serial-id=' + serial, |
| 221 '--unmap', str(device_port)] | 221 '--unmap', str(device_port)] |
| 222 logging.info('Undo forwarding using command: %s', redirection_command) | 222 logging.info('Undo forwarding using command: %s', redirection_command) |
| 223 (exit_code, output) = cmd_helper.GetCmdStatusAndOutput( | 223 (exit_code, output) = cmd_helper.GetCmdStatusAndOutput( |
| 224 [instance._host_forwarder_path] + redirection_command) | 224 [instance._host_forwarder_path] + redirection_command) |
| 225 if exit_code != 0: | 225 if exit_code != 0: |
| 226 logging.error( | 226 logging.error( |
| 227 '%s exited with %d:\n%s', | 227 '%s exited with %d:\n%s', |
| 228 instance._host_forwarder_path, exit_code, '\n'.join(output)) | 228 instance._host_forwarder_path, exit_code, '\n'.join(output)) |
| 229 host_port = instance._device_to_host_port_map[serial_with_port] | 229 host_port = instance._device_to_host_port_map[serial_with_port] |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 317 logging.info('Killing device_forwarder.') | 317 logging.info('Killing device_forwarder.') |
| 318 Forwarder._instance._initialized_devices.discard(str(device)) | 318 Forwarder._instance._initialized_devices.discard(str(device)) |
| 319 if not device.FileExists(Forwarder._DEVICE_FORWARDER_PATH): | 319 if not device.FileExists(Forwarder._DEVICE_FORWARDER_PATH): |
| 320 return | 320 return |
| 321 | 321 |
| 322 cmd = '%s %s --kill-server' % (tool.GetUtilWrapper(), | 322 cmd = '%s %s --kill-server' % (tool.GetUtilWrapper(), |
| 323 Forwarder._DEVICE_FORWARDER_PATH) | 323 Forwarder._DEVICE_FORWARDER_PATH) |
| 324 device.RunShellCommand( | 324 device.RunShellCommand( |
| 325 cmd, env={'LD_LIBRARY_PATH': Forwarder._DEVICE_FORWARDER_FOLDER}, | 325 cmd, env={'LD_LIBRARY_PATH': Forwarder._DEVICE_FORWARDER_FOLDER}, |
| 326 check_return=True) | 326 check_return=True) |
| OLD | NEW |