• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 #include "test_utilities.h"
16 
17 #include <securec.h>
18 #include <cinttypes>
19 #include <cstdio>
20 #include <iostream>
21 
22 namespace OHOS {
23 namespace Developtools {
24 namespace HiPerf {
CheckTestApp(const std::string & appName)25 bool CheckTestApp(const std::string& appName)
26 {
27     FILE *fp = nullptr;
28     char buf[128] = {0}; // 128: buf size
29     std::string cmd = "pidof " + appName;
30     if ((fp = popen(cmd.c_str(), "r")) != nullptr) {
31         if (fgets(buf, sizeof(buf), fp) == nullptr) {
32             pclose(fp);
33             return false;
34         }
35         pclose(fp);
36     }
37     return true;
38 }
39 
GetMemMapOffset(pid_t devhostPid,uint64_t & mapOffset,std::vector<std::shared_ptr<OHOS::HiviewDFX::DfxMap>> & memMaps,std::string & line)40 bool GetMemMapOffset(pid_t devhostPid, uint64_t &mapOffset,
41                      std::vector<std::shared_ptr<OHOS::HiviewDFX::DfxMap>> &memMaps, std::string &line)
42 {
43     pid_t pid = 0;
44     pid_t tid = 0;
45     uint64_t addr = 0;
46     uint64_t len = 0;
47     int ret = sscanf_s(line.c_str(), "  %*s %d, tid %d, addr 0x%" PRIx64 ", len 0x%" PRIx64 "",
48                        &pid, &tid, &addr, &len);
49     constexpr int numSlices {4};
50     if (ret != numSlices) {
51         printf("unknown line %d: '%s' \n", ret, line.c_str());
52         return false;
53     }
54     if (devhostPid != pid || devhostPid != tid) {
55         return false;
56     }
57     for (auto& map: memMaps) {
58         if (map->begin == addr && map->end - map->begin == len) {
59             mapOffset = map->offset;
60             return true;
61         }
62     }
63     return false;
64 }
65 } // namespace HiPerf
66 } // namespace Developtools
67 } // namespace OHOS