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 |
12 from devil.utils import cmd_helper | 13 from devil.utils import cmd_helper |
13 from pylib import constants | 14 from pylib import constants |
14 from pylib import valgrind_tools | 15 from pylib import valgrind_tools |
15 | 16 |
16 | 17 |
17 def _GetProcessStartTime(pid): | 18 def _GetProcessStartTime(pid): |
18 return psutil.Process(pid).create_time | 19 return psutil.Process(pid).create_time |
19 | 20 |
20 | 21 |
21 class _FileLock(object): | 22 class _FileLock(object): |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
70 Exception on failure to forward the port. | 71 Exception on failure to forward the port. |
71 """ | 72 """ |
72 if not tool: | 73 if not tool: |
73 tool = valgrind_tools.CreateTool(None, device) | 74 tool = valgrind_tools.CreateTool(None, device) |
74 with _FileLock(Forwarder._LOCK_PATH): | 75 with _FileLock(Forwarder._LOCK_PATH): |
75 instance = Forwarder._GetInstanceLocked(tool) | 76 instance = Forwarder._GetInstanceLocked(tool) |
76 instance._InitDeviceLocked(device, tool) | 77 instance._InitDeviceLocked(device, tool) |
77 | 78 |
78 device_serial = str(device) | 79 device_serial = str(device) |
79 redirection_commands = [ | 80 redirection_commands = [ |
80 ['--adb=' + constants.GetAdbPath(), | 81 ['--adb=' + devil_env.config.FetchPath('adb'), |
81 '--serial-id=' + device_serial, | 82 '--serial-id=' + device_serial, |
82 '--map', str(device_port), str(host_port)] | 83 '--map', str(device_port), str(host_port)] |
83 for device_port, host_port in port_pairs] | 84 for device_port, host_port in port_pairs] |
84 logging.info('Forwarding using commands: %s', redirection_commands) | 85 logging.info('Forwarding using commands: %s', redirection_commands) |
85 | 86 |
86 for redirection_command in redirection_commands: | 87 for redirection_command in redirection_commands: |
87 try: | 88 try: |
88 (exit_code, output) = cmd_helper.GetCmdStatusAndOutput( | 89 (exit_code, output) = cmd_helper.GetCmdStatusAndOutput( |
89 [instance._host_forwarder_path] + redirection_command) | 90 [instance._host_forwarder_path] + redirection_command) |
90 except OSError as e: | 91 except OSError as e: |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
190 | 191 |
191 Args: | 192 Args: |
192 tool: Tool class to use to get wrapper, if necessary, for executing the | 193 tool: Tool class to use to get wrapper, if necessary, for executing the |
193 forwarder (see valgrind_tools.py). | 194 forwarder (see valgrind_tools.py). |
194 """ | 195 """ |
195 assert not Forwarder._instance | 196 assert not Forwarder._instance |
196 self._tool = tool | 197 self._tool = tool |
197 self._initialized_devices = set() | 198 self._initialized_devices = set() |
198 self._device_to_host_port_map = dict() | 199 self._device_to_host_port_map = dict() |
199 self._host_to_device_port_map = dict() | 200 self._host_to_device_port_map = dict() |
200 self._host_forwarder_path = os.path.join( | 201 self._host_forwarder_path = devil_env.config.FetchPath('forwarder_host') |
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 = os.path.join( | |
204 constants.GetOutDirectory(), 'forwarder_dist') | |
205 self._InitHostLocked() | 203 self._InitHostLocked() |
206 | 204 |
207 @staticmethod | 205 @staticmethod |
208 def _UnmapDevicePortLocked(device_port, device): | 206 def _UnmapDevicePortLocked(device_port, device): |
209 """Internal method used by UnmapDevicePort(). | 207 """Internal method used by UnmapDevicePort(). |
210 | 208 |
211 Note that the global lock must be acquired before calling this method. | 209 Note that the global lock must be acquired before calling this method. |
212 """ | 210 """ |
213 instance = Forwarder._GetInstanceLocked(None) | 211 instance = Forwarder._GetInstanceLocked(None) |
214 serial = str(device) | 212 serial = str(device) |
215 serial_with_port = (serial, device_port) | 213 serial_with_port = (serial, device_port) |
216 if not serial_with_port in instance._device_to_host_port_map: | 214 if not serial_with_port in instance._device_to_host_port_map: |
217 logging.error('Trying to unmap non-forwarded port %d', device_port) | 215 logging.error('Trying to unmap non-forwarded port %d', device_port) |
218 return | 216 return |
219 redirection_command = ['--adb=' + constants.GetAdbPath(), | 217 redirection_command = ['--adb=' + devil_env.config.FetchPath('adb'), |
220 '--serial-id=' + serial, | 218 '--serial-id=' + serial, |
221 '--unmap', str(device_port)] | 219 '--unmap', str(device_port)] |
222 logging.info('Undo forwarding using command: %s', redirection_command) | 220 logging.info('Undo forwarding using command: %s', redirection_command) |
223 (exit_code, output) = cmd_helper.GetCmdStatusAndOutput( | 221 (exit_code, output) = cmd_helper.GetCmdStatusAndOutput( |
224 [instance._host_forwarder_path] + redirection_command) | 222 [instance._host_forwarder_path] + redirection_command) |
225 if exit_code != 0: | 223 if exit_code != 0: |
226 logging.error( | 224 logging.error( |
227 '%s exited with %d:\n%s', | 225 '%s exited with %d:\n%s', |
228 instance._host_forwarder_path, exit_code, '\n'.join(output)) | 226 instance._host_forwarder_path, exit_code, '\n'.join(output)) |
229 host_port = instance._device_to_host_port_map[serial_with_port] | 227 host_port = instance._device_to_host_port_map[serial_with_port] |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
272 | 270 |
273 Args: | 271 Args: |
274 device: A DeviceUtils instance. | 272 device: A DeviceUtils instance. |
275 tool: Tool class to use to get wrapper, if necessary, for executing the | 273 tool: Tool class to use to get wrapper, if necessary, for executing the |
276 forwarder (see valgrind_tools.py). | 274 forwarder (see valgrind_tools.py). |
277 """ | 275 """ |
278 device_serial = str(device) | 276 device_serial = str(device) |
279 if device_serial in self._initialized_devices: | 277 if device_serial in self._initialized_devices: |
280 return | 278 return |
281 Forwarder._KillDeviceLocked(device, tool) | 279 Forwarder._KillDeviceLocked(device, tool) |
| 280 forwarder_device_path_on_host = devil_env.config.FetchPath( |
| 281 'forwarder_device', device=device) |
| 282 forwarder_device_path_on_device = ( |
| 283 Forwarder._DEVICE_FORWARDER_FOLDER |
| 284 if os.path.isdir(forwarder_device_path_on_host) |
| 285 else Forwarder._DEVICE_FORWARDER_PATH) |
282 device.PushChangedFiles([( | 286 device.PushChangedFiles([( |
283 self._device_forwarder_path_on_host, | 287 forwarder_device_path_on_host, |
284 Forwarder._DEVICE_FORWARDER_FOLDER)]) | 288 forwarder_device_path_on_device)]) |
| 289 |
285 cmd = '%s %s' % (tool.GetUtilWrapper(), Forwarder._DEVICE_FORWARDER_PATH) | 290 cmd = '%s %s' % (tool.GetUtilWrapper(), Forwarder._DEVICE_FORWARDER_PATH) |
286 device.RunShellCommand( | 291 device.RunShellCommand( |
287 cmd, env={'LD_LIBRARY_PATH': Forwarder._DEVICE_FORWARDER_FOLDER}, | 292 cmd, env={'LD_LIBRARY_PATH': Forwarder._DEVICE_FORWARDER_FOLDER}, |
288 check_return=True) | 293 check_return=True) |
289 self._initialized_devices.add(device_serial) | 294 self._initialized_devices.add(device_serial) |
290 | 295 |
291 def _KillHostLocked(self): | 296 def _KillHostLocked(self): |
292 """Kills the forwarder process running on the host. | 297 """Kills the forwarder process running on the host. |
293 | 298 |
294 Note that the global lock must be acquired before calling this method. | 299 Note that the global lock must be acquired before calling this method. |
(...skipping 22 matching lines...) Expand all Loading... |
317 logging.info('Killing device_forwarder.') | 322 logging.info('Killing device_forwarder.') |
318 Forwarder._instance._initialized_devices.discard(str(device)) | 323 Forwarder._instance._initialized_devices.discard(str(device)) |
319 if not device.FileExists(Forwarder._DEVICE_FORWARDER_PATH): | 324 if not device.FileExists(Forwarder._DEVICE_FORWARDER_PATH): |
320 return | 325 return |
321 | 326 |
322 cmd = '%s %s --kill-server' % (tool.GetUtilWrapper(), | 327 cmd = '%s %s --kill-server' % (tool.GetUtilWrapper(), |
323 Forwarder._DEVICE_FORWARDER_PATH) | 328 Forwarder._DEVICE_FORWARDER_PATH) |
324 device.RunShellCommand( | 329 device.RunShellCommand( |
325 cmd, env={'LD_LIBRARY_PATH': Forwarder._DEVICE_FORWARDER_FOLDER}, | 330 cmd, env={'LD_LIBRARY_PATH': Forwarder._DEVICE_FORWARDER_FOLDER}, |
326 check_return=True) | 331 check_return=True) |
OLD | NEW |