Merge pull request #6306 from thinkyhead/cleanup_after_6302

Adjustments to M100
2.0.x
Scott Lahteine 8 years ago committed by GitHub
commit b236562354

@ -155,7 +155,7 @@ script:
# INCH_MODE_SUPPORT, TEMPERATURE_UNITS_SUPPORT # INCH_MODE_SUPPORT, TEMPERATURE_UNITS_SUPPORT
# #
- restore_configs - restore_configs
- opt_enable EEPROM_SETTINGS EEPROM_CHITCHAT M100_FREE_MEMORY_WATCHER INCH_MODE_SUPPORT TEMPERATURE_UNITS_SUPPORT - opt_enable EEPROM_SETTINGS EEPROM_CHITCHAT M100_FREE_MEMORY_WATCHER M100_FREE_MEMORY_DUMPER M100_FREE_MEMORY_CORRUPTOR INCH_MODE_SUPPORT TEMPERATURE_UNITS_SUPPORT
- build_marlin - build_marlin
# #
# Mixing Extruder # Mixing Extruder

@ -26,227 +26,238 @@
* This code watches the free memory block between the bottom of the heap and the top of the stack. * This code watches the free memory block between the bottom of the heap and the top of the stack.
* This memory block is initialized and watched via the M100 command. * This memory block is initialized and watched via the M100 command.
* *
* M100 I Initializes the free memory block and prints vitals statistics about the area * M100 I Initializes the free memory block and prints vitals statistics about the area
* M100 F Identifies how much of the free memory block remains free and unused. It also *
* detects and reports any corruption within the free memory block that may have * M100 F Identifies how much of the free memory block remains free and unused. It also
* happened due to errant firmware. * detects and reports any corruption within the free memory block that may have
* M100 D Does a hex display of the free memory block along with a flag for any errant * happened due to errant firmware.
* data that does not match the expected value. *
* M100 C x Corrupts x locations within the free memory block. This is useful to check the * M100 D Does a hex display of the free memory block along with a flag for any errant
* correctness of the M100 F and M100 D commands. * data that does not match the expected value.
*
* M100 C x Corrupts x locations within the free memory block. This is useful to check the
* correctness of the M100 F and M100 D commands.
* *
* Initial version by Roxy-3D * Initial version by Roxy-3D
*/ */
#define M100_FREE_MEMORY_DUMPER // Comment out to remove Dump sub-command #define M100_FREE_MEMORY_DUMPER // Enable for the `M110 D` Dump sub-command
#define M100_FREE_MEMORY_CORRUPTOR // Comment out to remove Corrupt sub-command #define M100_FREE_MEMORY_CORRUPTOR // Enable for the `M100 C` Corrupt sub-command
#include "Marlin.h" #include "MarlinConfig.h"
#if ENABLED(M100_FREE_MEMORY_WATCHER) #if ENABLED(M100_FREE_MEMORY_WATCHER)
#define TEST_BYTE 0xE5
extern char* __brkval; extern char* __brkval;
extern size_t __heap_start, __heap_end, __flp; extern size_t __heap_start, __heap_end, __flp;
extern char __bss_end; extern char __bss_end;
#include "Marlin.h"
#include "hex_print_routines.h"
// //
// Utility functions used by M100 to get its work done. // Utility functions
// //
#include "hex_print_routines.h" #define END_OF_HEAP() (__brkval ? __brkval : &__bss_end)
char* top_of_stack(); // Location of a variable on its stack frame. Returns a value above
int how_many_E5s_are_here(char*); // the stack (once the function returns to the caller).
int free_memory_is_corrupted(); // int not bool!!!! it will tell us how many blocks of char* top_of_stack() {
// free memory it found. char x;
void gcode_M100() { return &x + 1; // x is pulled on return;
static bool m100_not_initialized = true; }
char* sp, *ptr;
int i, j, n; // Count the number of test bytes at the specified location.
// int16_t count_test_bytes(const char * const ptr) {
// M100 D dumps the free memory block from __brkval to the stack pointer. for (uint16_t i = 0; i < 32000; i++)
// malloc() eats memory from the start of the block and the stack grows if (ptr[i] != TEST_BYTE)
// up from the bottom of the block. Solid 0xE5's indicate nothing has return i - 1;
// used that memory yet. There should not be anything but 0xE5's within
// the block of 0xE5's. If there is, that would indicate memory corruption return -1;
// probably caused by bad pointers. Any unexpected values will be flagged in }
// the right hand column to help spotting them.
// // Return a count of free memory blocks.
SERIAL_ECHOPAIR("\n__brkval : 0x", hex_word((uint16_t)__brkval) ); uint16_t free_memory_is_corrupted(char * const ptr, const uint16_t size) {
SERIAL_ECHOPAIR("\n__bss_end : 0x", hex_word((uint16_t)&__bss_end)); // Find the longest block of test bytes in the given buffer
// uint16_t block_cnt = 0;
// With out malloc() we need to be smart and use &__bss_end for (uint16_t i = 0; i < size; i++) {
// if (ptr[i] == TEST_BYTE) {
ptr = __brkval ? __brkval : &__bss_end; const uint16_t j = count_test_bytes(ptr + i);
SERIAL_ECHOPAIR("\nstart of free space : 0x", hex_word((uint16_t)ptr)); if (j > 8) {
//SERIAL_ECHOPAIR("Found ", j);
sp = top_of_stack(); //SERIAL_ECHOLNPAIR(" bytes free at 0x", hex_word((uint16_t)ptr + i));
SERIAL_ECHOLNPAIR("\nStack Pointer : 0x", hex_word((uint16_t)sp)); i += j;
block_cnt++;
#if ENABLED(M100_FREE_MEMORY_DUMPER) // Disable to remove Dump sub-command
if (code_seen('D')) {
//
// We want to start and end the dump on a nice 16 byte boundry even though
// the values we are using are not 16 byte aligned.
//
ptr = (char*) ((uint16_t) ptr & 0xfff0);
sp = (char*) ((uint16_t) sp | 0x000f);
n = sp - ptr;
//
// This is the main loop of the Dump command.
//
while (ptr < sp) {
print_hex_word((uint16_t)ptr); // Print the address
SERIAL_CHAR(':');
for (i = 0; i < 16; i++) { // and 16 data bytes
if (i==8)
SERIAL_CHAR('-');
print_hex_byte(*(ptr + i));
SERIAL_CHAR(' ');
}
SERIAL_CHAR('|'); // now show where non 0xE5's are
for (i = 0; i < 16; i++)
SERIAL_CHAR((*(ptr + i) == (char)0xe5) ? ' ' : '?');
SERIAL_EOL;
ptr += 16;
idle();
} }
return;
} }
#endif }
// //if (block_cnt > 1) {
// M100 F requests the code to return the number of free bytes in the memory pool along with // SERIAL_ECHOLNPGM("\nMemory Corruption detected in free memory area.");
// other vital statistics that define the memory pool. // SERIAL_ECHOLNPAIR("\nLargest free block is ", max_cnt);
// //}
if (code_seen('F')) { return block_cnt;
int max_cnt = -1, block_cnt = 0; }
uint16_t max_addr=0;
ptr = __brkval ? __brkval : &__bss_end; //
sp = top_of_stack(); // M100 sub-commands
n = sp - ptr; //
// Scan through the range looking for the biggest block of 0xE5's we can find
for (i = 0; i < n; i++) { #if ENABLED(M100_FREE_MEMORY_DUMPER)
if (*(ptr + i) == (char)0xe5) { /**
j = how_many_E5s_are_here(ptr + i); * M100 D
if (j > 8) { * Dump the free memory block from __brkval to the stack pointer.
SERIAL_ECHOPAIR("Found ", j); * malloc() eats memory from the start of the block and the stack grows
SERIAL_ECHOLNPAIR(" bytes free at 0x", hex_word((uint16_t)(ptr + i))); * up from the bottom of the block. Solid test bytes indicate nothing has
if (j > max_cnt) { * used that memory yet. There should not be anything but test bytes within
max_cnt = j; * the block. If so, it may indicate memory corruption due to a bad pointer.
max_addr = (uint16_t) ptr + i; * Unexpected bytes are flagged in the right column.
} */
i += j; void dump_free_memory(char *ptr, char *sp) {
block_cnt++; //
} // Start and end the dump on a nice 16 byte boundary
// (even though the values are not 16-byte aligned).
//
ptr = (char*)((uint16_t)ptr & 0xFFF0); // Align to 16-byte boundary
sp = (char*)((uint16_t)sp | 0x000F); // Align sp to the 15th byte (at or above sp)
// Dump command main loop
while (ptr < sp) {
print_hex_word((uint16_t)ptr); // Print the address
SERIAL_CHAR(':');
for (uint8_t i = 0; i < 16; i++) { // and 16 data bytes
if (i == 8) SERIAL_CHAR('-');
print_hex_byte(ptr[i]);
SERIAL_CHAR(' ');
} }
SERIAL_CHAR('|'); // Point out non test bytes
for (uint8_t i = 0; i < 16; i++)
SERIAL_CHAR(ptr[i] == TEST_BYTE ? ' ' : '?');
SERIAL_EOL;
ptr += 16;
idle();
} }
if (block_cnt > 1) { }
SERIAL_ECHOLNPGM("\nMemory Corruption detected in free memory area."); #endif // M100_FREE_MEMORY_DUMPER
SERIAL_ECHOPAIR("\nLargest free block is ", max_cnt);
SERIAL_ECHOLNPAIR(" bytes big at 0x", hex_word(max_addr)); /**
* M100 F
* Return the number of free bytes in the memory pool,
* with other vital statistics defining the pool.
*/
void free_memory_pool_report(const char * const ptr, const uint16_t size) {
int16_t max_cnt = -1;
uint16_t block_cnt = 0;
char *max_addr = NULL;
// Find the longest block of test bytes in the buffer
for (uint16_t i = 0; i < size; i++) {
char * const addr = ptr + i;
if (*addr == TEST_BYTE) {
const uint16_t j = count_test_bytes(addr);
if (j > 8) {
SERIAL_ECHOPAIR("Found ", j);
SERIAL_ECHOLNPAIR(" bytes free at 0x", hex_word((uint16_t)addr));
if (j > max_cnt) {
max_cnt = j;
max_addr = addr;
}
i += j;
block_cnt++;
}
} }
SERIAL_ECHOLNPAIR("free_memory_is_corrupted() = ", free_memory_is_corrupted());
return;
} }
// if (block_cnt > 1) {
// M100 C x Corrupts x locations in the free memory pool and reports the locations of the corruption. SERIAL_ECHOLNPGM("\nMemory Corruption detected in free memory area.");
// This is useful to check the correctness of the M100 D and the M100 F commands. SERIAL_ECHOPAIR("\nLargest free block is ", max_cnt);
// SERIAL_ECHOLNPAIR(" bytes at 0x", hex_word((uint16_t)max_addr));
#if ENABLED(M100_FREE_MEMORY_CORRUPTOR) }
SERIAL_ECHOLNPAIR("free_memory_is_corrupted() = ", free_memory_is_corrupted(ptr, size));
}
#if ENABLED(M100_FREE_MEMORY_CORRUPTOR)
/**
* M100 C<num>
* Corrupt <num> locations in the free memory pool and report the corrupt addresses.
* This is useful to check the correctness of the M100 D and the M100 F commands.
*/
void corrupt_free_memory(char *ptr, const uint16_t size) {
if (code_seen('C')) { if (code_seen('C')) {
int x = code_value_int(); // x gets the # of locations to corrupt within the memory pool
SERIAL_ECHOLNPGM("Corrupting free memory block.\n");
ptr += 8; ptr += 8;
sp = top_of_stack(); const uint16_t near_top = top_of_stack() - ptr - 250, // -250 to avoid interrupt activity that's altered the stack.
n = sp - ptr - 250; // -250 just to keep us from finding interrupt activity that j = near_top / (size + 1);
// has altered the stack.
j = n / (x + 1); SERIAL_ECHOLNPGM("Corrupting free memory block.\n");
for (i = 1; i <= x; i++) { for (uint16_t i = 1; i <= size; i++) {
*(ptr + (i * j)) = i; char * const addr = ptr + i * j;
SERIAL_ECHOPAIR("\nCorrupting address: 0x", hex_word((uint16_t)(ptr + i * j))); *addr = i;
} SERIAL_ECHOPAIR("\nCorrupting address: 0x", hex_word((uint16_t)addr));
SERIAL_ECHOLNPGM("\n");
return;
}
#endif
//
// M100 I Initializes the free memory pool so it can be watched and prints vital
// statistics that define the free memory pool.
//
if (m100_not_initialized || code_seen('I')) { // If no sub-command is specified, the first time
SERIAL_ECHOLNPGM("Initializing free memory block.\n"); // this happens, it will Initialize.
// Repeated M100 with no sub-command will not destroy the
// state of the initialized free memory pool.
ptr += 8;
SERIAL_ECHOLNPGM("\n");
n = sp - ptr - 250; // -250 just to keep us from finding interrupt activity that
// has altered the stack.
SERIAL_ECHO(n);
SERIAL_ECHOLNPGM(" bytes of memory initialized.\n");
for (i = 0; i < n; i++)
*(ptr + i) = (char)0xe5;
for (i = 0; i < n; i++) {
if (*(ptr + i) != (char)0xe5) {
SERIAL_ECHOPAIR("? address : ", hex_word(ptr+i) );
SERIAL_ECHOPAIR("=", hex_byte(*(ptr + i)) );
SERIAL_ECHOLNPGM("\n");
} }
SERIAL_EOL;
} }
m100_not_initialized = false;
return;
} }
return; #endif // M100_FREE_MEMORY_CORRUPTOR
}
// top_of_stack() returns the location of a variable on its stack frame. The value returned is above /**
// the stack once the function returns to the caller. * M100 I
* Init memory for the M100 tests. (Automatically applied on the first M100.)
*/
void init_free_memory(char *ptr, int16_t size) {
SERIAL_ECHOLNPGM("Initializing free memory block.\n\n");
char* top_of_stack() { size -= 250; // -250 to avoid interrupt activity that's altered the stack.
char x; if (size < 0) return;
return &x + 1; // x is pulled on return;
}
// how_many_E5s_are_here() is a utility function to easily find out how many 0xE5's are ptr += 8;
// at the specified location. Having this logic as a function simplifies the search code. memset(ptr, TEST_BYTE, size);
//
int how_many_E5s_are_here(char* p) { SERIAL_ECHO(size);
int n; SERIAL_ECHOLNPGM(" bytes of memory initialized.\n");
for (n = 0; n < 32000; n++) {
if (*(p + n) != (char)0xe5) for (uint16_t i = 0; i < size; i++) {
return n - 1; if (ptr[i] != TEST_BYTE) {
SERIAL_ECHOPAIR("? address : 0x", hex_word((uint16_t)ptr + i));
SERIAL_ECHOPAIR("=", hex_byte(ptr[i]));
SERIAL_EOL; SERIAL_EOL;
}
} }
return -1;
} }
/**
* M100: Free Memory Check
*/
void gcode_M100() {
SERIAL_ECHOPAIR("\n__brkval : 0x", hex_word((uint16_t)__brkval));
SERIAL_ECHOPAIR("\n__bss_end : 0x", hex_word((uint16_t)&__bss_end));
int free_memory_is_corrupted() { char *ptr = END_OF_HEAP(), *sp = top_of_stack();
char *sp, *ptr;
int block_cnt = 0, i, j, n;
ptr = __brkval ? __brkval : &__bss_end; SERIAL_ECHOPAIR("\nstart of free space : 0x", hex_word((uint16_t)ptr));
sp = top_of_stack(); SERIAL_ECHOLNPAIR("\nStack Pointer : 0x", hex_word((uint16_t)sp));
n = sp - ptr; // Always init on the first invocation of M100
static bool m100_not_initialized = true;
if (m100_not_initialized || code_seen('I')) {
m100_not_initialized = false;
init_free_memory(ptr, sp - ptr);
}
// Scan through the range looking for the biggest block of 0xE5's we can find #if ENABLED(M100_FREE_MEMORY_DUMPER)
for (i = 0; i < n; i++) {
if (*(ptr + i) == (char)0xe5) {
j = how_many_E5s_are_here(ptr + i);
if (j > 8) {
// SERIAL_ECHOPAIR("Found ", j);
// SERIAL_ECHOLNPAIR(" bytes free at 0x", hex_word((uint16_t)(ptr + i)));
i += j; if (code_seen('D'))
block_cnt++; return dump_free_memory(ptr, sp);
}
}
}
// if (block_cnt > 1) { #endif
// SERIAL_ECHOLNPGM("\nMemory Corruption detected in free memory area.");
// SERIAL_ECHOLNPAIR("\nLargest free block is ", max_cnt);
// }
return block_cnt;
}
#endif if (code_seen('F'))
return free_memory_pool_report(ptr, sp - ptr);
#if ENABLED(M100_FREE_MEMORY_CORRUPTOR)
if (code_seen('C'))
return corrupt_free_memory(ptr, code_value_int());
#endif
}
#endif // M100_FREE_MEMORY_WATCHER

@ -43,7 +43,7 @@ char* hex_word(const uint16_t w) {
return _hex; return _hex;
} }
void print_hex_nybble(const uint8_t n) { SERIAL_CHAR(hex_nybble(n)); } void print_hex_nybble(const uint8_t n) { SERIAL_CHAR(hex_nybble(n)); }
void print_hex_byte(const uint8_t b) { SERIAL_ECHO(hex_byte(b)); } void print_hex_byte(const uint8_t b) { SERIAL_ECHO(hex_byte(b)); }
void print_hex_word(const uint16_t w) { SERIAL_ECHO(hex_word(w)); } void print_hex_word(const uint16_t w) { SERIAL_ECHO(hex_word(w)); }

Loading…
Cancel
Save