1 /*
2 * Copyright (c) 2023-2025 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
16 #include "faultlog_bundle_util.h"
17
18 #include <regex>
19 #include "bundle_mgr_client.h"
20 #include "hiview_logger.h"
21
22 #undef LOG_DOMAIN
23 #define LOG_DOMAIN 0xD002D11
24
25 #undef LOG_TAG
26 #define LOG_TAG "FaultlogBundleUtil"
27
28 namespace OHOS {
29 namespace HiviewDFX {
30 using namespace OHOS::AppExecFwk;
IsNameValid(const std::string & name,const std::string & sep,bool canEmpty)31 bool IsNameValid(const std::string& name, const std::string& sep, bool canEmpty)
32 {
33 std::vector<std::string> nameVec;
34 SplitStr(name, sep, nameVec, canEmpty, false);
35 std::regex re("^[a-zA-Z0-9][a-zA-Z0-9_]{0,127}$");
36 for (auto const& splitName : nameVec) {
37 if (!std::regex_match(splitName, re)) {
38 HILOG_INFO(LOG_CORE, "Invalid splitName:%{public}s", splitName.c_str());
39 return false;
40 }
41 }
42 return true;
43 }
44
IsModuleNameValid(const std::string & name)45 bool IsModuleNameValid(const std::string& name)
46 {
47 if (name.empty() || name.size() > MAX_NAME_LENGTH) {
48 HILOG_INFO(LOG_CORE, "invalid log name.");
49 return false;
50 }
51
52 if (name.find("/") != std::string::npos || name.find(".") == std::string::npos) {
53 std::string path = name.substr(1); // may skip first .
54 path.erase(path.find_last_not_of(" \n\r\t") + 1);
55 HILOG_INFO(LOG_CORE, "module name:%{public}s", name.c_str());
56 return IsNameValid(path, "/", false);
57 }
58
59 return IsNameValid(name, ".", true);
60 }
61
GetApplicationNameById(int32_t uid)62 std::string GetApplicationNameById(int32_t uid)
63 {
64 std::string bundleName;
65 AppExecFwk::BundleMgrClient client;
66 if (client.GetNameForUid(uid, bundleName) != ERR_OK) {
67 HILOG_WARN(LOG_CORE, "Failed to query bundleName from bms, uid:%{public}d.", uid);
68 } else {
69 HILOG_INFO(LOG_CORE, "bundleName of uid:%{public}d is %{public}s", uid, bundleName.c_str());
70 }
71 return bundleName;
72 }
73
GetDfxBundleInfo(const std::string & bundleName,DfxBundleInfo & bundleInfo)74 bool GetDfxBundleInfo(const std::string& bundleName, DfxBundleInfo& bundleInfo)
75 {
76 AppExecFwk::BundleInfo info;
77 AppExecFwk::BundleMgrClient client;
78 if (!client.GetBundleInfo(bundleName, AppExecFwk::BundleFlag::GET_BUNDLE_DEFAULT,
79 info, Constants::ALL_USERID)) {
80 HILOG_WARN(LOG_CORE, "Failed to query BundleInfo from bms, bundle:%{public}s.", bundleName.c_str());
81 return false;
82 } else {
83 HILOG_INFO(LOG_CORE, "The version of %{public}s is %{public}s", bundleName.c_str(),
84 info.versionName.c_str());
85 }
86 bundleInfo.isPreInstalled = info.isPreInstallApp;
87 bundleInfo.versionName = info.versionName;
88 bundleInfo.versionCode = info.versionCode;
89 return true;
90 }
91 } // namespace HiviewDFX
92 } // namespace OHOS