155c6b4c7Sopenharmony_ci/* 255c6b4c7Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 355c6b4c7Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 455c6b4c7Sopenharmony_ci * you may not use this file except in compliance with the License. 555c6b4c7Sopenharmony_ci * You may obtain a copy of the License at 655c6b4c7Sopenharmony_ci * 755c6b4c7Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 855c6b4c7Sopenharmony_ci * 955c6b4c7Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1055c6b4c7Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1155c6b4c7Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1255c6b4c7Sopenharmony_ci * See the License for the specific language governing permissions and 1355c6b4c7Sopenharmony_ci * limitations under the License. 1455c6b4c7Sopenharmony_ci */ 1555c6b4c7Sopenharmony_ci 1655c6b4c7Sopenharmony_ci#include <securec.h> 1755c6b4c7Sopenharmony_ci#include <stdio.h> 1855c6b4c7Sopenharmony_ci#include <stdlib.h> 1955c6b4c7Sopenharmony_ci 2055c6b4c7Sopenharmony_ci#include "hidumper.h" 2155c6b4c7Sopenharmony_ci 2255c6b4c7Sopenharmony_ci#ifdef __cplusplus 2355c6b4c7Sopenharmony_ci#if __cplusplus 2455c6b4c7Sopenharmony_ciextern "C" { 2555c6b4c7Sopenharmony_ci#endif 2655c6b4c7Sopenharmony_ci#endif 2755c6b4c7Sopenharmony_ci 2855c6b4c7Sopenharmony_ci#define FAULT_ADDR 0x123 2955c6b4c7Sopenharmony_ci#define FAULT_VALUE 0x456 3055c6b4c7Sopenharmony_ci#define SYS_INFO_HEADER "*********** sys info ***********" 3155c6b4c7Sopenharmony_ci#define CPU_USAGE_HEADER "*********** cpu usage ***********" 3255c6b4c7Sopenharmony_ci#define MEM_USAGE_HEADER "*********** mem usage ***********" 3355c6b4c7Sopenharmony_ci#define TASK_INFO_HEADER "*********** task info ***********" 3455c6b4c7Sopenharmony_ci 3555c6b4c7Sopenharmony_cistatic int g_isAdapterRegistered = 0; 3655c6b4c7Sopenharmony_cistatic struct HiDumperAdapter g_hidumperAdapter; 3755c6b4c7Sopenharmony_ci 3855c6b4c7Sopenharmony_cistatic void Usage(void) 3955c6b4c7Sopenharmony_ci{ 4055c6b4c7Sopenharmony_ci printf("usage:\r\n" 4155c6b4c7Sopenharmony_ci "AT+HIDUMPER= dump cpu usage, memory usage and all tasks\r\n" 4255c6b4c7Sopenharmony_ci "or:\r\n"); 4355c6b4c7Sopenharmony_ci printf("AT+HIDUMPER=-dc dump the cpu usage\r\n" 4455c6b4c7Sopenharmony_ci "AT+HIDUMPER=-df dump the latest fault logs\r\n"); 4555c6b4c7Sopenharmony_ci printf("AT+HIDUMPER=-dm dump the memory usage\r\n" 4655c6b4c7Sopenharmony_ci "AT+HIDUMPER=-dt dump all the tasks\r\n"); 4755c6b4c7Sopenharmony_ci printf("AT+HIDUMPER=-h help text for the tool\r\n" 4855c6b4c7Sopenharmony_ci "AT+HIDUMPER=-ikc inject kernel crash\r\n"); 4955c6b4c7Sopenharmony_ci printf("AT+HIDUMPER=-m dump memory to stdout in hex format\r\n" 5055c6b4c7Sopenharmony_ci "AT+HIDUMPER=-m,filepath dump memory to filepath in the device in hex format\r\n"); 5155c6b4c7Sopenharmony_ci printf("AT+HIDUMPER=-m,memstart,memsize dump memory with starting address memstart(hex) and\r\n" 5255c6b4c7Sopenharmony_ci " size memsize(hex) to stdout in hex format\r\n"); 5355c6b4c7Sopenharmony_ci printf("AT+HIDUMPER=-m,memstart,memsize,filepath dump memory with starting address memstart(hex) and\r\n" 5455c6b4c7Sopenharmony_ci " size memsize(hex) to filepath in the device in hex format\r\n"); 5555c6b4c7Sopenharmony_ci} 5655c6b4c7Sopenharmony_ci 5755c6b4c7Sopenharmony_cistatic void DumpAllInfo(void) 5855c6b4c7Sopenharmony_ci{ 5955c6b4c7Sopenharmony_ci printf("%s\n", SYS_INFO_HEADER); 6055c6b4c7Sopenharmony_ci g_hidumperAdapter.DumpSysInfo(); 6155c6b4c7Sopenharmony_ci printf("%s\n", CPU_USAGE_HEADER); 6255c6b4c7Sopenharmony_ci g_hidumperAdapter.DumpCpuUsage(); 6355c6b4c7Sopenharmony_ci printf("%s\n", MEM_USAGE_HEADER); 6455c6b4c7Sopenharmony_ci g_hidumperAdapter.DumpMemUsage(); 6555c6b4c7Sopenharmony_ci printf("%s\n", TASK_INFO_HEADER); 6655c6b4c7Sopenharmony_ci g_hidumperAdapter.DumpTaskInfo(); 6755c6b4c7Sopenharmony_ci} 6855c6b4c7Sopenharmony_ci 6955c6b4c7Sopenharmony_cistatic void InjectKernelCrash(void) 7055c6b4c7Sopenharmony_ci{ 7155c6b4c7Sopenharmony_ci#ifdef OHOS_DEBUG 7255c6b4c7Sopenharmony_ci int *ptr = (int *)FAULT_ADDR; 7355c6b4c7Sopenharmony_ci *ptr = FAULT_VALUE; 7455c6b4c7Sopenharmony_ci#else 7555c6b4c7Sopenharmony_ci printf("Unsupported!\n"); 7655c6b4c7Sopenharmony_ci#endif 7755c6b4c7Sopenharmony_ci} 7855c6b4c7Sopenharmony_ci 7955c6b4c7Sopenharmony_ciint HiDumperRegisterAdapter(struct HiDumperAdapter *pAdapter) 8055c6b4c7Sopenharmony_ci{ 8155c6b4c7Sopenharmony_ci if (pAdapter == NULL) { 8255c6b4c7Sopenharmony_ci printf("Invalid pAdapter: %p\n", pAdapter); 8355c6b4c7Sopenharmony_ci return -1; 8455c6b4c7Sopenharmony_ci } 8555c6b4c7Sopenharmony_ci 8655c6b4c7Sopenharmony_ci if (pAdapter->DumpSysInfo == NULL || 8755c6b4c7Sopenharmony_ci pAdapter->DumpCpuUsage == NULL || 8855c6b4c7Sopenharmony_ci pAdapter->DumpMemUsage == NULL || 8955c6b4c7Sopenharmony_ci pAdapter->DumpTaskInfo == NULL || 9055c6b4c7Sopenharmony_ci pAdapter->DumpFaultLog == NULL || 9155c6b4c7Sopenharmony_ci pAdapter->DumpMemRegion == NULL || 9255c6b4c7Sopenharmony_ci pAdapter->DumpAllMem == NULL) { 9355c6b4c7Sopenharmony_ci printf("Invalid adapter funcs!\n"); 9455c6b4c7Sopenharmony_ci return -1; 9555c6b4c7Sopenharmony_ci } 9655c6b4c7Sopenharmony_ci if (memcpy_s(&g_hidumperAdapter, sizeof(g_hidumperAdapter), 9755c6b4c7Sopenharmony_ci pAdapter, sizeof(*pAdapter)) != EOK) { 9855c6b4c7Sopenharmony_ci printf("memcpy_s is error\n"); 9955c6b4c7Sopenharmony_ci } 10055c6b4c7Sopenharmony_ci g_isAdapterRegistered = 1; 10155c6b4c7Sopenharmony_ci 10255c6b4c7Sopenharmony_ci return 0; 10355c6b4c7Sopenharmony_ci} 10455c6b4c7Sopenharmony_ci 10555c6b4c7Sopenharmony_civoid ParameterMatching(int argc, const char *argv[]) 10655c6b4c7Sopenharmony_ci{ 10755c6b4c7Sopenharmony_ci if (argc == 0) { 10855c6b4c7Sopenharmony_ci DumpAllInfo(); 10955c6b4c7Sopenharmony_ci } else if (argc == ONE_OF_ARGC_PARAMETERS) { 11055c6b4c7Sopenharmony_ci if (strcmp(argv[0], "-h") == 0) { 11155c6b4c7Sopenharmony_ci Usage(); 11255c6b4c7Sopenharmony_ci } else if (strcmp(argv[0], "-dc") == 0) { 11355c6b4c7Sopenharmony_ci printf("%s\n", CPU_USAGE_HEADER); 11455c6b4c7Sopenharmony_ci g_hidumperAdapter.DumpCpuUsage(); 11555c6b4c7Sopenharmony_ci } else if (strcmp(argv[0], "-df") == 0) { 11655c6b4c7Sopenharmony_ci g_hidumperAdapter.DumpFaultLog(); 11755c6b4c7Sopenharmony_ci } else if (strcmp(argv[0], "-dm") == 0) { 11855c6b4c7Sopenharmony_ci printf("%s\n", MEM_USAGE_HEADER); 11955c6b4c7Sopenharmony_ci g_hidumperAdapter.DumpMemUsage(); 12055c6b4c7Sopenharmony_ci } else if (strcmp(argv[0], "-dt") == 0) { 12155c6b4c7Sopenharmony_ci printf("%s\n", TASK_INFO_HEADER); 12255c6b4c7Sopenharmony_ci g_hidumperAdapter.DumpTaskInfo(); 12355c6b4c7Sopenharmony_ci } else if (strcmp(argv[0], "-ikc") == 0) { 12455c6b4c7Sopenharmony_ci InjectKernelCrash(); 12555c6b4c7Sopenharmony_ci } else if (strcmp(argv[0], "-m") == 0) { 12655c6b4c7Sopenharmony_ci#ifdef OHOS_DEBUG 12755c6b4c7Sopenharmony_ci g_hidumperAdapter.DumpAllMem(); 12855c6b4c7Sopenharmony_ci#else 12955c6b4c7Sopenharmony_ci printf("Unsupported!\n"); 13055c6b4c7Sopenharmony_ci#endif 13155c6b4c7Sopenharmony_ci } else { 13255c6b4c7Sopenharmony_ci Usage(); 13355c6b4c7Sopenharmony_ci } 13455c6b4c7Sopenharmony_ci } else if (argc == TWO_OF_ARGC_PARAMETERS && strcmp(argv[0], "-m") == 0) { 13555c6b4c7Sopenharmony_ci printf("Unsupported!\n"); 13655c6b4c7Sopenharmony_ci } else if (argc == THREE_OF_ARGC_PARAMETERS) { 13755c6b4c7Sopenharmony_ci if (strcmp(argv[0], "-m") == 0) { 13855c6b4c7Sopenharmony_ci#ifdef OHOS_DEBUG 13955c6b4c7Sopenharmony_ci g_hidumperAdapter.DumpMemRegion( 14055c6b4c7Sopenharmony_ci strtoull(argv[ONE_OF_ARGC_PARAMETERS], NULL, BUF_SIZE_16), 14155c6b4c7Sopenharmony_ci strtoull(argv[TWO_OF_ARGC_PARAMETERS], NULL, BUF_SIZE_16)); 14255c6b4c7Sopenharmony_ci#else 14355c6b4c7Sopenharmony_ci printf("Unsupported!\n"); 14455c6b4c7Sopenharmony_ci#endif 14555c6b4c7Sopenharmony_ci } else { 14655c6b4c7Sopenharmony_ci Usage(); 14755c6b4c7Sopenharmony_ci } 14855c6b4c7Sopenharmony_ci } else if (argc == FOUR_OF_ARGC_PARAMETERS && strcmp(argv[0], "-m") == 0) { 14955c6b4c7Sopenharmony_ci printf("Unsupported!\r\n"); 15055c6b4c7Sopenharmony_ci } else { 15155c6b4c7Sopenharmony_ci Usage(); 15255c6b4c7Sopenharmony_ci } 15355c6b4c7Sopenharmony_ci} 15455c6b4c7Sopenharmony_ci 15555c6b4c7Sopenharmony_ciunsigned int at_hidumper(unsigned int argc, const char **argv) 15655c6b4c7Sopenharmony_ci{ 15755c6b4c7Sopenharmony_ci if (g_isAdapterRegistered == 0) { 15855c6b4c7Sopenharmony_ci printf("No adapter has been registered!\n"); 15955c6b4c7Sopenharmony_ci return 1; 16055c6b4c7Sopenharmony_ci } 16155c6b4c7Sopenharmony_ci 16255c6b4c7Sopenharmony_ci ParameterMatching(argc, argv); 16355c6b4c7Sopenharmony_ci 16455c6b4c7Sopenharmony_ci return 0; 16555c6b4c7Sopenharmony_ci} 16655c6b4c7Sopenharmony_ci 16755c6b4c7Sopenharmony_ci#ifdef __cplusplus 16855c6b4c7Sopenharmony_ci#if __cplusplus 16955c6b4c7Sopenharmony_ci} 17055c6b4c7Sopenharmony_ci#endif 17155c6b4c7Sopenharmony_ci#endif