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
15 #define PW_LOG_LEVEL PW_THREAD_CONFIG_LOG_LEVEL
16
17 #include "pw_thread/snapshot.h"
18
19 #include <cinttypes>
20 #include <string_view>
21
22 #include "pw_bytes/span.h"
23 #include "pw_function/function.h"
24 #include "pw_log/log.h"
25 #include "pw_protobuf/encoder.h"
26 #include "pw_status/status.h"
27 #include "pw_thread/config.h"
28 #include "pw_thread_protos/thread.pwpb.h"
29
30 namespace pw::thread {
31
SnapshotStack(const StackContext & stack,proto::pwpb::Thread::StreamEncoder & encoder,const ProcessThreadStackCallback & thread_stack_callback)32 Status SnapshotStack(const StackContext& stack,
33 proto::pwpb::Thread::StreamEncoder& encoder,
34 const ProcessThreadStackCallback& thread_stack_callback) {
35 // TODO: b/234890430 - Add support for ascending stacks.
36 encoder.WriteStackStartPointer(stack.stack_high_addr).IgnoreError();
37 encoder.WriteStackEndPointer(stack.stack_low_addr).IgnoreError();
38 encoder.WriteStackPointer(stack.stack_pointer).IgnoreError();
39 // The PRIxPTR is an appropriate format specifier for hex uintptr_t values
40 // https://stackoverflow.com/a/5796039/1224002
41 PW_LOG_DEBUG("Active stack: 0x%08" PRIxPTR "-0x%08" PRIxPTR " (%ld bytes)",
42 stack.stack_high_addr,
43 stack.stack_pointer,
44 static_cast<long>(stack.stack_high_addr) -
45 static_cast<long>(stack.stack_pointer));
46 if (stack.stack_pointer_est_peak.has_value()) {
47 const uintptr_t stack_pointer_est_peak =
48 stack.stack_pointer_est_peak.value();
49 encoder.WriteStackPointerEstPeak(stack_pointer_est_peak).IgnoreError();
50 PW_LOG_DEBUG("Est peak stack: 0x%08" PRIxPTR "-0x%08" PRIxPTR
51 " (%ld bytes)",
52 stack.stack_high_addr,
53 stack_pointer_est_peak,
54 static_cast<long>(stack.stack_high_addr) -
55 static_cast<long>(stack_pointer_est_peak));
56 }
57 PW_LOG_DEBUG("Stack Limits: 0x%08" PRIxPTR "-0x%08" PRIxPTR " (%ld bytes)",
58 stack.stack_low_addr,
59 stack.stack_high_addr,
60 static_cast<long>(stack.stack_high_addr) -
61 static_cast<long>(stack.stack_low_addr));
62
63 if (stack.stack_pointer > stack.stack_high_addr) {
64 PW_LOG_ERROR("%s's stack underflowed by %lu bytes",
65 stack.thread_name.data(),
66 static_cast<long unsigned>(stack.stack_pointer -
67 stack.stack_high_addr));
68 return Status::OutOfRange();
69 }
70
71 // Log an error, but don't prevent the capture.
72 if (stack.stack_pointer < stack.stack_low_addr) {
73 PW_LOG_ERROR(
74 "%s's stack overflowed by %lu bytes",
75 stack.thread_name.data(),
76 static_cast<long unsigned>(stack.stack_low_addr - stack.stack_pointer));
77 }
78
79 return thread_stack_callback(
80 encoder,
81 ConstByteSpan(reinterpret_cast<const std::byte*>(stack.stack_pointer),
82 stack.stack_high_addr - stack.stack_pointer));
83 }
84
85 } // namespace pw::thread
86