1 /*
2 * Copyright (c) 2022 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 "legacy/softbus_hidumper_trans.h"
16
17 #include <stdio.h>
18 #include <string.h>
19
20 #include "anonymizer.h"
21 #include "comm_log.h"
22 #include "securec.h"
23 #include "softbus_adapter_mem.h"
24 #include "softbus_error_code.h"
25 #include "softbus_utils.h"
26
27 #define MAX_ID_LEN (10)
28 #define MODULE_NAME_TRAN "trans"
29 #define SOFTBUS_TRANS_MODULE_HELP "List all the dump item of trans"
30
31 const char* g_linkTypeList[DUMPER_LANE_LINK_TYPE_BUTT] = {
32 "BR",
33 "BLE",
34 "P2P",
35 "wlan",
36 "eth",
37 };
38
39 const char* g_dataTypeList[BUSINESS_TYPE_BUTT] = {
40 "Message",
41 "Byte",
42 "File",
43 "Stream",
44 "NotCare",
45 };
46
47 static LIST_HEAD(g_trans_var_list);
48
SoftBusRegTransVarDump(const char * dumpVar,SoftBusVarDumpCb cb)49 int32_t SoftBusRegTransVarDump(const char *dumpVar, SoftBusVarDumpCb cb)
50 {
51 if (dumpVar == NULL || strlen(dumpVar) >= SOFTBUS_DUMP_VAR_NAME_LEN || cb == NULL) {
52 COMM_LOGE(COMM_DFX, "SoftBusRegTransVarDump invalid param");
53 return SOFTBUS_INVALID_PARAM;
54 }
55 return SoftBusAddDumpVarToList(dumpVar, cb, &g_trans_var_list);
56 }
57
SoftBusTransDumpRegisterSession(int fd,const char * pkgName,const char * sessionName,int uid,int pid)58 void SoftBusTransDumpRegisterSession(int fd, const char* pkgName, const char* sessionName,
59 int uid, int pid)
60 {
61 if (fd < 0 || pkgName == NULL || sessionName == NULL || uid < 0 || pid < 0) {
62 COMM_LOGE(COMM_DFX, "param is invalid");
63 return;
64 }
65
66 char uidArr[MAX_ID_LEN] = {0};
67 char pidArr[MAX_ID_LEN] = {0};
68 char uidStr[MAX_ID_LEN] = {0};
69 char pidStr[MAX_ID_LEN] = {0};
70 if (sprintf_s(uidArr, MAX_ID_LEN, "%d", uid) < 0 || sprintf_s(pidArr, MAX_ID_LEN, "%d", pid) < 0) {
71 COMM_LOGE(COMM_DFX, "set uidArr or pidArr failed");
72 return;
73 }
74
75 DataMasking(uidArr, MAX_ID_LEN, ID_DELIMITER, uidStr);
76 DataMasking(pidArr, MAX_ID_LEN, ID_DELIMITER, pidStr);
77
78 char *tmpPkgName = NULL;
79 char *tmpName = NULL;
80 Anonymize(sessionName, &tmpName);
81 Anonymize(pkgName, &tmpPkgName);
82 SOFTBUS_DPRINTF(fd, "SessionName : %s\n", AnonymizeWrapper(tmpName));
83 SOFTBUS_DPRINTF(fd, "pkgName : %s\n", AnonymizeWrapper(tmpPkgName));
84 SOFTBUS_DPRINTF(fd, "PID : %s\n", uidStr);
85 SOFTBUS_DPRINTF(fd, "UID : %s\n", pidStr);
86 AnonymizeFree(tmpName);
87 AnonymizeFree(tmpPkgName);
88 }
89
SoftBusTransDumpRunningSession(int fd,TransDumpLaneLinkType type,AppInfo * appInfo)90 void SoftBusTransDumpRunningSession(int fd, TransDumpLaneLinkType type, AppInfo* appInfo)
91 {
92 if (fd < 0 || type < DUMPER_LANE_BR || type >= DUMPER_LANE_LINK_TYPE_BUTT || appInfo == NULL) {
93 COMM_LOGE(COMM_DFX, "param is invalid");
94 return;
95 }
96
97 char *localSessionName = NULL;
98 char *remoteSessionName = NULL;
99 char *remoteDeviceId = NULL;
100 char *localAddr = NULL;
101 char *remoteAddr = NULL;
102 Anonymize(appInfo->myData.sessionName, &localSessionName);
103 Anonymize(appInfo->peerData.sessionName, &remoteSessionName);
104 Anonymize(appInfo->peerData.deviceId, &remoteDeviceId);
105 Anonymize(appInfo->myData.addr, &localAddr);
106 Anonymize(appInfo->peerData.addr, &remoteAddr);
107 SOFTBUS_DPRINTF(fd, "LocalSessionName : %s\n", AnonymizeWrapper(localSessionName));
108 SOFTBUS_DPRINTF(fd, "RemoteSessionName : %s\n", AnonymizeWrapper(remoteSessionName));
109 SOFTBUS_DPRINTF(fd, "PeerDeviceId : %s\n", AnonymizeWrapper(remoteDeviceId));
110 SOFTBUS_DPRINTF(fd, "LinkType : %s\n", g_linkTypeList[type]);
111 SOFTBUS_DPRINTF(fd, "SourceAddress : %s\n", AnonymizeWrapper(localAddr));
112 SOFTBUS_DPRINTF(fd, "DestAddress : %s\n", AnonymizeWrapper(remoteAddr));
113 SOFTBUS_DPRINTF(fd, "DataType : %s\n", g_dataTypeList[appInfo->businessType]);
114 AnonymizeFree(localSessionName);
115 AnonymizeFree(remoteSessionName);
116 AnonymizeFree(remoteDeviceId);
117 AnonymizeFree(localAddr);
118 AnonymizeFree(remoteAddr);
119 }
120
SoftBusTransDumpHandler(int fd,int argc,const char ** argv)121 static int SoftBusTransDumpHandler(int fd, int argc, const char **argv)
122 {
123 if (fd < 0 || argv == NULL || argc < 0) {
124 COMM_LOGE(COMM_DFX, "param is invalid ");
125 return SOFTBUS_INVALID_PARAM;
126 }
127 if (argc == 0 || ((argc == 1) && (strcmp(argv[0], "-h") == 0)) || (argc == 1 && strcmp(argv[0], "-l") == 0)) {
128 SoftBusDumpSubModuleHelp(fd, (char *)MODULE_NAME_TRAN, &g_trans_var_list);
129 return SOFTBUS_OK;
130 }
131
132 int32_t ret = SOFTBUS_OK;
133 int32_t isModuleExist = SOFTBUS_DUMP_NOT_EXIST;
134 if (argc > 1 && strcmp(argv[0], "-l") == 0) {
135 ListNode *item = NULL;
136 LIST_FOR_EACH(item, &g_trans_var_list) {
137 SoftBusDumpVarNode *itemNode = LIST_ENTRY(item, SoftBusDumpVarNode, node);
138 if (strcmp(itemNode->varName, argv[1]) == 0 && itemNode->dumpCallback != NULL) {
139 ret = itemNode->dumpCallback(fd);
140 isModuleExist = SOFTBUS_DUMP_EXIST;
141 break;
142 }
143 }
144 if (isModuleExist == SOFTBUS_DUMP_NOT_EXIST) {
145 SoftBusDumpErrInfo(fd, argv[1]);
146 SoftBusDumpSubModuleHelp(fd, (char *)MODULE_NAME_TRAN, &g_trans_var_list);
147 }
148 }
149 return ret;
150 }
151
SoftBusTransDumpHandlerInit(void)152 int32_t SoftBusTransDumpHandlerInit(void)
153 {
154 int32_t ret = SoftBusRegHiDumperHandler((char*)MODULE_NAME_TRAN, (char*)SOFTBUS_TRANS_MODULE_HELP,
155 &SoftBusTransDumpHandler);
156 if (ret != SOFTBUS_OK) {
157 COMM_LOGE(COMM_INIT, "SoftBusTransDumpHander regist fail");
158 }
159 return ret;
160 }
161
SoftBusHiDumperTransDeInit(void)162 void SoftBusHiDumperTransDeInit(void)
163 {
164 SoftBusReleaseDumpVar(&g_trans_var_list);
165 }
166