blob: 21a0568033a637e9d3ede655ded645796c81d979 [file] [log] [blame]
Alex Light543d8452018-07-13 16:25:58 +00001/*
2 * Copyright (C) 2018 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#include "backtrace_helper.h"
18
19#if defined(__linux__)
20
21#include <backtrace/Backtrace.h>
22#include <backtrace/BacktraceMap.h>
23
24#include <unistd.h>
25#include <sys/types.h>
26
27#include "thread-inl.h"
28
29#else
30
31// For UNUSED
32#include "base/macros.h"
33
34#endif
35
36namespace art {
37
38// We only really support libbacktrace on linux which is unfortunate but since this is only for
39// gcstress this isn't a huge deal.
40#if defined(__linux__)
41
42static const char* kBacktraceCollectorTlsKey = "BacktraceCollectorTlsKey";
43
44struct BacktraceMapHolder : public TLSData {
45 BacktraceMapHolder() : map_(BacktraceMap::Create(getpid())) {}
46
47 std::unique_ptr<BacktraceMap> map_;
48};
49
50static BacktraceMap* GetMap(Thread* self) {
51 BacktraceMapHolder* map_holder =
52 reinterpret_cast<BacktraceMapHolder*>(self->GetCustomTLS(kBacktraceCollectorTlsKey));
53 if (map_holder == nullptr) {
54 map_holder = new BacktraceMapHolder;
55 // We don't care about the function names. Turning this off makes everything significantly
56 // faster.
57 map_holder->map_->SetResolveNames(false);
58 // Only created and queried on Thread::Current so no sync needed.
59 self->SetCustomTLS(kBacktraceCollectorTlsKey, map_holder);
60 }
61
62 return map_holder->map_.get();
63}
64
65void BacktraceCollector::Collect() {
66 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(BACKTRACE_CURRENT_PROCESS,
67 BACKTRACE_CURRENT_THREAD,
68 GetMap(Thread::Current())));
69 backtrace->SetSkipFrames(true);
70 if (!backtrace->Unwind(skip_count_, nullptr)) {
71 return;
72 }
73 for (Backtrace::const_iterator it = backtrace->begin();
74 max_depth_ > num_frames_ && it != backtrace->end();
75 ++it) {
76 out_frames_[num_frames_++] = static_cast<uintptr_t>(it->pc);
77 }
78}
79
80#else
81
82#pragma clang diagnostic push
83#pragma clang diagnostic warning "-W#warnings"
84#warning "Backtrace collector is not implemented. GCStress cannot be used."
85#pragma clang diagnostic pop
86
87// We only have an implementation for linux. On other plaforms just return nothing. This is not
88// really correct but we only use this for hashing and gcstress so it's not too big a deal.
89void BacktraceCollector::Collect() {
90 UNUSED(skip_count_);
91 UNUSED(out_frames_);
92 UNUSED(max_depth_);
93 num_frames_ = 0;
94}
95
96#endif
97
98} // namespace art