• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 HdlcRpcClient, default_channels
40from pw_trace_tokenized import trace_tokenized
41
42_LOG = logging.getLogger('pw_trace_tokenizer')
43
44PW_RPC_MAX_PACKET_SIZE = 256
45SOCKET_SERVER = 'localhost'
46SOCKET_PORT = 33000
47MKFIFO_MODE = 0o666
48
49
50class SocketClientImpl:
51    def __init__(self, config: str):
52        self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
53        socket_server = ''
54        socket_port = 0
55
56        if config == 'default':
57            socket_server = SOCKET_SERVER
58            socket_port = SOCKET_PORT
59        else:
60            socket_server, socket_port_str = config.split(':')
61            socket_port = int(socket_port_str)
62        self.socket.connect((socket_server, socket_port))
63
64    def write(self, data: bytes):
65        self.socket.sendall(data)
66
67    def read(self, num_bytes: int = PW_RPC_MAX_PACKET_SIZE):
68        return self.socket.recv(num_bytes)
69
70
71def _expand_globs(globs: Iterable[str]) -> Iterator[Path]:
72    for pattern in globs:
73        for file in glob.glob(pattern, recursive=True):
74            yield Path(file)
75
76
77def get_hdlc_rpc_client(
78    device: str,
79    baudrate: int,
80    proto_globs: Collection[str],
81    socket_addr: str,
82    **kwargs,
83):
84    """Get the HdlcRpcClient based on arguments."""
85    del kwargs  # ignore
86    if not proto_globs:
87        proto_globs = ['**/*.proto']
88
89    protos = list(_expand_globs(proto_globs))
90
91    if not protos:
92        _LOG.critical(
93            'No .proto files were found with %s', ', '.join(proto_globs)
94        )
95        _LOG.critical('At least one .proto file is required')
96        return 1
97
98    _LOG.debug(
99        'Found %d .proto files found with %s',
100        len(protos),
101        ', '.join(proto_globs),
102    )
103
104    # TODO(rgoliver): When pw has a generalized transport for RPC this should
105    # use it so it isn't specific to HDLC
106    if socket_addr is None:
107        serial_device = serial.Serial(device, baudrate, timeout=1)
108        read = lambda: serial_device.read(8192)
109        write = serial_device.write
110    else:
111        try:
112            socket_device = SocketClientImpl(socket_addr)
113            read = socket_device.read
114            write = socket_device.write
115        except ValueError:
116            _LOG.exception('Failed to initialize socket at %s', socket_addr)
117            return 1
118
119    return HdlcRpcClient(read, protos, default_channels(write))
120
121
122def get_trace_data_from_device(client):
123    """Get the trace data using RPC from a Client"""
124    data = b''
125    service = client.client.channel(1).rpcs.pw.trace.TraceService
126    result = service.GetTraceData().responses
127    for streamed_data in result:
128        data = data + bytes([len(streamed_data.data)])
129        data = data + streamed_data.data
130        _LOG.debug(''.join(format(x, '02x') for x in streamed_data.data))
131    return data
132
133
134def _parse_args():
135    """Parse and return command line arguments."""
136
137    parser = argparse.ArgumentParser(
138        description=__doc__,
139        formatter_class=argparse.RawDescriptionHelpFormatter,
140    )
141    group = parser.add_mutually_exclusive_group(required=True)
142    group.add_argument('-d', '--device', help='the serial port to use')
143    parser.add_argument(
144        '-b',
145        '--baudrate',
146        type=int,
147        default=115200,
148        help='the baud rate to use',
149    )
150    group.add_argument(
151        '-s',
152        '--socket-addr',
153        type=str,
154        help='use socket to connect to server, type default for\
155            localhost:33000, or manually input the server address:port',
156    )
157    parser.add_argument(
158        '-o',
159        '--trace_output',
160        dest='trace_output_file',
161        help=('The json file to which to write the output.'),
162    )
163    parser.add_argument(
164        '-t',
165        '--trace_token_database',
166        help='Databases (ELF, binary, or CSV) to use to lookup trace tokens.',
167    )
168    parser.add_argument(
169        'proto_globs', nargs='+', help='glob pattern for .proto files'
170    )
171    parser.add_argument(
172        '-f',
173        '--ticks_per_second',
174        type=int,
175        dest='ticks_per_second',
176        default=1000,
177        help=('The clock rate of the trace events (Default 1000).'),
178    )
179    parser.add_argument(
180        '--time_offset',
181        type=int,
182        dest='time_offset',
183        default=0,
184        help=('Time offset (us) of the trace events (Default 0).'),
185    )
186    return parser.parse_args()
187
188
189def _main(args):
190    token_database = database.load_token_database(
191        args.trace_token_database, domain="trace"
192    )
193    _LOG.info(database.database_summary(token_database))
194    client = get_hdlc_rpc_client(**vars(args))
195    data = get_trace_data_from_device(client)
196    events = trace_tokenized.get_trace_events(
197        [token_database], data, args.ticks_per_second, args.time_offset
198    )
199    json_lines = trace.generate_trace_json(events)
200    trace_tokenized.save_trace_file(json_lines, args.trace_output_file)
201
202
203if __name__ == '__main__':
204    if sys.version_info[0] < 3:
205        sys.exit('ERROR: The detokenizer command line tools require Python 3.')
206    _main(_parse_args())
207