def runTest(self): mock_backend_1 = MockBackend('mock-backend-1') mock_backend_2 = MockBackend('mock-backend-2') self.assertEqual(mock_backend_1.settings['key_1'], 'key descritpion 1') backends.Register(mock_backend_1) backends.Register(mock_backend_2) devices = list(backends.ListDevices()) self.assertEqual(len(devices), 4) self.assertIsNotNone(backends.GetDevice('mock-backend-1', 'device-1')) self.assertIsNotNone(backends.GetDevice('mock-backend-1', 'device-2')) self.assertIsNotNone(backends.GetDevice('mock-backend-2', 'device-1')) self.assertIsNotNone(backends.GetDevice('mock-backend-2', 'device-1')) self.assertTrue('key_1' in mock_backend_1.settings)
def _ListDevices(args, req_vars): # pylint: disable=W0613 resp = [] for device in backends.ListDevices(): # The device settings must loaded at discovery time (i.e. here), not during # startup, because it might have been plugged later. for k, v in _persistent_storage.LoadSettings(device.id).iteritems(): device.settings[k] = v resp += [{ 'backend': device.backend.name, 'id': device.id, 'name': device.name }] return _HTTP_OK, [], resp
def main(): COMMANDS = ['devices', 'ps', 'stats', 'mmaps', 'classified_mmaps'] usage = ('%prog [options] ' + ' | '.join(COMMANDS)) parser = optparse.OptionParser(usage=usage) parser.add_option('-b', '--backend', help='Backend name ' '(e.g., Android)', type='string', default='Android') parser.add_option('-s', '--device_id', help='Device ' 'id (e.g., Android serial)', type='string') parser.add_option('-p', '--process_id', help='Target process id', type='int') parser.add_option('-m', '--filter_process_name', help='Process ' 'name to match', type='string') parser.add_option('-r', '--mmap_rule', help='mmap rule', type='string', default=os.path.join(constants.CLASSIFICATION_RULES_PATH, 'default', 'mmap-android.py')) (options, args) = parser.parse_args() memory_inspector.RegisterAllBackends() if not args or args[0] not in COMMANDS: parser.print_help() return -1 if args[0] == 'devices': _ListDevices(options.backend) return 0 number_of_devices = 0 if options.device_id: device_id = options.device_id number_of_devices = 1 else: for device in backends.ListDevices(): if device.backend.name == options.backend: number_of_devices += 1 device_id = device.id if number_of_devices == 0: print "No devices connected" return -1 if number_of_devices > 1: print( 'More than 1 device connected. You need to provide' ' --device_id') return -1 device = backends.GetDevice(options.backend, device_id) if not device: print 'Device', device_id, 'does not exist' return -1 device.Initialize() if args[0] == 'ps': if not options.filter_process_name: print 'Listing all processes' else: print('Listing processes matching ' + options.filter_process_name.lower()) print '' print '%-10s : %-50s : %12s %12s %12s' % ( 'Process ID', 'Process Name', 'RUN_TIME', 'THREADS', 'MEM_RSS_KB') print '' for process in device.ListProcesses(): if (not options.filter_process_name or options.filter_process_name.lower() in process.name.lower()): stats = process.GetStats() run_time_min, run_time_sec = divmod(stats.run_time, 60) print '%10s : %-50s : %6s m %2s s %8s %12s' % ( process.pid, _Truncate(process.name, 50), run_time_min, run_time_sec, stats.threads, stats.vm_rss) return 0 if not options.process_id: print 'You need to provide --process_id' return -1 process = device.GetProcess(options.process_id) if not process: print 'Cannot find process [%d] on device %s' % (options.process_id, device.id) return -1 elif args[0] == 'stats': _ListProcessStats(process) return 0 elif args[0] == 'mmaps': _ListProcessMmaps(process) return 0 elif args[0] == 'classified_mmaps': _ListProcessClassifiedMmaps(process, options.mmap_rule) return 0
def _ListDevices(backend_name): print 'Device list:' print '' for device in backends.ListDevices(): if device.backend.name == backend_name: print '%-16s : %s' % (device.id, device.name)