1@page debug debug
2
3[更正文档](https://gitee.com/alios-things/debug/edit/master/README.md)      [贡献说明](https://help.aliyun.com/document_detail/302301.html)
4
5# 概述
6AliOS Things 调试诊断(debug)组件支持以下功能:
7- 异常管理,在系统异常后接管异常,并输出系统快照coredump信息
8- 提供常见的系统调试诊断接口;
9- 提供常见的CLI调试命令(可输入help查看),如:
10- cpuusage:  查询cpu利用率
11- tasklist:  查询系统任务状态
12- dumpsys mm: 查询系统内存使用状态
13- debug:     查询系统内存/任务/队列/信号量/互斥锁等使用状态
14- p:          查询内存值
15- m:          改写内存值
16
17
18## 版权信息
19> Apache license v2.0
20## 目录结构
21```tree
22├── include
23│   └── aos
24│       └── debug.h         #维测组件对外头接口
25├── package.yaml
26├── README.md
27└── src
28    ├── debug_api.h         #维测组件内部头文件
29    ├── debug_backtrace.c   #栈回溯
30    ├── debug_backtrace.h
31    ├── debug.c             #维测组件对外接口实现
32    ├── debug_cli_cmd.c     #调试命令
33    ├── debug_cli_cmd.h
34    ├── debug_cpuusage.c    #cpu利用率
35    ├── debug_cpuusage.h
36    ├── debug_dumpsys.c     #内存信息查询
37    ├── debug_dumpsys.h
38    ├── debug_infoget.c     #组件内部接口实现
39    ├── debug_infoget.h
40    ├── debug_lastword.c    #临终遗言管理
41    ├── debug_lastword.h
42    ├── debug_overview.c    #系统调试信息查询
43    ├── debug_overview.h
44    ├── debug_panic.c       #系统异常coredump
45    ├── debug_panic.h
46    ├── debug_print.c       #printk调试打印
47    ├── debug_print.h
48    ├── debug_test.c        #一些测试命令
49    └── debug_test.h
50```
51
52## 依赖组件
53* rhino
54* vfs(lastword功能依赖)
55* littlefs(lastword功能依赖)
56
57
58# 常用配置
59系统中相关配置已有默认值,如需修改配置,统一在yaml中**def_config**节点修改,具体如下:
60> 系统异常后进入调试模式,可继续执行cli命令,系统不会自动复位(默认模式),可修改yaml配置如下,修改为0后,系统异常则会自动复位。
61```yaml
62def_config:
63  DEBUG_PANIC_CLI: 1
64```
65> 系统异常后,默认不会将ulog缓存里的log输出,可修改yaml配置如下,修改为1后,会输出ulog缓存里的log
66```yaml
67def_config:
68  DEBUG_ULOG_FLUSH: 0
69```
70> cpuusage统计结果默认输出到串口终端,可修改yaml配置如下,修改为1后,会将cpuusage统计结果写入文件,不在终端输出
71```yaml
72def_config:
73  DEBUG_CPUUSAGE_RECODE_TO_FILE_ENABLE: 0
74
75  #可配置写入文件的cpuusage的统计任务的最大数量
76  DEBUG_CPUUSAGE_MAX_TASK: 80
77
78  #可配置写入文件的路径
79  DEBUG_CPUUSAGE_FILE_NAME: "/data/cpuusage"
80```
81
82# API说明
83- 参考 [debug_aos_api](https://dev.g.alicdn.com/alios-things-3.3/doc/group__debug__aos__api.html)
84
85
86# 使用示例
87
88组件使用示例相关的代码下载、编译和固件烧录均依赖AliOS Things配套的开发工具,所以首先需要参考[《AliOS Things集成开发环境使用说明之搭建开发环境》](https://help.aliyun.com/document_detail/302378.html),下载安装。
89待开发环境搭建完成后,可以按照以下步骤进行示例的测试。
90
91
92## 步骤1 创建或打开工程
93
94**打开已有工程**
95
96如果用于测试的案例工程已存在,可参考[《AliOS Things集成开发环境使用说明之打开工程》](https://help.aliyun.com/document_detail/302381.html)打开已有工程。
97
98**创建新的工程**
99
100组件的示例代码可以通过编译链接到AliOS Things的任意案例(solution)来运行,这里选择helloworld_demo案例。helloworld_demo案例相关的源代码下载可参考[《AliOS Things集成开发环境使用说明之创建工程》](https://help.aliyun.com/document_detail/302379.html)101
102
103## 步骤2 添加组件
104
105案例下载完成后,以运行helloworld_demo为例,需要在helloworld_demo组件的package.yaml中添加对组件的依赖:
106
107```yaml
108
109depends:
110  - debug: master  # helloworld_demo中引入debug组件
111
112```
113
114## 步骤3 下载组件
115
116在已安装了  的开发环境工具栏中,选择Terminal -> New Terminal启动终端,并且默认工作路径为当前工程的workspace,此时在终端命令行中输入:
117
118```shell
119
120aos install debug
121
122```
123
124上述命令执行成功后,组件源码则被下载到了./components/debug路径中。
125
126## 步骤4 添加示例
127
128在debug组件的package.yaml中添加[example示例代码](https://gitee.com/alios-things/debug/tree/master/example)129
130```yaml
131source_file:
132  - example/debug_example.c
133```
134
135
136## 步骤5 编译固件
137
138在示例代码已经添加至组件的配置文件,并且helloworld_demo已添加了对该组件的依赖后,就可以编译helloworld_demo案例来生成固件了,具体编译方法可参考[《AliOS Things集成开发环境使用说明之编译固件》](https://help.aliyun.com/document_detail/302384.html)139
140## 步骤6 烧录固件
141
142helloworld_demo案例的固件生成后,可参考[《AliOS Things集成开发环境使用说明之烧录固件》](https://help.aliyun.com/document_detail/302383.html)来烧录固件。
143
144## 步骤7 打开串口
145
146固件烧录完成后,可以通过串口查看示例的运行结果,打开串口的具体方法可参考[《AliOS Things集成开发环境使用说明之查看日志》](https://help.aliyun.com/document_detail/302382.html)147
148当串口终端打开成功后,可在串口中输入help来查看已添加的测试命令。
149
150## 步骤8 测试示例
151
152**CLI命令行输入:**
153```shell
154debug_api help # debug接口测试
155```
156
157> 关键日志:
158```shell
159You can use debug cmd to show api test:
160debug_api help  --- show this
161debug_api 1     --- show memory info
162debug_api 2     --- show task info
163debug_api 3     --- show bufqueue info
164debug_api 4     --- show queue info
165debug_api 5     --- show sem info
166debug_api 6     --- show mutex info
167debug_api 7     --- show backtrace now
168debug_api 8     --- show backtrace task
169debug_api all   --- show all above
170```
171接着可根据提示使用debug_api n 进行相应的接口演示,如
172
173
174**CLI命令行输入:**
175```shell
176debug_api 1 # show memory info
177```
178
179> 关键日志:
180```shell
181========== Heap Info  ==========
182---------------------------------------------------------------------------
183[HEAP]| TotalSz    | FreeSz     | UsedSz     | MinFreeSz  | MaxFreeBlkSz  |
184      | 0x0067FFF8 | 0x0064D530 | 0x00032AC8 | 0x0064D120 | 0x0064D530    |
185---------------------------------------------------------------------------
186```
187
188# FAQ
189Q1: cpuusage命令的使用说明是什么?
190答:
191cpuusage [-d n] [-t m] 命令启动CPU利用率统计,结果输出到串口终端
192其中:-d 选项用于指定统计周期,单位为ms,默认为1 s;
193      -t 选项用于指定统计时长,单位为ms,默认为连续运行。
194
195举例说明:
196(1) cpuusage                   -- 启动一个cpuusage任务,该任务默认每隔1s执行一次统计;
197(2) cpuusage -d 3000           -- 启动一个cpuusage任务,该任务默认每隔3s(3000ms)执行一次统计;
198(3) cpuusage -d 2000 -t 10000  -- 启动一个cpuusage任务,该任务默认每隔2s(2000ms)执行一次统计,统计到10s(10000ms)后停止;
199(4) cpuusage -e                -- 停止统计
200
201
202Q2: 查看修改内存p/m命令的使用说明是什么?
203答:
204查看内存
205p addr [数量:默认为16个] [字节宽度显示,可选1/2/4,默认为4字节]
206
207举例说明:
208p 0x80000000     -- 查看0x80000000处的内存值,默认输出16个地址,以4字节数据宽度显示
2090x80000000: 80020000 60012ee1 60012f81 60012f87
2100x80000010: 60012fa9 60012fb5 60012fc1 00000000
2110x80000020: 00000000 00000000 00000000 60013005
2120x80000030: 60013005 00000000 60013005 60013005
213
214p 0x80000000 32   -- 查看0x80000000处的内存值,输出32个地址,以4字节数据宽度显示
2150x80000000: 80020000 60012ee1 60012f81 60012f87
2160x80000010: 60012fa9 60012fb5 60012fc1 00000000
2170x80000020: 00000000 00000000 00000000 60013005
2180x80000030: 60013005 00000000 60013005 60013005
2190x80000040: 60008919 600076f1 60008919 600145cd
2200x80000050: 60008919 60008919 60008919 60014e65
2210x80000060: 60008389 60008919 60014939 60008919
2220x80000070: 60008919 60008919 600147fb 60008919
223
224p 0x80000000 32 2  -- 查看0x80000000处的内存值,输出32个地址,以2字节数据宽度显示
2250x80000000: 0000 8002 2ee1 6001 2f81 6001 2f87 6001
2260x80000010: 2fa9 6001 2fb5 6001 2fc1 6001 0000 0000
2270x80000020: 0000 0000 0000 0000 0000 0000 3005 6001
2280x80000030: 3005 6001 0000 0000 3005 6001 3005 6001
229
230p 0x80000000 32 1  -- 查看0x80000000处的内存值,输出32个地址,以1字节数据宽度显示
2310x80000000: 00 00 02 80 e1 2e 01 60 81 2f 01 60 87 2f 01 60
2320x80000010: a9 2f 01 60 b5 2f 01 60 c1 2f 01 60 00 00 00 00
233
234
235修改内存
236m addr new_value 将addr处的内存值修改为new_value
237
238举例说明:
239m 0x80000000 0x12345678  -- 将0x80000000处的内存值修改为0x12345678
240value on 0x80000000 change from 0x80020000 to 0x12345678.
241
242验证:
243p 0x80000000
2440x80000000: 12345678 60012ee1 60012f81 60012f87
2450x80000010: 60012fa9 60012fb5 60012fc1 00000000
2460x80000020: 00000000 00000000 00000000 60013005
2470x80000030: 60013005 00000000 60013005 60013005
248