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 #include "comm_log.h"
18 #include "softbus_errcode.h"
19 #include "softbus_hidumper.h"
20 #include "fillpinc.h"
21 #include "nstackx.h"
22 #include "nstackx_dfile.h"
23 #include "softbus_hidumper_nstack.h"
24 
25 #define SOFTBUS_DSTREAM_MODULE_NAME "dstream"
26 #define SOFTBUS_DSTREAM_MODULE_HELP "List all the dump item of dstream"
27 #define SOFTBUS_DFILE_MODULE_NAME "dfile"
28 #define SOFTBUS_DFILE_MODULE_HELP "List all the dump item of dfile"
29 #define SOFTBUS_DFINDLER_MODULE_NAME "dfinder"
30 #define SOFTBUS_DFINDLER_MODULE_HELP "List all the dump item of dfinder"
31 #define SOFTBUS_DMSG_MODULE_NAME "dmsg"
32 #define SOFTBUS_DMSG_MODULE_HELP "List all the dump item of dmsg"
33 
34 #define SOFTBUF_NSTACK_DUMP_BUF_LEN (2048)
35 
SoftBufNstackDumpFunc(void * softObj,const char * data,uint32_t len)36 void SoftBufNstackDumpFunc(void *softObj, const char *data, uint32_t len)
37 {
38     int fd = *(int *)softObj;
39     size_t dataLen = strnlen(data, SOFTBUF_NSTACK_DUMP_BUF_LEN);
40     if (dataLen == 0 || dataLen == SOFTBUF_NSTACK_DUMP_BUF_LEN || dataLen != len) {
41         COMM_LOGE(COMM_DFX, "SoftBufNstackDumpFunc len error, dataStrlen=%{public}zu, len=%{public}d.", dataLen, len);
42         return;
43     }
44     SOFTBUS_DPRINTF(fd, "%s", data);
45 }
46 
SoftBusNStackDstreamDumpHander(int fd,int32_t argc,const char ** argv)47 static int32_t SoftBusNStackDstreamDumpHander(int fd, int32_t argc, const char **argv)
48 {
49     if (fd < 0 || argc < 0 || argv == NULL) {
50         COMM_LOGE(COMM_DFX, "SoftBusNStackDstreamDumpHander invalid input");
51         return SOFTBUS_INVALID_PARAM;
52     }
53 #ifdef FILLP_ENHANCED
54     if (FtDfxHiDumper((uint32_t)argc, argv, &fd, SoftBufNstackDumpFunc) != 0) {
55         COMM_LOGE(COMM_DFX, "call FtDfxHiDumper failed!");
56         return SOFTBUS_NSTACK_DUMP_ERR;
57     }
58 #endif
59     return SOFTBUS_OK;
60 }
SoftBusNStackDfileDumpHander(int fd,int32_t argc,const char ** argv)61 static int32_t SoftBusNStackDfileDumpHander(int fd, int32_t argc, const char **argv)
62 {
63     if (fd < 0 || argc < 0 || argv == NULL) {
64         COMM_LOGE(COMM_DFX, "SoftBusNStackDfileDumpHander invalid input");
65         return SOFTBUS_INVALID_PARAM;
66     }
67 #ifdef FILLP_ENHANCED
68     if (NSTACKX_DFileDump((uint32_t)argc, argv, &fd, SoftBufNstackDumpFunc) != 0) {
69         COMM_LOGE(COMM_DFX, "call NSTACKX_DFileDump failed!");
70         return SOFTBUS_NSTACK_DUMP_ERR;
71     }
72 #endif
73     return SOFTBUS_OK;
74 }
SoftBusNStackDumpDfinderHander(int fd,int32_t argc,const char ** argv)75 static int32_t SoftBusNStackDumpDfinderHander(int fd, int32_t argc, const char **argv)
76 {
77     if (fd < 0 || argc < 0 || argv == NULL) {
78         COMM_LOGE(COMM_DFX, "NSTACKX_DFinderDump invalid input!");
79         return SOFTBUS_INVALID_PARAM;
80     }
81 #ifdef FILLP_ENHANCED
82     if (NSTACKX_DFinderDump(argv, (uint32_t)argc, &fd, SoftBufNstackDumpFunc) != 0) {
83         COMM_LOGE(COMM_DFX, "call NSTACKX_DFinderDump failed!");
84         return SOFTBUS_NSTACK_DUMP_ERR;
85     }
86 #endif
87     return SOFTBUS_OK;
88 }
89 
SoftBusNStackDmsgDumpHander(int fd,int32_t argc,const char ** argv)90 static int32_t SoftBusNStackDmsgDumpHander(int fd, int32_t argc, const char **argv)
91 {
92     if (fd < 0 || argc < 0 || argv == NULL) {
93         COMM_LOGE(COMM_DFX, "invalid param");
94         return SOFTBUS_INVALID_PARAM;
95     }
96 
97     return SOFTBUS_OK;
98 }
99 
SoftBusNStackHiDumperInit(void)100 int32_t SoftBusNStackHiDumperInit(void)
101 {
102     int32_t ret = SoftBusRegHiDumperHandler(SOFTBUS_DSTREAM_MODULE_NAME, SOFTBUS_DSTREAM_MODULE_HELP,
103         &SoftBusNStackDstreamDumpHander);
104     if (ret != SOFTBUS_OK) {
105         COMM_LOGE(COMM_INIT, "SoftBusNStackHiDumperInit regist dstream handler fail");
106         return ret;
107     }
108 
109     ret = SoftBusRegHiDumperHandler(SOFTBUS_DFILE_MODULE_NAME, SOFTBUS_DFILE_MODULE_HELP,
110         &SoftBusNStackDfileDumpHander);
111     if (ret != SOFTBUS_OK) {
112         COMM_LOGE(COMM_INIT, "SoftBusNStackHiDumperInit regist dstream handler fail");
113         return ret;
114     }
115 
116     ret = SoftBusRegHiDumperHandler(SOFTBUS_DFINDLER_MODULE_NAME, SOFTBUS_DFINDLER_MODULE_HELP,
117         &SoftBusNStackDumpDfinderHander);
118     if (ret != SOFTBUS_OK) {
119         COMM_LOGE(COMM_INIT, "SoftBusNStackHiDumperInit regist dstream handler fail");
120         return ret;
121     }
122 
123     ret = SoftBusRegHiDumperHandler(SOFTBUS_DMSG_MODULE_NAME, SOFTBUS_DMSG_MODULE_HELP,
124         &SoftBusNStackDmsgDumpHander);
125     if (ret != SOFTBUS_OK) {
126         COMM_LOGE(COMM_INIT, "SoftBusNStackHiDumperInit regist dstream handler fail");
127         return ret;
128     }
129     return ret;
130 }
131