• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 <securec.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 
20 #include "hidumper.h"
21 
22 #ifdef __cplusplus
23 #if __cplusplus
24 extern "C" {
25 #endif
26 #endif
27 
28 #define FAULT_ADDR          0x123
29 #define FAULT_VALUE         0x456
30 #define SYS_INFO_HEADER     "*********** sys info ***********"
31 #define CPU_USAGE_HEADER    "*********** cpu usage ***********"
32 #define MEM_USAGE_HEADER    "*********** mem usage ***********"
33 #define TASK_INFO_HEADER    "*********** task info ***********"
34 
35 static int g_isAdapterRegistered = 0;
36 static struct HiDumperAdapter g_hidumperAdapter;
37 
Usage(void)38 static void Usage(void)
39 {
40     printf("usage:\r\n"
41         "AT+HIDUMPER=                                dump cpu usage, memory usage and all tasks\r\n"
42         "or:\r\n");
43     printf("AT+HIDUMPER=-dc                             dump the cpu usage\r\n"
44         "AT+HIDUMPER=-df                             dump the latest fault logs\r\n");
45     printf("AT+HIDUMPER=-dm                             dump the memory usage\r\n"
46         "AT+HIDUMPER=-dt                             dump all the tasks\r\n");
47     printf("AT+HIDUMPER=-h                              help text for the tool\r\n"
48         "AT+HIDUMPER=-ikc                            inject kernel crash\r\n");
49     printf("AT+HIDUMPER=-m                              dump memory to stdout in hex format\r\n"
50         "AT+HIDUMPER=-m,filepath                     dump memory to filepath in the device in hex format\r\n");
51     printf("AT+HIDUMPER=-m,memstart,memsize             dump memory with starting address memstart(hex) and\r\n"
52         "                                            size memsize(hex) to stdout in hex format\r\n");
53     printf("AT+HIDUMPER=-m,memstart,memsize,filepath    dump memory with starting address memstart(hex) and\r\n"
54         "                                            size memsize(hex) to filepath in the device in hex format\r\n");
55 }
56 
DumpAllInfo(void)57 static void DumpAllInfo(void)
58 {
59     printf("%s\n", SYS_INFO_HEADER);
60     g_hidumperAdapter.DumpSysInfo();
61     printf("%s\n", CPU_USAGE_HEADER);
62     g_hidumperAdapter.DumpCpuUsage();
63     printf("%s\n", MEM_USAGE_HEADER);
64     g_hidumperAdapter.DumpMemUsage();
65     printf("%s\n", TASK_INFO_HEADER);
66     g_hidumperAdapter.DumpTaskInfo();
67 }
68 
InjectKernelCrash(void)69 static void InjectKernelCrash(void)
70 {
71 #ifdef OHOS_DEBUG
72     int *ptr = (int *)FAULT_ADDR;
73     *ptr = FAULT_VALUE;
74 #else
75     printf("Unsupported!\n");
76 #endif
77 }
78 
HiDumperRegisterAdapter(struct HiDumperAdapter * pAdapter)79 int HiDumperRegisterAdapter(struct HiDumperAdapter *pAdapter)
80 {
81     if (pAdapter == NULL) {
82         printf("Invalid pAdapter: %p\n", pAdapter);
83         return -1;
84     }
85 
86     if (pAdapter->DumpSysInfo == NULL ||
87         pAdapter->DumpCpuUsage == NULL ||
88         pAdapter->DumpMemUsage == NULL ||
89         pAdapter->DumpTaskInfo == NULL ||
90         pAdapter->DumpFaultLog == NULL ||
91         pAdapter->DumpMemRegion == NULL ||
92         pAdapter->DumpAllMem == NULL) {
93             printf("Invalid adapter funcs!\n");
94             return -1;
95     }
96     if (memcpy_s(&g_hidumperAdapter, sizeof(g_hidumperAdapter),
97         pAdapter, sizeof(*pAdapter)) != EOK) {
98             printf("memcpy_s is error\n");
99     }
100     g_isAdapterRegistered = 1;
101 
102     return 0;
103 }
104 
ParameterMatching(int argc,const char * argv[])105 void ParameterMatching(int argc, const char *argv[])
106 {
107     if (argc == 0) {
108         DumpAllInfo();
109     } else if (argc == ONE_OF_ARGC_PARAMETERS) {
110         if (strcmp(argv[0], "-h") == 0) {
111             Usage();
112         } else if (strcmp(argv[0], "-dc") == 0) {
113             printf("%s\n", CPU_USAGE_HEADER);
114             g_hidumperAdapter.DumpCpuUsage();
115         } else if (strcmp(argv[0], "-df") == 0) {
116             g_hidumperAdapter.DumpFaultLog();
117         } else if (strcmp(argv[0], "-dm") == 0) {
118             printf("%s\n", MEM_USAGE_HEADER);
119             g_hidumperAdapter.DumpMemUsage();
120         } else if (strcmp(argv[0], "-dt") == 0) {
121             printf("%s\n", TASK_INFO_HEADER);
122             g_hidumperAdapter.DumpTaskInfo();
123         } else if (strcmp(argv[0], "-ikc") == 0) {
124             InjectKernelCrash();
125         } else if (strcmp(argv[0], "-m") == 0) {
126 #ifdef OHOS_DEBUG
127             g_hidumperAdapter.DumpAllMem();
128 #else
129             printf("Unsupported!\n");
130 #endif
131         } else {
132             Usage();
133         }
134     } else if (argc == TWO_OF_ARGC_PARAMETERS && strcmp(argv[0], "-m") == 0) {
135         printf("Unsupported!\n");
136     } else if (argc == THREE_OF_ARGC_PARAMETERS) {
137         if (strcmp(argv[0], "-m") == 0) {
138 #ifdef OHOS_DEBUG
139             g_hidumperAdapter.DumpMemRegion(
140                 strtoull(argv[ONE_OF_ARGC_PARAMETERS], NULL, BUF_SIZE_16),
141                 strtoull(argv[TWO_OF_ARGC_PARAMETERS], NULL, BUF_SIZE_16));
142 #else
143             printf("Unsupported!\n");
144 #endif
145         } else {
146             Usage();
147         }
148     } else if (argc == FOUR_OF_ARGC_PARAMETERS && strcmp(argv[0], "-m") == 0) {
149         printf("Unsupported!\r\n");
150     } else {
151         Usage();
152     }
153 }
154 
at_hidumper(unsigned int argc,const char ** argv)155 unsigned int at_hidumper(unsigned int argc, const char **argv)
156 {
157     if (g_isAdapterRegistered == 0) {
158         printf("No adapter has been registered!\n");
159         return 1;
160     }
161 
162     ParameterMatching(argc, argv);
163 
164     return 0;
165 }
166 
167 #ifdef __cplusplus
168 #if __cplusplus
169 }
170 #endif
171 #endif