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
16 #include <stdio.h>
17 #include <string.h>
18
19 #include "comm_log.h"
20 #include "softbus_def.h"
21 #include "softbus_error_code.h"
22 #include "softbus_hidumper.h"
23 #include "softbus_hidumper_buscenter.h"
24
25 #define SOFTBUS_BUSCENTER_MODULE_NAME "buscenter"
26 #define SOFTBUS_CONN_MODULE_HELP "List all the dump item of buscenter"
27
28 static LIST_HEAD(g_busCenter_var_list);
29
SoftBusRegBusCenterVarDump(char * dumpVar,SoftBusVarDumpCb cb)30 int32_t SoftBusRegBusCenterVarDump(char *dumpVar, SoftBusVarDumpCb cb)
31 {
32 if (dumpVar == NULL || strlen(dumpVar) >= SOFTBUS_DUMP_VAR_NAME_LEN || cb == NULL) {
33 COMM_LOGE(COMM_DFX, "SoftBusRegBusCenterVarDump invalid param");
34 return SOFTBUS_INVALID_PARAM;
35 }
36 return SoftBusAddDumpVarToList(dumpVar, cb, &g_busCenter_var_list);
37 }
38
SoftBusBusCenterDumpHander(int fd,int32_t argc,const char ** argv)39 static int32_t SoftBusBusCenterDumpHander(int fd, int32_t argc, const char **argv)
40 {
41 if (fd < 0 || argc < 0 || argv == NULL) {
42 return SOFTBUS_INVALID_PARAM;
43 }
44
45 if (argc == 0 || strcmp(argv[0], "-h") == 0) {
46 SoftBusDumpSubModuleHelp(fd, SOFTBUS_BUSCENTER_MODULE_NAME, &g_busCenter_var_list);
47 return SOFTBUS_OK;
48 }
49
50 if (argc == 1 && strcmp(argv[0], "-l") == 0) {
51 SoftBusDumpSubModuleHelp(fd, SOFTBUS_BUSCENTER_MODULE_NAME, &g_busCenter_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_busCenter_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 }
67
68 if (isModuleExist == SOFTBUS_DUMP_NOT_EXIST) {
69 SoftBusDumpErrInfo(fd, argv[0]);
70 SoftBusDumpSubModuleHelp(fd, SOFTBUS_BUSCENTER_MODULE_NAME, &g_busCenter_var_list);
71 }
72 return ret;
73 }
74
SoftBusHiDumperBusCenterInit(void)75 int32_t SoftBusHiDumperBusCenterInit(void)
76 {
77 int32_t ret = SoftBusRegHiDumperHandler(
78 SOFTBUS_BUSCENTER_MODULE_NAME, SOFTBUS_CONN_MODULE_HELP, &SoftBusBusCenterDumpHander);
79 if (ret != SOFTBUS_OK) {
80 COMM_LOGE(COMM_INIT, "SoftBusBusCenterDumpHander regist fail");
81 }
82 return ret;
83 }
84
SoftBusHiDumperBusCenterDeInit(void)85 void SoftBusHiDumperBusCenterDeInit(void)
86 {
87 SoftBusReleaseDumpVar(&g_busCenter_var_list);
88 }
89