1 // Copyright 2021 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 #pragma once
15
16 #include <cstdint>
17
18 #include "pw_cpu_exception_cortex_m/cpu_state.h"
19 #include "pw_cpu_exception_cortex_m_protos/cpu_state.pwpb.h"
20 #include "pw_protobuf/encoder.h"
21 #include "pw_status/status.h"
22 #include "pw_thread/snapshot.h"
23 #include "pw_thread_protos/thread.pwpb.h"
24
25 namespace pw::cpu_exception::cortex_m {
26
27 // Takes the provided pw_cpu_exception_State, and writes the cpu register state
28 // to the provided SnapshotCpuStateOverlay encoder.
29 //
30 // Captures the pw.cpu_exception.cortex_m.ArmV7mCpuState proto field.
31 Status SnapshotCpuState(
32 const pw_cpu_exception_State& cpu_state,
33 cpu_exception::cortex_m::pwpb::SnapshotCpuStateOverlay::StreamEncoder&
34 encoder);
35
36 // Captures the main stack thread if active as part of a snapshot based on a
37 // previously captured cpu_state.
38 Status SnapshotMainStackThread(
39 const pw_cpu_exception_State& cpu_state,
40 uintptr_t stack_low_addr,
41 uintptr_t stack_high_addr,
42 thread::proto::pwpb::SnapshotThreadInfo::StreamEncoder& encoder,
43 thread::ProcessThreadStackCallback& thread_stack_callback);
44
45 // Captures the main stack thread if active as part of a snapshot based on the
46 // current context.
47 //
48 // Note: This is NOT the recommended way to capture the main stack as it will
49 // include the snapshot handling as part of the main stack capture.
50 Status SnapshotMainStackThread(
51 uintptr_t stack_low_addr,
52 uintptr_t stack_high_addr,
53 thread::proto::pwpb::SnapshotThreadInfo::StreamEncoder& encoder,
54 thread::ProcessThreadStackCallback& thread_stack_callback);
55
56 // Captures the main stack thread if active as part of the cpu register state if
57 // optionally provided, else the current context is used.
58 //
59 // Note: This is NOT the recommended way to capture the main stack as it will
60 // include the snapshot handling as part of the main stack capture.
SnapshotMainStackThread(const pw_cpu_exception_State * optional_cpu_state,uintptr_t stack_low_addr,uintptr_t stack_high_addr,thread::proto::pwpb::SnapshotThreadInfo::StreamEncoder & encoder,thread::ProcessThreadStackCallback & thread_stack_callback)61 inline Status SnapshotMainStackThread(
62 const pw_cpu_exception_State* optional_cpu_state,
63 uintptr_t stack_low_addr,
64 uintptr_t stack_high_addr,
65 thread::proto::pwpb::SnapshotThreadInfo::StreamEncoder& encoder,
66 thread::ProcessThreadStackCallback& thread_stack_callback) {
67 if (optional_cpu_state != nullptr) {
68 return SnapshotMainStackThread(*optional_cpu_state,
69 stack_low_addr,
70 stack_high_addr,
71 encoder,
72 thread_stack_callback);
73 }
74 return SnapshotMainStackThread(
75 stack_low_addr, stack_high_addr, encoder, thread_stack_callback);
76 }
77
78 } // namespace pw::cpu_exception::cortex_m
79