xref: /aosp_15_r20/external/pigweed/pw_thread_threadx/public/pw_thread_threadx/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 "pw_protobuf/encoder.h"
17 #include "pw_status/status.h"
18 #include "pw_thread/snapshot.h"
19 #include "pw_thread_protos/thread.pwpb.h"
20 #include "tx_api.h"
21 
22 namespace pw::thread::threadx {
23 
24 // Captures all threadx 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::threadx::SnapshotThread(my_thread, stack_ptr,
39 //                                        snapshot_encoder, cb);
40 //
41 // Warning: This is only safe to use when interrupts and the scheduler are
42 // disabled!
43 // Warning: SMP ports are not yet supported.
44 Status SnapshotThreads(void* running_thread_stack_pointer,
45                        proto::SnapshotThreadInfo::StreamEncoder& encoder,
46                        ProcessThreadStackCallback& thread_stack_callback);
47 
48 // Captures only the provided thread handle as a pw::Thread proto message. After
49 // thread info capture, the ProcessThreadStackCallback is called to capture
50 // either the raw_stack or raw_backtrace.
51 //
52 // An updated running_thread_stack_pointer must be provided in order for the
53 // running thread's context to reflect the current state. If the thread being
54 // captured is not the running thread, the value is ignored. Note that the
55 // stack pointer in the thread handle is almost always stale on the running
56 // thread.
57 //
58 // Captures the following proto fields:
59 //   pw.thread.Thread:
60 //     name
61 //     state
62 //     stack_start_pointer
63 //     stack_end_pointer
64 //     stack_pointer
65 //
66 // Warning: This is only safe to use when interrupts and the scheduler are
67 // disabled!
68 // Warning: SMP ports are not yet supported.
69 Status SnapshotThread(const TX_THREAD& thread,
70                       void* running_thread_stack_pointer,
71                       proto::Thread::StreamEncoder& encoder,
72                       ProcessThreadStackCallback& thread_stack_callback);
73 
74 }  // namespace pw::thread::threadx
75