xref: /aosp_15_r20/external/pigweed/pw_cpu_exception_risc_v/public/pw_cpu_exception_risc_v/snapshot.h (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2024 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_risc_v/cpu_state.h"
19 #include "pw_cpu_exception_risc_v_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::risc_v {
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.risc_v.RiscvmCpuState proto field.
31 Status SnapshotCpuState(
32     const pw_cpu_exception_State& cpu_state,
33     cpu_exception::risc_v::SnapshotCpuStateOverlay::StreamEncoder& encoder);
34 
35 // Captures the main stack thread if active as part of a snapshot based on a
36 // previously captured cpu_state.
37 Status SnapshotMainStackThread(
38     const pw_cpu_exception_State& cpu_state,
39     uintptr_t stack_low_addr,
40     uintptr_t stack_high_addr,
41     thread::proto::SnapshotThreadInfo::StreamEncoder& encoder,
42     thread::ProcessThreadStackCallback& thread_stack_callback);
43 
44 // Captures the main stack thread if active as part of a snapshot based on the
45 // current context.
46 //
47 // Note: This is NOT the recommended way to capture the main stack as it will
48 // include the snapshot handling as part of the main stack capture.
49 Status SnapshotMainStackThread(
50     uintptr_t stack_low_addr,
51     uintptr_t stack_high_addr,
52     thread::proto::SnapshotThreadInfo::StreamEncoder& encoder,
53     thread::ProcessThreadStackCallback& thread_stack_callback);
54 
55 // Captures the main stack thread if active as part of the cpu register state if
56 // optionally provided, else the current context is used.
57 //
58 // Note: This is NOT the recommended way to capture the main stack as it will
59 // 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::SnapshotThreadInfo::StreamEncoder & encoder,thread::ProcessThreadStackCallback & thread_stack_callback)60 inline Status SnapshotMainStackThread(
61     const pw_cpu_exception_State* optional_cpu_state,
62     uintptr_t stack_low_addr,
63     uintptr_t stack_high_addr,
64     thread::proto::SnapshotThreadInfo::StreamEncoder& encoder,
65     thread::ProcessThreadStackCallback& thread_stack_callback) {
66   if (optional_cpu_state != nullptr) {
67     return SnapshotMainStackThread(*optional_cpu_state,
68                                    stack_low_addr,
69                                    stack_high_addr,
70                                    encoder,
71                                    thread_stack_callback);
72   }
73   return SnapshotMainStackThread(
74       stack_low_addr, stack_high_addr, encoder, thread_stack_callback);
75 }
76 
77 }  // namespace pw::cpu_exception::risc_v
78