1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #ifndef HIPERF_PERF_EVENT_RECORD_H 16 #define HIPERF_PERF_EVENT_RECORD_H 17 18 #include <atomic> 19 #include <chrono> 20 #include <map> 21 #include <memory> 22 #include <stdint.h> 23 #include <string> 24 #include <variant> 25 #include <vector> 26 27 #include <sys/types.h> 28 #include <unique_fd.h> 29 #include <linux/perf_event.h> 30 #include <linux/types.h> 31 32 #include "debug_logger.h" 33 #include "utilities.h" 34 35 namespace OHOS { 36 namespace Developtools { 37 namespace NativeDaemon { 38 namespace { 39 constexpr uint8_t CALL_FRAME_REPORT = 0x01; // 0001 40 constexpr uint8_t SYMBOL_NAME_ID_REPORT = 0x02; // 0010 41 constexpr uint8_t FILE_PATH_ID_REPORT = 0x04; // 0100 42 } 43 struct CallFrame { 44 uint64_t ip_ = 0; 45 uint64_t sp_ = 0; 46 47 uint64_t vaddrInFile_ = 0; // in symbol file vaddr 48 int32_t symbolIndex_ = -1; // symbols index , should update after sort 49 std::string_view symbolName_; 50 std::string_view filePath_; // lib path , elf path 51 uint64_t offset_ = 0; 52 uint64_t symbolOffset_ = 0; 53 uint32_t filePathId_ = 0; // for memMpaItem filePathId_ 54 uint32_t symbolNameId_ = 0; // for symbolName_ id 55 uint32_t callFrameId_ = 0; // for frame map id 56 uint8_t needReport_ = 0; // 0X01 for CALL_FRAME_REPORT,0x02 for SYMBOL_NAME_ID_REPORT,0x04 for FILE_PATH_ID_REPORT 57 ip_CallFrame58 CallFrame(uint64_t ip, uint64_t sp = 0) : ip_(ip), sp_(sp) {} 59 60 // this is for ut test CallFrameCallFrame61 CallFrame(uint64_t ip, uint64_t vaddrInFile, const char *name, const char *filePath) 62 : ip_(ip), vaddrInFile_(vaddrInFile), symbolName_(name), filePath_(filePath) 63 { 64 } 65 bool operator==(const CallFrame &b) const 66 { 67 return (ip_ == b.ip_) && (sp_ == b.sp_); 68 } 69 bool operator!=(const CallFrame &b) const 70 { 71 return (ip_ != b.ip_) || (sp_ != b.sp_); 72 } ToStringCallFrame73 std::string ToString() const 74 { 75 return StringPrintf("ip: 0x%016llx sp: 0x%016llx", ip_, sp_); 76 } ToSymbolStringCallFrame77 std::string ToSymbolString() const 78 { 79 std::string output; 80 if (vaddrInFile_ != 0) { 81 output = StringPrintf("va: 0x%016llx(%llx) ", vaddrInFile_, ip_); 82 } else { 83 output = StringPrintf("ip: 0x%016llx ", ip_); 84 } 85 output.append(": "); 86 output.append(symbolName_); 87 88 output.append("@"); 89 output.append(filePath_); 90 if (symbolIndex_ != -1) { 91 output.append(":"); 92 output.append(std::to_string(symbolIndex_)); 93 } 94 return output; 95 } 96 }; 97 98 struct AttrWithId { 99 perf_event_attr attr; 100 std::vector<uint64_t> ids; 101 std::string name; // will be empty in GetAttrSection 102 }; 103 } // namespace NativeDaemon 104 } // namespace Developtools 105 } // namespace OHOS 106 #endif