aboutsummaryrefslogtreecommitdiff
path: root/pw_cpu_exception_cortex_m/support.cc
blob: 719ac5b61a09c496a8a4b54c2ec521af17077351 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright 2019 The Pigweed Authors
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.

#include "pw_cpu_exception/support.h"

#include <cinttypes>
#include <cstdint>
#include <span>

#include "pw_cpu_exception_cortex_m/cpu_state.h"
#include "pw_cpu_exception_cortex_m/util.h"
#include "pw_cpu_exception_cortex_m_private/config.h"
#include "pw_cpu_exception_cortex_m_private/cortex_m_constants.h"
#include "pw_log/log.h"
#include "pw_preprocessor/arch.h"
#include "pw_string/string_builder.h"

namespace pw::cpu_exception {

std::span<const uint8_t> RawFaultingCpuState(
    const pw_cpu_exception_State& cpu_state) {
  return std::span(reinterpret_cast<const uint8_t*>(&cpu_state),
                   sizeof(cpu_state));
}

// Using this function adds approximately 100 bytes to binary size.
void ToString(const pw_cpu_exception_State& cpu_state,
              const std::span<char>& dest) {
  StringBuilder builder(dest);
  const cortex_m::ExceptionRegisters& base = cpu_state.base;
  const cortex_m::ExtraRegisters& extended = cpu_state.extended;

#define _PW_FORMAT_REGISTER(state_section, name) \
  builder.Format("%s=0x%08" PRIx32 "\n", #name, state_section.name)

  // Other registers.
  if (base.pc != cortex_m::kUndefinedPcLrOrPsrRegValue) {
    _PW_FORMAT_REGISTER(base, pc);
  }
  if (base.lr != cortex_m::kUndefinedPcLrOrPsrRegValue) {
    _PW_FORMAT_REGISTER(base, lr);
  }
  if (base.psr != cortex_m::kUndefinedPcLrOrPsrRegValue) {
    _PW_FORMAT_REGISTER(base, psr);
  }
  _PW_FORMAT_REGISTER(extended, msp);
  _PW_FORMAT_REGISTER(extended, psp);
  _PW_FORMAT_REGISTER(extended, exc_return);
#if _PW_ARCH_ARM_V8M_MAINLINE
  _PW_FORMAT_REGISTER(extended, msplim);
  _PW_FORMAT_REGISTER(extended, psplim);
#endif  // _PW_ARCH_ARM_V8M_MAINLINE
  _PW_FORMAT_REGISTER(extended, cfsr);
  _PW_FORMAT_REGISTER(extended, mmfar);
  _PW_FORMAT_REGISTER(extended, bfar);
  _PW_FORMAT_REGISTER(extended, icsr);
  _PW_FORMAT_REGISTER(extended, hfsr);
  _PW_FORMAT_REGISTER(extended, shcsr);
  _PW_FORMAT_REGISTER(extended, control);

  // General purpose registers.
  _PW_FORMAT_REGISTER(base, r0);
  _PW_FORMAT_REGISTER(base, r1);
  _PW_FORMAT_REGISTER(base, r2);
  _PW_FORMAT_REGISTER(base, r3);
  _PW_FORMAT_REGISTER(extended, r4);
  _PW_FORMAT_REGISTER(extended, r5);
  _PW_FORMAT_REGISTER(extended, r6);
  _PW_FORMAT_REGISTER(extended, r7);
  _PW_FORMAT_REGISTER(extended, r8);
  _PW_FORMAT_REGISTER(extended, r9);
  _PW_FORMAT_REGISTER(extended, r10);
  _PW_FORMAT_REGISTER(extended, r11);
  _PW_FORMAT_REGISTER(base, r12);

#undef _PW_FORMAT_REGISTER
}

// Using this function adds approximately 100 bytes to binary size.
void LogCpuState(const pw_cpu_exception_State& cpu_state) {
  const cortex_m::ExceptionRegisters& base = cpu_state.base;
  const cortex_m::ExtraRegisters& extended = cpu_state.extended;

  cortex_m::LogExceptionAnalysis(cpu_state);

  PW_LOG_INFO("All captured CPU registers:");

#define _PW_LOG_REGISTER(state_section, name) \
  PW_LOG_INFO("  %-10s 0x%08" PRIx32, #name, state_section.name)

  // Other registers.
  if (base.pc != cortex_m::kUndefinedPcLrOrPsrRegValue) {
    _PW_LOG_REGISTER(base, pc);
  }
  if (base.lr != cortex_m::kUndefinedPcLrOrPsrRegValue) {
    _PW_LOG_REGISTER(base, lr);
  }
  if (base.psr != cortex_m::kUndefinedPcLrOrPsrRegValue) {
    _PW_LOG_REGISTER(base, psr);
  }
  _PW_LOG_REGISTER(extended, msp);
  _PW_LOG_REGISTER(extended, psp);
  _PW_LOG_REGISTER(extended, exc_return);
#if _PW_ARCH_ARM_V8M_MAINLINE
  _PW_LOG_REGISTER(extended, msplim);
  _PW_LOG_REGISTER(extended, psplim);
#endif  // _PW_ARCH_ARM_V8M_MAINLINE
  _PW_LOG_REGISTER(extended, cfsr);
  _PW_LOG_REGISTER(extended, mmfar);
  _PW_LOG_REGISTER(extended, bfar);
  _PW_LOG_REGISTER(extended, icsr);
  _PW_LOG_REGISTER(extended, hfsr);
  _PW_LOG_REGISTER(extended, shcsr);
  _PW_LOG_REGISTER(extended, control);

  // General purpose registers.
  _PW_LOG_REGISTER(base, r0);
  _PW_LOG_REGISTER(base, r1);
  _PW_LOG_REGISTER(base, r2);
  _PW_LOG_REGISTER(base, r3);
  _PW_LOG_REGISTER(extended, r4);
  _PW_LOG_REGISTER(extended, r5);
  _PW_LOG_REGISTER(extended, r6);
  _PW_LOG_REGISTER(extended, r7);
  _PW_LOG_REGISTER(extended, r8);
  _PW_LOG_REGISTER(extended, r9);
  _PW_LOG_REGISTER(extended, r10);
  _PW_LOG_REGISTER(extended, r11);
  _PW_LOG_REGISTER(base, r12);

#undef _PW_LOG_REGISTER
}

}  // namespace pw::cpu_exception