• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved.
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     bool isJsFrame_ = false;
58 
ip_CallFrame59     CallFrame(uint64_t ip, uint64_t sp = 0, bool isJsFrame = false) : ip_(ip), sp_(sp), isJsFrame_(isJsFrame) {}
60 
61     // this is for ut test
CallFrameCallFrame62     CallFrame(uint64_t ip, uint64_t vaddrInFile, const char *name, const char *filePath)
63         : ip_(ip), vaddrInFile_(vaddrInFile), symbolName_(name), filePath_(filePath)
64     {
65     }
66     bool operator==(const CallFrame &b) const
67     {
68         return (ip_ == b.ip_) && (sp_ == b.sp_);
69     }
70     bool operator!=(const CallFrame &b) const
71     {
72         return (ip_ != b.ip_) || (sp_ != b.sp_);
73     }
ToStringCallFrame74     std::string ToString() const
75     {
76         return StringPrintf("ip: 0x%016llx sp: 0x%016llx", ip_, sp_);
77     }
ToSymbolStringCallFrame78     std::string ToSymbolString() const
79     {
80         std::string output;
81         if (vaddrInFile_ != 0) {
82             output = StringPrintf("va: 0x%016llx(%llx) ", vaddrInFile_, ip_);
83         } else {
84             output = StringPrintf("ip: 0x%016llx ", ip_);
85         }
86         output.append(": ");
87         output.append(symbolName_);
88 
89         output.append("@");
90         output.append(filePath_);
91         if (symbolIndex_ != -1) {
92             output.append(":");
93             output.append(std::to_string(symbolIndex_));
94         }
95         return output;
96     }
97 };
98 
99 struct AttrWithId {
100     perf_event_attr attr;
101     std::vector<uint64_t> ids;
102     std::string name; // will be empty in GetAttrSection
103 };
104 } // namespace NativeDaemon
105 } // namespace Developtools
106 } // namespace OHOS
107 #endif