1#!/usr/bin/env python3 2# Copyright 2021 The Pigweed Authors 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); you may not 5# use this file except in compliance with the License. You may obtain a copy of 6# the License at 7# 8# https://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13# License for the specific language governing permissions and limitations under 14# the License. 15""" 16Generates json trace files viewable using chrome://tracing using RPCs from a 17connected HdlcRpcClient. 18 19Example usage: 20python pw_trace_tokenized/py/pw_trace_tokenized/get_trace.py -s localhost:33000 21 -o trace.json 22 -t 23 out/pw_strict_host_clang_debug/obj/pw_trace_tokenized/bin/trace_tokenized_example_rpc 24 pw_trace_tokenized/pw_trace_protos/trace_rpc.proto 25""" # pylint: disable=line-too-long 26# pylint: enable=line-too-long 27 28import argparse 29import glob 30import logging 31from pathlib import Path 32import socket 33import sys 34from typing import Collection, Iterable, Iterator 35 36import serial 37from pw_tokenizer import database 38from pw_trace import trace 39from pw_hdlc.rpc import ( 40 HdlcRpcClient, 41 default_channels, 42 SerialReader, 43 SocketReader, 44) 45from pw_trace_tokenized import trace_tokenized 46 47_LOG = logging.getLogger('pw_trace_tokenizer') 48 49PW_RPC_MAX_PACKET_SIZE = 256 50SOCKET_SERVER = 'localhost' 51SOCKET_PORT = 33000 52MKFIFO_MODE = 0o666 53 54 55class SocketClientImpl: 56 def __init__(self, config: str): 57 self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 58 socket_server = '' 59 socket_port = 0 60 61 if config == 'default': 62 socket_server = SOCKET_SERVER 63 socket_port = SOCKET_PORT 64 else: 65 socket_server, socket_port_str = config.split(':') 66 socket_port = int(socket_port_str) 67 self.socket.connect((socket_server, socket_port)) 68 69 def write(self, data: bytes): 70 self.socket.sendall(data) 71 72 def read(self, num_bytes: int = PW_RPC_MAX_PACKET_SIZE): 73 return self.socket.recv(num_bytes) 74 75 76def _expand_globs(globs: Iterable[str]) -> Iterator[Path]: 77 for pattern in globs: 78 for file in glob.glob(pattern, recursive=True): 79 yield Path(file) 80 81 82def get_hdlc_rpc_client( 83 device: str, 84 baudrate: int, 85 proto_globs: Collection[str], 86 socket_addr: str, 87 **kwargs, 88): 89 """Get the HdlcRpcClient based on arguments.""" 90 del kwargs # ignore 91 if not proto_globs: 92 proto_globs = ['**/*.proto'] 93 94 protos = list(_expand_globs(proto_globs)) 95 96 if not protos: 97 _LOG.critical( 98 'No .proto files were found with %s', ', '.join(proto_globs) 99 ) 100 _LOG.critical('At least one .proto file is required') 101 return 1 102 103 _LOG.debug( 104 'Found %d .proto files found with %s', 105 len(protos), 106 ', '.join(proto_globs), 107 ) 108 109 # TODO(rgoliver): When pw has a generalized transport for RPC this should 110 # use it so it isn't specific to HDLC 111 if socket_addr is None: 112 serial_device = serial.Serial(device, baudrate, timeout=1) 113 reader = SerialReader(serial_device) 114 write_function = serial_device.write 115 else: 116 try: 117 socket_device = SocketClientImpl(socket_addr) 118 reader = SocketReader(socket_device.socket, PW_RPC_MAX_PACKET_SIZE) 119 write_function = socket_device.write 120 except ValueError: 121 _LOG.exception('Failed to initialize socket at %s', socket_addr) 122 return 1 123 124 return HdlcRpcClient(reader, protos, default_channels(write_function)) 125 126 127def get_trace_data_from_device(client): 128 """Get the trace data using RPC from a Client""" 129 data = b'' 130 service = client.client.channel(1).rpcs.pw.trace.TraceService 131 result = service.GetTraceData().responses 132 for streamed_data in result: 133 data = data + bytes([len(streamed_data.data)]) 134 data = data + streamed_data.data 135 _LOG.debug(''.join(format(x, '02x') for x in streamed_data.data)) 136 return data 137 138 139def _parse_args(): 140 """Parse and return command line arguments.""" 141 142 parser = argparse.ArgumentParser( 143 description=__doc__, 144 formatter_class=argparse.RawDescriptionHelpFormatter, 145 ) 146 group = parser.add_mutually_exclusive_group(required=True) 147 group.add_argument('-d', '--device', help='the serial port to use') 148 parser.add_argument( 149 '-b', 150 '--baudrate', 151 type=int, 152 default=115200, 153 help='the baud rate to use', 154 ) 155 group.add_argument( 156 '-s', 157 '--socket-addr', 158 type=str, 159 help='use socket to connect to server, type default for\ 160 localhost:33000, or manually input the server address:port', 161 ) 162 parser.add_argument( 163 '-o', 164 '--trace_output', 165 dest='trace_output_file', 166 help=('The json file to which to write the output.'), 167 ) 168 parser.add_argument( 169 '-t', 170 '--trace_token_database', 171 help='Databases (ELF, binary, or CSV) to use to lookup trace tokens.', 172 ) 173 parser.add_argument( 174 'proto_globs', nargs='+', help='glob pattern for .proto files' 175 ) 176 parser.add_argument( 177 '-f', 178 '--ticks_per_second', 179 type=int, 180 dest='ticks_per_second', 181 default=1000, 182 help=('The clock rate of the trace events (Default 1000).'), 183 ) 184 parser.add_argument( 185 '--time_offset', 186 type=int, 187 dest='time_offset', 188 default=0, 189 help=('Time offset (us) of the trace events (Default 0).'), 190 ) 191 return parser.parse_args() 192 193 194def _main(args): 195 token_database = database.load_token_database( 196 args.trace_token_database, domain="trace" 197 ) 198 _LOG.info(database.database_summary(token_database)) 199 client = get_hdlc_rpc_client(**vars(args)) 200 data = get_trace_data_from_device(client) 201 events = trace_tokenized.get_trace_events( 202 [token_database], data, args.ticks_per_second, args.time_offset 203 ) 204 json_lines = trace.generate_trace_json(events) 205 trace_tokenized.save_trace_file(json_lines, args.trace_output_file) 206 207 208if __name__ == '__main__': 209 if sys.version_info[0] < 3: 210 sys.exit('ERROR: The detokenizer command line tools require Python 3.') 211 _main(_parse_args()) 212