1 /*
2 * Copyright (c) 2023 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 "anonymizer.h"
16 #include "legacy/softbus_hidumper_alarm.h"
17
18 #include <stdio.h>
19 #include <string.h>
20
21 #include "comm_log.h"
22 #include "softbus_error_code.h"
23 #include "softbus_adapter_mem.h"
24 #include "softbus_def.h"
25 #include "legacy/softbus_hidumper.h"
26 #include "legacy/softbus_hidumper_util.h"
27
28 #define SOFTBUS_CONTROL_ALARM_ORDER "control"
29 #define SOFTBUS_MANAGEMENT_ALARM_ORDER "management"
30
31 #define SOFTBUS_ALARM_MODULE_NAME "alert"
32 #define SOFTBUS_ALARM_MODULE_HELP "List all the dump item of alert"
33
34 #define TWENTY_FOUR_HOURS (24 * 60)
35
SoftBusGetAlarmInfo(int fd,AlarmRecord * record)36 static void SoftBusGetAlarmInfo(int fd, AlarmRecord *record)
37 {
38 SOFTBUS_DPRINTF(fd, "Time=%s, Type=%d", record->time, record->type);
39 if (record->errorCode > 0) {
40 SOFTBUS_DPRINTF(fd, ", ErrorCode=%d", record->errorCode);
41 }
42
43 if (record->callerPid > 0) {
44 SOFTBUS_DPRINTF(fd, ", CallerPid=%d", record->callerPid);
45 }
46
47 if (record->linkType > 0) {
48 SOFTBUS_DPRINTF(fd, ", LinkType=%d", record->linkType);
49 }
50
51 if (record->minBw > 0) {
52 SOFTBUS_DPRINTF(fd, ", MinBw=%d", record->minBw);
53 }
54
55 if (record->methodId > 0) {
56 SOFTBUS_DPRINTF(fd, ", MethodId=%d", record->minBw);
57 }
58
59 if (record->permissionName != NULL) {
60 SOFTBUS_DPRINTF(fd, ", PermissionName=%s", record->permissionName);
61 }
62
63 if (record->sessionName != NULL) {
64 char *tmpName = NULL;
65 Anonymize(record->sessionName, &tmpName);
66 SOFTBUS_DPRINTF(fd, ", SessionName=%s", AnonymizeWrapper(tmpName));
67 AnonymizeFree(tmpName);
68 }
69 SOFTBUS_DPRINTF(fd, "\n");
70 }
71
SoftBusAlarmDumpHander(int fd,int32_t argc,const char ** argv)72 static int32_t SoftBusAlarmDumpHander(int fd, int32_t argc, const char **argv)
73 {
74 if (fd < 0 || argc != 1 || argv == NULL) {
75 return SOFTBUS_ERR;
76 }
77
78 SoftBusAlarmEvtResult *result = (SoftBusAlarmEvtResult *)SoftBusMalloc(sizeof(SoftBusAlarmEvtResult));
79 if (result == NULL) {
80 SOFTBUS_DPRINTF(fd, "SoftBusAlarmDumpHander result malloc fail!\n");
81 return SOFTBUS_ERR;
82 }
83 if (strcmp(argv[0], SOFTBUS_MANAGEMENT_ALARM_ORDER) == SOFTBUS_OK) {
84 if (SoftBusQueryAlarmInfo(TWENTY_FOUR_HOURS, SOFTBUS_MANAGEMENT_ALARM_TYPE, result) != SOFTBUS_OK) {
85 SOFTBUS_DPRINTF(fd, "SoftBusAlarmDumpHander query fail!\n");
86 SoftBusFree(result);
87 return SOFTBUS_ERR;
88 }
89 SOFTBUS_DPRINTF(fd, "SoftBus Management Plane Alarms:\n");
90 } else if (strcmp(argv[0], SOFTBUS_CONTROL_ALARM_ORDER) == SOFTBUS_OK) {
91 if (SoftBusQueryAlarmInfo(TWENTY_FOUR_HOURS, SOFTBUS_CONTROL_ALARM_TYPE, result) != SOFTBUS_OK) {
92 SOFTBUS_DPRINTF(fd, "SoftBusAlarmDumpHander query fail!\n");
93 SoftBusFree(result);
94 return SOFTBUS_ERR;
95 }
96 SOFTBUS_DPRINTF(fd, "SoftBus Control Plane Alarms:\n");
97 } else {
98 SOFTBUS_DPRINTF(fd, "SoftBusAlarmDumpHander invalid param!\n");
99 SoftBusFree(result);
100 return SOFTBUS_ERR;
101 }
102
103 if (result->recordSize == 0) {
104 SOFTBUS_DPRINTF(fd, "SoftBusAlarmDumpHander query result is zero!\n");
105 SoftBusFree(result);
106 return SOFTBUS_OK;
107 }
108
109 for (size_t i = 0; i < result->recordSize; i++) {
110 AlarmRecord *record = &result->records[i];
111 if (record == NULL) {
112 continue;
113 }
114 SoftBusGetAlarmInfo(fd, record);
115 }
116
117 SoftBusFree(result);
118 return SOFTBUS_OK;
119 }
120
SoftBusAlarmHiDumperInit(void)121 int32_t SoftBusAlarmHiDumperInit(void)
122 {
123 int32_t ret = SoftBusRegHiDumperHandler(SOFTBUS_ALARM_MODULE_NAME, SOFTBUS_ALARM_MODULE_HELP,
124 &SoftBusAlarmDumpHander);
125 if (ret != SOFTBUS_OK) {
126 COMM_LOGE(COMM_DFX, "SoftBusRegAlarmDumpCb registe fail");
127 }
128 return ret;
129 }
130