Mathieu Chartier | 3458359 | 2017-03-23 23:51:34 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef ART_RUNTIME_BACKTRACE_HELPER_H_ |
| 18 | #define ART_RUNTIME_BACKTRACE_HELPER_H_ |
| 19 | |
Alex Light | 543d845 | 2018-07-13 16:25:58 +0000 | [diff] [blame] | 20 | #include <stddef.h> |
| 21 | #include <stdint.h> |
Mathieu Chartier | 3458359 | 2017-03-23 23:51:34 -0700 | [diff] [blame] | 22 | |
David Srbecky | a3e1240 | 2021-03-23 13:36:55 +0000 | [diff] [blame] | 23 | namespace unwindstack { |
| 24 | class Unwinder; |
| 25 | } |
| 26 | |
Mathieu Chartier | 3458359 | 2017-03-23 23:51:34 -0700 | [diff] [blame] | 27 | namespace art { |
| 28 | |
Alex Light | 543d845 | 2018-07-13 16:25:58 +0000 | [diff] [blame] | 29 | // Using libbacktrace |
Mathieu Chartier | 3458359 | 2017-03-23 23:51:34 -0700 | [diff] [blame] | 30 | class BacktraceCollector { |
| 31 | public: |
| 32 | BacktraceCollector(uintptr_t* out_frames, size_t max_depth, size_t skip_count) |
| 33 | : out_frames_(out_frames), max_depth_(max_depth), skip_count_(skip_count) {} |
| 34 | |
| 35 | size_t NumFrames() const { |
| 36 | return num_frames_; |
| 37 | } |
| 38 | |
| 39 | // Collect the backtrace, do not call more than once. |
Alex Light | 543d845 | 2018-07-13 16:25:58 +0000 | [diff] [blame] | 40 | void Collect(); |
Mathieu Chartier | 3458359 | 2017-03-23 23:51:34 -0700 | [diff] [blame] | 41 | |
| 42 | private: |
David Srbecky | 2f78a9c | 2020-01-17 15:53:42 +0000 | [diff] [blame] | 43 | // Try to collect backtrace. Returns false on failure. |
| 44 | // It is used to retry backtrace on temporary failure. |
David Srbecky | a3e1240 | 2021-03-23 13:36:55 +0000 | [diff] [blame] | 45 | bool CollectImpl(unwindstack::Unwinder* unwinder); |
David Srbecky | 2f78a9c | 2020-01-17 15:53:42 +0000 | [diff] [blame] | 46 | |
Mathieu Chartier | 3458359 | 2017-03-23 23:51:34 -0700 | [diff] [blame] | 47 | uintptr_t* const out_frames_ = nullptr; |
| 48 | size_t num_frames_ = 0u; |
| 49 | const size_t max_depth_ = 0u; |
| 50 | size_t skip_count_ = 0u; |
| 51 | }; |
| 52 | |
| 53 | // A bounded sized backtrace. |
| 54 | template <size_t kMaxFrames> |
| 55 | class FixedSizeBacktrace { |
| 56 | public: |
| 57 | void Collect(size_t skip_count) { |
| 58 | BacktraceCollector collector(frames_, kMaxFrames, skip_count); |
| 59 | collector.Collect(); |
| 60 | num_frames_ = collector.NumFrames(); |
| 61 | } |
| 62 | |
| 63 | uint64_t Hash() const { |
| 64 | uint64_t hash = 9314237; |
| 65 | for (size_t i = 0; i < num_frames_; ++i) { |
| 66 | hash = hash * 2654435761 + frames_[i]; |
| 67 | hash += (hash >> 13) ^ (hash << 6); |
| 68 | } |
| 69 | return hash; |
| 70 | } |
| 71 | |
| 72 | private: |
| 73 | uintptr_t frames_[kMaxFrames]; |
| 74 | size_t num_frames_; |
| 75 | }; |
| 76 | |
| 77 | } // namespace art |
| 78 | |
| 79 | #endif // ART_RUNTIME_BACKTRACE_HELPER_H_ |