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 20 #include <stddef.h> 21 #include <stdint.h> 22 23 #include "base/macros.h" 24 25 namespace unwindstack { 26 class Unwinder; 27 } 28 29 namespace art HIDDEN { 30 31 // Using libunwindstack 32 class BacktraceCollector { 33 public: BacktraceCollector(uintptr_t * out_frames,size_t max_depth,size_t skip_count)34 BacktraceCollector(uintptr_t* out_frames, size_t max_depth, size_t skip_count) 35 : out_frames_(out_frames), max_depth_(max_depth), skip_count_(skip_count) {} 36 NumFrames()37 size_t NumFrames() const { 38 return num_frames_; 39 } 40 41 // Collect the backtrace, do not call more than once. 42 void Collect(); 43 44 private: 45 // Try to collect backtrace. Returns false on failure. 46 // It is used to retry backtrace on temporary failure. 47 bool CollectImpl(unwindstack::Unwinder* unwinder); 48 49 uintptr_t* const out_frames_ = nullptr; 50 size_t num_frames_ = 0u; 51 const size_t max_depth_ = 0u; 52 size_t skip_count_ = 0u; 53 }; 54 55 // A bounded sized backtrace. 56 template <size_t kMaxFrames> 57 class FixedSizeBacktrace { 58 public: Collect(size_t skip_count)59 void Collect(size_t skip_count) { 60 BacktraceCollector collector(frames_, kMaxFrames, skip_count); 61 collector.Collect(); 62 num_frames_ = collector.NumFrames(); 63 } 64 Hash()65 uint64_t Hash() const { 66 uint64_t hash = 9314237; 67 for (size_t i = 0; i < num_frames_; ++i) { 68 hash = hash * 2654435761 + frames_[i]; 69 hash += (hash >> 13) ^ (hash << 6); 70 } 71 return hash; 72 } 73 74 private: 75 uintptr_t frames_[kMaxFrames]; 76 size_t num_frames_; 77 }; 78 79 } // namespace art 80 81 #endif // ART_RUNTIME_BACKTRACE_HELPER_H_ 82