def __StartForwarder(self): logging.warning('Forwarding %s %s', self.ports_to_forward, self.has_forwarded_ports) if self.ports_to_forward and not self.has_forwarded_ports: self.has_forwarded_ports = True tool = valgrind_tools.CreateTool(None, self.device) forwarder.Forwarder.Map([(port, port) for port in self.ports_to_forward], self.device, tool)
def Map(port_pairs, adb, tool=None): """Runs the forwarder. Args: port_pairs: A list of tuples (device_port, host_port) to forward. Note that you can specify 0 as a device_port, in which case a port will by dynamically assigned on the device. You can get the number of the assigned port using the DevicePortForHostPort method. adb: An AndroidCommands instance. tool: Tool class to use to get wrapper, if necessary, for executing the forwarder (see valgrind_tools.py). Raises: Exception on failure to forward the port. """ if not tool: tool = valgrind_tools.CreateTool(None, adb) with _FileLock(Forwarder._LOCK_PATH): instance = Forwarder._GetInstanceLocked(tool) instance._InitDeviceLocked(adb, tool) device_serial = adb.Adb().GetSerialNumber() redirection_commands = [[ '--serial-id=' + device_serial, '--map', str(device), str(host) ] for device, host in port_pairs] logging.info('Forwarding using commands: %s', redirection_commands) for redirection_command in redirection_commands: try: (exit_code, output) = cmd_helper.GetCmdStatusAndOutput( [instance._host_forwarder_path] + redirection_command) except OSError as e: if e.errno == 2: raise Exception( 'Unable to start host forwarder. Make sure you have' ' built host_forwarder.') else: raise if exit_code != 0: raise Exception('%s exited with %d:\n%s' % (instance._host_forwarder_path, exit_code, '\n'.join(output))) tokens = output.split(':') if len(tokens) != 2: raise Exception( ('Unexpected host forwarder output "%s", ' + 'expected "device_port:host_port"') % output) device_port = int(tokens[0]) host_port = int(tokens[1]) serial_with_port = (device_serial, device_port) instance._device_to_host_port_map[serial_with_port] = host_port instance._host_to_device_port_map[host_port] = serial_with_port logging.info('Forwarding device port: %d to host port: %d.', device_port, host_port)
def UnmapAllDevicePorts(device): """Unmaps all the previously forwarded ports for the provided device. Args: device: A DeviceUtils instance. port_pairs: A list of tuples (device_port, host_port) to unmap. """ with _FileLock(Forwarder._LOCK_PATH): if not Forwarder._instance: return adb_serial = str(device) if adb_serial not in Forwarder._instance._initialized_devices: return port_map = Forwarder._GetInstanceLocked( None)._device_to_host_port_map for (device_serial, device_port) in port_map.keys(): if adb_serial == device_serial: Forwarder._UnmapDevicePortLocked(device_port, device) # There are no more ports mapped, kill the device_forwarder. tool = valgrind_tools.CreateTool(None, device) Forwarder._KillDeviceLocked(device, tool)
def UnmapAllDevicePorts(adb): """Unmaps all the previously forwarded ports for the provided device. Args: adb: An AndroidCommands instance. port_pairs: A list of tuples (device_port, host_port) to unmap. """ with _FileLock(Forwarder._LOCK_PATH): if not Forwarder._instance: return adb_serial = adb.Adb().GetSerialNumber() if adb_serial not in Forwarder._instance._initialized_devices: return port_map = Forwarder._GetInstanceLocked( None)._device_to_host_port_map for (device_serial, device_port) in port_map.keys(): if adb_serial == device_serial: Forwarder._UnmapDevicePortLocked(device_port, adb) # There are no more ports mapped, kill the device_forwarder. tool = valgrind_tools.CreateTool(None, adb) Forwarder._KillDeviceLocked(adb, tool) Forwarder._instance._initialized_devices.remove(adb_serial)
def UnmapAllDevicePorts(device): """Unmaps all the previously forwarded ports for the provided device. Args: device: A DeviceUtils instance. port_pairs: A list of tuples (device_port, host_port) to unmap. """ # TODO(jbudorick) Remove once telemetry gets switched over. if isinstance(device, pylib.android_commands.AndroidCommands): device = pylib.device.device_utils.DeviceUtils(device) with _FileLock(Forwarder._LOCK_PATH): if not Forwarder._instance: return adb_serial = str(device) if adb_serial not in Forwarder._instance._initialized_devices: return port_map = Forwarder._GetInstanceLocked( None)._device_to_host_port_map for (device_serial, device_port) in port_map.keys(): if adb_serial == device_serial: Forwarder._UnmapDevicePortLocked(device_port, device) # There are no more ports mapped, kill the device_forwarder. tool = valgrind_tools.CreateTool(None, device) Forwarder._KillDeviceLocked(device, tool)
def GetTool(self, device): if not str(device) in self._tools: self._tools[str(device)] = valgrind_tools.CreateTool( self._env.tool, device) return self._tools[str(device)]