blob: 8eda3fa0a10b885192cb6a7b63ff996df357cef9 [file] [log] [blame]
Mathieu Chartier34583592017-03-23 23:51:34 -07001/*
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 Light543d8452018-07-13 16:25:58 +000020#include <stddef.h>
21#include <stdint.h>
Mathieu Chartier34583592017-03-23 23:51:34 -070022
23namespace art {
24
Alex Light543d8452018-07-13 16:25:58 +000025// Using libbacktrace
Mathieu Chartier34583592017-03-23 23:51:34 -070026class BacktraceCollector {
27 public:
28 BacktraceCollector(uintptr_t* out_frames, size_t max_depth, size_t skip_count)
29 : out_frames_(out_frames), max_depth_(max_depth), skip_count_(skip_count) {}
30
31 size_t NumFrames() const {
32 return num_frames_;
33 }
34
35 // Collect the backtrace, do not call more than once.
Alex Light543d8452018-07-13 16:25:58 +000036 void Collect();
Mathieu Chartier34583592017-03-23 23:51:34 -070037
38 private:
Mathieu Chartier34583592017-03-23 23:51:34 -070039 uintptr_t* const out_frames_ = nullptr;
40 size_t num_frames_ = 0u;
41 const size_t max_depth_ = 0u;
42 size_t skip_count_ = 0u;
43};
44
45// A bounded sized backtrace.
46template <size_t kMaxFrames>
47class FixedSizeBacktrace {
48 public:
49 void Collect(size_t skip_count) {
50 BacktraceCollector collector(frames_, kMaxFrames, skip_count);
51 collector.Collect();
52 num_frames_ = collector.NumFrames();
53 }
54
55 uint64_t Hash() const {
56 uint64_t hash = 9314237;
57 for (size_t i = 0; i < num_frames_; ++i) {
58 hash = hash * 2654435761 + frames_[i];
59 hash += (hash >> 13) ^ (hash << 6);
60 }
61 return hash;
62 }
63
64 private:
65 uintptr_t frames_[kMaxFrames];
66 size_t num_frames_;
67};
68
69} // namespace art
70
71#endif // ART_RUNTIME_BACKTRACE_HELPER_H_