1#!/usr/bin/env python3 2# Copyright 2024 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"""Tests dumped RISCV CPU state.""" 16 17import unittest 18 19from pw_cpu_exception_risc_v import exception_analyzer 20from pw_cpu_exception_risc_v_protos import cpu_state_pb2 21import pw_symbolizer 22 23# pylint: disable=protected-access 24 25 26class TextDumpTest(unittest.TestCase): 27 """Test larger state dumps.""" 28 29 def test_registers(self): 30 """Validate output of general register dumps.""" 31 cpu_state_proto = cpu_state_pb2.RiscvCpuState() 32 cpu_state_proto.mepc = 0xDFADD966 33 cpu_state_proto.mcause = 0xAF2EA98A 34 cpu_state_proto.mstatus = 0xF3B235B1 35 cpu_state_info = exception_analyzer.RiscvExceptionAnalyzer( 36 cpu_state_proto 37 ) 38 expected_dump = '\n'.join( 39 ( 40 'mepc 0xdfadd966', 41 'mcause 0xaf2ea98a', 42 'mstatus 0xf3b235b1', 43 ) 44 ) 45 self.assertEqual(cpu_state_info.dump_registers(), expected_dump) 46 47 def test_symbolization(self): 48 """Ensure certain registers are symbolized.""" 49 cpu_state_proto = cpu_state_pb2.RiscvCpuState() 50 known_symbols = ( 51 pw_symbolizer.Symbol(0x0800A200, 'foo()', 'src/foo.c', 41), 52 ) 53 symbolizer = pw_symbolizer.FakeSymbolizer(known_symbols) 54 cpu_state_proto.mepc = 0x0800A200 55 cpu_state_info = exception_analyzer.RiscvExceptionAnalyzer( 56 cpu_state_proto, symbolizer 57 ) 58 expected_dump = '\n'.join( 59 ('mepc 0x0800a200 foo() (src/foo.c:41)',) 60 ) 61 self.assertEqual(cpu_state_info.dump_registers(), expected_dump) 62 63 64if __name__ == '__main__': 65 unittest.main() 66