• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2013 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 <inttypes.h>
18 #include <stdint.h>
19 #include <stdlib.h>
20 #include <sys/types.h>
21 #include <ucontext.h>
22 
23 #include <string>
24 
25 #include <base/stringprintf.h>
26 
27 #include <backtrace/Backtrace.h>
28 #include <backtrace/BacktraceMap.h>
29 
30 #include <cutils/threads.h>
31 
32 #include "BacktraceLog.h"
33 #include "thread_utils.h"
34 #include "UnwindCurrent.h"
35 #include "UnwindPtrace.h"
36 
37 using android::base::StringPrintf;
38 
39 //-------------------------------------------------------------------------
40 // Backtrace functions.
41 //-------------------------------------------------------------------------
Backtrace(pid_t pid,pid_t tid,BacktraceMap * map)42 Backtrace::Backtrace(pid_t pid, pid_t tid, BacktraceMap* map)
43     : pid_(pid), tid_(tid), map_(map), map_shared_(true) {
44   if (map_ == nullptr) {
45     map_ = BacktraceMap::Create(pid);
46     map_shared_ = false;
47   }
48 }
49 
~Backtrace()50 Backtrace::~Backtrace() {
51   if (map_ && !map_shared_) {
52     delete map_;
53     map_ = nullptr;
54   }
55 }
56 
GetFunctionName(uintptr_t pc,uintptr_t * offset)57 std::string Backtrace::GetFunctionName(uintptr_t pc, uintptr_t* offset) {
58   std::string func_name = GetFunctionNameRaw(pc, offset);
59   return func_name;
60 }
61 
VerifyReadWordArgs(uintptr_t ptr,word_t * out_value)62 bool Backtrace::VerifyReadWordArgs(uintptr_t ptr, word_t* out_value) {
63   if (ptr & (sizeof(word_t)-1)) {
64     BACK_LOGW("invalid pointer %p", reinterpret_cast<void*>(ptr));
65     *out_value = static_cast<word_t>(-1);
66     return false;
67   }
68   return true;
69 }
70 
FormatFrameData(size_t frame_num)71 std::string Backtrace::FormatFrameData(size_t frame_num) {
72   if (frame_num >= frames_.size()) {
73     return "";
74   }
75   return FormatFrameData(&frames_[frame_num]);
76 }
77 
FormatFrameData(const backtrace_frame_data_t * frame)78 std::string Backtrace::FormatFrameData(const backtrace_frame_data_t* frame) {
79   const char* map_name;
80   if (BacktraceMap::IsValid(frame->map) && !frame->map.name.empty()) {
81     map_name = frame->map.name.c_str();
82   } else {
83     map_name = "<unknown>";
84   }
85 
86   uintptr_t relative_pc = BacktraceMap::GetRelativePc(frame->map, frame->pc);
87 
88   std::string line(StringPrintf("#%02zu pc %" PRIPTR "  %s", frame->num, relative_pc, map_name));
89   // Special handling for non-zero offset maps, we need to print that
90   // information.
91   if (frame->map.offset != 0) {
92     line += " (offset " + StringPrintf("0x%" PRIxPTR, frame->map.offset) + ")";
93   }
94   if (!frame->func_name.empty()) {
95     line += " (" + frame->func_name;
96     if (frame->func_offset) {
97       line += StringPrintf("+%" PRIuPTR, frame->func_offset);
98     }
99     line += ')';
100   }
101 
102   return line;
103 }
104 
FillInMap(uintptr_t pc,backtrace_map_t * map)105 void Backtrace::FillInMap(uintptr_t pc, backtrace_map_t* map) {
106   if (map_ != nullptr) {
107     map_->FillIn(pc, map);
108   }
109 }
110 
Create(pid_t pid,pid_t tid,BacktraceMap * map)111 Backtrace* Backtrace::Create(pid_t pid, pid_t tid, BacktraceMap* map) {
112   if (pid == BACKTRACE_CURRENT_PROCESS) {
113     pid = getpid();
114     if (tid == BACKTRACE_CURRENT_THREAD) {
115       tid = gettid();
116     }
117   } else if (tid == BACKTRACE_CURRENT_THREAD) {
118     tid = pid;
119   }
120 
121   if (pid == getpid()) {
122     return new UnwindCurrent(pid, tid, map);
123   } else {
124     return new UnwindPtrace(pid, tid, map);
125   }
126 }
127