xref: /aosp_15_r20/external/pigweed/pw_thread_embos/public/pw_thread_embos/snapshot.h (revision 61c4878ac05f98d0ceed94b57d316916de578985)
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 "RTOS.h"
17 #include "pw_protobuf/encoder.h"
18 #include "pw_status/status.h"
19 #include "pw_thread/snapshot.h"
20 #include "pw_thread_protos/thread.pwpb.h"
21 
22 namespace pw::thread::embos {
23 
24 // Captures all embos threads in a system as part of a snapshot.
25 //
26 // An updated running_thread_stack_pointer must be provided in order for the
27 // running thread's context to reflect the running state. For ARM, you might do
28 // something like this:
29 //
30 //    // Capture PSP.
31 //    void* stack_ptr = 0;
32 //    asm volatile("mrs %0, psp\n" : "=r"(stack_ptr));
33 //    pw::thread::ProcessThreadStackCallback cb =
34 //        [](pw::thread::proto::Thread::StreamEncoder& encoder,
35 //           pw::ConstByteSpan stack) -> pw::Status {
36 //      return encoder.WriteRawStack(stack);
37 //    };
38 //    pw::thread::embos::SnapshotThread(stack_ptr, snapshot_encoder, cb);
39 //
40 // Warning: This is only safe to use when interrupts and the scheduler are
41 // disabled!
42 Status SnapshotThreads(void* running_thread_stack_pointer,
43                        proto::SnapshotThreadInfo::StreamEncoder& encoder,
44                        ProcessThreadStackCallback& thread_stack_callback);
45 
46 // Captures only the provided thread handle as a pw::Thread proto message. After
47 // thread info capture, the ProcessThreadStackCallback is called to capture
48 // either the raw_stack or raw_backtrace.
49 //
50 // An updated running_thread_stack_pointer must be provided in order for the
51 // running thread's context to reflect the current state. If the thread being
52 // captured is not the running thread, the value is ignored. Note that the
53 // stack pointer in the thread handle is almost always stale on the running
54 // thread.
55 //
56 // Captures the following proto fields:
57 //   pw.thread.Thread:
58 //     name (when OS_TRACKNAME is enabled)
59 //     state
60 //     stack_start_pointer (when OS_CHECKSTACK or OS_SUPPORT_MPU are enabled)
61 //     stack_end_pointer (when OS_CHECKSTACK or OS_SUPPORT_MPU are enabled)
62 //     stack_pointer
63 //
64 //
65 //
66 //
67 // Warning: This is only safe to use when interrupts and the scheduler are
68 // disabled!
69 Status SnapshotThread(const OS_TASK& thread,
70                       void* running_thread_stack_pointer,
71                       proto::Thread::StreamEncoder& encoder,
72                       ProcessThreadStackCallback& thread_stack_callback);
73 
74 }  // namespace pw::thread::embos
75