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 <stdio.h>
16 #include <string.h>
17 
18 #include "comm_log.h"
19 #include "softbus_errcode.h"
20 #include "softbus_adapter_mem.h"
21 #include "softbus_def.h"
22 #include "softbus_hidumper_disc.h"
23 
24 #define SOFTBUS_DISC_MODULE_NAME "disc"
25 #define SOFTBUS_DISC_MODULE_HELP "List all the dump item of disc"
26 
27 static LIST_HEAD(g_disc_var_list);
28 
SoftBusRegDiscVarDump(char * dumpVar,SoftBusVarDumpCb cb)29 int32_t SoftBusRegDiscVarDump(char *dumpVar, SoftBusVarDumpCb cb)
30 {
31     if (dumpVar == NULL || strlen(dumpVar) >= SOFTBUS_DUMP_VAR_NAME_LEN || cb == NULL) {
32         COMM_LOGE(COMM_DFX, "SoftBusRegDiscDumpCb invalid param");
33         return SOFTBUS_INVALID_PARAM;
34     }
35     return SoftBusAddDumpVarToList(dumpVar, cb, &g_disc_var_list);
36 }
37 
SoftBusDiscDumpHander(int fd,int32_t argc,const char ** argv)38 static int32_t SoftBusDiscDumpHander(int fd, int32_t argc, const char **argv)
39 {
40     if (fd < 0 || argc < 0 || argv == NULL) {
41         COMM_LOGE(COMM_DFX, "SoftBusDiscDumpHander invalid param");
42         return SOFTBUS_INVALID_PARAM;
43     }
44 
45     if (argc == 0 || strcmp(argv[0], "-h") == 0) {
46         SoftBusDumpSubModuleHelp(fd, SOFTBUS_DISC_MODULE_NAME, &g_disc_var_list);
47         return SOFTBUS_OK;
48     }
49 
50     if (argc == 1 && strcmp(argv[0], "-l") == 0) {
51         SoftBusDumpSubModuleHelp(fd, SOFTBUS_DISC_MODULE_NAME, &g_disc_var_list);
52         return SOFTBUS_OK;
53     }
54     int32_t ret = SOFTBUS_OK;
55     int32_t isModuleExist = SOFTBUS_DUMP_NOT_EXIST;
56     if (strcmp(argv[0], "-l") == 0) {
57         ListNode *item = NULL;
58         LIST_FOR_EACH(item, &g_disc_var_list) {
59             SoftBusDumpVarNode *itemNode = LIST_ENTRY(item, SoftBusDumpVarNode, node);
60             if (strcmp(itemNode->varName, argv[1]) == 0) {
61                 ret = itemNode->dumpCallback(fd);
62                 isModuleExist = SOFTBUS_DUMP_EXIST;
63                 break;
64             }
65         }
66         if (isModuleExist == SOFTBUS_DUMP_NOT_EXIST) {
67             SoftBusDumpErrInfo(fd, argv[1]);
68             SoftBusDumpSubModuleHelp(fd, SOFTBUS_DISC_MODULE_NAME, &g_disc_var_list);
69         }
70     }
71 
72     return ret;
73 }
74 
SoftBusDiscHiDumperInit(void)75 int32_t SoftBusDiscHiDumperInit(void)
76 {
77     int32_t ret = SoftBusRegHiDumperHandler(SOFTBUS_DISC_MODULE_NAME, SOFTBUS_DISC_MODULE_HELP,
78         &SoftBusDiscDumpHander);
79     if (ret != SOFTBUS_OK) {
80         COMM_LOGE(COMM_INIT, "SoftBusRegDiscDumpCb registe fail");
81     }
82     return ret;
83 }
84 
SoftBusHiDumperDiscDeInit(void)85 void SoftBusHiDumperDiscDeInit(void)
86 {
87     SoftBusReleaseDumpVar(&g_disc_var_list);
88 }