Debug
Mbed OS 提供了一组调试功能,可用于在运行时将调试消息输出到 STDIO。mbed_debug.h 声明了这些函数,这些函数仅在调试版本中可用。
debug 函数是一个 printf 样式的函数,它接受一个格式字符串后跟参数。 以下是一些示例用法。
void *operator new(std::size_t count) {
void *buffer = malloc(count);
if (NULL == buffer) {
error("Operator new out of memory\r\n");
} else {
debug("Operator new succeded");
}
return buffer;
}
debug_if 函数类似于调试函数,只是它需要一个额外的参数作为其第一个参数。仅当第一个参数计算结果为 true 时,该消息才会打印到 STDIO。
void *operator new(std::size_t count) {
debug_if( count == 0, "\nnew called with 0 size");
...
}
Debug 函数参考
Debug 示例
/* Example usage for Debug, Assert, Error and MemoryStats
* Copyright (c) 2016 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "mbed.h"
#include "platform/mbed_assert.h"
#include "platform/mbed_debug.h"
#include "platform/mbed_error.h"
#include "platform/mbed_stats.h"
#define MAX_THREAD_INFO 10
mbed_stats_heap_t heap_info;
mbed_stats_stack_t stack_info[ MAX_THREAD_INFO ];
int main()
{
debug("\nThis message is from debug function");
debug_if(1,"\nThis message is from debug_if function");
debug_if(0,"\nSOMETHING WRONG!!! This message from debug_if function shouldn't show on bash");
printf("\nMemoryStats:");
mbed_stats_heap_get( &heap_info );
printf("\n\tBytes allocated currently: %d", heap_info.current_size);
printf("\n\tMax bytes allocated at a given time: %d", heap_info.max_size);
printf("\n\tCumulative sum of bytes ever allocated: %d", heap_info.total_size);
printf("\n\tCurrent number of bytes allocated for the heap: %d", heap_info.reserved_size);
printf("\n\tCurrent number of allocations: %d", heap_info.alloc_cnt);
printf("\n\tNumber of failed allocations: %d", heap_info.alloc_fail_cnt);
mbed_stats_stack_get( &stack_info[0] );
printf("\nCumulative Stack Info:");
printf("\n\tMaximum number of bytes used on the stack: %d", stack_info[0].max_size);
printf("\n\tCurrent number of bytes allocated for the stack: %d", stack_info[0].reserved_size);
printf("\n\tNumber of stacks stats accumulated in the structure: %d", stack_info[0].stack_cnt);
mbed_stats_stack_get_each( stack_info, MAX_THREAD_INFO );
printf("\nThread Stack Info:");
for(int i=0;i < MAX_THREAD_INFO; i++) {
if(stack_info[i].thread_id != 0) {
printf("\n\tThread: %d", i);
printf("\n\t\tThread Id: 0x%08X", stack_info[i].thread_id);
printf("\n\t\tMaximum number of bytes used on the stack: %d", stack_info[i].max_size);
printf("\n\t\tCurrent number of bytes allocated for the stack: %d", stack_info[i].reserved_size);
printf("\n\t\tNumber of stacks stats accumulated in the structure: %d", stack_info[i].stack_cnt);
}
}
printf("\nDone...\n\n");
}