huangcm
2025-08-25 f350412dc55c15118d0a7925d1071877498e5e24
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
 * Copyright 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include <android/hidl/manager/1.0/IServiceManager.h>
#include <cutils/properties.h>
#include <hidl/ServiceManagement.h>
#include <iostream>
 
using namespace std;
using namespace android;
 
const string kSysPropHalCoverage = "hal.coverage.enable";
 
// Print usage directions.
void usage() {
  cout << "usage:\n";
  cout << "vts_coverage_configure flush\t\t\t\t: to flush coverage on all "
          "HALs\n";
  cout << "vts_coverage_configure flush <hal name>@<hal version>\t: to flush "
          "coverage on one HAL name/version instance"
       << std::endl;
}
 
// Parse the fully-qualified instance name and call the func with the interface
// name, instance name, and HAL name.
template <typename Lambda>
bool parseFqInstaceName(string fqInstanceName, Lambda &&func) {
  string::size_type n = fqInstanceName.find("/");
  if (n == std::string::npos || fqInstanceName.size() == n + 1) return false;
 
  string fqInterfaceName = fqInstanceName.substr(0, n);
  string instanceName = fqInstanceName.substr(n + 1, std::string::npos);
 
  n = fqInstanceName.find("::");
  if (n == std::string::npos || fqInstanceName.size() == n + 1) return false;
  string halName = fqInstanceName.substr(0, n);
  std::forward<Lambda>(func)(fqInterfaceName, instanceName, halName);
  return true;
}
 
// Flush coverage on all HAL processes, or just the provided HAL name if
// provided.
bool FlushHALCoverage(string flushHal = "") {
  using ::android::hidl::base::V1_0::IBase;
  using ::android::hidl::manager::V1_0::IServiceManager;
  using ::android::hardware::hidl_string;
  using ::android::hardware::Return;
 
  sp<IServiceManager> sm = ::android::hardware::defaultServiceManager();
 
  if (sm == nullptr) {
    cerr << "failed to get IServiceManager to poke HAL services." << std::endl;
    return false;
  }
  property_set(kSysPropHalCoverage.c_str(), "true");
  auto listRet = sm->list([&](const auto &interfaces) {
    for (const string &fqInstanceName : interfaces) {
      hidl_string fqInterfaceName;
      hidl_string instanceName;
      string halName;
 
      auto cb = [&](string fqIface, string instance, string hal) {
        fqInterfaceName = fqIface;
        instanceName = instance;
        halName = hal;
      };
      if (!parseFqInstaceName(fqInstanceName, cb)) continue;
      if (halName.find("android.hidl") == 0) continue;
      if (flushHal == "" || !flushHal.compare(halName)) {
        Return<sp<IBase>> interfaceRet = sm->get(fqInterfaceName, instanceName);
        if (!interfaceRet.isOk()) {
          cerr << "failed to get service " << fqInstanceName << ": "
               << interfaceRet.description() << std::endl;
          continue;
        }
        sp<IBase> interface = interfaceRet;
        auto notifyRet = interface->notifySyspropsChanged();
        if (!notifyRet.isOk()) {
          cerr << "failed to notifySyspropsChanged on service "
               << fqInstanceName << ": " << notifyRet.description()
               << std::endl;
        }
        cout << "- flushed the coverage for HAL " << fqInstanceName
             << std::endl;
      }
    }
  });
  property_set(kSysPropHalCoverage.c_str(), "false");
  if (!listRet.isOk()) {
    cerr << "failed to list services: " << listRet.description() << std::endl;
    return false;
  }
  return true;
}
 
// The provided binary can be used to flush coverage on one or all HALs.
// Usage examples:
//   To flush gcov and/or sancov coverage data on all hals: <binary> flush
//   To flush gcov and/or sancov coverage data on one hal: <binary> flush <hal
//   name>@<hal version>
int main(int argc, char *argv[]) {
  bool flush_coverage = false;
  if (argc < 2) {
    usage();
    return -1;
  }
  if (!strcmp(argv[1], "flush")) {
    flush_coverage = true;
    string halString = "";
    if (argc == 3) {
      halString = string(argv[2]);
    }
    cout << "* flush coverage" << std::endl;
    if (!FlushHALCoverage(halString)) {
      cerr << "failed to flush coverage" << std::endl;
    }
  } else {
    usage();
  }
  return 0;
}