1 /*
2 * Copyright (c) 2021 HiSilicon (Shanghai) Technologies CO., LIMITED.
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 <hi_dumper.h>
17 #include <hi_dumper_adapter.h>
18
19 #include <securec.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <stdlib.h>
23
24 #include <cpup_diag_dfx.h>
25 #include <hi_at.h>
26 #include <hi_cpu.h>
27 #include <hi_fs.h>
28 #include <hi_mem.h>
29 #include <hi_os_stat.h>
30 #include <hi_task.h>
31 #include <hi_ver.h>
32 #include <hi_watchdog.h>
33 #include <hi_at.h>
34 #include <hi_errno.h>
35 #include <los_compiler.h>
36 #include <los_task_pri.h>
37
38 #include "sal_common.h"
39
40 #ifdef __cplusplus
41 #if __cplusplus
42 extern "C" {
43 #endif
44 #endif
45
46 #define FAULT_ADDR 0x123
47 #define FAULT_VALUE 0x456
48 #define DDR_START_ADDR 0xd8000
49 #define DDR_SIZE 0x46000
50 #define HEX_NUM_PER_LINE 16
51 #define FAULT_LOG_PATH "/kernel_fault.log"
52
53 extern unsigned int at_hidumper(unsigned int argc, const char **argv);
54
at_hidumper(unsigned int argc,const char ** argv)55 WEAK unsigned int at_hidumper(unsigned int argc, const char **argv)
56 {
57 (void)argc;
58 (void)argv;
59 return 0;
60 }
61
62 static const at_cmd_func g_hidumperAT[] = {
63 {"+HIDUMPER", 9, HI_NULL, HI_NULL, (at_call_back_func)at_hidumper, (at_call_back_func)at_hidumper},
64 };
65
DumpSysInfo(void)66 int DumpSysInfo(void)
67 {
68 printf("SDK Version: %s\n", hi_get_sdk_version());
69 printf("Boot Version: 0x%x\n", hi_get_boot_ver());
70 printf("Kernel Version: 0x%x\n", hi_get_kernel_ver());
71
72 return 0;
73 }
74
DumpCpuUsage(void)75 int DumpCpuUsage(void)
76 {
77 cmd_get_cpup(0, HI_NULL);
78 sal_show_run_time();
79
80 return 0;
81 }
82
DumpMemUsage(void)83 int DumpMemUsage(void)
84 {
85 hi_os_resource_use_stat os_resource_stat = {0};
86 hi_mdm_mem_info mem_inf = {0};
87
88 (hi_void)hi_os_get_resource_status(&os_resource_stat);
89 (hi_void)hi_mem_get_sys_info(&mem_inf);
90 printf("Unit: Byte\n");
91 printf("total=%d,", (mem_inf.total + mem_inf.total_lmp));
92 printf("used=%d,", (mem_inf.used + mem_inf.used_lmp));
93 printf("free=%d,", (mem_inf.free + mem_inf.free_lmp));
94 printf("peek_size=%d\n", mem_inf.peek_size);
95 printf("os_resource:\r\n");
96 printf("timer_usage=%d,", os_resource_stat.timer_usage);
97 printf("task_usage=%d,", os_resource_stat.task_usage);
98 printf("sem_usage=%d,", os_resource_stat.sem_usage);
99 printf("queue_usage=%d,", os_resource_stat.queue_usage);
100 printf("mux_usage=%d,", os_resource_stat.mux_usage);
101 printf("event_usage=%d\n", os_resource_stat.event_usage);
102
103 return 0;
104 }
105
DumpTaskInfo(void)106 int DumpTaskInfo(void)
107 {
108 #ifndef CONFIG_FACTORY_TEST_MODE
109 TSK_INFO_S* ptask_info = HI_NULL;
110 hi_u32 i;
111
112 ptask_info = (TSK_INFO_S*)hi_malloc(HI_MOD_ID_SAL_DFX, sizeof(TSK_INFO_S));
113 if (ptask_info == HI_NULL) {
114 printf("hi_malloc failed!\n");
115 return -1;
116 }
117
118 printf("TID Status Prio TaskSem TaskMux EventMask"
119 " StackSize TopOfStack BottomOfStack mstatus mepc"
120 " tp ra SP SatckCurrUsed"
121 " SatckPeakUsed SatckOverFlow taskName\n");
122 for (i = 0; i < g_taskMaxNum; ++i) {
123 memset_s(ptask_info, sizeof(TSK_INFO_S), 0, sizeof(TSK_INFO_S));
124 hi_u32 ret = LOS_TaskInfoGet(i, ptask_info);
125 if (ret == HI_ERR_SUCCESS) {
126 printf("%#3d", ptask_info->uwTaskID);
127 printf(" %#6d", ptask_info->usTaskStatus);
128 printf(" %#4d", ptask_info->usTaskPrio);
129 printf(" 0x%08x", ptask_info->pTaskSem);
130 printf(" 0x%08x", ptask_info->pTaskMux);
131 printf(" 0x%08x", ptask_info->uwEventMask);
132 printf(" 0x%08x", ptask_info->uwStackSize);
133 printf(" 0x%08x", ptask_info->uwTopOfStack);
134 printf(" 0x%08x", ptask_info->uwBottomOfStack);
135 printf(" 0x%08x", ptask_info->mstatus);
136 printf(" 0x%08x", ptask_info->mepc);
137 printf(" 0x%08x", ptask_info->tp);
138 printf(" 0x%08x", ptask_info->ra);
139 printf(" 0x%08x", ptask_info->uwSP);
140 printf(" 0x%08x", ptask_info->uwCurrUsed);
141 printf(" 0x%08x", ptask_info->uwPeakUsed);
142 printf(" %#7d", ptask_info->bOvf);
143 printf(" %s\n", ptask_info->acName);
144 }
145 }
146 hi_free(HI_MOD_ID_SAL_DFX, ptask_info);
147 #else
148 printf("Unsupported!\n");
149 #endif
150
151 return 0;
152 }
153
DumpFaultLog(void)154 int DumpFaultLog(void)
155 {
156 hi_s32 fd;
157 hi_char buf[128];
158
159 fd = hi_open(FAULT_LOG_PATH, HI_FS_O_RDONLY);
160 if (fd < 0) {
161 printf("Open [%s] failed [0x%x]!\n", FAULT_LOG_PATH,
162 hi_get_fs_error());
163 return -1;
164 }
165
166 memset_s(buf, sizeof(buf), 0, sizeof(buf));
167 while (hi_read(fd, buf, sizeof(buf) - 1) > 0) {
168 printf("%s", buf);
169 memset_s(buf, sizeof(buf), 0, sizeof(buf));
170 }
171 hi_close(fd);
172
173 return 0;
174 }
175
DumpMemRegion(unsigned long long addr,unsigned long long size)176 int DumpMemRegion(unsigned long long addr, unsigned long long size)
177 {
178 hi_u32 i;
179 if (size == 0 || size > DDR_SIZE) {
180 printf("Invalid DDR size: 0x%x, it must be: (0, 0x%x]\n",
181 size, DDR_SIZE);
182 return -1;
183 }
184
185 if (addr < DDR_START_ADDR || addr >= (DDR_START_ADDR + DDR_SIZE)) {
186 printf("Invalid DDR start addr: 0x%x, it must be: [0x%x, 0x%x)",
187 addr, DDR_START_ADDR, (DDR_START_ADDR + DDR_SIZE));
188 return -1;
189 }
190
191 hi_watchdog_disable();
192 printf("DDR start addr: 0x%x, size: 0x%x\n", addr, size);
193 for (i = 0; i < size; ++i) {
194 printf("%02X ", *((hi_u8 *)addr + i));
195 if ((i + 1) % HEX_NUM_PER_LINE == 0) {
196 printf("\n");
197 }
198 }
199 hi_watchdog_enable();
200
201 return 0;
202 }
203
DumpAllMem(void)204 int DumpAllMem(void)
205 {
206 return DumpMemRegion(DDR_START_ADDR, DDR_SIZE);
207 }
208
PlatformHiDumperIinit(void)209 int PlatformHiDumperIinit(void)
210 {
211 if (hi_at_register_cmd(g_hidumperAT, sizeof(g_hidumperAT) /
212 sizeof(g_hidumperAT[0])) != HI_ERR_SUCCESS) {
213 printf("Register hidumper AT failed!\n");
214 }
215 }
216
217 #ifdef __cplusplus
218 #if __cplusplus
219 }
220 #endif
221 #endif