Ejemplo n.º 1
0
 def KillHost(build_type):
   logging.info('Killing host_forwarder.')
   host_forwarder_path = _MakeBinaryPath(build_type, 'host_forwarder')
   (exit_code, output) = cmd_helper.GetCmdStatusAndOutput(
       [host_forwarder_path, 'kill-server'])
   if exit_code != 0:
     (exit_code, output) = cmd_helper.GetCmdStatusAndOutput(
         ['pkill', 'host_forwarder'])
     if exit_code != 0:
       raise Exception('%s exited with %d:\n%s' % (
             host_forwarder_path, exit_code, '\n'.join(output)))
Ejemplo n.º 2
0
  def _KillHostLocked(self):
    """Kills the forwarder process running on the host.

    Note that the global lock must be acquired before calling this method.
    """
    logging.info('Killing host_forwarder.')
    (exit_code, output) = cmd_helper.GetCmdStatusAndOutput(
        [self._host_forwarder_path, '--kill-server'])
    if exit_code != 0:
      (exit_code, output) = cmd_helper.GetCmdStatusAndOutput(
          ['pkill', '-9', 'host_forwarder'])
      if exit_code != 0:
        raise Exception('%s exited with %d:\n%s' % (
              self._host_forwarder_path, exit_code, '\n'.join(output)))
Ejemplo n.º 3
0
    def _UnmapDevicePortLocked(device_port, adb):
        """Internal method used by UnmapDevicePort().

    Note that the global lock must be acquired before calling this method.
    """
        instance = Forwarder._GetInstanceLocked(None)
        serial = adb.Adb().GetSerialNumber()
        serial_with_port = (serial, device_port)
        if not serial_with_port in instance._device_to_host_port_map:
            logging.error('Trying to unmap non-forwarded port %d' %
                          device_port)
            return
        redirection_command = [
            '--serial-id=' + serial, '--unmap',
            str(device_port)
        ]
        (exit_code, output
         ) = cmd_helper.GetCmdStatusAndOutput([instance._host_forwarder_path] +
                                              redirection_command)
        if exit_code != 0:
            logging.error(
                '%s exited with %d:\n%s' %
                (instance._host_forwarder_path, exit_code, '\n'.join(output)))
        host_port = instance._device_to_host_port_map[serial_with_port]
        del instance._device_to_host_port_map[serial_with_port]
        del instance._host_to_device_port_map[host_port]
Ejemplo n.º 4
0
    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 KillHost(build_type):
        """Kills the forwarder process running on the host.

    Args:
      build_type: 'Release' or 'Debug'
    """
        logging.info('Killing host_forwarder.')
        host_forwarder_path = _MakeBinaryPath(build_type, 'host_forwarder')
        assert os.path.exists(host_forwarder_path), 'Please build forwarder2'
        (exit_code, output) = cmd_helper.GetCmdStatusAndOutput(
            [host_forwarder_path, 'kill-server'])
        if exit_code != 0:
            (exit_code, output) = cmd_helper.GetCmdStatusAndOutput(
                ['pkill', 'host_forwarder'])
            if exit_code != 0:
                raise Exception(
                    '%s exited with %d:\n%s' %
                    (host_forwarder_path, exit_code, '\n'.join(output)))
Ejemplo n.º 6
0
  def Run(self, port_pairs, tool, host_name):
    """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.
      tool: Tool class to use to get wrapper, if necessary, for executing the
            forwarder (see valgrind_tools.py).
      host_name: Address to forward to, must be addressable from the
                 host machine. Usually use loopback '127.0.0.1'.

    Raises:
      Exception on failure to forward the port.
    """
    host_adb_control_port = ports.AllocateTestServerPort()
    if not host_adb_control_port:
      raise Exception('Failed to allocate a TCP port in the host machine.')
    self._adb.PushIfNeeded(
        self._device_forwarder_path, Forwarder._DEVICE_FORWARDER_PATH)
    redirection_commands = [
        '%d:%d:%d:%s' % (host_adb_control_port, device, host,
                         host_name) for device, host in port_pairs]
    logging.info('Command format: <ADB port>:<Device port>' +
                 '[:<Forward to port>:<Forward to address>]')
    logging.info('Forwarding using commands: %s', redirection_commands)
    if cmd_helper.RunCmd(
        ['adb', '-s', self._adb._adb.GetSerialNumber(), 'forward',
         'tcp:%s' % host_adb_control_port,
         'localabstract:%s' % Forwarder._DEVICE_ADB_CONTROL_PORT]) != 0:
      raise Exception('Error while running adb forward.')

    (exit_code, output) = self._adb.GetShellCommandStatusAndOutput(
        '%s %s %s' % (tool.GetUtilWrapper(), Forwarder._DEVICE_FORWARDER_PATH,
                      Forwarder._DEVICE_ADB_CONTROL_PORT))
    if exit_code != 0:
      raise Exception(
          'Failed to start device forwarder:\n%s' % '\n'.join(output))

    for redirection_command in redirection_commands:
      (exit_code, output) = cmd_helper.GetCmdStatusAndOutput(
          [self._host_forwarder_path, redirection_command])
      if exit_code != 0:
        raise Exception('%s exited with %d:\n%s' % (
            self._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])
      self._host_to_device_port_map[host_port] = device_port
      logging.info('Forwarding device port: %d to host port: %d.', device_port,
                   host_port)
    def Run(self, port_pairs, tool, host_name):
        """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.
      tool: Tool class to use to get wrapper, if necessary, for executing the
            forwarder (see valgrind_tools.py).
      host_name: Address to forward to, must be addressable from the
                 host machine. Usually use loopback '127.0.0.1'.

    Raises:
      Exception on failure to forward the port.
    """
        with self._lock:
            self._InitDeviceLocked(tool)
            self._InitHostLocked()
            redirection_commands = [
                '%d:%d:%d:%s' %
                (self._host_adb_control_port, device, host, host_name)
                for device, host in port_pairs
            ]
            logging.info('Command format: <ADB port>:<Device port>' +
                         '[:<Forward to port>:<Forward to address>]')
            logging.info('Forwarding using commands: %s', redirection_commands)

            for redirection_command in redirection_commands:
                try:
                    (exit_code, output) = cmd_helper.GetCmdStatusAndOutput(
                        [self._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' %
                                    (self._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])
                self._host_to_device_port_map[host_port] = device_port
                logging.info('Forwarding device port: %d to host port: %d.',
                             device_port, host_port)
    def UnmapDevicePort(self, device_port):
        """Unmaps a previously forwarded device port.

    Args:
      device_port: A previously forwarded port (through Run()).
    """
        with self._lock:
            # Please note the minus sign below.
            redirection_command = '%d:-%d' % (self._host_adb_control_port,
                                              device_port)
            (exit_code, output) = cmd_helper.GetCmdStatusAndOutput(
                [self._host_forwarder_path, redirection_command])
            if exit_code != 0:
                raise Exception(
                    '%s exited with %d:\n%s' %
                    (self._host_forwarder_path, exit_code, '\n'.join(output)))