def parse_value(self, data): """ Parse value of a field in a tracepoint event. The return value depends on the type of the field, and can be an int value, a string, an array of int values, etc. If the type can't be parsed, return a byte array or an array of byte arrays. """ if self.elem_count > 1 and self.elem_size == 1 and self.is_signed == 0: # The field is a string. length = 0 while length < self.elem_count and bytes_to_str( data[self.offset + length]) != '\x00': length += 1 return bytes_to_str(data[self.offset:self.offset + length]) unpack_key = self._unpack_key_dict.get(self.elem_size) if unpack_key: if not self.is_signed: unpack_key = unpack_key.upper() value = struct.unpack( '%d%s' % (self.elem_count, unpack_key), data[self.offset:self.offset + self.elem_count * self.elem_size]) else: # Since we don't know the element type, just return the bytes. value = [] offset = self.offset for _ in range(self.elem_count): value.append(data[offset:offset + self.elem_size]) offset += self.elem_size if self.elem_count == 1: value = value[0] return value
def test_bytes(self): self.assertEquals(utils.str_to_bytes("27"), 27) self.assertEquals(utils.str_to_bytes("27k"), 27*1024) self.assertEquals(utils.str_to_bytes("27T"), 27*2**40) self.assertEquals(utils.str_to_bytes("27mbT"), 27*2**20) self.assertEquals(utils.str_to_bytes("27gbT"), 27*2**30) self.assertEquals(utils.bytes_to_str(27*2**30), "27.000G") self.assertEquals(utils.bytes_to_str(27*2**30+11*2**10), "27.000G") self.assertEquals(utils.bytes_to_str(27*2**30+11*2**10), "27.000G") self.assertEquals(utils.bytes_to_str(27*2**20), "27.000M") self.assertEquals(utils.str_to_bytes("1.2k"), 1228)
def histogram(self): hist = f"{len(self.scanner)} total files, need {self.copies} copies\n" buckets = {0: 0} bucketsize = {0: 0} self.expire_claims() with self.scanner: scanned_files = self.scanner.keys() for filename in scanned_files: size = self.scanner[filename] if filename in self.clients: bucket = len(self.clients[filename]) if bucket not in buckets: buckets[bucket] = 0 bucketsize[bucket] = 0 buckets[bucket] += 1 bucketsize[bucket] += size else: buckets[0] += 1 bucketsize[0] += size for bucket in sorted(buckets.keys(), reverse=True): if buckets[bucket]: size = utils.bytes_to_str(bucketsize[bucket]) hist += f"{buckets[bucket]:6d} files, {size.rjust(8)}: {'## ' * bucket}" if bucket < self.copies: missing = self.copies - bucket hist += "__ " * missing hist += "\n" return hist
def GetRecordCmd(self): if self.record_cmd is not None: return self.record_cmd self.record_cmd = '' feature_data = self._GetFeatureSection(self.getInstance(), _char_pt('cmdline')) if not _is_null(feature_data): void_p = ct.cast(feature_data[0].data, ct.c_void_p) arg_count = ct.cast(void_p, ct.POINTER(ct.c_uint32)).contents.value void_p.value += 4 args = [] for _ in range(arg_count): str_len = ct.cast(void_p, ct.POINTER(ct.c_uint32)).contents.value void_p.value += 4 char_p = ct.cast(void_p, ct.POINTER(ct.c_char)) current_str = '' for j in range(str_len): c = bytes_to_str(char_p[j]) if c != '\0': current_str += c if ' ' in current_str: current_str = '"' + current_str + '"' args.append(current_str) void_p.value += str_len self.record_cmd = ' '.join(args) return self.record_cmd
def show_cities(self): self.client.sendall(b'cities') data = self.client.recv(SERVER_DATA_LENGTH) data = bytes_to_str(data) print('----------------------------------') print('Danh sach thoi tiet cac thanh pho') print(data)
def register(self): username = input('Nhap username: '******'Nhap password: '******'register {} {}'.format(username, password) self.client.sendall(str_to_bytes(data)) data = client.recv(SERVER_DATA_LENGTH) data = bytes_to_str(data) print(data)
def load_exchange_data_impl(token_address, exchange_address): token = web3.eth.contract(abi=STR_ERC_20_ABI, address=token_address) if token_address in HARDCODED_INFO: token_name, token_symbol, token_decimals = HARDCODED_INFO[ token_address] else: try: token_name = token.functions.name().call() token_symbol = token.functions.symbol().call() token_decimals = token.functions.decimals().call() except: try: token = web3.eth.contract(abi=STR_CAPS_ERC_20_ABI, address=token_address) token_name = token.functions.NAME().call() token_symbol = token.functions.SYMBOL().call() token_decimals = token.functions.DECIMALS().call() except: try: token = web3.eth.contract(abi=ERC_20_ABI, address=token_address) token_name = bytes_to_str(token.functions.name().call()) token_symbol = bytes_to_str( token.functions.symbol().call()) token_decimals = token.functions.decimals().call() except: logging.warning('F****D UP {}'.format(token_address)) return None try: token_balance = token.functions.balanceOf(exchange_address).call( block_identifier=CURRENT_BLOCK) except: logging.warning('F****D UP {}'.format(token_address)) return None eth_balance = web3.eth.getBalance(exchange_address, block_identifier=CURRENT_BLOCK) exchange = web3.eth.contract(abi=UNISWAP_EXCHANGE_ABI, address=exchange_address) swap_fee = exchange.functions.swap_fee().call() platform_fee = exchange.functions.platform_fee().call() return ExchangeInfo(token_address, token_name, token_symbol, token_decimals, exchange_address, eth_balance, token_balance, swap_fee, platform_fee)
def version_match(tp, protover): try: response = utils.bytes_to_str( tp.send_version_data(utils.str_to_bytes(protover))) if response != "SUCCESS": return False return True except RuntimeError as e: print(e) return None
def read_messages(r, clients): messages = [] for sock in r: try: message = utils.bytes_to_str(sock.recv(1024)) messages.append(message) except: clients.remove(sock) return messages
def collect_native_libs_on_device(self): self.device_build_id_map.clear() self.adb.check_run(['shell', 'mkdir', '-p', self.dir_on_device]) if os.path.exists(self.build_id_list_file): os.remove(self.build_id_list_file) self.adb.run(['pull', self.dir_on_device + self.build_id_list_file]) if os.path.exists(self.build_id_list_file): with open(self.build_id_list_file, 'rb') as fh: for line in fh.readlines(): line = bytes_to_str(line).strip() items = line.split('=') if len(items) == 2: self.device_build_id_map[items[0]] = items[1] remove(self.build_id_list_file)
def collect_native_libs_on_device(self): self.device_build_id_map.clear() self.adb.check_run(['shell', 'mkdir', '-p', self.dir_on_device]) if os.path.exists(self.build_id_list_file): os.remove(self.build_id_list_file) self.adb.run(['pull', self.dir_on_device + self.build_id_list_file]) if os.path.exists(self.build_id_list_file): with open(self.build_id_list_file, 'rb') as fh: for line in fh.readlines(): line = bytes_to_str(line).strip() items = line.split('=') if len(items) == 2: self.device_build_id_map[items[0]] = items[1] remove(self.build_id_list_file)
def update_weather(self): if not self.is_admin: print('Ban nhap khong hop le') return self.client.sendall(b'list_city') data = client.recv(SERVER_DATA_LENGTH) data = bytes_to_str(data) print(data) dict = {} dict['city_id'] = input('City id: ').strip() dict['day'] = input('Ngay (yyyy-mm-dd): ').strip() dict['status'] = input('Trang thai: ').strip() dict['temp_min'] = input('Nhiet do thap nhat: ').strip() dict['temp_max'] = input('Nhiet do cao nhat: ').strip() data = 'update_weather {}'.format(str(dict)) data = str_to_bytes(data) self.client.sendall(data) data = self.client.recv(SERVER_DATA_LENGTH) data = bytes_to_str(data) print(data)
def login(self): username = input('Nhap username: '******'Nhap password: '******'login {} {}'.format(username, password) client.sendall(str_to_bytes(data)) data = client.recv(SERVER_DATA_LENGTH) data = bytes_to_str(data) if data != SUCCESS: print(data) return elif username == 'admin': self.is_admin = True self.menu()
def _GetFeatureString(self, feature_name): feature_data = self._GetFeatureSection(self.getInstance(), _char_pt(feature_name)) result = '' if not _is_null(feature_data): void_p = ct.cast(feature_data[0].data, ct.c_void_p) str_len = ct.cast(void_p, ct.POINTER(ct.c_uint32)).contents.value void_p.value += 4 char_p = ct.cast(void_p, ct.POINTER(ct.c_char)) for i in range(str_len): c = bytes_to_str(char_p[i]) if c == '\0': break result += c return result
def add_city(self): if not self.is_admin: print('Ban nhap khong hop le') return name = input('Nhap ten thanh pho: ').strip() if len(name) == 0: print('Khong duoc de trong ten') return data = 'add_city {}'.format(name) data = str_to_bytes(data) self.client.sendall(data) data = self.client.recv(SERVER_DATA_LENGTH) data = bytes_to_str(data) print(data)
def show_city(self): while True: text = input('Nhap id thanh pho ban muon xem: ').strip() try: number = int(text) except ValueError: print('Ban phai nhap so') continue data = 'city {}'.format(number) data = str_to_bytes(data) self.client.sendall(data) data = self.client.recv(SERVER_DATA_LENGTH) data = bytes_to_str(data) print('----------------------------------') print('Danh sach thoi tiet cac ngay toi') print(data) break
def collect_native_libs_on_device(self): self.device_build_id_map.clear() self.adb.check_run(['shell', 'mkdir', '-p', self.dir_on_device]) if os.path.exists(self.build_id_list_file): os.remove(self.build_id_list_file) result, output = self.adb.run_and_return_output(['shell', 'ls', self.dir_on_device]) if not result: return file_set = set(output.strip().split()) if self.build_id_list_file not in file_set: return self.adb.run(['pull', self.dir_on_device + self.build_id_list_file]) if os.path.exists(self.build_id_list_file): with open(self.build_id_list_file, 'rb') as fh: for line in fh.readlines(): line = bytes_to_str(line).strip() items = line.split('=') if len(items) == 2: build_id, filename = items if filename in file_set: self.device_build_id_map[build_id] = filename remove(self.build_id_list_file)
def MetaInfo(self): """ Return a string to string map stored in meta_info section in perf.data. It is used to pass some short meta information. """ if self.meta_info is None: self.meta_info = {} feature_data = self._GetFeatureSection(self.getInstance(), _char_pt('meta_info')) if not _is_null(feature_data): str_list = [] data = feature_data[0].data data_size = feature_data[0].data_size current_str = '' for i in range(data_size): c = bytes_to_str(data[i]) if c != '\0': current_str += c else: str_list.append(current_str) current_str = '' for i in range(0, len(str_list), 2): self.meta_info[str_list[i]] = str_list[i + 1] return self.meta_info
def start(self): try: while True: data: bytes = self.client.recv(SERVER_DATA_LENGTH) data: str = bytes_to_str(data) data: str = data.strip() # break if data.startswith('exit'): break print(data) if data.startswith('login'): self.login(data) elif data.startswith('register'): self.register(data) elif data.startswith('cities'): self.find_cities() elif data.startswith('city'): self.find_city(data) elif data.startswith('add_city'): self.add_city(data) elif data.startswith('update_weather'): self.update_weather(data) elif data.startswith('list_city'): self.list_city() else: self.client.sendall(b'Command khong hop le') except Exception as e: print(e) finally: print('client {} out'.format(self.username)) self.semaphore.release() self.client.close() self.connect.close()
import socket import utils import jim import argparse parser = argparse.ArgumentParser("My client parser") parser.add_argument("host", type=str, help="Host of server") parser.add_argument("port", type=int, help="Port of server") parser.add_argument("mode", type=str, help="Mode of client") args = parser.parse_args() with utils.create_tcp_client_socket(args.host, args.port) as s: if args.mode == "r": print("Enter in read socket") while True: msg = s.recv(1024) print(utils.bytes_to_str(msg)) else: print("Enter in write socket") user = input("Input user: "******"Input status: ") s.send(utils.str_to_bytes(jim.get_presence_message(user, status)))
def load_pprof_profile(filename): profile = profile_pb2.Profile() with open(filename, "rb") as f: profile.ParseFromString(bytes_to_str(f.read())) return profile
def build_unwinding_result_report(args): simpleperf_path = get_host_binary_path('simpleperf') proc = subprocess.Popen([simpleperf_path, 'dump', args.record_file[0]], stdout=subprocess.PIPE) (stdoutdata, _) = proc.communicate() stdoutdata = bytes_to_str(stdoutdata) if 'debug_unwind = true' not in stdoutdata: log_exit("Can't parse unwinding result. Because " + "%s was not generated by the debug-unwind cmd." % args.record_file[0]) unwinding_report = UnwindingResultErrorReport( args.omit_callchains_fixed_by_joiner) process_maps = unwinding_report.process_maps lines = stdoutdata.split('\n') i = 0 while i < len(lines): if lines[i].startswith('record mmap:') or lines[i].startswith( 'record mmap2:'): i += 1 pid = None start = None end = None filename = None while i < len(lines) and not lines[i].startswith('record'): if lines[i].startswith(' pid'): m = re.search( r'pid\s+(\d+).+addr\s+0x(\w+).+len\s+0x(\w+)', lines[i]) if m: pid = int(m.group(1)) start = int(m.group(2), 16) end = start + int(m.group(3), 16) elif 'filename' in lines[i]: pos = lines[i].find('filename') + len('filename') filename = lines[i][pos:].strip() i += 1 if None in [pid, start, end, filename]: log_fatal('unexpected dump output near line %d' % i) process_maps.add(pid, MapEntry(start, end, filename)) elif lines[i].startswith('record unwinding_result:'): i += 1 unwinding_result = collections.OrderedDict() while i < len(lines) and not lines[i].startswith('record'): strs = (lines[i].strip()).split() if len(strs) == 2: unwinding_result[strs[0]] = strs[1] i += 1 for key in ['time', 'used_time', 'stop_reason']: if key not in unwinding_result: log_fatal('unexpected dump output near line %d' % i) i, sample_record = parse_sample_record(lines, i) i, original_record = parse_callchain_record( lines, i, 'ORIGINAL_OFFLINE', process_maps) i, joined_record = parse_callchain_record(lines, i, 'JOINED_OFFLINE', process_maps) if args.omit_sample: sample_record = [] sample_result = SampleResult(original_record.pid, original_record.tid, unwinding_result, original_record.callchain, sample_record) unwinding_report.add_sample_result(sample_result, joined_record) elif lines[i].startswith('record fork:'): i += 1 pid = None ppid = None while i < len(lines) and not lines[i].startswith('record'): if lines[i].startswith(' pid'): m = re.search(r'pid\s+(\w+),\s+ppid\s+(\w+)', lines[i]) if m: pid = int(m.group(1)) ppid = int(m.group(2)) i += 1 if None in [pid, ppid]: log_fatal('unexpected dump output near line %d' % i) process_maps.fork_pid(pid, ppid) elif lines[i].startswith(' debug_unwind_mem'): items = lines[i].strip().split(' = ') if len(items) == 2: unwinding_report.add_mem_stat(items[0], items[1]) i += 1 else: i += 1 return unwinding_report
def build_unwinding_result_report(args): simpleperf_path = get_host_binary_path('simpleperf') proc = subprocess.Popen([simpleperf_path, 'dump', args.record_file[0]], stdout=subprocess.PIPE) (stdoutdata, _) = proc.communicate() stdoutdata = bytes_to_str(stdoutdata) if 'debug_unwind = true' not in stdoutdata: log_exit("Can't parse unwinding result. Because " + "%s was not generated by the debug-unwind cmd." % args.record_file[0]) unwinding_report = UnwindingResultErrorReport(args.omit_callchains_fixed_by_joiner) process_maps = unwinding_report.process_maps lines = stdoutdata.split('\n') i = 0 while i < len(lines): if lines[i].startswith('record mmap:') or lines[i].startswith('record mmap2:'): i += 1 pid = None start = None end = None filename = None while i < len(lines) and not lines[i].startswith('record'): if lines[i].startswith(' pid'): m = re.search(r'pid\s+(\d+).+addr\s+0x(\w+).+len\s+0x(\w+)', lines[i]) if m: pid = int(m.group(1)) start = int(m.group(2), 16) end = start + int(m.group(3), 16) elif 'filename' in lines[i]: pos = lines[i].find('filename') + len('filename') filename = lines[i][pos:].strip() i += 1 if None in [pid, start, end, filename]: log_fatal('unexpected dump output near line %d' % i) process_maps.add(pid, MapEntry(start, end, filename)) elif lines[i].startswith('record unwinding_result:'): i += 1 unwinding_result = collections.OrderedDict() while i < len(lines) and not lines[i].startswith('record'): strs = (lines[i].strip()).split() if len(strs) == 2: unwinding_result[strs[0]] = strs[1] i += 1 for key in ['time', 'used_time', 'stop_reason']: if key not in unwinding_result: log_fatal('unexpected dump output near line %d' % i) i, sample_record = parse_sample_record(lines, i) i, original_record = parse_callchain_record(lines, i, 'ORIGINAL_OFFLINE', process_maps) i, joined_record = parse_callchain_record(lines, i, 'JOINED_OFFLINE', process_maps) if args.omit_sample: sample_record = [] sample_result = SampleResult(original_record.pid, original_record.tid, unwinding_result, original_record.callchain, sample_record) unwinding_report.add_sample_result(sample_result, joined_record) elif lines[i].startswith('record fork:'): i += 1 pid = None ppid = None while i < len(lines) and not lines[i].startswith('record'): if lines[i].startswith(' pid'): m = re.search(r'pid\s+(\w+),\s+ppid\s+(\w+)', lines[i]) if m: pid = int(m.group(1)) ppid = int(m.group(2)) i += 1 if None in [pid, ppid]: log_fatal('unexpected dump output near line %d' % i) process_maps.fork_pid(pid, ppid) elif lines[i].startswith(' debug_unwind_mem'): items = lines[i].strip().split(' = ') if len(items) == 2: unwinding_report.add_mem_stat(items[0], items[1]) i += 1 else: i += 1 return unwinding_report
addr = 0x9 # bus address bus = SMBus(1) # indicates /dev/ic2-1 bus.write_byte(addr, 0) bus.write_byte(addr, 0) time.sleep(.1) estimated_value = 1 while True: # data = bus.read_word_data(addr,0) data = bus.read_i2c_block_data(addr, 1, 2) b1 = bus.read_byte(addr) b2 = bus.read_byte(addr) # val = struct.unpack('h', b1 + b2)[0] val = b2 + (b1 << 8) s = bytes_to_str(b1, b2) print(s + " | " + str(val) + " | " + str(estimated_value)) assert (estimated_value == val) estimated_value += 1 # data = bus.read_i2c_block_data(addr, 0, 1) time.sleep(.01) # bus.write_byte(addr, 0x1) # switch it on # input("Press return to exit") # bus.write_byte(addr, 0x0) # switch it on
def _char_pt_to_str(char_pt): return bytes_to_str(char_pt)
import struct import time from utils import bytes_to_str HEART_ADDR = 0x09 bus = SMBus(1) estimated_value = 127 errors = [] n = 0 bus.write_bytes(HEART_ADDR, b'\x7f\x00') time.sleep(.1) for n in range(2**32): raw_data = bus.read_bytes(HEART_ADDR, 2) val = struct.unpack('h', raw_data)[0] s = bytes_to_str(raw_data[0], raw_data[1]) print(s + " | " + str(val) + " | " + str(estimated_value) + " [ " + str(len(errors)) + " / " + str(n) + " ]" + str(errors)) if estimated_value != val: errors.append(estimated_value) estimated_value += 1 if estimated_value == 1024: estimated_value = 0 # data = bus.read_word_data(HEART_ADDR, 1) # input('press a key') time.sleep(.03) # timing here is super sensitive