• 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 
16 #define HILOG_TAG "Symbols"
17 
18 #include "symbols_file.h"
19 
20 #include <algorithm>
21 #include <chrono>
22 #include <cxxabi.h>
23 #include <fcntl.h>
24 #include <fstream>
25 
26 #if is_mingw
27 #include <memoryapi.h>
28 #else
29 #include <sys/mman.h>
30 #include <sys/stat.h>
31 #endif
32 
33 #include <cstdlib>
34 #include <unistd.h>
35 #include "dfx_ark.h"
36 #include "dfx_extractor_utils.h"
37 #include "dfx_symbols.h"
38 #include "dwarf_encoding.h"
39 #include "utilities.h"
40 #include "logging.h"
41 
42 using namespace OHOS::HiviewDFX;
43 using namespace std::chrono;
44 
45 namespace OHOS {
46 namespace Developtools {
47 namespace NativeDaemon {
48 bool SymbolsFile::onRecording_ = true;
GetBuildId() const49 const std::string SymbolsFile::GetBuildId() const
50 {
51     return buildId_;
52 }
53 
UpdateBuildIdIfMatch(std::string buildId)54 bool SymbolsFile::UpdateBuildIdIfMatch(std::string buildId)
55 {
56     /*
57         here we have two case
58         1 buildId_ is empty
59             a) always return match
60         2 buildId_ is not empty
61             a) really check if the same one
62     */
63 
64     if (buildId_.empty()) {
65         // we have new empty build
66         if (buildId.empty()) {
67             // both empty , no build id provided
68             HLOGD("build id is empty.");
69             return true;
70         } else {
71             buildId_ = buildId;
72             HLOGD("new buildId %s", buildId_.c_str());
73             return true;
74         }
75     } else {
76         // we already have a build id
77         // so this is not the first time load symbol
78         // we need check if it match
79         HLOGV("expected buildid: %s vs %s", buildId_.c_str(), buildId.c_str());
80 
81         if (buildId_ != buildId) {
82             HLOGW("id not match");
83             return false;
84         } else {
85             HLOGD("id match");
86             return true;
87         }
88     }
89 }
90 
SearchReadableFile(const std::vector<std::string> & searchPaths,const std::string & filePath) const91 std::string SymbolsFile::SearchReadableFile(const std::vector<std::string> &searchPaths,
92                                             const std::string &filePath) const
93 {
94     UNWIND_CHECK_TRUE(!filePath.empty(), filePath, "nothing to found");
95     for (auto searchPath : searchPaths) {
96         if (searchPath.back() != PATH_SEPARATOR) {
97             searchPath += PATH_SEPARATOR;
98         }
99         std::string PossibleFilePath = searchPath + filePath;
100         if (CheckPathReadable(PossibleFilePath)) {
101             return PossibleFilePath;
102         }
103         HLOGW("have not found '%s' in search paths %s", filePath.c_str(), searchPath.c_str());
104     }
105     return EMPTY_STRING;
106 }
107 
FindSymbolFile(const std::vector<std::string> & symbolsFileSearchPaths,std::string symboleFilePath) const108 const std::string SymbolsFile::FindSymbolFile(
109     const std::vector<std::string> &symbolsFileSearchPaths, std::string symboleFilePath) const
110 {
111     /*
112         this function do 2 things:
113         find by name:
114             1 find dso path
115             2 find search path
116                 a) search path + dso path
117                 b) search path + dso name
118 
119         show we should return filePath_ as default ?
120     */
121     if (symboleFilePath.empty()) {
122         symboleFilePath = filePath_;
123         HLOGD("use default filename: %s ", symboleFilePath.c_str());
124     }
125     symboleFilePath = PlatformPathConvert(symboleFilePath);
126     std::string foundPath;
127     // search fisrt if we have path
128     if (symbolsFileSearchPaths.size() != 0) {
129         foundPath = SearchReadableFile(symbolsFileSearchPaths, symboleFilePath);
130         if (foundPath.empty()) {
131             HLOGV("try base name for: %s split with %s", symboleFilePath.c_str(),
132                   PATH_SEPARATOR_STR.c_str());
133             auto pathSplit = StringSplit(symboleFilePath, PATH_SEPARATOR_STR);
134             if (pathSplit.size() > 1) {
135                 HLOGV("base name is: %s ", pathSplit.back().c_str());
136                 // found it again with base name , split it and get last name
137                 foundPath = SearchReadableFile(symbolsFileSearchPaths, pathSplit.back());
138             }
139         }
140     }
141     auto pathSplit = StringSplit(symboleFilePath, "!");
142     if (pathSplit.size() > 1) {
143         HLOGV("base name is: %s ", pathSplit.back().c_str());
144         if (StringEndsWith(pathSplit[0], ".hap")) {
145             foundPath = pathSplit[0];
146         }
147     }
148     // only access the patch in onRecording_
149     // in report mode we don't load any thing in runtime path
150     if (foundPath.empty() and onRecording_) {
151         // try access direct at last
152         if (CheckPathReadable(symboleFilePath)) {
153             // found direct folder
154             HLOGD("find %s in current work dir", symboleFilePath.c_str());
155             return symboleFilePath;
156         }
157     }
158     return foundPath;
159 }
160 
161 class ElfFileSymbols : public SymbolsFile {
162 public:
ElfFileSymbols(const std::string & symbolFilePath,const SymbolsFileType symbolsFileType=SYMBOL_ELF_FILE)163     explicit ElfFileSymbols(const std::string &symbolFilePath,
164                             const SymbolsFileType symbolsFileType = SYMBOL_ELF_FILE)
165         : SymbolsFile(symbolsFileType, symbolFilePath)
166     {
167     }
168 
~ElfFileSymbols()169     virtual ~ElfFileSymbols()
170     {
171     }
172 
LoadSymbols(std::shared_ptr<DfxMap> map,const std::string & symbolFilePath)173     bool LoadSymbols(std::shared_ptr<DfxMap> map, const std::string &symbolFilePath) override
174     {
175         symbolsLoaded_ = true;
176         std::string findPath = FindSymbolFile(symbolsFileSearchPaths_, symbolFilePath);
177         UNWIND_CHECK_TRUE(!findPath.empty(), false, "elf found failed (belong to %s)", filePath_.c_str());
178         if (LoadElfSymbols(map, findPath)) {
179             return true;
180         } else {
181             HLOGW("elf open failed with '%s'", findPath.c_str());
182             return false;
183         }
184         return false;
185     }
186 
GetElfFile()187     std::shared_ptr<DfxElf> GetElfFile() override
188     {
189         return elfFile_;
190     }
191 
192 protected:
LoadDebugInfo(std::shared_ptr<DfxMap> map,const std::string & symbolFilePath)193     bool LoadDebugInfo(std::shared_ptr<DfxMap> map, const std::string &symbolFilePath) override
194     {
195         if (debugInfoLoaded_) {
196             return true;
197         } else {
198             debugInfoLoaded_ = true;
199         }
200         std::string elfPath = FindSymbolFile(symbolsFileSearchPaths_, symbolFilePath);
201         UNWIND_CHECK_TRUE(!elfPath.empty(), false, "elf found failed (belong to %s)", filePath_.c_str());
202         if (elfFile_ == nullptr) {
203             if (StringEndsWith(elfPath, ".hap")) {
204                 elfFile_ = DfxElf::CreateFromHap(elfPath, map->prevMap, map->offset);
205                 map->elf = elfFile_;
206             } else {
207                 elfFile_ = std::make_shared<DfxElf>(elfPath);
208             }
209         }
210 
211         if (elfFile_ == nullptr) {
212             HLOGE("Failed to create elf file for %s.", elfPath.c_str());
213             return false;
214         }
215 
216         if (!elfFile_->IsValid()) {
217             HLOGE("parse elf file failed.");
218             return false;
219         }
220 
221         HLOGD("loaded elf %s", elfPath.c_str());
222         // update path for so in hap
223         if (StringEndsWith(elfPath, ".hap")) {
224             filePath_ = elfPath + "!" + elfFile_->GetElfName();
225             HLOGD("update path for so in hap %s.", filePath_.c_str());
226             map->name = filePath_;
227             map->elf = elfFile_;
228             map->prevMap->name = filePath_;
229             map->prevMap->elf = elfFile_;
230         }
231         textExecVaddr_ = elfFile_->GetStartVaddr();
232         textExecVaddrFileOffset_ = elfFile_->GetStartOffset();
233         HLOGD("textExecVaddr_ 0x%016" PRIx64 " file offset 0x%016" PRIx64 "", textExecVaddr_,
234               textExecVaddrFileOffset_);
235 
236 #ifndef __arm__
237         ShdrInfo shinfo;
238         if (elfFile_->GetSectionInfo(shinfo, ".eh_frame_hdr")) {
239             LoadEhFrameHDR(elfFile_->GetMmapPtr() + shinfo.offset, shinfo.size, shinfo.offset);
240         }
241 #endif
242         return true;
243     }
244 
245 private:
246     bool ehFrameHDRValid_ {false};
247     uint64_t ehFrameHDRElfOffset_ {0};
248     uint64_t ehFrameHDRFdeCount_ {0};
249     uint64_t ehFrameHDRFdeTableItemSize_ {0};
250     uint64_t ehFrameHDRFdeTableElfOffset_ {0};
251     std::shared_ptr<DfxElf> elfFile_;
GetSectionInfo(const std::string & name,uint64_t & sectionVaddr,uint64_t & sectionSize,uint64_t & sectionFileOffset) const252     bool GetSectionInfo(const std::string &name, uint64_t &sectionVaddr, uint64_t &sectionSize,
253                         uint64_t &sectionFileOffset) const override
254     {
255         struct ShdrInfo shdrInfo;
256         if (elfFile_->GetSectionInfo(shdrInfo, name)) {
257             sectionVaddr = shdrInfo.addr;
258             sectionSize = shdrInfo.size;
259             sectionFileOffset = shdrInfo.offset;
260             HLOGM("Get Section '%s' %" PRIx64 " - %" PRIx64 "", name.c_str(), sectionVaddr, sectionSize);
261             return true;
262         } else {
263             HLOGW("Section '%s' not found", name.c_str());
264             return false;
265         }
266     }
267 
268 #ifndef __arm__
GetHDRSectionInfo(uint64_t & ehFrameHdrElfOffset,uint64_t & fdeTableElfOffset,uint64_t & fdeTableSize)269     bool GetHDRSectionInfo(uint64_t &ehFrameHdrElfOffset, uint64_t &fdeTableElfOffset,
270                            uint64_t &fdeTableSize) override
271     {
272         ShdrInfo shinfo;
273         if (!elfFile_->GetSectionInfo(shinfo, ".eh_frame_hdr")) {
274             return false;
275         }
276 
277         ehFrameHDRElfOffset_ = shinfo.offset;
278         if (ehFrameHDRValid_) {
279             ehFrameHdrElfOffset = ehFrameHDRElfOffset_;
280             fdeTableElfOffset = ehFrameHDRFdeTableElfOffset_;
281             fdeTableSize = ehFrameHDRFdeCount_;
282             return true;
283         } else {
284             HLOGW("!ehFrameHDRValid_");
285             return false;
286         }
287         if (!LoadEhFrameHDR(elfFile_->GetMmapPtr() + shinfo.offset, elfFile_->GetMmapSize(), shinfo.offset)) {
288             HLOGW("Failed to load eh_frame_hdr");
289             return false;
290         }
291 
292         ehFrameHdrElfOffset = ehFrameHDRElfOffset_;
293         fdeTableElfOffset = ehFrameHDRFdeTableElfOffset_;
294         fdeTableSize = ehFrameHDRFdeCount_;
295         return true;
296     }
297 #endif
298 
DumpEhFrameHDR() const299     void DumpEhFrameHDR() const
300     {
301         HLOGD("  ehFrameHDRElfOffset_:          0x%" PRIx64 "", ehFrameHDRElfOffset_);
302         HLOGD("  ehFrameHDRFdeCount_:           0x%" PRIx64 "", ehFrameHDRFdeCount_);
303         HLOGD("  ehFrameHDRFdeTableElfOffset_:  0x%" PRIx64 "", ehFrameHDRFdeTableElfOffset_);
304         HLOGD("  ehFrameHDRFdeTableItemSize_:   0x%" PRIx64 "", ehFrameHDRFdeTableItemSize_);
305     }
306 
LoadEhFrameHDR(const unsigned char * buffer,size_t bufferSize,uint64_t shdrOffset)307     bool LoadEhFrameHDR(const unsigned char *buffer, size_t bufferSize, uint64_t shdrOffset)
308     {
309         eh_frame_hdr *ehFrameHdr = (eh_frame_hdr *)buffer;
310         const uint8_t *dataPtr = ehFrameHdr->encode_data;
311         DwarfEncoding dwEhFramePtr(ehFrameHdr->eh_frame_ptr_enc, dataPtr);
312         DwarfEncoding dwFdeCount(ehFrameHdr->fde_count_enc, dataPtr);
313         DwarfEncoding dwTable(ehFrameHdr->table_enc, dataPtr);
314         DwarfEncoding dwTableValue(ehFrameHdr->table_enc, dataPtr);
315 
316         HLOGD("eh_frame_hdr:");
317         HexDump(buffer, BITS_OF_FOUR_BYTE, bufferSize);
318         unsigned char version = ehFrameHdr->version;
319         HLOGD("  version:             %02x:%s", version, (version == 1) ? "valid" : "invalid");
320         HLOGD("  eh_frame_ptr_enc:    %s", dwEhFramePtr.ToString().c_str());
321         HLOGD("  fde_count_enc:       %s", dwFdeCount.ToString().c_str());
322         HLOGD("  table_enc:           %s", dwTable.ToString().c_str());
323         HLOGD("  table_value_enc:     %s", dwTableValue.ToString().c_str());
324         HLOGD("  table_item_size:     %zd", dwTable.GetSize() + dwTableValue.GetSize());
325         HLOGD("  table_offset_in_hdr: %zu", dwTable.GetData() - buffer);
326 
327         if (version != 1) {
328             HLOGD("eh_frame_hdr version is invalid");
329             return false;
330         }
331         ehFrameHDRValid_ = true;
332         ehFrameHDRElfOffset_ = shdrOffset;
333         ehFrameHDRFdeCount_ = dwFdeCount.GetAppliedValue();
334         ehFrameHDRFdeTableElfOffset_ = dwTable.GetData() - buffer + shdrOffset;
335         ehFrameHDRFdeTableItemSize_ = dwTable.GetSize() + dwTableValue.GetSize();
336         DumpEhFrameHDR();
337 
338         if (!dwFdeCount.IsOmit() && dwFdeCount.GetValue() > 0) {
339             return true;
340         } else {
341             HLOGW("fde table not found.\n");
342         }
343         return false;
344     }
345 
UpdateSymbols(std::vector<DfxSymbol> & symbolsTable,const std::string & elfPath)346     void UpdateSymbols(std::vector<DfxSymbol> &symbolsTable, const std::string &elfPath)
347     {
348         symbols_.clear();
349         HLOGD("%zu symbols loadded from symbolsTable.", symbolsTable.size());
350         symbols_.swap(symbolsTable);
351         AdjustSymbols();
352         HLOGD("%zu symbols loadded from elf '%s'.", symbols_.size(), elfPath.c_str());
353         for (auto& symbol: symbols_) {
354             HLOGD("symbol %s", symbol.ToDebugString().c_str());
355         }
356         if (buildId_.empty()) {
357             HLOGD("buildId not found from elf '%s'.", elfPath.c_str());
358             // dont failed. some time the lib have not got the build id
359             // buildId not found from elf '/system/bin/ld-musl-arm.so.1'.
360         }
361     }
362 
LoadElfSymbols(std::shared_ptr<DfxMap> map,std::string elfPath)363     bool LoadElfSymbols(std::shared_ptr<DfxMap> map, std::string elfPath)
364     {
365 #ifdef HIPERF_DEBUG_TIME
366         const auto startTime = steady_clock::now();
367 #endif
368         if (elfFile_ == nullptr) {
369             if (StringEndsWith(elfPath, ".hap") && map != nullptr) {
370                 elfFile_ = DfxElf::CreateFromHap(elfPath, map->prevMap, map->offset);
371                 map->elf = elfFile_;
372                 HLOGD("loaded map %s", elfPath.c_str());
373             } else {
374                 elfFile_ = std::make_shared<DfxElf>(elfPath);
375                 HLOGD("loaded elf %s", elfPath.c_str());
376             }
377         }
378 
379         if (elfFile_ == nullptr || !elfFile_->IsValid()) {
380             HLOGD("parser elf file failed.");
381             return false;
382         }
383         textExecVaddr_ = elfFile_->GetStartVaddr();
384         textExecVaddrFileOffset_ = elfFile_->GetStartOffset();
385         HLOGD("textExecVaddr_ 0x%016" PRIx64 " file offset 0x%016" PRIx64 "", textExecVaddr_,
386               textExecVaddrFileOffset_);
387 
388         // we prepare two table here
389         // only one we will push in to symbols_
390         // or both drop if build id is not same
391         std::string buildIdFound = elfFile_->GetBuildId();
392         std::vector<DfxSymbol> symbolsTable;
393 
394         // use elfFile_ to get symbolsTable
395         DfxSymbols::ParseSymbols(symbolsTable, elfFile_, elfPath);
396         DfxSymbols::AddSymbolsByPlt(symbolsTable, elfFile_, elfPath);
397 
398         if (UpdateBuildIdIfMatch(buildIdFound)) {
399             UpdateSymbols(symbolsTable, elfPath);
400         } else {
401             HLOGW("symbols will not update for '%s' because buildId is not match.",
402                   elfPath.c_str());
403             // this mean failed . we dont goon for this.
404             return false;
405         }
406 
407 #ifdef HIPERF_DEBUG_TIME
408         auto usedTime = duration_cast<microseconds>(steady_clock::now() - startTime);
409         if (usedTime.count() != 0) {
410             HLOGV("cost %0.3f ms to load symbols '%s'",
411                   usedTime.count() / static_cast<double>(milliseconds::duration::period::den),
412                   elfPath.c_str());
413         }
414 #endif
415         return true;
416     }
417 
GetVaddrInSymbols(uint64_t ip,uint64_t mapStart,uint64_t mapPageOffset) const418     uint64_t GetVaddrInSymbols(uint64_t ip, uint64_t mapStart,
419                                uint64_t mapPageOffset) const override
420     {
421         /*
422             00200000-002c5000 r--p 00000000 08:02 46400311
423             002c5000-00490000 r-xp 000c5000 08:02 4640031
424 
425             [14] .text             PROGBITS         00000000002c5000  000c5000
426 
427             if ip is 0x46e6ab
428             1. find the map range is 002c5000-00490000
429             2. ip - map start(002c5000) = map section offset
430             3. map section offset + map page offset(000c5000) = elf file offset
431             4. elf file offset - exec file offset(000c5000)
432                 = ip offset (ip always in exec file offset)
433             5. ip offset + exec begin vaddr(2c5000) = virtual ip in elf
434         */
435         uint64_t vaddr = ip - mapStart + mapPageOffset - textExecVaddrFileOffset_ + textExecVaddr_;
436         HLOGM(" ip :0x%016" PRIx64 " -> elf offset :0x%016" PRIx64 " -> vaddr :0x%016" PRIx64 " ",
437               ip, ip - mapStart + mapPageOffset, vaddr);
438         HLOGM("(minExecAddrFileOffset_ is 0x%" PRIx64 " textExecVaddr_ is 0x%" PRIx64 ")",
439               textExecVaddrFileOffset_, textExecVaddr_);
440         return vaddr;
441     }
442 };
443 
444 class KernelSymbols : public ElfFileSymbols {
445 public:
KernelSymbols(const std::string & symbolFilePath)446     explicit KernelSymbols(const std::string &symbolFilePath)
447         : ElfFileSymbols(symbolFilePath, SYMBOL_KERNEL_FILE)
448     {
449     }
450 
451     static constexpr const int KSYM_MIN_TOKENS = 3;
452     static constexpr const int KSYM_DEFAULT_LINE = 35000;
453     static constexpr const int KSYM_DEFAULT_SIZE = 1024 * 1024 * 1; // 1MB
454 
ParseKallsymsLine()455     bool ParseKallsymsLine()
456     {
457         size_t lines = 0;
458         std::string kallsym;
459         if (!ReadFileToString("/proc/kallsyms", kallsym, KSYM_DEFAULT_SIZE)) {
460             HLOGW("/proc/kallsyms load failed.");
461             return false;
462         }
463         // reduce the mem alloc
464         symbols_.reserve(KSYM_DEFAULT_LINE);
465 
466         char *lineBegin = kallsym.data();
467         char *dataEnd = lineBegin + kallsym.size();
468         while (lineBegin < dataEnd) {
469             char *lineEnd = strchr(lineBegin, '\n');
470             if (lineEnd != nullptr) {
471                 *lineEnd = '\0';
472             }
473             size_t lineSize = (lineEnd != nullptr) ? (lineEnd - lineBegin) : (dataEnd - lineBegin);
474 
475             lines++;
476             uint64_t addr = 0;
477             char type = '\0';
478 
479             char nameRaw[lineSize];
480             char moduleRaw[lineSize];
481             int ret = sscanf_s(lineBegin, "%" PRIx64 " %c %s%s", &addr, &type, sizeof(type),
482                                nameRaw, sizeof(nameRaw), moduleRaw, sizeof(moduleRaw));
483 
484             lineBegin = lineEnd + 1;
485             if (ret >= KSYM_MIN_TOKENS) {
486                 if (ret == KSYM_MIN_TOKENS) {
487                     moduleRaw[0] = '\0';
488                 }
489                 HLOGM(" 0x%016" PRIx64 " %c '%s' '%s'", addr, type, nameRaw, moduleRaw);
490             } else {
491                 HLOGW("unknow line %d: '%s'", ret, lineBegin);
492                 continue;
493             }
494             std::string name = nameRaw;
495             std::string module = moduleRaw;
496 
497             /*
498             T
499             The symbol is in the text (code) section.
500 
501             W
502             The symbol is a weak symbol that has not been specifically
503             tagged as a weak object symbol. When a weak defined symbol is
504             linked with a normal defined symbol, the normal defined symbol
505             is used with no error. When a weak undefined symbol is linked
506             and the symbol is not defined, the value of the weak symbol
507             becomes zero with no error.
508             */
509             if (addr != 0 && strchr("TtWw", type)) {
510                 // we only need text symbols
511                 symbols_.emplace_back(addr, name, module.empty() ? filePath_ : module);
512             }
513         }
514         HLOGD("%zu line processed(%zu symbols)", lines, symbols_.size());
515         return true;
516     }
517 
518     const std::string KPTR_RESTRICT = "/proc/sys/kernel/kptr_restrict";
519 
LoadKernelSyms()520     bool LoadKernelSyms()
521     {
522         if (!IsRoot()) {
523             HLOGE("only root mode can access kernel symbols");
524             return false;
525         }
526         HLOGD("try read /proc/kallsyms");
527         if (access("/proc/kallsyms", R_OK) != 0) {
528             printf("No vmlinux path is given, and kallsyms cannot be opened\n");
529             return false;
530         }
531 
532         if (ReadFileToString(KPTR_RESTRICT).front() != '0') {
533             printf("/proc/sys/kernel/kptr_restrict is NOT 0, will try set it to 0.\n");
534             if (!WriteStringToFile(KPTR_RESTRICT, "0")) {
535                 printf("/proc/sys/kernel/kptr_restrict write failed and we cant not change it.\n");
536             }
537         }
538 
539         // getline end
540         if (!ParseKallsymsLine()) {
541             return false;
542         }
543 
544         if (symbols_.empty()) {
545             printf("The symbol table addresses in /proc/kallsyms are all 0.\n"
546                    "Please check the value of /proc/sys/kernel/kptr_restrict, it "
547                    "should be 0.\n"
548                    "Or provide a separate vmlinux path.\n");
549 
550             if (buildId_.size() != 0) {
551                 // but we got the buildid , so we make a dummpy symbols
552                 HLOGD("kallsyms not found. but we have the buildid");
553                 return true;
554             } else {
555                 // we got nothing
556                 return false;
557             }
558         } else {
559             AdjustSymbols();
560             HLOGV("%zu symbols_ loadded from kallsyms.\n", symbols_.size());
561             return true;
562         }
563     }
LoadSymbols(std::shared_ptr<DfxMap> map,const std::string & symbolFilePath)564     bool LoadSymbols(std::shared_ptr<DfxMap> map, const std::string &symbolFilePath) override
565     {
566         symbolsLoaded_ = true;
567         HLOGV("KernelSymbols try read '%s' search paths size %zu, inDeviceRecord %d",
568               symbolFilePath.c_str(), symbolsFileSearchPaths_.size(), onRecording_);
569 
570         if (onRecording_) {
571             // try read
572             HLOGD("try read /sys/kernel/notes");
573             std::string notes = ReadFileToString("/sys/kernel/notes");
574             if (notes.empty()) {
575                 printf("notes cannot be opened, unable get buildid\n");
576                 return false;
577             } else {
578                 HLOGD("kernel notes size: %zu", notes.size());
579                 buildId_ = DfxElf::GetBuildId((uint64_t)notes.data(), (uint64_t)notes.size());
580             }
581 
582             const auto startTime = std::chrono::steady_clock::now();
583             if (!LoadKernelSyms()) {
584                 printf("parse kalsyms failed.\n");
585                 return false;
586             } else {
587                 const auto thisTime = std::chrono::steady_clock::now();
588                 const auto usedTimeMsTick =
589                     std::chrono::duration_cast<std::chrono::milliseconds>(thisTime - startTime);
590                 HLOGV("Load kernel symbols (total %" PRId64 " ms)\n", (int64_t)usedTimeMsTick.count());
591                 // load complete
592                 return true;
593             }
594         } // no search path
595 
596         // try vmlinux
597         return ElfFileSymbols::LoadSymbols(nullptr, KERNEL_ELF_NAME);
598     }
GetVaddrInSymbols(uint64_t ip,uint64_t mapStart,uint64_t) const599     uint64_t GetVaddrInSymbols(uint64_t ip, uint64_t mapStart, uint64_t) const override
600     {
601         // ip is vaddr in /proc/kallsyms
602         return ip;
603     }
~KernelSymbols()604     ~KernelSymbols() override {}
605 };
606 
607 class KernelModuleSymbols : public ElfFileSymbols {
608 public:
KernelModuleSymbols(const std::string & symbolFilePath)609     explicit KernelModuleSymbols(const std::string &symbolFilePath) : ElfFileSymbols(symbolFilePath)
610     {
611         HLOGV("create %s", symbolFilePath.c_str());
612         symbolFileType_ = SYMBOL_KERNEL_MODULE_FILE;
613         module_ = symbolFilePath;
614     }
~KernelModuleSymbols()615     ~KernelModuleSymbols() override {};
616 
LoadSymbols(std::shared_ptr<DfxMap> map,const std::string & symbolFilePath)617     bool LoadSymbols(std::shared_ptr<DfxMap> map, const std::string &symbolFilePath) override
618     {
619         symbolsLoaded_ = true;
620         if (module_ == filePath_ and onRecording_) {
621             // file name sitll not convert to ko file path
622             // this is in record mode
623             HLOGV("find ko name %s", module_.c_str());
624             for (const std::string &path : kernelModulePaths) {
625                 if (access(path.c_str(), R_OK) == 0) {
626                     std::string koPath = path + module_ + KERNEL_MODULES_EXT_NAME;
627                     HLOGV("found ko in %s", koPath.c_str());
628                     if (access(koPath.c_str(), R_OK) == 0) {
629                         // create symbol
630                         filePath_ = koPath;
631                         break; // find next ko
632                     }
633                 }
634             }
635             LoadBuildId();
636         } else {
637             HLOGV("we have file path, load with %s", filePath_.c_str());
638             return ElfFileSymbols::LoadSymbols(nullptr, filePath_);
639         }
640         return false;
641     }
GetVaddrInSymbols(uint64_t ip,uint64_t mapStart,uint64_t) const642     uint64_t GetVaddrInSymbols(uint64_t ip, uint64_t mapStart, uint64_t) const override
643     {
644         return ip - mapStart;
645     }
646 
647 private:
LoadBuildId()648     bool LoadBuildId()
649     {
650         std::string sysFile = "/sys/module/" + module_ + "/notes/.note.gnu.build-id";
651         std::string buildIdRaw = ReadFileToString(sysFile);
652         if (!buildIdRaw.empty()) {
653             buildId_ = DfxElf::GetBuildId((uint64_t)buildIdRaw.data(), (uint64_t)buildIdRaw.size());
654             HLOGD("kerne module %s(%s) build id %s", module_.c_str(), filePath_.c_str(),
655                   buildId_.c_str());
656             return buildId_.empty() ? false : true;
657         }
658         return false;
659     }
660 
661     const std::vector<std::string> kernelModulePaths = {"/vendor/modules/"};
662     std::string module_ = "";
663 };
664 
665 class JavaFileSymbols : public ElfFileSymbols {
666 public:
JavaFileSymbols(const std::string & symbolFilePath)667     explicit JavaFileSymbols(const std::string &symbolFilePath) : ElfFileSymbols(symbolFilePath)
668     {
669         symbolFileType_ = SYMBOL_KERNEL_FILE;
670     }
LoadSymbols(std::shared_ptr<DfxMap> map,const std::string & symbolFilePath)671     bool LoadSymbols(std::shared_ptr<DfxMap> map, const std::string &symbolFilePath) override
672     {
673         symbolsLoaded_ = true;
674         return false;
675     }
~JavaFileSymbols()676     ~JavaFileSymbols() override {}
677 
GetVaddrInSymbols(uint64_t ip,uint64_t mapStart,uint64_t mapPageOffset) const678     uint64_t GetVaddrInSymbols(uint64_t ip, uint64_t mapStart,
679                                        uint64_t mapPageOffset) const override
680     {
681         // this is different with elf
682         // elf use  ip - mapStart + mapPageOffset - minExecAddrFileOffset_ + textExecVaddr_
683         return ip - mapStart + mapPageOffset;
684     }
685 };
686 
687 class JSFileSymbols : public ElfFileSymbols {
688 public:
JSFileSymbols(const std::string & symbolFilePath)689     explicit JSFileSymbols(const std::string &symbolFilePath) : ElfFileSymbols(symbolFilePath)
690     {
691         symbolFileType_ = SYMBOL_KERNEL_FILE;
692     }
LoadSymbols(std::shared_ptr<DfxMap> map,const std::string & symbolFilePath)693     bool LoadSymbols(std::shared_ptr<DfxMap> map, const std::string &symbolFilePath) override
694     {
695         symbolsLoaded_ = true;
696         return false;
697     }
~JSFileSymbols()698     ~JSFileSymbols() override {}
699 };
700 
701 class HapFileSymbols : public ElfFileSymbols {
702 private:
703 #if defined(is_ohos) && is_ohos
704     std::unique_ptr<DfxExtractor> dfxExtractor_;
705     bool hapExtracted_ = false;
706 #endif
707     std::unique_ptr<uint8_t[]> abcDataPtr_ = nullptr;
708     [[maybe_unused]] uintptr_t loadOffSet_ = 0;
709     [[maybe_unused]] size_t abcDataSize_ = 0;
710     [[maybe_unused]] uintptr_t arkExtractorptr_ = 0;
711     bool isHapAbc_ = false;
712     pid_t pid_ = 0;
713 public:
HapFileSymbols(const std::string & symbolFilePath,pid_t pid)714     explicit HapFileSymbols(const std::string &symbolFilePath, pid_t pid)
715         : ElfFileSymbols(symbolFilePath, SYMBOL_HAP_FILE)
716     {
717         pid_ = pid;
718     }
719 
~HapFileSymbols()720     ~HapFileSymbols() override
721     {
722 #if defined(is_ohos) && is_ohos
723         if (arkExtractorptr_ != 0) {
724             DfxArk::ArkDestoryJsSymbolExtractor(arkExtractorptr_);
725             arkExtractorptr_ = 0;
726         }
727 #endif
728     }
729     // abc is the ark bytecode
IsHapAbc()730     bool IsHapAbc()
731     {
732 #if defined(is_ohos) && is_ohos
733         if (hapExtracted_) {
734             return isHapAbc_;
735         }
736         hapExtracted_ = true;
737         HLOGD("the symbol file is %s.", filePath_.c_str());
738         if (StringEndsWith(filePath_, ".hap") && map_->IsMapExec()) {
739             HLOGD("map is exec not abc file , the symbol file is:%s", map_->name.c_str());
740             return false;
741         }
742         if (StringEndsWith(filePath_, ".hap") || StringEndsWith(filePath_, ".hsp")) {
743             dfxExtractor_ = std::make_unique<DfxExtractor>(filePath_);
744             if (dfxExtractor_ == nullptr) {
745                 HLOGD("DfxExtractor create failed.");
746                 return false;
747             }
748             // extract the bytecode information of the ark in the hap package
749             if (!dfxExtractor_->GetHapAbcInfo(loadOffSet_, abcDataPtr_, abcDataSize_)) {
750                 HLOGD("failed to call GetHapAbcInfo, the symbol file is:%s", filePath_.c_str());
751                 return false;
752             }
753             HLOGD("loadOffSet %u", (uint32_t)loadOffSet_);
754             if (abcDataPtr_ != nullptr) {
755                 isHapAbc_ = true;
756                 HLOGD("input abcDataPtr : %s, isAbc: %d", abcDataPtr_.get(), isHapAbc_);
757             }
758         } else {
759             if (map_ == nullptr) {
760                 return false;
761             }
762             loadOffSet_ = map_->offset;
763             abcDataSize_ = map_->end - map_->begin;
764             abcDataPtr_ = std::make_unique<uint8_t[]>(abcDataSize_);
765             auto size = DfxMemory::ReadProcMemByPid(pid_, map_->begin, abcDataPtr_.get(), map_->end - map_->begin);
766             if (size != abcDataSize_) {
767                 HLOGD("return size is small abcDataPtr : %s, isAbc: %d", abcDataPtr_.get(), isHapAbc_);
768                 return false;
769             }
770             isHapAbc_ = true;
771             HLOGD("symbol file name %s loadOffSet %u abcDataSize_ %u abcDataPtr_ %s",
772                   filePath_.c_str(), (uint32_t)loadOffSet_, (uint32_t)abcDataSize_, abcDataPtr_.get());
773         }
774         auto ret = DfxArk::ArkCreateJsSymbolExtractor(&arkExtractorptr_);
775         if (ret < 0) {
776             arkExtractorptr_ = 0;
777             HLOGE("failed to call ArkCreateJsSymbolExtractor, the symbol file is:%s", filePath_.c_str());
778         }
779 #endif
780         return isHapAbc_;
781     }
782 
IsAbc()783     bool IsAbc() override
784     {
785         return isHapAbc_ == true;
786     }
787 
SetBoolValue(bool value)788     void SetBoolValue(bool value) override
789     {
790         isHapAbc_ = value;
791     }
792 
LoadDebugInfo(std::shared_ptr<DfxMap> map,const std::string & symbolFilePath)793     bool LoadDebugInfo(std::shared_ptr<DfxMap> map, const std::string &symbolFilePath) override
794     {
795         HLOGD("map ptr:%p, map name:%s", map.get(), map->name.c_str());
796         if (debugInfoLoaded_) {
797             return true;
798         }
799 
800         if (!onRecording_) {
801             return true;
802         }
803 
804         if (!IsHapAbc()) {
805             ElfFileSymbols::LoadDebugInfo(map, "");
806         }
807         debugInfoLoaded_ = true;
808         debugInfoLoadResult_ = true;
809         return true;
810     }
811 
LoadSymbols(std::shared_ptr<DfxMap> map,const std::string & symbolFilePath)812     bool LoadSymbols(std::shared_ptr<DfxMap> map, const std::string &symbolFilePath) override
813     {
814         HLOGD("map ptr:%p, map name:%s", map.get(), map->name.c_str());
815         if (symbolsLoaded_ || !onRecording_) {
816             return true;
817         }
818         symbolsLoaded_ = true;
819         if (!IsHapAbc()) {
820             ElfFileSymbols::LoadSymbols(map, "");
821         }
822         return true;
823     }
824 
GetSymbolWithPcAndMap(uint64_t ip,std::shared_ptr<DfxMap> map)825     DfxSymbol GetSymbolWithPcAndMap(uint64_t ip, std::shared_ptr<DfxMap> map) override
826     {
827         // get cache
828         auto iter = symbolsMap_.find(ip);
829         if (iter != symbolsMap_.end()) {
830             return iter->second;
831         }
832         if (map == nullptr) {
833             return DfxSymbol(ip, "");
834         }
835         HLOGD("map ptr:%p, map name:%s", map.get(), map->name.c_str());
836 
837 #if defined(is_ohos) && is_ohos
838         if (IsAbc()) {
839             JsFunction jsFunc;
840             std::string module = map->name;
841             HLOGD("map->name module:%s", module.c_str());
842             // symbolization based on ark bytecode
843             auto ret = DfxArk::ParseArkFrameInfo(static_cast<uintptr_t>(ip), static_cast<uintptr_t>(map->begin),
844                                                  loadOffSet_, abcDataPtr_.get(), abcDataSize_,
845                                                  arkExtractorptr_, &jsFunc);
846             if (ret == -1) {
847                 HLOGD("failed to call ParseArkFrameInfo, the symbol file is : %s", map->name.c_str());
848                 return DfxSymbol(ip, "");
849             }
850             this->symbolsMap_.insert(std::make_pair(ip,
851                                                     DfxSymbol(ip,
852                                                     jsFunc.codeBegin,
853                                                     jsFunc.functionName,
854                                                     jsFunc.ToString(),
855                                                     map->name)));
856 
857             DfxSymbol &foundSymbol = symbolsMap_[ip];
858             if (!foundSymbol.matched_) {
859                 foundSymbol.matched_ = true;
860                 matchedSymbols_.push_back(&(symbolsMap_[ip]));
861             }
862 
863             HLOGD("ip : 0x%" PRIx64 " the symbol file is : %s, function is %s demangle_ : %s", ip,
864                   symbolsMap_[ip].module_.data(), jsFunc.functionName, matchedSymbols_.back()->demangle_.data());
865             return symbolsMap_[ip];
866         }
867 #endif
868         DfxSymbol symbol(ip, "");
869         return symbol;
870     }
871 };
872 
873 class UnknowFileSymbols : public SymbolsFile {
874 public:
UnknowFileSymbols(const std::string & symbolFilePath)875     explicit UnknowFileSymbols(const std::string &symbolFilePath)
876         : SymbolsFile(SYMBOL_UNKNOW_FILE, symbolFilePath)
877     {
878     }
LoadSymbols(std::shared_ptr<DfxMap> map,const std::string & symbolFilePath)879     bool LoadSymbols(std::shared_ptr<DfxMap> map, const std::string &symbolFilePath) override
880     {
881         symbolsLoaded_ = true;
882         return false;
883     }
~UnknowFileSymbols()884     ~UnknowFileSymbols() override {}
885 };
886 
~SymbolsFile()887 SymbolsFile::~SymbolsFile() {}
888 
CreateSymbolsFile(SymbolsFileType symbolType,const std::string symbolFilePath,pid_t pid)889 std::unique_ptr<SymbolsFile> SymbolsFile::CreateSymbolsFile(SymbolsFileType symbolType,
890                                                             const std::string symbolFilePath, pid_t pid)
891 {
892     switch (symbolType) {
893         case SYMBOL_KERNEL_FILE:
894             return std::make_unique<KernelSymbols>(symbolFilePath.empty() ? KERNEL_MMAP_NAME
895                                                                           : symbolFilePath);
896         case SYMBOL_KERNEL_MODULE_FILE:
897             return std::make_unique<KernelModuleSymbols>(symbolFilePath);
898         case SYMBOL_ELF_FILE:
899             return std::make_unique<ElfFileSymbols>(symbolFilePath);
900         case SYMBOL_JAVA_FILE:
901             return std::make_unique<JavaFileSymbols>(symbolFilePath);
902         case SYMBOL_JS_FILE:
903             return std::make_unique<JSFileSymbols>(symbolFilePath);
904         case SYMBOL_HAP_FILE:
905             return std::make_unique<HapFileSymbols>(symbolFilePath, pid);
906         default:
907             return std::make_unique<SymbolsFile>(SYMBOL_UNKNOW_FILE, symbolFilePath);
908     }
909 }
910 
CreateSymbolsFile(const std::string & symbolFilePath,pid_t pid)911 std::unique_ptr<SymbolsFile> SymbolsFile::CreateSymbolsFile(const std::string &symbolFilePath, pid_t pid)
912 {
913     // we need check file name here
914     if (symbolFilePath == KERNEL_MMAP_NAME) {
915         return SymbolsFile::CreateSymbolsFile(SYMBOL_KERNEL_FILE, symbolFilePath);
916     } else if (StringEndsWith(symbolFilePath, KERNEL_MODULES_EXT_NAME)) {
917         return SymbolsFile::CreateSymbolsFile(SYMBOL_KERNEL_MODULE_FILE, symbolFilePath);
918     } else if (IsArkJsFile(symbolFilePath)) {
919         return SymbolsFile::CreateSymbolsFile(SYMBOL_HAP_FILE, symbolFilePath, pid);
920     } else {
921         // default is elf
922         return SymbolsFile::CreateSymbolsFile(SYMBOL_ELF_FILE, symbolFilePath);
923     }
924 }
925 
AdjustSymbols()926 void SymbolsFile::AdjustSymbols()
927 {
928     if (symbols_.size() <= 1u) {
929         return;
930     }
931 
932     // order
933     sort(symbols_.begin(), symbols_.end(), [](const DfxSymbol& a, const DfxSymbol& b) {
934         return a.funcVaddr_ < b.funcVaddr_;
935     });
936     HLOGV("sort completed");
937 
938     size_t fullSize = symbols_.size();
939     size_t erased = 0;
940 
941     // Check for duplicate vaddr
942     auto last = std::unique(symbols_.begin(), symbols_.end(), [](const DfxSymbol &a, const DfxSymbol &b) {
943         return (a.funcVaddr_ == b.funcVaddr_);
944     });
945     symbols_.erase(last, symbols_.end());
946     erased = fullSize - symbols_.size();
947     HLOGV("uniqued completed");
948     auto it = symbols_.begin();
949     while (it != symbols_.end()) {
950         it->index_ = it - symbols_.begin();
951         it++;
952     }
953     HLOGV("indexed completed");
954 
955     HLOG_ASSERT(symbols_.size() != 0);
956 
957     if (textExecVaddrRange_ == maxVaddr) {
958         textExecVaddrRange_ = symbols_.back().funcVaddr_ - symbols_.front().funcVaddr_;
959     }
960 
961     HLOGDDD("%zu symbols after adjust (%zu erased) 0x%016" PRIx64 " - 0x%016" PRIx64
962             " @0x%016" PRIx64 " ",
963             symbols_.size(), erased, symbols_.front().funcVaddr_, symbols_.back().funcVaddr_,
964             textExecVaddrFileOffset_);
965 }
966 
SortMatchedSymbols()967 void SymbolsFile::SortMatchedSymbols()
968 {
969     if (matchedSymbols_.size() <= 1u) {
970         return;
971     }
972     sort(matchedSymbols_.begin(), matchedSymbols_.end(), [](const DfxSymbol* a, const DfxSymbol* b) {
973         return a->funcVaddr_ < b->funcVaddr_;
974     });
975 }
976 
GetSymbols()977 const std::vector<DfxSymbol> &SymbolsFile::GetSymbols()
978 {
979     return symbols_;
980 }
981 
GetMatchedSymbols()982 const std::vector<DfxSymbol *> &SymbolsFile::GetMatchedSymbols()
983 {
984     return matchedSymbols_;
985 }
986 
GetSymbolWithVaddr(uint64_t vaddrInFile)987 const DfxSymbol SymbolsFile::GetSymbolWithVaddr(uint64_t vaddrInFile)
988 {
989 #ifdef HIPERF_DEBUG_TIME
990     const auto startTime = steady_clock::now();
991 #endif
992     DfxSymbol symbol;
993     // it should be already order from small to large
994     auto found =
995         std::upper_bound(symbols_.begin(), symbols_.end(), vaddrInFile, DfxSymbol::ValueLessThen);
996     /*
997     if data is { 1, 2, 4, 5, 5, 6 };
998     upper_bound for each val :
999         0 < 1 at index 0
1000         1 < 2 at index 1
1001         2 < 4 at index 2
1002         3 < 4 at index 2
1003         4 < 5 at index 3
1004         5 < 6 at index 5
1005         6 < not found
1006     if key symbol vaddr is { 1, 2, 4, 5, 5, 6 };
1007      check ip vaddr for each val :
1008        ip   sym
1009         0   not found
1010         1   1
1011         1   1
1012         2   2
1013         3   3
1014         4   4
1015         5   5
1016         6   6
1017         7   7
1018     */
1019     if (found != symbols_.begin()) {
1020         found = std::prev(found);
1021         if (found->Contain(vaddrInFile)) {
1022             if (!found->matched_) {
1023                 found->matched_ = true;
1024                 matchedSymbols_.push_back(&(*found));
1025             }
1026             symbol = *found; // copy
1027             HLOGV("found '%s' for vaddr 0x%016" PRIx64 "", symbol.ToString().c_str(), vaddrInFile);
1028         }
1029     }
1030 
1031     if (!symbol.IsValid()) {
1032         HLOGV("NOT found vaddr 0x%" PRIx64 " in symbole file %s(%zu)", vaddrInFile,
1033               filePath_.c_str(), symbols_.size());
1034     }
1035     symbol.SetIpVAddress(vaddrInFile);
1036 
1037 #ifdef HIPERF_DEBUG_TIME
1038     auto usedTime = duration_cast<milliseconds>(steady_clock::now() - startTime);
1039     if (usedTime > 1ms) {
1040         HLOGW("cost %" PRId64 "ms to search ", usedTime.count());
1041     }
1042 #endif
1043     return symbol;
1044 }
1045 
CheckPathReadable(const std::string & path) const1046 bool SymbolsFile::CheckPathReadable(const std::string &path) const
1047 {
1048     if (access(path.c_str(), R_OK) == 0) {
1049         return true;
1050     } else {
1051         HLOGM("'%s' is unable read", path.c_str());
1052         return false;
1053     }
1054 }
1055 
setSymbolsFilePath(const std::vector<std::string> & symbolsSearchPaths)1056 bool SymbolsFile::setSymbolsFilePath(const std::vector<std::string> &symbolsSearchPaths)
1057 {
1058     symbolsFileSearchPaths_.clear();
1059     for (auto &symbolsSearchPath : symbolsSearchPaths) {
1060         if (CheckPathReadable(symbolsSearchPath)) {
1061             symbolsFileSearchPaths_.emplace_back(symbolsSearchPath);
1062             HLOGV("'%s' is add to symbolsSearchPath", symbolsSearchPath.c_str());
1063         }
1064     }
1065     return (symbolsFileSearchPaths_.size() > 0);
1066 }
1067 
LoadSymbolsFromSaved(const SymbolFileStruct & symbolFileStruct)1068 std::unique_ptr<SymbolsFile> SymbolsFile::LoadSymbolsFromSaved(
1069     const SymbolFileStruct &symbolFileStruct)
1070 {
1071     auto symbolsFile = CreateSymbolsFile(symbolFileStruct.filePath_);
1072     symbolsFile->filePath_ = symbolFileStruct.filePath_;
1073     symbolsFile->symbolFileType_ = (SymbolsFileType)symbolFileStruct.symbolType_;
1074     symbolsFile->textExecVaddr_ = symbolFileStruct.textExecVaddr_;
1075     symbolsFile->textExecVaddrFileOffset_ = symbolFileStruct.textExecVaddrFileOffset_;
1076     symbolsFile->buildId_ = symbolFileStruct.buildId_;
1077     for (auto &symbolStruct : symbolFileStruct.symbolStructs_) {
1078         symbolsFile->symbols_.emplace_back(symbolStruct.vaddr_, symbolStruct.len_,
1079                                            symbolStruct.symbolName_, symbolFileStruct.filePath_);
1080     }
1081     symbolsFile->AdjustSymbols(); // reorder
1082     HLOGV("load %zu symbol from SymbolFileStruct for file '%s'", symbolsFile->symbols_.size(),
1083           symbolsFile->filePath_.c_str());
1084     return symbolsFile;
1085 }
1086 
SetBoolValue(bool value)1087 void SymbolsFile::SetBoolValue(bool value)
1088 {
1089 }
1090 
ExportSymbolToFileFormat(SymbolFileStruct & symbolFileStruct)1091 void SymbolsFile::ExportSymbolToFileFormat(SymbolFileStruct &symbolFileStruct)
1092 {
1093     symbolFileStruct.filePath_ = filePath_;
1094     symbolFileStruct.symbolType_ = symbolFileType_;
1095     symbolFileStruct.textExecVaddr_ = textExecVaddr_;
1096     symbolFileStruct.textExecVaddrFileOffset_ = textExecVaddrFileOffset_;
1097     symbolFileStruct.buildId_ = buildId_;
1098 
1099     SortMatchedSymbols();
1100     auto symbols = GetMatchedSymbols();
1101     symbolFileStruct.symbolStructs_.reserve(symbols.size());
1102     for (auto symbol : symbols) {
1103         auto &symbolStruct = symbolFileStruct.symbolStructs_.emplace_back();
1104         symbolStruct.vaddr_ = symbol->funcVaddr_;
1105         symbolStruct.len_ = symbol->size_;
1106         symbolStruct.symbolName_ = symbol->GetName();
1107     }
1108 
1109     HLOGV("export %zu symbol to SymbolFileStruct from %s", symbolFileStruct.symbolStructs_.size(),
1110           filePath_.c_str());
1111 }
1112 
GetVaddrInSymbols(uint64_t ip,uint64_t,uint64_t) const1113 uint64_t SymbolsFile::GetVaddrInSymbols(uint64_t ip, uint64_t, uint64_t) const
1114 {
1115     // no convert
1116     return ip;
1117 }
1118 } // namespace NativeDaemon
1119 } // namespace Developtools
1120 } // namespace OHOS
1121