xref: /aosp_15_r20/external/pigweed/pw_snapshot/py/generate_example_snapshot.py (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1#!/usr/bin/env python3
2# Copyright 2020 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"""Generates an example snapshot useful for updating documentation."""
16
17import argparse
18import sys
19from typing import TextIO
20from pw_snapshot_protos import snapshot_pb2
21from pw_snapshot import processor
22from pw_thread_protos import thread_pb2
23
24
25def _add_threads(snapshot: snapshot_pb2.Snapshot) -> snapshot_pb2.Snapshot:
26    # Build example idle thread.
27    thread = thread_pb2.Thread()
28    thread.name = 'Idle'.encode()
29    thread.stack_start_pointer = 0x2001AC00
30    thread.stack_end_pointer = 0x2001AA00
31    thread.stack_pointer = 0x2001AB0C
32    thread.state = thread_pb2.ThreadState.Enum.RUNNING
33    snapshot.threads.append(thread)
34
35    # Build example interrupt handler thread.
36    thread = thread_pb2.Thread()
37    thread.name = 'Main Stack (Handler Mode)'.encode()
38    thread.active = True
39    thread.stack_start_pointer = 0x2001B000
40    thread.stack_pointer = 0x2001AE20
41    thread.state = thread_pb2.ThreadState.Enum.INTERRUPT_HANDLER
42    thread.raw_stack = b'\x00\xCA\xAD\xDE'
43    snapshot.threads.append(thread)
44
45    return snapshot
46
47
48def _main(out_file: TextIO):
49    snapshot = snapshot_pb2.Snapshot()
50
51    snapshot.metadata.reason = (
52        '■msg♦Assert failed: 1+1 == 42' '■file♦../examples/example_rpc.cc'
53    ).encode('utf-8')
54    snapshot.metadata.fatal = True
55    snapshot.metadata.project_name = 'gShoe'.encode('utf-8')
56    snapshot.metadata.software_version = 'QUANTUM_CORE-0.1.325-e4a84b1a'
57    snapshot.metadata.software_build_uuid = (
58        b'\xAD\x2D\x39\x25\x8C\x1B\xC4\x87'
59        b'\xF0\x7C\xA7\xE0\x49\x91\xA8\x36'
60        b'\xFD\xF7\xD0\xA0'
61    )
62    snapshot.metadata.device_name = 'GSHOE-QUANTUM_CORE-REV_0.1'.encode('utf-8')
63    snapshot.metadata.snapshot_uuid = (
64        b'\x84\x81\xBB\x12\xA1\x62\x16\x4F' b'\x5C\x74\x85\x5F\x6D\x94\xEA\x1A'
65    )
66
67    # Add some thread-related info.
68    snapshot = _add_threads(snapshot)
69
70    serialized_snapshot = snapshot.SerializeToString()
71    out_file.write(processor.process_snapshots(serialized_snapshot))
72
73
74def _parse_args():
75    parser = argparse.ArgumentParser(description=__doc__)
76    parser.add_argument(
77        '--out-file',
78        '-o',
79        type=argparse.FileType('w'),
80        help='File to output serialized snapshot to.',
81    )
82
83    return parser.parse_args()
84
85
86if __name__ == '__main__':
87    _main(**vars(_parse_args()))
88    sys.exit(0)
89