Home | History | Annotate | Download | only in simpleperf
      1 /*
      2  * Copyright (C) 2015 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 SIMPLE_PERF_OFFLINE_UNWINDER_H_
     18 #define SIMPLE_PERF_OFFLINE_UNWINDER_H_
     19 
     20 #include <memory>
     21 #include <vector>
     22 #include <unordered_map>
     23 
     24 #include "perf_regs.h"
     25 
     26 #include <backtrace/BacktraceMap.h>
     27 
     28 namespace simpleperf {
     29 struct ThreadEntry;
     30 
     31 struct UnwindingResult {
     32   // time used for unwinding, in ns.
     33   uint64_t used_time;
     34   enum {
     35     UNKNOWN_REASON,
     36     EXCEED_MAX_FRAMES_LIMIT,
     37     ACCESS_REG_FAILED,
     38     ACCESS_STACK_FAILED,
     39     ACCESS_MEM_FAILED,
     40     FIND_PROC_INFO_FAILED,
     41     EXECUTE_DWARF_INSTRUCTION_FAILED,
     42     DIFFERENT_ARCH,
     43     MAP_MISSING,
     44   } stop_reason;
     45   union {
     46     // for ACCESS_REG_FAILED
     47     uint64_t regno;
     48     // for ACCESS_MEM_FAILED and ACCESS_STACK_FAILED
     49     uint64_t addr;
     50   } stop_info;
     51   uint64_t stack_start;
     52   uint64_t stack_end;
     53 };
     54 
     55 class OfflineUnwinder {
     56  public:
     57   OfflineUnwinder(bool collect_stat);
     58 
     59   bool UnwindCallChain(const ThreadEntry& thread, const RegSet& regs, const char* stack,
     60                        size_t stack_size, std::vector<uint64_t>* ips, std::vector<uint64_t>* sps);
     61 
     62   bool HasStat() const {
     63     return collect_stat_;
     64   }
     65 
     66   const UnwindingResult& GetUnwindingResult() const {
     67     return unwinding_result_;
     68   }
     69 
     70  private:
     71   bool collect_stat_;
     72   UnwindingResult unwinding_result_;
     73 
     74   // Cache of the most recently used map.
     75   struct CachedMap {
     76     uint64_t version = 0u;
     77     std::unique_ptr<BacktraceMap> map;
     78   };
     79   // use unused attribute to pass mac build.
     80   std::unordered_map<pid_t, CachedMap> cached_maps_  __attribute__((unused));
     81 };
     82 
     83 } // namespace simpleperf
     84 
     85 #endif  // SIMPLE_PERF_OFFLINE_UNWINDER_H_
     86