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 /*-----------includes---------*/
17 #include <hi_blackbox.h>
18 #include <hi_blackbox_adapter.h>
19 #include <hi_crash.h>
20 #include <hi_flash.h>
21 #include <hi_fs.h>
22 #include <hi_partition_table.h>
23 #include <hi_reset.h>
24 #include <hi_stdlib.h>
25
26 /* ------------ local macroes -----------*/
27 #define FAULT_LOG_SIZE 0x800 /*2KB*/
28 #define LONG_BUF_SIZE 512
29 #define SHORT_BUF_SIZE 64
30 #define LOG_SAVED_FLAG "LOGSAVED!"
31 #define SYSERR_INFO_FLAG "====fault info====\r\n"
32 #define EXCP_INFO_FLAG "====exc info====\r\n"
33 #define REG_INFO_FLAG "====reg info====\r\n"
34 #define MEMORY_INFO_FLAG "====mem info====\r\n"
35 #define TASK_INFO_FLAG "====task info====\r\n"
36 #define TRACK_INFO_FLAG "====track info====\r\n"
37 #define CALL_STACK_START "====call stack====\r\n"
38 #define FAULT_LOG_PATH "/kernel_fault.log"
39
40 /* --------global variables----------*/
41 extern hi_u32 __rom_text_begin__;
42 extern hi_u32 __rom_text_end__;
43 extern hi_u32 __ram_text_start;
44 extern hi_u32 __ram_text_end;
45 extern hi_u32 __text_cache_start1_;
46 extern hi_u32 __text_cache_end1_;
47 extern hi_u32 __text_cache_start2_;
48 extern hi_u32 __text_cache_end2_;
49
50 /* ----function definitions---------*/
SaveSysInfo(const char * filePath,struct ErrorInfo * errInfo,hi_syserr_info * sysErrInfo)51 static void SaveSysInfo(const char* filePath, struct ErrorInfo *errInfo,
52 hi_syserr_info *sysErrInfo)
53 {
54 hi_s32 len;
55 hi_char buf[LONG_BUF_SIZE];
56
57 if (filePath == NULL || errInfo == NULL || sysErrInfo == NULL){
58 printf("filePath: %p, errInfo: %p, sysErrInfo: %p!\r\n",
59 filePath, errInfo, sysErrInfo);
60 return;
61 }
62
63 /* save header */
64 (hi_void)FullWriteFile(filePath, SYSERR_INFO_FLAG,
65 strlen(SYSERR_INFO_FLAG), 0);
66
67 /* format and save useful log */
68 len = snprintf_s(buf, sizeof(buf), sizeof(buf) - 1,
69 ERROR_INFO_HEADER_FORMAT "Ver:%s\r\n",
70 errInfo->event, errInfo->module, errInfo->errorDesc,
71 sysErrInfo->basic_info.kernel_ver);
72 if (len > 0) {
73 (hi_void)FullWriteFile(filePath, buf, len, 1);
74 } else {
75 printf("snprintf_s failed or the buffer is too short!\r\n");
76 }
77 }
78
SaveExcpInfo(const char * filePath,hi_syserr_info * sysErrInfo)79 static void SaveExcpInfo(const char *filePath, hi_syserr_info *sysErrInfo)
80 {
81 hi_s32 len;
82 hi_char buf[LONG_BUF_SIZE];
83
84 if (filePath == NULL || sysErrInfo == NULL) {
85 printf("filePath: %p, sysErrInfo: %p\r\n",
86 filePath, sysErrInfo);
87 return;
88 }
89
90 /* save neader*/
91 (hi_void)FullWriteFile(filePath, EXCP_INFO_FLAG,
92 strlen(EXCP_INFO_FLAG), 1);
93
94 /* format and save usefull log*/
95 len = snprintf_s(buf, sizeof(buf), sizeof(buf) - 1,
96 "TaskName:%s\r\n"
97 "TaskID:%d\r\n"
98 "CurTaskID:%d\r\n"
99 "StackSize:0x%x\r\n"
100 "ExcType:0x%x\r\n",
101 sysErrInfo->os_info.task.name,
102 sysErrInfo->os_info.task.id,
103 sysErrInfo->core_info.cur_task_id,
104 sysErrInfo->os_info.task.stack_size,
105 sysErrInfo->basic_info.rid);
106 if (len > 0) {
107 (hi_void)FullWriteFile(filePath, buf, len, 1);
108 } else {
109 printf("snprintf_s failed or the buffer is too short!\r\n");
110 }
111 }
112
SaveRegInfo(const char * filePath,hi_syserr_info * sysErrInfo)113 static void SaveRegInfo(const char* filePath, hi_syserr_info *sysErrInfo)
114 {
115 hi_s32 len;
116 hi_char buf[LONG_BUF_SIZE];
117
118 if (filePath == NULL || sysErrInfo == NULL) {
119 printf("filePath: %p, sysErrInfo: %p!\r\n",
120 filePath, sysErrInfo);
121 return;
122 }
123
124 /* save header */
125 (hi_void)FullWriteFile(filePath, REG_INFO_FLAG, strlen(REG_INFO_FLAG), 1);
126 len = snprintf_s(buf, sizeof(buf), sizeof(buf) - 1,
127 "mepc:0x%x\r\nmstatus:0x%x\r\nmtval:0x%x\r\n"
128 "mcause:0x%x\r\nccause:0x%x\r\n", sysErrInfo->reg_info.mepc,
129 sysErrInfo->core_info.mstatus, sysErrInfo->core_info.mtval,
130 sysErrInfo->core_info.mcause, sysErrInfo->core_info.ccause);
131 if (len > 0) {
132 (hi_void)FullWriteFile(filePath, buf, len, 1);
133 }
134
135 len = snprintf_s(buf, sizeof(buf), sizeof(buf) - 1,
136 "ra:0x%x\r\nsp:0x%x\r\ngp:0x%x\r\ntp:0x%x\r\n",
137 sysErrInfo->reg_info.ra, sysErrInfo->reg_info.sp,
138 sysErrInfo->reg_info.gp, sysErrInfo->reg_info.tp);
139 if (len > 0) {
140 (hi_void)FullWriteFile(filePath, buf, len, 1);
141 }
142
143 len = snprintf_s(buf, sizeof(buf), sizeof(buf) - 1,
144 "t0:0x%x\r\nt1:0x%x\r\nt2:0x%x\r\ns0:0x%x\r\n",
145 sysErrInfo->reg_info.t0, sysErrInfo->reg_info.t1,
146 sysErrInfo->reg_info.t2, sysErrInfo->reg_info.s0);
147 if (len > 0) {
148 (hi_void)FullWriteFile(filePath, buf, len, 1);
149 }
150
151 len = snprintf_s(buf, sizeof(buf), sizeof(buf) - 1,
152 "s1:0x%x\r\na0:0x%x\r\na1:0x%x\r\na2:0x%x\r\n",
153 sysErrInfo->reg_info.s1, sysErrInfo->reg_info.a0,
154 sysErrInfo->reg_info.a1, sysErrInfo->reg_info.a2);
155 if (len > 0) {
156 (hi_void)FullWriteFile(filePath, buf, len, 1);
157 }
158
159 len = snprintf_s(buf, sizeof(buf), sizeof(buf) - 1,
160 "a3:0x%x\r\na4:0x%x\r\na5:0x%x\r\na6:0x%x\r\n",
161 sysErrInfo->reg_info.a3, sysErrInfo->reg_info.a4,
162 sysErrInfo->reg_info.a5, sysErrInfo->reg_info.a6);
163 if (len > 0) {
164 (hi_void)FullWriteFile(filePath, buf, len, 1);
165 }
166
167 len = snprintf_s(buf, sizeof(buf), sizeof(buf) - 1,
168 "a7:0x%x\r\ns2:0x%x\r\ns3:0x%x\r\ns4:0x%x\r\n",
169 sysErrInfo->reg_info.a7, sysErrInfo->reg_info.s2,
170 sysErrInfo->reg_info.s3, sysErrInfo->reg_info.s4);
171 if (len > 0) {
172 (hi_void)FullWriteFile(filePath, buf, len, 1);
173 }
174
175 len = snprintf_s(buf, sizeof(buf), sizeof(buf) - 1,
176 "s5:0x%x\r\ns6:0x%x\r\ns7:0x%x\r\ns8:0x%x\r\n",
177 sysErrInfo->reg_info.s5, sysErrInfo->reg_info.s6,
178 sysErrInfo->reg_info.s7, sysErrInfo->reg_info.s8);
179 if (len > 0) {
180 (hi_void)FullWriteFile(filePath, buf, len, 1);
181 }
182
183 len = snprintf_s(buf, sizeof(buf), sizeof(buf) - 1,
184 "s9:0x%x\r\ns10:0x%x\r\ns11:0x%x\r\n",
185 sysErrInfo->reg_info.s9, sysErrInfo->reg_info.s10,
186 sysErrInfo->reg_info.s11);
187 if (len > 0) {
188 (hi_void)FullWriteFile(filePath, buf, len, 1);
189 }
190
191 len = snprintf_s(buf, sizeof(buf), sizeof(buf) - 1,
192 "t3:0x%x\r\nt4:0x%x\r\nt5:0x%x\r\nt6:0x%x\r\n",
193 sysErrInfo->reg_info.t3, sysErrInfo->reg_info.t4,
194 sysErrInfo->reg_info.t5, sysErrInfo->reg_info.t6);
195 if (len > 0) {
196 (hi_void)FullWriteFile(filePath, buf, len, 1);
197 }
198 }
199
SaveMemInfo(const char * filePath,hi_syserr_info * sysErrInfo)200 static void SaveMemInfo(const char *filePath, hi_syserr_info *sysErrInfo)
201 {
202 hi_s32 len;
203 hi_char buf[LONG_BUF_SIZE];
204
205 if (filePath == NULL || sysErrInfo == NULL) {
206 printf("filePath: %p, sysErrInfo: %p!\r\n",
207 filePath, sysErrInfo);
208 return;
209 }
210
211 /* save header */
212 (hi_void)FullWriteFile(filePath, MEMORY_INFO_FLAG,
213 strlen(MEMORY_INFO_FLAG), 1);
214
215 /* format and save usefull log*/
216 len = snprintf_s(buf, sizeof(buf), sizeof(buf) - 1,
217 "PoolAddr:0x%x\r\n"
218 "PoolSize:0x%x\r\n"
219 "FailCount:0x%x\r\n"
220 "PeekSize:0x%x\r\n"
221 "UsedSize:0x%x\r\n",
222 sysErrInfo->os_info.mem.pool_addr,
223 sysErrInfo->os_info.mem.pool_size,
224 sysErrInfo->os_info.mem.fail_count,
225 sysErrInfo->os_info.mem.peek_size,
226 sysErrInfo->os_info.mem.cur_use_size);
227 if (len > 0) {
228 (hi_void)FullWriteFile(filePath, buf, len, 1);
229 } else {
230 printf("snprintf_s failed or the buffer is too short!\r\n");
231 }
232 }
233
SaveTaskInfo(const char * filePath,hi_syserr_info * sysErrInfo)234 static void SaveTaskInfo(const char *filePath, hi_syserr_info *sysErrInfo)
235 {
236 hi_s32 len;
237 hi_char buf[LONG_BUF_SIZE];
238
239 if (filePath == NULL || sysErrInfo == NULL) {
240 printf("filePath: %p, sysErrInfo: %p!\r\n",
241 filePath, sysErrInfo);
242 return;
243 }
244
245 /* save header */
246 (hi_void)FullWriteFile(filePath, TASK_INFO_FLAG,
247 strlen(TASK_INFO_FLAG), 1);
248
249 /* format and save usefull log */
250 len = snprintf_s(buf, sizeof(buf), sizeof(buf) - 1,
251 "Status:0x%x\r\n"
252 "StackIndex:0x%x\r\n"
253 "StackPeak:0x%x\r\n"
254 "StackSize:0x%x\r\n",
255 sysErrInfo->os_info.task.status,
256 sysErrInfo->os_info.task.stack_data_index,
257 sysErrInfo->os_info.task.stack_peak,
258 sysErrInfo->os_info.task.stack_size);
259 if (len > 0) {
260 (hi_void)FullWriteFile(filePath, buf, len, 1);
261 }
262
263 len = snprintf_s(buf, sizeof(buf), sizeof(buf) - 1,
264 "SP:0x%x\r\n"
265 "Stack:0x%x to 0x%x\r\n"
266 "RealSP:0x%x\r\n"
267 "StackOverflow:%d\r\n",
268 sysErrInfo->os_info.task.sp,
269 sysErrInfo->os_info.task.stack[0], sysErrInfo->os_info.task.stack[1],
270 sysErrInfo->os_info.task.real_sp,
271 sysErrInfo->os_info.task.overflow_flag);
272 if (len > 0) {
273 (hi_void)FullWriteFile(filePath, buf, len, 1);
274 }
275 }
276
SaveTrackInfo(const char * filePath,hi_syserr_info * sysErrInfo)277 static void SaveTrackInfo(const char *filePath, hi_syserr_info *sysErrInfo)
278 {
279 hi_s32 i;
280 hi_s32 len;
281 hi_char buf[LONG_BUF_SIZE];
282
283 if (filePath == NULL || sysErrInfo == NULL) {
284 printf("filePath: %p, sysErrInfo: %p!\r\n",
285 filePath, sysErrInfo);
286 return;
287 }
288
289 /* save header */
290 (hi_void)FullWriteFile(filePath, TRACK_INFO_FLAG,
291 strlen(TRACK_INFO_FLAG), 1);
292 len = snprintf_s(buf, sizeof(buf), sizeof(buf) - 1,
293 "CurItem:0x%x\r\n"
294 "ItemCnt:%d\r\n"
295 "Index Type ID CurTime Data1 Data2\r\n",
296 sysErrInfo->track_info.current_item,
297 sysErrInfo->track_info.item_cnt);
298 if (len > 0) {
299 (hi_void)FullWriteFile(filePath, buf, len, 1);
300 }
301
302 for (i = 0; i < sysErrInfo->track_info.item_cnt; i++) {
303 len = snprintf_s(buf, sizeof(buf), sizeof(buf) - 1,
304 "%04d %04d %04d 0x%x 0x%x 0x%x\r\n",
305 i,
306 sysErrInfo->track_info.item[i].type,
307 sysErrInfo->track_info.item[i].id,
308 sysErrInfo->track_info.item[i].timestamp,
309 sysErrInfo->track_info.item[i].data,
310 sysErrInfo->track_info.item[i].entry);
311 if (len > 0) {
312 (hi_void)FullWriteFile(filePath, buf, len, 1);
313 }
314 }
315 }
316
IsValidText(hi_u32 addr)317 static hi_bool IsValidText(hi_u32 addr)
318 {
319 return ((addr >= (hi_u32)&__rom_text_begin__ &&
320 addr <= (hi_u32)&__rom_text_end__) ||
321 (addr >= (hi_u32)&__text_cache_start2_ &&
322 addr <= (hi_u32)&__text_cache_end2_) ||
323 (addr >= (hi_u32)&__ram_text_start &&
324 addr <= (hi_u32)&__ram_text_end) ||
325 (addr >= (hi_u32)&__text_cache_start1_ &&
326 addr <= (hi_u32)&__text_cache_end1_));
327 }
328
SaveCallBack(const char * filePath,hi_syserr_info * sysErrInfo)329 static void SaveCallBack(const char *filePath, hi_syserr_info *sysErrInfo)
330 {
331 hi_char buf[SHORT_BUF_SIZE];
332 hi_bool findMepc = HI_FALSE;
333 hi_s32 len;
334 hi_u32 addr;
335 hi_u32 size;
336 hi_u32 i;
337 hi_u32 index = 0;
338 hi_u32 stackSize = sysErrInfo->os_info.task.stack[1] -
339 sysErrInfo->os_info.task.stack[0];
340 hi_u32 *pData;
341
342 if (filePath == NULL || sysErrInfo == NULL) {
343 printf("filePath: %p, sysErrInfo: %p!\r\n",
344 filePath, sysErrInfo);
345 return;
346 }
347
348 if (hi_get_crash_partition_table(&addr, &size) != 0) {
349 printf("hi_get_crash_partition_table failed!\r\n");
350 return;
351 }
352
353 pData = hi_malloc(HI_MOD_ID_SAL_DFX, stackSize);
354 if (pData == NULL) {
355 printf("hi_malloc failed!\r\n");
356 return;
357 }
358
359 if (hi_flash_read(addr + size - stackSize, stackSize,
360 (hi_u8 *)pData) != HI_ERR_SUCCESS) {
361 printf("Read stack from crash partition failed!\r\n");
362 hi_free(HI_MOD_ID_SAL_DFX, pData);
363 return;
364 }
365
366 /* save header */
367 (hi_void) FullWriteFile(filePath, CALL_STACK_START,
368 strlen(CALL_STACK_START), 1);
369 for (i = 0; i < stackSize / sizeof(hi_u32); ++i) {
370 hi_u32 *ptemp = pData + i;
371 if (!findMepc && *ptemp == sysErrInfo->reg_info.mepc) {
372 findMepc = HI_TRUE;
373 }
374 if (IsValidText(*ptemp) && findMepc) {
375 len = snprintf_s(buf, sizeof(buf), sizeof(buf) - 1,
376 "#%d %x\r\n", index, *ptemp);
377 if (len > 0) {
378 (hi_void)FullWriteFile(filePath, buf, len, 1);
379 }
380 ++index;
381 }
382 }
383 hi_free(HI_MOD_ID_SAL_DFX, pData);
384 }
385
SaveFaultLog(struct ErrorInfo * errInfo,hi_syserr_info * sysErrInfo)386 static void SaveFaultLog(struct ErrorInfo *errInfo, hi_syserr_info *sysErrInfo)
387 {
388 if (errInfo == NULL || sysErrInfo == NULL) {
389 printf("errInfo: %p, sysErrInfo: %p!\r\n",
390 errInfo, sysErrInfo);
391 return;
392 }
393
394 SaveSysInfo(FAULT_LOG_PATH, errInfo, sysErrInfo);
395 SaveExcpInfo(FAULT_LOG_PATH, sysErrInfo);
396 SaveRegInfo(FAULT_LOG_PATH, sysErrInfo);
397 SaveMemInfo(FAULT_LOG_PATH, sysErrInfo);
398 SaveTaskInfo(FAULT_LOG_PATH, sysErrInfo);
399 SaveTrackInfo(FAULT_LOG_PATH, sysErrInfo);
400 SaveCallBack(FAULT_LOG_PATH, sysErrInfo);
401 }
402
SetLogSavedFlag(hi_void)403 static void SetLogSavedFlag(hi_void)
404 {
405 hi_u32 addr;
406 hi_u32 size;
407
408 if (hi_get_crash_partition_table(&addr, &size) != 0) {
409 printf("hi_get_crash_partition_table failed!\r\n");
410 return;
411 }
412
413 if (hi_flash_write(addr, sizeof(LOG_SAVED_FLAG),
414 (hi_u8 *)LOG_SAVED_FLAG, HI_TRUE) != HI_ERR_SUCCESS) {
415 printf("Set log saved flag failed!\r\n");
416 }
417 }
418
GetEvent(hi_syserr_eid err_id)419 static char *GetEvent(hi_syserr_eid err_id)
420 {
421 switch (err_id) {
422 case HI_SYSERR_EID_POWER_DOWN:
423 return EVENT_POWEROFF;
424 case HI_SYSERR_EID_FAULT_IN_TASK:
425 case HI_SYSERR_EID_FAULT_IN_ISR:
426 return EVENT_PANIC;
427 case HI_SYSERR_EID_WATCHDOG_ISR:
428 case HI_SYSERR_EID_WATCHDOG_TSK:
429 return EVENT_SYS_WATCHDOG;
430 case HI_SYSERR_EID_SYS_SOFT_REBOOT:
431 return EVENT_SYSREBOOT;
432 default:
433 break;
434 }
435
436 return "Unknow";
437 }
438
GetErrorDesc(hi_syserr_eid err_id)439 static char *GetErrorDesc(hi_syserr_eid err_id)
440 {
441 switch (err_id) {
442 case HI_SYSERR_EID_POWER_DOWN:
443 return "Power down or hard reboot";
444 case HI_SYSERR_EID_FAULT_IN_TASK:
445 return "fault in task";
446 case HI_SYSERR_EID_FAULT_IN_ISR:
447 return "fault in interrupt";
448 case HI_SYSERR_EID_WATCHDOG_TSK:
449 return "watch dog fault in task";
450 case HI_SYSERR_EID_WATCHDOG_ISR:
451 return "watch dog fault in interrupt";
452 case HI_SYSERR_EID_SYS_SOFT_REBOOT:
453 return "soft reboot";
454 default:
455 break;
456 }
457
458 return "Unknown fault type!";
459 }
460
FullWriteFile(const char * filePath,const char * buf,size_t bufSize,int isAppend)461 int FullWriteFile(const char *filePath, const char *buf, size_t bufSize, int isAppend)
462 {
463 int fd;
464 int totalToWrite = (int) bufSize;
465 int totalWrite = 0;
466
467 if (filePath == NULL || buf == NULL || bufSize == 0) {
468 printf("filePath: %p, buf: %p, bufSize: %lu!\n", filePath, buf, bufSize);
469 return -1;
470 }
471
472 fd = hi_open(filePath, HI_FS_O_WRONLY | HI_FS_O_CREAT |
473 (isAppend ? HI_FS_O_APPEND : HI_FS_O_TRUNC));
474 if (fd < 0) {
475 printf("Create file [%s] failed!\n", filePath);
476 return -1;
477 }
478
479 while (totalToWrite > 0) {
480 int writeThisTime = hi_write(fd, buf, totalToWrite);
481 if (writeThisTime < 0) {
482 printf("Failed to write file: %s\n", filePath);
483 (void)hi_close(fd);
484 return -1;
485 }
486 buf += writeThisTime;
487 totalToWrite -= writeThisTime;
488 totalWrite += writeThisTime;
489 }
490 (void)hi_close(fd);
491
492 return totalWrite == (int)bufSize ? 0 : -1;
493 }
494
GetFaultLogPath(void)495 char *GetFaultLogPath(void)
496 {
497 return FAULT_LOG_PATH;
498 }
499
RebootSystem(void)500 void RebootSystem(void)
501 {
502 hi_hard_reboot(HI_SYS_REBOOT_CAUSE_CMD);
503 }
504
SystemModuleDump(const char * logDir,struct ErrorInfo * info)505 void SystemModuleDump(const char *logDir, struct ErrorInfo *info)
506 {
507 int len;
508 char *buf;
509 (void)logDir;
510
511 if (logDir == NULL || info == NULL) {
512 printf("logDir: %p, info: %p\r\n", logDir, info);
513 return;
514 }
515
516 buf = hi_malloc(HI_MOD_ID_SAL_DFX, FAULT_LOG_SIZE);
517 if (buf == NULL) {
518 printf("malloc failed!\r\n");
519 return;
520 }
521 (void)memset_s(buf, FAULT_LOG_SIZE, 0, FAULT_LOG_SIZE);
522 len = snprintf_s(buf, FAULT_LOG_SIZE, FAULT_LOG_SIZE - 1,
523 ERROR_INFO_HEADER ERROR_INFO_HEADER_FORMAT,
524 info->event, info->module, info->errorDesc);
525 if (len <= 0) {
526 printf("buf isn't enough or vsnprintf_s failed\r\n");
527 hi_free(HI_MOD_ID_SAL_DFX, buf);
528 return;
529 }
530 (hi_void)FullWriteFile(FAULT_LOG_PATH, buf, len, 0);
531 hi_free(HI_MOD_ID_SAL_DFX, buf);
532 }
533
SystemModuleReset(struct ErrorInfo * info)534 void SystemModuleReset(struct ErrorInfo *info)
535 {
536 if (info == NULL) {
537 printf("info: %p\r\n", info);
538 return;
539 }
540 }
541
SystemModuleGetLastLogInfo(struct ErrorInfo * info)542 int SystemModuleGetLastLogInfo(struct ErrorInfo *info)
543 {
544 hi_u16 err_id = 0;
545 hi_u32 rid = 0;
546 hi_u32 ret;
547 hi_syserr_info *errInfo;
548 char *data;
549
550 if (info == NULL) {
551 printf("info: %p\r\n", info);
552 return -1;
553 }
554
555 errInfo = hi_malloc(HI_MOD_ID_SAL_DFX, sizeof(*errInfo));
556 if (errInfo == NULL) {
557 printf("hi_malloc failed!\r\n");
558 return -1;
559 }
560 memset_s(errInfo, sizeof(*errInfo), 0, sizeof(*errInfo));
561 ret = hi_syserr_get(errInfo);
562 if (ret != HI_ERR_SUCCESS) {
563 printf("No crash dump found!\r\n");
564 hi_free(HI_MOD_ID_SAL_DFX, errInfo);
565 return -1;
566 }
567 hi_free(HI_MOD_ID_SAL_DFX, errInfo);
568 ret = hi_syserr_get_reboot_reason(&err_id, &rid);
569 if (ret == HI_ERR_SUCCESS) {
570 printf("latest reboot reason:\r\neid:%d,"
571 " rid:%d:\r\n", err_id, rid);
572 }
573 (void)memset_s(info, sizeof(*info), 0, sizeof(*info));
574 data = GetEvent(err_id);
575 (void)strncpy_s(info->event, sizeof(info->event) - 1,
576 data, strlen(data));
577 (void)strncpy_s(info->module, sizeof(info->module) - 1,
578 MODULE_SYSTEM, strlen(MODULE_SYSTEM));
579 data = GetErrorDesc(err_id);
580 (void)strncpy_s(info->errorDesc, sizeof(info->errorDesc) - 1,
581 data, strlen(data));
582
583 return 0;
584 }
585
SystemModuleSaveLastLog(const char * logDir,struct ErrorInfo * info)586 int SystemModuleSaveLastLog(const char *logDir, struct ErrorInfo *info)
587 {
588 hi_u32 ret;
589 hi_syserr_info *errInfo;
590 (void)logDir;
591
592 errInfo = hi_malloc(HI_MOD_ID_SAL_DFX, sizeof(*errInfo));
593 if (errInfo == NULL) {
594 printf("hi_malloc failed!\r\n");
595 return -1;
596 }
597 memset_s(errInfo, sizeof(*errInfo), 0, sizeof(*errInfo));
598 ret = hi_syserr_get(errInfo);
599 if (ret != HI_ERR_SUCCESS) {
600 printf("No crash dump found!\r\n");
601 return -1;
602 } else {
603 SaveFaultLog(info, errInfo);
604 SetLogSavedFlag();
605 }
606 hi_free(HI_MOD_ID_SAL_DFX, errInfo);
607
608 return 0;
609 }