Armando Montanez | fec572b | 2021-06-28 12:13:57 -0700 | [diff] [blame^] | 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 | #include "pw_thread/snapshot.h" |
| 15 | |
| 16 | #include <string_view> |
| 17 | |
| 18 | #include "pw_bytes/span.h" |
| 19 | #include "pw_function/function.h" |
| 20 | #include "pw_log/log.h" |
| 21 | #include "pw_protobuf/encoder.h" |
| 22 | #include "pw_status/status.h" |
| 23 | #include "pw_thread_protos/thread.pwpb.h" |
| 24 | |
| 25 | namespace pw::thread { |
| 26 | |
| 27 | Status SnapshotStack(const StackContext& stack, |
| 28 | Thread::StreamEncoder& encoder, |
| 29 | ProcessThreadStackCallback& thread_stack_callback) { |
| 30 | // TODO(pwbug/422): Add support for ascending stacks. |
| 31 | encoder.WriteStackStartPointer(stack.stack_high_addr); |
| 32 | encoder.WriteStackEndPointer(stack.stack_low_addr); |
| 33 | encoder.WriteStackPointer(stack.stack_pointer); |
| 34 | |
| 35 | if (stack.stack_pointer > stack.stack_high_addr) { |
| 36 | PW_LOG_ERROR("%s's stack underflowed by %lu bytes", |
| 37 | stack.thread_name.data(), |
| 38 | static_cast<long unsigned>(stack.stack_pointer - |
| 39 | stack.stack_high_addr)); |
| 40 | return Status::OutOfRange(); |
| 41 | } |
| 42 | |
| 43 | // Log an error, but don't prevent the capture. |
| 44 | if (stack.stack_pointer < stack.stack_low_addr) { |
| 45 | PW_LOG_ERROR( |
| 46 | "%s's stack overflowed by %lu bytes", |
| 47 | stack.thread_name.data(), |
| 48 | static_cast<long unsigned>(stack.stack_low_addr - stack.stack_pointer)); |
| 49 | } |
| 50 | |
| 51 | return thread_stack_callback( |
| 52 | encoder, |
| 53 | ConstByteSpan(reinterpret_cast<const std::byte*>(stack.stack_pointer), |
| 54 | stack.stack_high_addr - stack.stack_pointer)); |
| 55 | } |
| 56 | |
| 57 | } // namespace pw::thread |