Armando Montanez | 5104cd6 | 2019-12-10 14:36:43 -0800 | [diff] [blame] | 1 | // Copyright 2019 The Pigweed Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 4 | // use this file except in compliance with the License. You may obtain a copy of |
| 5 | // the License at |
| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | // License for the specific language governing permissions and limitations under |
| 13 | // the License. |
| 14 | |
| 15 | #include "pw_cpu_exception_armv7m/cpu_state.h" |
| 16 | |
| 17 | #include <cinttypes> |
| 18 | #include <cstdint> |
| 19 | |
| 20 | #include "pw_cpu_exception/cpu_exception.h" |
| 21 | #include "pw_span/span.h" |
| 22 | #include "pw_string/string_builder.h" |
| 23 | |
| 24 | namespace pw::cpu_exception { |
| 25 | |
| 26 | span<const uint8_t> RawFaultingCpuState(const CpuState& cpu_state) { |
| 27 | return span(reinterpret_cast<const uint8_t*>(&cpu_state), sizeof(cpu_state)); |
| 28 | } |
| 29 | |
| 30 | // Using this function adds approximately 100 bytes to binary size. |
| 31 | void ToString(const CpuState& cpu_state, StringBuilder* builder) { |
| 32 | const ArmV7mFaultRegisters& base = cpu_state.base; |
| 33 | const ArmV7mExtraRegisters& extended = cpu_state.extended; |
| 34 | |
| 35 | #define _PW_FORMAT_REGISTER(state_section, name) \ |
| 36 | builder->Format("%s=0x%08" PRIx32 "\n", #name, state_section.name) |
| 37 | |
| 38 | // Other registers. |
| 39 | _PW_FORMAT_REGISTER(base, pc); |
| 40 | _PW_FORMAT_REGISTER(base, lr); |
| 41 | _PW_FORMAT_REGISTER(base, psr); |
| 42 | _PW_FORMAT_REGISTER(extended, msp); |
| 43 | _PW_FORMAT_REGISTER(extended, psp); |
| 44 | _PW_FORMAT_REGISTER(extended, exc_return); |
| 45 | _PW_FORMAT_REGISTER(extended, cfsr); |
| 46 | _PW_FORMAT_REGISTER(extended, mmfar); |
| 47 | _PW_FORMAT_REGISTER(extended, bfar); |
| 48 | _PW_FORMAT_REGISTER(extended, icsr); |
| 49 | _PW_FORMAT_REGISTER(extended, control); |
| 50 | |
| 51 | // General purpose registers. |
| 52 | _PW_FORMAT_REGISTER(base, r0); |
| 53 | _PW_FORMAT_REGISTER(base, r1); |
| 54 | _PW_FORMAT_REGISTER(base, r2); |
| 55 | _PW_FORMAT_REGISTER(base, r3); |
| 56 | _PW_FORMAT_REGISTER(extended, r4); |
| 57 | _PW_FORMAT_REGISTER(extended, r5); |
| 58 | _PW_FORMAT_REGISTER(extended, r6); |
| 59 | _PW_FORMAT_REGISTER(extended, r7); |
| 60 | _PW_FORMAT_REGISTER(extended, r8); |
| 61 | _PW_FORMAT_REGISTER(extended, r9); |
| 62 | _PW_FORMAT_REGISTER(extended, r10); |
| 63 | _PW_FORMAT_REGISTER(extended, r11); |
| 64 | _PW_FORMAT_REGISTER(base, r12); |
| 65 | |
| 66 | #undef _PW_FORMAT_REGISTER |
| 67 | } |
| 68 | |
| 69 | } // namespace pw::cpu_exception |