Browse Source

Memory: use DEBUG for hex_dump output instead of printf

congestion
Evgeny 5 months ago
parent
commit
41952703d9
  1. 29
      lib/mem.c

29
lib/mem.c

@ -51,29 +51,28 @@ static void init_mutex() {
static void* allocated_head = NULL; static void* allocated_head = NULL;
static size_t allocated_count = 0; static size_t allocated_count = 0;
static void hex_dump_buf(const void* mem, size_t len, char* buf, size_t buf_size) { static void hex_dump(const void* mem, size_t len) {
const uint8_t* data = (const uint8_t*)mem; const uint8_t* data = (const uint8_t*)mem;
size_t off = 0; char line[128];
for (size_t i = 0; i < len && i < 256; i += 16) { for (size_t i = 0; i < len; i += 16) {
if (off >= buf_size - 80) break; int off = snprintf(line, sizeof(line), "%08zx: ", i);
off += snprintf(buf + off, buf_size - off, "%08zx: ", i);
for (size_t j = 0; j < 16; j++) { for (size_t j = 0; j < 16; j++) {
if (i + j < len) { if (i + j < len) {
off += snprintf(buf + off, buf_size - off, "%02x ", data[i + j]); off += snprintf(line + off, sizeof(line) - off, "%02x ", data[i + j]);
} else { } else {
off += snprintf(buf + off, buf_size - off, " "); off += snprintf(line + off, sizeof(line) - off, " ");
} }
} }
off += snprintf(buf + off, buf_size - off, " "); off += snprintf(line + off, sizeof(line) - off, " ");
for (size_t j = 0; j < 16; j++) { for (size_t j = 0; j < 16; j++) {
if (i + j < len) { if (i + j < len) {
uint8_t c = data[i + j]; uint8_t c = data[i + j];
off += snprintf(buf + off, buf_size - off, "%c", (c >= 32 && c <= 126) ? c : '.'); off += snprintf(line + off, sizeof(line) - off, "%c", (c >= 32 && c <= 126) ? c : '.');
} else { } else {
off += snprintf(buf + off, buf_size - off, " "); off += snprintf(line + off, sizeof(line) - off, " ");
} }
} }
off += snprintf(buf + off, buf_size - off, "\n"); DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, "%s\n", line);
} }
} }
@ -117,9 +116,7 @@ void u_check(void* ptr, const char* text, const char* location) {
printf("%s: %s: Allocated at: %s\n", location, text, alloc_loc); printf("%s: %s: Allocated at: %s\n", location, text, alloc_loc);
printf("%s: %s: Memory block size (user): %u bytes, total allocated: %u bytes\n", location, text, size, total); printf("%s: %s: Memory block size (user): %u bytes, total allocated: %u bytes\n", location, text, size, total);
printf("%s: %s: Full memory dump at base %p:\n", location, text, base); printf("%s: %s: Full memory dump at base %p:\n", location, text, base);
char dump_buf[4096]; hex_dump(base, total);
hex_dump_buf(base, total, dump_buf, sizeof(dump_buf));
DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, "%s", dump_buf);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
} }
@ -231,9 +228,7 @@ void u_report_unfreed_blocks(void) {
u_check(user_ptr, "unfreed_check", "u_report_unfreed_blocks"); u_check(user_ptr, "unfreed_check", "u_report_unfreed_blocks");
DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, " - Allocated at: %s, size: %u bytes\n", alloc_loc, size); DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, " - Allocated at: %s, size: %u bytes\n", alloc_loc, size);
DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, " Full memory dump at %p:\n", user_ptr); DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, " Full memory dump at %p:\n", user_ptr);
char dump_buf[4096]; hex_dump(user_ptr, size);
hex_dump_buf(user_ptr, size, dump_buf, sizeof(dump_buf));
DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, "%s", dump_buf);
curr = *(void**)((uint8_t*)curr + NEXT_OFFSET); curr = *(void**)((uint8_t*)curr + NEXT_OFFSET);
} }
} }

Loading…
Cancel
Save